20#include "ops/BinaryArithmeticLayer.h"
21#include "ops/ConvolutionLayer.h"
22#include "ops/DepthwiseConvolutionLayer.h"
23#include "ops/ElementwiseActivationLayer.h"
24#include "ops/FullyConnectedLayer.h"
27#include "ops/MeanLayer.h"
29#include "ops/PadLayer.h"
30#include "ops/PoolLayer.h"
31#include "ops/ReshapeLayer.h"
32#include "ops/SoftMaxLayer.h"
51ops::ElementwiseActivationType
59 throw std::runtime_error(
"train KernelGenerator : Not supported operation yet");
73 throw std::runtime_error(
"train KernelGenerator : Not supported operation yet");
77std::unique_ptr<ops::BackPropAccumulator>
78generateBackPropAccumulator(
const IPortableTensor *disposable,
BackPropTensor *gradient)
80 auto update_fn = std::make_unique<ops::BackPropAccumulator>(disposable, gradient);
84void appendBackPropAccumulators(
const ir::train::ITrainableOperation &op,
86 exec::train::TrainableFnSequence *seq)
88 if (!op.isRequiredForBackward())
91 for (
const auto &input_index : (op.getInputs() | ir::
Remove::
UNDEFINED))
93 const auto disposable =
94 tensor_reg->getDisposableBackPropTensor(DisposableTensorIndex{op_index,
input_index});
95 if (disposable !=
nullptr)
97 auto back_prop = tensor_reg->getBackPropTensor(input_index);
99 seq->append(generateBackPropAccumulator(disposable, back_prop));
104std::unique_ptr<ops::GradientApplier>
105generateGradientApplier(
const exec::train::optimizer::Optimizer *optimizer,
106 const IPortableTensor *gradient, ITrainableTensor *trainable)
108 auto update_fn = std::make_unique<ops::GradientApplier>();
109 update_fn->configure(optimizer, gradient, trainable);
119 auto ret = std::make_unique<exec::train::TrainableFnSequence>();
125 appendBackPropAccumulators(op, idx, _tensor_reg.get(), ret.get());
131 for (
auto &&update_fn : _update_funcs)
132 ret->append(std::move(update_fn));
133 _update_funcs.clear();
137 auto tensor = _tensor_reg->getNonConstTensor(ind);
140 tensor->increase_ref();
147 const std::shared_ptr<TensorRegistry> &tensor_reg,
148 const std::shared_ptr<ExternalContext> &external_context,
151 _external_context(external_context), _optimizer{optimizer}, _update_funcs{}, _node_to_idx{}
155 assert(_node_to_idx.find(&op) == _node_to_idx.end());
156 _node_to_idx[&op] = idx;
165 const auto lhs_index{node.
getInputs().
at(BinaryArithmetic::Input::LHS)};
166 const auto rhs_index{node.
getInputs().
at(BinaryArithmetic::Input::RHS)};
171 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
172 auto lhs_tensor = _tensor_reg->getPortableTensor(lhs_index);
173 auto rhs_tensor = _tensor_reg->getPortableTensor(rhs_index);
175 auto fn = std::make_unique<ops::BinaryArithmeticLayer>();
176 fn->configure(lhs_tensor, rhs_tensor, output_tensor, activation,
181 auto back_prop_output_tensor = getBackPropOut(output_index);
182 auto back_prop_lhs_tensor = getBackPropIn(node, lhs_index);
183 auto back_prop_rhs_tensor = getBackPropIn(node, rhs_index);
185 fn->configureBackward(back_prop_lhs_tensor, back_prop_rhs_tensor, back_prop_output_tensor,
196 const auto in_index{node.
getInputs().
at(Conv2D::Input::INPUT)};
197 const auto ker_index{node.
getInputs().
at(Conv2D::Input::KERNEL)};
198 const auto bias_index{node.
getInputs().
at(Conv2D::Input::BIAS)};
200 auto out_tensor = _tensor_reg->getPortableTensor(out_index);
201 auto in_tensor = _tensor_reg->getPortableTensor(in_index);
202 auto ker_tensor = _tensor_reg->getTrainableTensor(ker_index);
203 auto bias_tensor = _tensor_reg->getTrainableTensor(bias_index);
210 auto fn = std::make_unique<ops::ConvolutionLayer>();
213 const auto ifm_shape = operands.
at(in_index).shape().asFeature();
214 const auto ofm_shape = operands.at(out_index).shape().asFeature();
216 const auto &ker_shape = operands.at(ker_index).shape();
217 const auto ker_height = ker_shape.dim(1);
218 const auto ker_width = ker_shape.dim(2);
222 dilation.width_factor, dilation.height_factor);
224 const bool is_cacheable_weights =
false;
225 fn->configure(in_tensor, ker_tensor, bias_tensor, param_padding.type, padding.left, padding.right,
226 padding.top, padding.bottom, stride.horizontal, stride.vertical,
227 dilation.width_factor, dilation.height_factor, activation, out_tensor,
228 is_cacheable_weights);
230 auto ker_grad_tensor = _tensor_reg->getGradientTensor(ker_index);
231 auto bias_grad_tensor = _tensor_reg->getGradientTensor(bias_index);
236 auto out_back_prop_tensor = getBackPropOut(out_index);
237 auto in_back_prop_tensor = getBackPropIn(node, in_index);
239 fn->configureBackward(ker_tensor, in_back_prop_tensor, ker_grad_tensor, bias_grad_tensor,
240 out_back_prop_tensor, activation);
244 _update_funcs.emplace_back(
245 generateGradientApplier(_optimizer, bias_grad_tensor, bias_tensor));
246 _update_funcs.emplace_back(generateGradientApplier(_optimizer, ker_grad_tensor, ker_tensor));
257 const auto ifm_index{node.
getInputs().
at(DepthwiseConv2D::Input::INPUT)};
258 const auto ker_index{node.
getInputs().
at(DepthwiseConv2D::Input::KERNEL)};
259 const auto bias_index{node.
getInputs().
at(DepthwiseConv2D::Input::BIAS)};
261 auto ofm_tensor = _tensor_reg->getPortableTensor(ofm_index);
262 auto ifm_tensor = _tensor_reg->getPortableTensor(ifm_index);
263 auto ker_tensor = _tensor_reg->getTrainableTensor(ker_index);
264 auto bias_tensor = _tensor_reg->getTrainableTensor(bias_index);
268 const auto ofm_shape = operands.
at(ofm_index).shape().asFeature();
269 const auto ifm_shape = operands.at(ifm_index).shape().asFeature();
271 const auto &ker_shape = operands.at(ker_index).shape();
272 const auto ker_height = ker_shape.dim(1);
273 const auto ker_width = ker_shape.dim(2);
277 ker_width, ker_height, dilation_width, dilation_height);
281 auto fn = std::make_unique<ops::DepthwiseConvolutionLayer>();
283 fn->configure(ifm_tensor, ker_tensor, bias_tensor, padding.left, padding.right, padding.top,
284 padding.bottom, stride.horizontal, stride.vertical, multiplier, dilation_width,
285 dilation_height, activation, ofm_tensor, _external_context);
289 auto ker_grad_tensor = _tensor_reg->getGradientTensor(ker_index);
290 auto bias_grad_tensor = _tensor_reg->getGradientTensor(bias_index);
292 auto ofm_back_prop_tensor = getBackPropOut(ofm_index);
293 auto ifm_back_prop_tensor = getBackPropIn(node, ifm_index);
295 fn->configureBackward(ifm_back_prop_tensor, ker_grad_tensor, bias_grad_tensor,
296 ofm_back_prop_tensor, activation);
300 _update_funcs.emplace_back(
301 generateGradientApplier(_optimizer, bias_grad_tensor, bias_tensor));
302 _update_funcs.emplace_back(generateGradientApplier(_optimizer, ker_grad_tensor, ker_tensor));
313 const auto input_index{node.
getInputs().
at(ElementwiseActivation::Input::INPUT)};
315 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
316 auto input_tensor = _tensor_reg->getPortableTensor(input_index);
318 auto fn = std::make_unique<ops::ElementwiseActivationLayer>();
326 throw std::invalid_argument(
"Unsupported ElementwiseActivation::Type");
335 auto back_prop_input_tensor = getBackPropIn(node, input_index);
336 auto back_prop_output_tensor = getBackPropOut(output_index);
338 fn->configureBackward(input_tensor, back_prop_input_tensor, back_prop_output_tensor,
340 convertElementwiseActivationType(node.
param().
op_type));
351 const auto in_index{node.
getInputs().
at(FullyConnected::Input::INPUT)};
352 const auto weights_index{node.
getInputs().
at(FullyConnected::Input::WEIGHT)};
353 const auto bias_index{node.
getInputs().
at(FullyConnected::Input::BIAS)};
355 auto out_tensor = _tensor_reg->getPortableTensor(out_index);
356 auto in_tensor = _tensor_reg->getPortableTensor(in_index);
357 auto weights_tensor = _tensor_reg->getTrainableTensor(weights_index);
358 auto bias_tensor = _tensor_reg->getTrainableTensor(bias_index);
364 auto fn = std::make_unique<ops::FullyConnectedLayer>();
366 fn->configure(in_tensor, weights_tensor, bias_tensor, activation, weights_format, out_tensor,
371 auto out_back_prop_tensor = getBackPropOut(out_index);
372 auto in_back_prop_tensor = getBackPropIn(node, in_index);
373 auto weights_grad_tensor = _tensor_reg->getGradientTensor(weights_index);
374 auto bias_grad_tensor = _tensor_reg->getGradientTensor(bias_index);
376 fn->configureBackward(in_tensor, weights_tensor, out_tensor, in_back_prop_tensor,
377 weights_grad_tensor, bias_grad_tensor, out_back_prop_tensor, activation,
382 _update_funcs.emplace_back(
383 generateGradientApplier(_optimizer, bias_grad_tensor, bias_tensor));
384 _update_funcs.emplace_back(
385 generateGradientApplier(_optimizer, weights_grad_tensor, weights_tensor));
396 const auto y_pred_index{node.
getInputs().
at(Loss::Y_PRED)};
397 const auto y_true_index{node.
getInputs().
at(Loss::Y_TRUE)};
399 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
400 auto y_pred_tensor = _tensor_reg->getPortableTensor(y_pred_index);
401 auto y_true_tensor = _tensor_reg->getPortableTensor(y_true_index);
405 auto back_prop_y_pred_tensor = getBackPropIn(node, y_pred_index);
415 auto fn = std::make_unique<ops::LossMeanSquaredErrorLayer>();
416 fn->configure(y_pred_tensor, y_true_tensor, output_tensor, back_prop_y_pred_tensor,
424 bool is_normalization_required = (y_pred_op_code != ir::OpCode::Softmax);
425 auto fn = std::make_unique<ops::LossCategoricalCrossentropyLayer>();
426 fn->configure(y_pred_tensor, y_true_tensor, output_tensor, back_prop_y_pred_tensor,
427 reduction_type, loss_param.cce.axis, loss_param.cce.label_smoothing,
428 is_normalization_required);
433 throw std::runtime_error(
"LossLayer: unsupported loss type");
444 auto input = _tensor_reg->getPortableTensor(input_index);
445 auto pad = _tensor_reg->getPortableTensor(pad_index);
446 auto output = _tensor_reg->getPortableTensor(output_index);
448 auto fn = std::make_unique<ops::PadLayer>();
454 value = _tensor_reg->getPortableTensor(value_index);
457 fn->configure(input, pad, value, output);
460 auto out_back_prop_tensor = getBackPropOut(output_index);
461 auto in_back_prop_tensor = getBackPropIn(node, input_index);
462 fn->configureBackward(in_back_prop_tensor, out_back_prop_tensor);
475 const auto &ofm_shape = operands.
at(output_index).shape();
476 const auto &ifm_shape = operands.at(input_index).shape();
478 if (ifm_shape.rank() != 4)
480 throw std::runtime_error(node.
name() +
" only supports 4D tensor as input");
485 const auto kh = node.
param().
kh;
486 const auto kw = node.
param().
kw;
488 ofm_shape.asFeature(), stride, kw, kh);
490 auto out_tensor = _tensor_reg->getPortableTensor(output_index);
491 auto in_tensor = _tensor_reg->getPortableTensor(input_index);
494 const auto pool_type = convertPoolType(node.
param().
op_type);
496 auto fn = std::make_unique<ops::PoolLayer>();
506 throw std::runtime_error(
"PoolLayer: Unsupported pool type yet");
510 fn->configure(in_tensor, padding.left, padding.right, padding.top, padding.bottom,
511 stride.horizontal, stride.vertical, kw, kh, activation, out_tensor,
512 convertToInferPoolType(pool_type));
516 auto out_back_prop_tensor = getBackPropOut(output_index);
517 auto in_back_prop_tensor = getBackPropIn(node, input_index);
518 fn->configureBackward(padding.left, padding.right, padding.top, padding.bottom,
519 stride.horizontal, stride.vertical, kw, kh, activation, pool_type,
520 out_tensor, in_back_prop_tensor, out_back_prop_tensor);
531 const auto input_index{node.
getInputs().
at(Reduce::Input::INPUT)};
532 const auto axes_index{node.
getInputs().
at(Reduce::Input::AXES)};
536 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
537 auto input_tensor = _tensor_reg->getPortableTensor(input_index);
538 auto axes_tensor = _tensor_reg->getPortableTensor(axes_index);
542 auto fn = std::make_unique<ops::MeanLayer>();
543 fn->configure(input_tensor, axes_tensor, output_tensor, keep_dims);
546 auto back_prop_output_tensor = getBackPropOut(output_index);
547 auto back_prop_input_tensor = getBackPropIn(node, input_index);
548 fn->configureBackward(back_prop_input_tensor, back_prop_output_tensor);
554 throw std::runtime_error(
"ReduceLayer: unsupported reduce type");
565 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
566 auto input_tensor = _tensor_reg->getPortableTensor(input_index);
574 shape_tensor = _tensor_reg->getPortableTensor(shape_index);
577 auto fn = std::make_unique<ops::ReshapeLayer>();
579 fn->configure(input_tensor, shape_tensor, output_tensor);
582 auto output_back_prop_tensor = getBackPropOut(output_index);
583 auto input_back_prop_tensor = getBackPropIn(node, input_index);
584 fn->configureBackward(input_back_prop_tensor, output_back_prop_tensor);
598 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
599 auto input_tensor = _tensor_reg->getPortableTensor(input_index);
601 auto fn = std::make_unique<ops::SoftMaxLayer>();
603 fn->configure(input_tensor, beta, output_tensor);
607 auto output_back_prop_tensor = getBackPropOut(output_index);
608 auto input_back_prop_tensor = getBackPropIn(node, input_index);
609 fn->configureBackward(input_back_prop_tensor, output_back_prop_tensor);
617 const auto &op_index = _node_to_idx[&node];
620 const auto disposable_tensor =
621 _tensor_reg->getDisposableBackPropTensor(DisposableTensorIndex{op_index, operand_index});
622 if (disposable_tensor !=
nullptr)
624 [[maybe_unused]]
const auto &training_usedefs =
626 assert(std::count_if(training_usedefs.getTrainingDefs().begin(),
627 training_usedefs.getTrainingDefs().end(),
629 return _tgraph.operation(op_index.index()).isRequiredForBackward();
632 return disposable_tensor;
635 return _tensor_reg->getBackPropTensor(operand_index);
638IPortableTensor *KernelGenerator::getBackPropOut(
const ir::OperandIndex &output_index)
640 return _tensor_reg->getBackPropTensor(output_index);
A tensor class that is portable for other backends.
std::unique_ptr< exec::train::ITrainableFunction > _return_fn
const ir::train::TrainableGraph & _tgraph
std::unique_ptr< exec::train::TrainableFnSequence > generate(ir::OperationIndex op_ind) override
KernelGenerator(const ir::train::TrainableGraph &tgraph, const std::shared_ptr< TensorRegistry > &tensor_reg, const std::shared_ptr< ExternalContext > &external_context, const exec::train::optimizer::Optimizer *optimizer)
void visit(const ir::train::operation::BinaryArithmetic &) override
Base class for all optimizers.
const OperandIndex & at(IOIndex set_index) const
const OperandIndexSequence & getOutputs() const override
OperandIndexSequence & getInputs()
const Param & param() const
const Param & param() const
const Param & param() const
const Param & param() const
const Param & param() const
const Param & param() const
std::string name() const override
const Param & param() const
const Param & param() const
const ITrainableOperation & operation(OperationIndex index) const
const Operations & operations() const override
const Operands & operands() const override
const UseDefChains & trainingUseDefs() const
virtual bool isRequiredForBackward() const final
Class that provides index of tensor for training.
const LossInfo & param() const
ir::OpCode y_pred_op_code() const
void iterate(const std::function< void(const Index &, const Object &)> &fn) const
Iterate over the container with given function.
const Object & at(const Index &index) const
Get the object that is associated with the given index.
arm_compute::PoolingType convertPoolType(ir::operation::Pool2D::PoolType pool_type_ir)
@ CategoricalCrossentropy
const ExplicitPadding calculatePadding(const Padding &padding, const FeatureShape &ifm_shape, const FeatureShape &ofm_shape, const Stride &stride, uint32_t kw, uint32_t kh, uint32_t dwf=1, uint32_t dhf=1)
::onert::util::Index< uint32_t, OperationIndexTag > OperationIndex
::onert::util::Index< uint32_t, OperandIndexTag > OperandIndex
ArithmeticType arithmetic_type
FullyConnectedWeightsFormat weights_format
union onert::ir::train::LossInfo::LossParam loss_param
LossReductionType reduction_type