19#include <arm_compute/runtime/NEON/NEFunctions.h>
28#include "ir/DataType.h"
37using ::onert::backend::acl_common::asAclFunction;
42 const ir::Graph &graph,
const std::shared_ptr<TensorBuilder> &tensor_builder,
44 : basic::KernelGeneratorBase{graph}, _ctx(graph.operands()), _operations_ctx(graph.operations()),
45 _tensor_builder(tensor_builder), _tensor_reg(tensor_reg)
52 auto ret = std::make_unique<exec::FunctionSequence>();
53 ret->enableDynamicShapeInferer(
false);
67 const auto ifm_rank = _ctx.
at(ifm_index).shape().rank();
69 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
70 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
72 int axis_value = _ctx.
at(axis_index).asScalar<int32_t>();
75 axis_value += ifm_rank;
77 assert(axis_value >= 0 && axis_value < ifm_rank);
79 auto reduce_type = node.
param().
is_arg_max ? ::arm_compute::ReductionOperation::ARG_IDX_MAX
80 : ::arm_compute::ReductionOperation::ARG_IDX_MIN;
82 auto fn = acl_common::generateLayer<arm_compute::NEArgMinMaxLayer>(
83 ifm_tensor->handle(), fixed_axis, ofm_tensor->handle(), reduce_type);
88void KernelGenerator::visit(
const ir::operation::BatchToSpaceND &node)
90 const auto ofm_index{node.getOutputs().at(0)};
92 const auto block_size_index{
95 const auto NNApiInputs = 2;
96 if (node.getInputs().size() != NNApiInputs)
99 if (!_ctx.
at(crops_index).isConstant())
101 throw std::runtime_error(
"Non-constant crops NYI for acl_neon backend BatchToSpaceND");
104 auto crops = _ctx.
at(crops_index).asVector<int32_t>();
105 for (
auto &&crop : crops)
109 throw std::runtime_error(
"Non-zero crops NYI for acl_neon backend BatchToSpaceND");
114 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
115 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
117 if (!_ctx.
at(block_size_index).data())
118 throw std::runtime_error(
"ACL NEON does not support dynamic block size for BatchToSpaceND");
120 auto block = _ctx.
at(block_size_index).asVector<int32_t>();
121 int32_t height = block[0];
122 int32_t width = block[1];
124 auto fn = acl_common::generateLayer<arm_compute::NEBatchToSpaceLayer>(
125 ifm_tensor->handle(), width, height, ofm_tensor->handle());
130void KernelGenerator::visit(
const ir::operation::BinaryArithmetic &node)
132 const auto ofm_index{node.getOutputs().at(0)};
136 const auto activation = node.param().activation;
138 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
139 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
140 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
142 std::unique_ptr<arm_compute::IFunction> fn;
143 switch (node.param().arithmetic_type)
147 arm_compute::NEArithmeticAddition::validate(lhs_tensor->info(), rhs_tensor->info(),
149 arm_compute::ConvertPolicy::SATURATE)
151 fn = acl_common::generateLayer<arm_compute::NEArithmeticAddition>(
152 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
153 arm_compute::ConvertPolicy::SATURATE);
158 arm_compute::NEArithmeticSubtraction::validate(lhs_tensor->info(), rhs_tensor->info(),
160 arm_compute::ConvertPolicy::SATURATE)
162 fn = acl_common::generateLayer<arm_compute::NEArithmeticSubtraction>(
163 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
164 arm_compute::ConvertPolicy::SATURATE);
169 arm_compute::NEPixelWiseMultiplication::validate(
170 lhs_tensor->info(), rhs_tensor->info(), ofm_tensor->info(), 1.0,
171 arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO)
174 fn = acl_common::generateLayer<arm_compute::NEPixelWiseMultiplication>(
175 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(), 1.0,
176 arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO);
181 arm_compute::NEElementwiseDivision::validate(lhs_tensor->info(), rhs_tensor->info(),
184 fn = acl_common::generateLayer<arm_compute::NEElementwiseDivision>(
185 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle());
189 assert(
false &&
"The BinaryArithmetic operation supports only binary arithmetic operations");
192 _return_fn = std::make_unique<exec::FunctionSequence>(
196void KernelGenerator::visit(
const ir::operation::Conv2D &node)
198 using ir::operation::Conv2D;
200 const auto ofm_index{node.getOutputs().at(0)};
201 const auto ifm_index{node.getInputs().at(Conv2D::Input::INPUT)};
202 const auto ker_index{node.getInputs().at(Conv2D::Input::KERNEL)};
203 const auto bias_index{node.getInputs().at(Conv2D::Input::BIAS)};
205 const auto ofm_shape = _ctx.
at(ofm_index).shape().asFeature();
206 const auto ifm_shape = _ctx.
at(ifm_index).shape().asFeature();
208 const auto &ker_shape = _ctx.
at(ker_index).shape();
209 const auto ker_height = ker_shape.dim(1);
210 const auto ker_width = ker_shape.dim(2);
212 const auto stride = node.param().stride;
214 ir::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, ker_width, ker_height);
215 const auto activation = node.param().activation;
217 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
218 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
219 auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
220 auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
225 auto fn = acl_common::generateLayer<arm_compute::NEConvolutionLayer>(
226 _tensor_builder->acl_tensor_manager()->internal_buffer_manager(), ifm_tensor->handle(),
228 ::arm_compute::WeightsInfo(), ::arm_compute::Size2D(1U, 1U), act_info);
233void KernelGenerator::visit(
const ir::operation::DepthToSpace &node)
235 const auto output_index{node.getOutputs().at(0)};
238 auto block_size = node.param().block_size;
239 assert(block_size > 0);
241 auto output_tensor = _tensor_reg->getAclTensor(output_index);
242 auto input_tensor = _tensor_reg->getAclTensor(input_index);
244 auto fn = acl_common::generateLayer<arm_compute::NEDepthToSpaceLayer>(
245 input_tensor->handle(),
output_tensor->handle(), block_size);
250void KernelGenerator::visit(
const ir::operation::DepthwiseConv2D &node)
252 using ir::operation::DepthwiseConv2D;
254 const auto ofm_index{node.getOutputs().at(0)};
255 const auto ifm_index{node.getInputs().at(DepthwiseConv2D::Input::INPUT)};
256 const auto ker_index{node.getInputs().at(DepthwiseConv2D::Input::KERNEL)};
257 const auto bias_index{node.getInputs().at(DepthwiseConv2D::Input::BIAS)};
259 const auto ifm_shape = _ctx.
at(ifm_index).shape().asFeature();
260 const auto ofm_shape = _ctx.
at(ofm_index).shape().asFeature();
262 const auto &ker_shape = _ctx.
at(ker_index).shape();
263 const auto ker_height = ker_shape.dim(1);
264 const auto ker_width = ker_shape.dim(2);
266 const auto stride = node.param().stride;
267 const auto dilation = node.param().dilation;
270 dilation.width_factor, dilation.height_factor);
271 const auto multiplier = node.param().multiplier;
272 const auto activation = node.param().activation;
274 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
275 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
276 auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
277 auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
283 auto fn = acl_common::generateLayer<arm_compute::NEDepthwiseConvolutionLayer>(
285 conv_info, multiplier, act_info, dilation_info);
290void KernelGenerator::visit(
const ir::operation::Concat &node)
292 const auto ofm_index{node.getOutputs().at(0)};
294 std::vector<ir::OperandIndex> input_indexes;
295 for (
const auto &input : node.getInputs())
296 input_indexes.emplace_back(
input);
298 const auto axis = node.param().axis;
301 bool eliminated = _tensor_builder->areSubTensorsOf(ofm_index, node.getInputs());
305 VERBOSE(acl_neon_KernelGenerator_Concat) <<
"Concat eliminated" << std::endl;
306 _return_fn = std::make_unique<exec::NopFunction>();
311 std::vector<const ::arm_compute::ITensor *> input_tensors;
312 for (
const auto &ifm_ind : input_indexes)
313 input_tensors.emplace_back(_tensor_reg->getAclTensor(ifm_ind)->handle());
315 std::unique_ptr<::arm_compute::IFunction> fn;
316 if (input_indexes.size() < 2)
318 ::arm_compute::ITensor *input_tesor = _tensor_reg->getAclTensor(input_indexes.at(0))->handle();
319 fn = acl_common::generateLayer<arm_compute::NECopy>(input_tesor,
output_tensor->handle());
323 const auto rank = _ctx.
at(ofm_index).shape().rank();
325 fn = acl_common::generateLayer<arm_compute::NEConcatenateLayer>(
332void KernelGenerator::visit(
const ir::operation::ElementwiseActivation &node)
334 const auto ofm_index{node.getOutputs().at(0)};
337 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
338 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
340 const ::arm_compute::ActivationLayerInfo act_info =
343 std::unique_ptr<arm_compute::IFunction> fn =
344 acl_common::generateLayer<arm_compute::NEActivationLayer>(ifm_tensor->handle(),
345 ofm_tensor->handle(), act_info);
350void KernelGenerator::visit(
const ir::operation::ElementwiseBinary &node)
352 const auto output_index{node.getOutputs().at(0)};
356 auto output_tensor = _tensor_reg->getAclTensor(output_index);
357 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
358 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
360 std::unique_ptr<arm_compute::IFunction> fn;
361 switch (node.param().op_type)
365 fn = acl_common::generateLayer<arm_compute::NELogicalAnd>(
366 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
371 fn = acl_common::generateLayer<arm_compute::NELogicalOr>(
372 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
377 fn = acl_common::generateLayer<arm_compute::NEElementwiseMax>(
378 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
383 fn = acl_common::generateLayer<arm_compute::NEElementwiseMin>(
384 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
389 std::string err_msg(
"acl_neon KernelGenerator : " + node.name() +
390 "is not elementwise-binary operations");
391 assert(
false && err_msg.c_str());
398void KernelGenerator::visit(
const ir::operation::ElementwiseUnary &node)
400 const auto output_index{node.getOutputs().at(0)};
403 auto output_tensor = _tensor_reg->getAclTensor(output_index);
404 auto input_tensor = _tensor_reg->getAclTensor(input_index);
406 std::unique_ptr<arm_compute::IFunction> fn;
407 switch (node.param().op_type)
411 const ::arm_compute::ActivationLayerInfo act_info{
412 ::arm_compute::ActivationLayerInfo::ActivationFunction::ABS};
414 fn = acl_common::generateLayer<arm_compute::NEActivationLayer>(
422 fn = acl_common::generateLayer<arm_compute::NECopy>(input_tensor->handle(),
425 else if (_ctx.
at(input_index).typeInfo().
type() == ir::DataType::BOOL8)
427 fn = acl_common::generateLayer<arm_compute::NECastBool>(input_tensor->handle(),
432 fn = acl_common::generateLayer<arm_compute::NECast>(
433 input_tensor->handle(),
output_tensor->handle(), arm_compute::ConvertPolicy::SATURATE);
439 fn = acl_common::generateLayer<arm_compute::NEDequantizationLayer>(input_tensor->handle(),
445 fn = acl_common::generateLayer<arm_compute::NEExpLayer>(input_tensor->handle(),
451 fn = acl_common::generateLayer<arm_compute::NEFloor>(input_tensor->handle(),
457 fn = acl_common::generateLayer<arm_compute::NEBitwiseNot>(input_tensor->handle(),
463 fn = acl_common::generateLayer<arm_compute::NENegLayer>(input_tensor->handle(),
469 fn = acl_common::generateLayer<arm_compute::NERsqrtLayer>(input_tensor->handle(),
475 const ::arm_compute::ActivationLayerInfo act_info{
476 ::arm_compute::ActivationLayerInfo::ActivationFunction::SQRT};
478 fn = acl_common::generateLayer<arm_compute::NEActivationLayer>(
484 throw std::runtime_error(
"acl_neon KernelGenerator : " + node.name() +
485 "is not supported yet");
492void KernelGenerator::visit(
const ir::operation::EmbeddingLookup &node)
494 const auto output_index{node.getOutputs().at(0)};
498 auto output_tensor = _tensor_reg->getAclTensor(output_index);
499 auto lookups_tensor = _tensor_reg->getAclTensor(lookups_index);
500 auto values_tensor = _tensor_reg->getAclTensor(values_index);
502 auto fn = acl_common::generateLayer<arm_compute::NEEmbeddingLookup>(
503 values_tensor->handle(),
output_tensor->handle(), lookups_tensor->handle());
508void KernelGenerator::visit(
const ir::operation::FullyConnected &node)
510 const auto output_index{node.getOutputs().at(0)};
511 auto output_tensor = _tensor_reg->getAclTensor(output_index);
512 const auto activation = node.param().activation;
514 throw std::runtime_error(
515 "KernelGenerator(acl_neon): FullyConnected 16x1Float32 weights is not supported.");
519 node, _ctx, _tensor_builder, _tensor_reg);
520 _return_fn = std::make_unique<exec::FunctionSequence>(
524void KernelGenerator::visit(
const ir::operation::HashtableLookup &node)
533 auto output_tensor = _tensor_reg->getAclTensor(output_index);
534 auto hits_tensor = _tensor_reg->getAclTensor(hits_index);
536 auto lookups_tensor = _tensor_reg->getAclTensor(lookups_index);
537 auto keys_tensor = _tensor_reg->getAclTensor(keys_index);
538 auto values_tensor = _tensor_reg->getAclTensor(values_index);
540 auto fn = acl_common::generateLayer<arm_compute::NEHashtableLookup>(
541 lookups_tensor->handle(), keys_tensor->handle(), values_tensor->handle(),
547void KernelGenerator::visit(
const ir::operation::Gather &node)
549 const auto ofm_index{node.getOutputs().at(0)};
554 const auto ifm_rank = _ctx.at(ifm_index).shape().rank();
555 const auto axis_raw = node.param().axis;
556 const auto axis_value = (axis_raw < 0 ? (ifm_rank + axis_raw) : axis_raw);
560 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
561 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
562 auto indices_tensor = _tensor_reg->getAclTensor(indices_index);
566 assert(n == ifm_tensor->num_dimensions());
567 size_t k = _ctx.at(indices_index).shape().rank();
568 assert(k == indices_tensor->num_dimensions());
571 if (n != ifm_tensor->info()->num_dimensions())
576 if (k != indices_tensor->info()->num_dimensions())
582 auto fn = acl_common::generateLayer<arm_compute::NEGatherEx>(
583 ifm_tensor->handle(), indices_tensor->handle(), ofm_tensor->handle(), axis);
586 if (ifm_tensor->dimension(0) == 1)
590 if (indices_tensor->dimension(0) == 1)
598void KernelGenerator::visit(
const ir::operation::InstanceNorm &node)
600 const auto ofm_index{node.getOutputs().at(0)};
605 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
606 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
607 auto gamma_tensor = _tensor_reg->getAclTensor(gamma_index);
608 auto beta_tensor = _tensor_reg->getAclTensor(beta_index);
609 auto epsilon = node.param().epsilon;
610 auto activation = node.param().activation;
612 auto fn = acl_common::generateLayer<arm_compute::NEInstanceNormalizationLayerEx>(
613 ifm_tensor->handle(), ofm_tensor->handle(), gamma_tensor->handle(), beta_tensor->handle(),
616 _return_fn = std::make_unique<exec::FunctionSequence>(
620void KernelGenerator::visit(
const ir::operation::L2Normalization &node)
622 const auto ofm_index{node.getOutputs().at(0)};
630 const auto &ifm_shape = _ctx.at(ifm_index).shape();
632 const auto normalization_axis = _ctx.at(ifm_index).shape().rank() - 1;
634 2 * ifm_shape.dim(normalization_axis) + 1;
639 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
640 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
642 const auto norm_info = ::arm_compute::NormalizationLayerInfo(::arm_compute::NormType::CROSS_MAP,
643 radius, alpha, beta, bias,
false);
645 auto fn = acl_common::generateLayer<arm_compute::NENormalizationLayer>(
646 ifm_tensor->handle(), ofm_tensor->handle(), norm_info);
651void KernelGenerator::visit(
const ir::operation::LocalResponseNormalization &node)
653 const auto ofm_index{node.getOutputs().at(0)};
654 const auto ifm_index{
657 auto radius = node.param().radius;
658 auto alpha = node.param().alpha;
659 auto beta = node.param().beta;
660 auto bias = node.param().bias;
662 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
663 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
665 const auto norm_info = ::arm_compute::NormalizationLayerInfo(
666 ::arm_compute::NormType::CROSS_MAP, radius * 2 + 1, alpha, beta, bias,
false);
668 auto fn = acl_common::generateLayer<arm_compute::NENormalizationLayer>(
669 ifm_tensor->handle(), ofm_tensor->handle(), norm_info);
674void KernelGenerator::visit(
const ir::operation::LSTM &node)
677 ::arm_compute::NELSTMLayer>(node, _ctx, _tensor_reg);
680void KernelGenerator::visit(
const ir::operation::Pack &node)
682 const auto output_index{node.getOutputs().at(0)};
683 auto axis{node.param().axis};
685 const auto output_rank = _ctx.at(output_index).shape().rank();
687 std::vector<ir::OperandIndex> input_indexes;
688 for (
const auto &input_index : node.getInputs())
691 auto output = _tensor_reg->getAclTensor(output_index)->handle();
692 std::vector<arm_compute::ITensor *>
inputs;
693 for (
const auto &input_index : input_indexes)
701 for (
const auto &input_index : input_indexes)
703 const auto &input_tensor = _tensor_reg->getAclTensor(input_index);
704 if (input_tensor->num_dimensions() != input_tensor->info()->num_dimensions())
711 auto fn = acl_common::generateLayer<arm_compute::NEStackLayer>(inputs, axis, output);
714 for (
const auto &input_index : input_indexes)
716 const auto &input_tensor = _tensor_reg->getAclTensor(input_index);
717 if (input_tensor->dimension(0) == 1)
726void KernelGenerator::visit(
const ir::operation::Pad &node)
730 const auto output_index{node.getOutputs().at(0)};
731 assert(_ctx.at(pad_index).data());
733 auto rank = _ctx.at(input_index).shape().rank();
734 auto pad_base = _ctx.at(pad_index).data()->base();
736 auto input = _tensor_reg->getAclTensor(input_index)->handle();
737 auto output = _tensor_reg->getAclTensor(output_index)->handle();
739 ::arm_compute::PaddingList padding_list;
740 padding_list.resize(rank);
741 for (int32_t n = 0; n < rank; ++n)
743 const int32_t *from =
reinterpret_cast<const int32_t *
>(pad_base) + (n * 2);
746 padding_list[axis] = ::arm_compute::PaddingInfo{from[0], from[1]};
749 [[maybe_unused]]
const auto input_type = _ctx.at(input_index).typeInfo();
751 assert(
input->info()->quantization_info() ==
752 ::arm_compute::QuantizationInfo(input_type.scale(), input_type.zero_point()));
753 const auto pixel_value =
754 ::arm_compute::PixelValue(0,
input->info()->data_type(),
input->info()->quantization_info());
757 acl_common::generateLayer<arm_compute::NEPadLayer>(input, output, padding_list, pixel_value);
762void KernelGenerator::visit(
const ir::operation::Pool2D &node)
764 auto raw_fn = acl_common::kernelGenPool2D<::arm_compute::NEPoolingLayer>(
767 const auto ofm_index{node.getOutputs().at(0)};
768 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
769 const auto activation = node.param().activation;
770 _return_fn = std::make_unique<exec::FunctionSequence>(
775void KernelGenerator::visit(
const ir::operation::PReLU &node)
777 const auto ofm_index{node.getOutputs().at(0)};
781 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
782 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
783 auto alpha_tensor = _tensor_reg->getAclTensor(alpha_index);
785 auto fn = acl_common::generateLayer<arm_compute::NEPReluLayer>(
786 ifm_tensor->handle(), alpha_tensor->handle(), ofm_tensor->handle());
791void KernelGenerator::visit(
const ir::operation::Reduce &node)
793 const auto output_index{node.getOutputs().at(0)};
797 auto output_tensor = _tensor_reg->getAclTensor(output_index);
798 auto input_tensor = _tensor_reg->getAclTensor(input_index);
801 const auto &axes = _ctx.at(axes_index);
802 const auto input_rank = _ctx.at(input_index).shape().rank();
804 const auto reduce_type = node.param().reduce_type;
805 const auto keep_dims = node.param().keep_dims;
807 std::unique_ptr<::arm_compute::IFunction> fn;
810 fn = acl_common::generateLayer<arm_compute::NEReduceMean>(input_tensor->handle(), reduce_axes,
815 fn = acl_common::generateLayer<arm_compute::NEReduceSum>(input_tensor->handle(), reduce_axes,
820 fn = acl_common::generateLayer<arm_compute::NEReduceOperation>(
821 input_tensor->handle(), reduce_axes, keep_dims,
output_tensor->handle(),
827void KernelGenerator::visit(
const ir::operation::Reshape &node)
829 const auto output_index{node.getOutputs().at(0)};
832 auto output_tensor = _tensor_reg->getAclTensor(output_index);
833 auto input_tensor = _tensor_reg->getAclTensor(input_index);
835 auto fn = acl_common::generateLayer<arm_compute::NEReshapeLayer>(input_tensor->handle(),
841void KernelGenerator::visit(
const ir::operation::ResizeBilinear &node)
843 const auto ofm_index{node.getOutputs().at(0)};
846 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
847 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
849 auto fn = acl_common::generateLayer<arm_compute::NEScale>(
850 ifm_tensor->handle(), ofm_tensor->handle(),
851 ::arm_compute::ScaleKernelInfo{::arm_compute::InterpolationPolicy::BILINEAR,
852 ::arm_compute::BorderMode::REPLICATE,
853 ::arm_compute::PixelValue(0.f),
854 ::arm_compute::SamplingPolicy::TOP_LEFT, false });
859void KernelGenerator::visit(
const ir::operation::RNN &node)
862 const auto hidden_state_out_index{
867 const auto recurrent_weights_index{
872 const auto activation = node.param().activation;
874 auto output_tensor = _tensor_reg->getAclTensor(output_index);
875 auto hidden_state_out_tensor = _tensor_reg->getAclTensor(hidden_state_out_index);
877 auto input_tensor = _tensor_reg->getAclTensor(input_index);
878 auto weights_tensor = _tensor_reg->getAclTensor(weights_index);
879 auto recurrent_weights_tensor = _tensor_reg->getAclTensor(recurrent_weights_index);
880 auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
881 auto hidden_state_in_tensor = _tensor_reg->getAclTensor(hidden_state_in_index);
884 auto copy_layer = acl_common::generateLayer<arm_compute::NECopy>(
885 hidden_state_in_tensor->handle(), hidden_state_out_tensor->handle());
888 auto fn = acl_common::generateLayer<arm_compute::NERNNLayer>(
889 _tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
890 weights_tensor->handle(), recurrent_weights_tensor->handle(),
bias_tensor->handle(),
891 hidden_state_out_tensor->handle(),
output_tensor->handle(), act_info);
895void KernelGenerator::visit(
const ir::operation::Squeeze &node)
900 const auto output_index{node.getOutputs().at(0)};
902 const auto dims{node.param().dims};
903 const auto ndim{node.param().ndim};
907 auto output_tensor = _tensor_reg->getAclTensor(output_index);
908 auto input_tensor = _tensor_reg->getAclTensor(input_index);
909 auto fn = acl_common::generateLayer<arm_compute::NEReshapeLayer>(input_tensor->handle(),
914void KernelGenerator::visit(
const ir::operation::Softmax &node)
916 const auto output_index{node.getOutputs().at(0)};
918 const auto beta = node.param().beta;
920 auto output_tensor = _tensor_reg->getAclTensor(output_index);
921 auto input_tensor = _tensor_reg->getAclTensor(input_index);
924 auto fn = acl_common::generateLayer<arm_compute::NESoftmaxLayer>(
925 _tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
931void KernelGenerator::visit(
const ir::operation::SpaceToBatchND &node)
933 const auto ofm_index{node.getOutputs().at(0)};
935 const auto block_size_index{
939 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
940 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
941 auto block_size_tensor = _tensor_reg->getAclTensor(block_size_index);
942 auto paddings_tensor = _tensor_reg->getAclTensor(paddings_index);
944 assert(_ctx.at(block_size_index).data());
945 assert(_ctx.at(paddings_index).data());
947 auto fn = acl_common::generateLayer<arm_compute::NESpaceToBatchLayer>(
948 ifm_tensor->handle(), block_size_tensor->handle(), paddings_tensor->handle(),
949 ofm_tensor->handle());
954void KernelGenerator::visit(
const ir::operation::SpaceToDepth &node)
956 const auto ofm_index{node.getOutputs().at(0)};
959 auto block_size = node.param().block_size;
961 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
962 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
964 auto fn = acl_common::generateLayer<arm_compute::NESpaceToDepthLayer>(
965 ifm_tensor->handle(), ofm_tensor->handle(), block_size);
970void KernelGenerator::visit(
const ir::operation::Split &node)
976 assert(node.param().num_splits ==
static_cast<int>(node.getOutputs().size()));
977 if (!_ctx.at(axis_index).isConstant())
979 throw std::runtime_error(
"Non-constant axis_index NYI for acl_neon backend");
982 const auto ifm_rank = _ctx.at(ifm_index).shape().rank();
983 std::vector<ir::OperandIndex> output_indexes;
984 for (
const auto &output : node.getOutputs())
985 output_indexes.emplace_back(
output);
987 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
988 std::vector<arm_compute::ITensor *> output_tensors;
989 for (
const auto &ofm_ind : output_indexes)
990 output_tensors.emplace_back(_tensor_reg->getAclTensor(ofm_ind)->handle());
992 auto axis = _ctx.at(axis_index).asScalar<int32_t>();
998 acl_common::generateLayer<arm_compute::NESplit>(ifm_tensor->handle(), output_tensors, axis);
1003void KernelGenerator::visit(
const ir::operation::SquaredDifference &node)
1005 const auto ofm_index{node.getOutputs().at(0)};
1009 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
1010 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
1011 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
1013 auto fn = acl_common::generateLayer<arm_compute::NEElementwiseSquaredDiff>(
1014 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle());
1019void KernelGenerator::visit(
const ir::operation::Slice &node)
1021 const auto output_index{node.getOutputs().at(0)};
1026 auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
1027 auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
1030 int input_rank = _ctx.at(input_index).shape().rank();
1031 std::vector<int32_t> starts;
1032 std::vector<int32_t> ends;
1033 starts.resize(input_rank, 0);
1034 ends.resize(input_rank, 0);
1036 auto beginData_base = _ctx.at(begins_index).data()->base();
1037 auto sizeData_base = _ctx.at(sizes_index).data()->base();
1038 [[maybe_unused]]
const int beginData_size = _ctx.at(begins_index).shape().num_elements();
1039 [[maybe_unused]]
const int sizeData_size = _ctx.at(sizes_index).shape().num_elements();
1043 assert(_ctx.at(begins_index).typeInfo().type() == DataType::INT32);
1044 assert(_ctx.at(sizes_index).typeInfo().type() == DataType::INT32);
1045 assert(beginData_size == input_rank);
1046 assert(sizeData_size == input_rank);
1048 assert(beginData_base !=
nullptr);
1049 for (
int n = 0; n < input_rank; ++n)
1053 int32_t begin_value = *(
reinterpret_cast<const int32_t *
>(beginData_base) + n);
1054 starts[axis] = begin_value;
1056 int32_t size_value = *(
reinterpret_cast<const int32_t *
>(sizeData_base) + n);
1057 ends[axis] = begin_value + size_value;
1061 ::arm_compute::Coordinates starts_set;
1062 ::arm_compute::Coordinates ends_set;
1064 for (
size_t i = 0; i < starts.size(); ++i)
1066 starts_set.set(i, starts[i]);
1067 ends_set.set(i, ends[i]);
1070 auto fn = acl_common::generateLayer<arm_compute::NESlice>(
1071 inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set);
1076void KernelGenerator::visit(
const ir::operation::StridedSlice &node)
1078 const auto output_index{node.getOutputs().at(0)};
1084 auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
1085 auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
1088 int input_rank = _ctx.at(input_index).shape().rank();
1089 std::vector<int32_t> starts;
1090 std::vector<int32_t> ends;
1091 std::vector<int32_t> strides;
1092 starts.resize(input_rank, 0);
1093 ends.resize(input_rank, 0);
1094 strides.resize(input_rank, 0);
1096 auto startData_base = _ctx.at(starts_index).data()->base();
1097 auto endData_base = _ctx.at(ends_index).data()->base();
1098 auto stridesData_base = _ctx.at(strides_index).data()->base();
1099 [[maybe_unused]]
const int startData_size = _ctx.at(starts_index).shape().num_elements();
1100 [[maybe_unused]]
const int endData_size = _ctx.at(ends_index).shape().num_elements();
1101 [[maybe_unused]]
const int stridesData_size = _ctx.at(strides_index).shape().num_elements();
1105 assert(_ctx.at(starts_index).typeInfo().type() == DataType::INT32);
1106 assert(_ctx.at(ends_index).typeInfo().type() == DataType::INT32);
1107 assert(_ctx.at(strides_index).typeInfo().type() == DataType::INT32);
1108 assert(startData_size == input_rank);
1109 assert(endData_size == input_rank);
1110 assert(stridesData_size == input_rank);
1112 assert(startData_base !=
nullptr);
1113 for (
int n = 0; n < input_rank; ++n)
1117 int32_t start_value = *(
reinterpret_cast<const int32_t *
>(startData_base) + n);
1118 starts[axis] = start_value;
1120 int32_t end_value = *(
reinterpret_cast<const int32_t *
>(endData_base) + n);
1121 ends[axis] = end_value;
1123 int32_t strides_value = *(
reinterpret_cast<const int32_t *
>(stridesData_base) + n);
1124 strides[axis] = strides_value;
1130 const auto begin_mask = acl_common::ReorderBits<int32_t>(node.param().begin_mask, input_rank);
1131 const auto end_mask = acl_common::ReorderBits<int32_t>(node.param().end_mask, input_rank);
1132 const auto shrink_axis_mask =
1133 acl_common::ReorderBits<int32_t>(node.param().shrink_axis_mask, input_rank);
1135 ::arm_compute::Coordinates starts_set;
1136 ::arm_compute::Coordinates ends_set;
1137 ::arm_compute::BiStrides strides_set;
1139 for (
size_t i = 0; i < starts.size(); ++i)
1141 starts_set.set(i, starts[i]);
1142 ends_set.set(i, ends[i]);
1143 strides_set.set(i, strides[i]);
1147 if (
static_cast<size_t>(inputData_tensor->getShape().rank()) !=
1148 inputData_tensor->info()->num_dimensions())
1154 auto fn = acl_common::generateLayer<arm_compute::NEStridedSlice>(
1155 inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set, strides_set,
1156 begin_mask, end_mask, shrink_axis_mask);
1159 if (inputData_tensor->getShape().dim(0) == 1)
1167void KernelGenerator::visit(
const ir::operation::TransposeConv &node)
1169 const auto ofm_index{node.getOutputs().at(0)};
1173 const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature();
1174 const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature();
1175 const auto ker_shape = _ctx.at(ker_index).shape().asFeature();
1177 const auto stride = node.param().stride;
1182 ker_shape.W, ker_shape.H);
1184 uint32_t invalid_horizontal = 0;
1185 uint32_t invalid_vertical = 0;
1188 invalid_horizontal =
1189 ofm_shape.W - (1 + (ifm_shape.W - 1) * stride.horizontal) - (ker_shape.W - 1);
1190 invalid_vertical = ofm_shape.H - (1 + (ifm_shape.H - 1) * stride.vertical) - (ker_shape.H - 1);
1193 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
1194 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
1195 auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
1199 auto fn = acl_common::generateLayer<arm_compute::NETransposeConvLayer>(
1200 ifm_tensor->handle(),
ker_tensor->handle(),
nullptr, ofm_tensor->handle(), tconv_info,
1201 invalid_horizontal, invalid_vertical);
1206void KernelGenerator::visit(
const ir::operation::Transpose &node)
1208 const auto ofm_idx{node.getOutputs().at(0)};
1212 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_idx);
1213 const auto ifm_tensor = _tensor_reg->getAclTensor(ifm_idx);
1214 const auto rank = _ctx.at(ifm_idx).shape().rank();
1216 const auto &perms = _ctx.at(perm_idx);
1217 std::vector<int32_t> pv;
1218 if (perms.shape() == ir::Shape{0})
1221 std::iota(pv.begin(), pv.end(), 0);
1222 std::reverse(pv.begin(), pv.end());
1226 pv = _ctx.at(perm_idx).asVector<int32_t>();
1229 std::unique_ptr<arm_compute::IFunction> fn;
1232 fn = acl_common::generateLayer<arm_compute::NECopy>(ifm_tensor->handle(), ofm_tensor->handle());
1236 assert(pv.size() == 2 && pv.at(0) == 1 && pv.at(1) == 0);
1237 fn = acl_common::generateLayer<arm_compute::NETranspose>(ifm_tensor->handle(),
1238 ofm_tensor->handle());
1244 fn = acl_common::generateLayer<arm_compute::NEPermute>(ifm_tensor->handle(),
1245 ofm_tensor->handle(), backend_pv);
1250void KernelGenerator::visit(
const ir::operation::Unpack &node)
1253 auto axis{node.param().axis};
1255 const auto input_rank = _ctx.at(input_index).shape().rank();
1257 std::vector<ir::OperandIndex> output_indexes;
1258 for (
const auto &output_index : node.getOutputs())
1259 output_indexes.emplace_back(output_index);
1261 auto input_tensor = _tensor_reg->getAclTensor(input_index);
1262 std::vector<arm_compute::ITensor *> outputs;
1263 for (
const auto &output_index : output_indexes)
1264 outputs.emplace_back(_tensor_reg->getAclTensor(output_index)->handle());
1271 if (
static_cast<size_t>(input_tensor->getShape().rank()) !=
1272 input_tensor->info()->num_dimensions())
1279 acl_common::generateLayer<arm_compute::NEUnstack>(input_tensor->handle(), outputs, axis);
1282 if (input_tensor->getShape().dim(0) == 1)
1290void KernelGenerator::visit(
const ir::operation::ExpandDims &node)
1292 const auto output_index{node.getOutputs().at(0)};
1295 auto output_tensor = _tensor_reg->getAclTensor(output_index);
1296 auto input_tensor = _tensor_reg->getAclTensor(input_index);
1298 auto fn = acl_common::generateLayer<arm_compute::NEReshapeLayer>(input_tensor->handle(),
1304void KernelGenerator::visit(
const ir::operation::Comparison &node)
1306 const auto output_index{node.getOutputs().at(0)};
1310 const auto comparison_type = node.param().comparison_type;
1312 auto output_tensor = _tensor_reg->getAclTensor(output_index);
1313 auto input0_tensor = _tensor_reg->getAclTensor(input0_index);
1314 auto input1_tensor = _tensor_reg->getAclTensor(input1_index);
1316 auto fn = acl_common::generateLayer<arm_compute::NEElementwiseComparison>(
1317 input0_tensor->handle(), input1_tensor->handle(),
output_tensor->handle(),
1318 (arm_compute::ComparisonOperation)comparison_type);
1323void KernelGenerator::visit(
const ir::operation::OneHot &node)
1325 const auto out_idx{node.getOutputs().at(0)};
1332 auto indices_tensor = _tensor_reg->getAclTensor(indices_idx);
1333 auto depth_tensor = _tensor_reg->getAclTensor(depth_idx);
1334 auto onvalue_tensor = _tensor_reg->getAclTensor(onvalue_idx);
1335 auto offvalue_tensor = _tensor_reg->getAclTensor(offvalue_idx);
1337 const size_t output_rank = _ctx.at(out_idx).shape().rank();
1338 int32_t axis = node.param().axis == -1 ? output_rank - 1 : node.param().axis;
1341 auto fn = acl_common::generateLayer<arm_compute::NEOneHot>(
1342 indices_tensor->handle(), depth_tensor->handle(), onvalue_tensor->handle(),
This file defines NopFunction.
Class to run FullyConnected Layer after reshaping input tensor.
uint32_t value(void) const
static std::unique_ptr< exec::IFunction > generate(ir::Activation code, T_Tensor *ifm_alloc)
Tensor registry class for acl backends.
std::unique_ptr< exec::FunctionSequence > generate(ir::OperationIndex ind) override
KernelGenerator(const ir::Graph &graph, const std::shared_ptr< TensorBuilder > &tensor_builder, const std::shared_ptr< acl_common::AclTensorRegistry< TensorManager > > &_tensor_reg)
std::unique_ptr< exec::IFunction > _return_fn
std::unique_ptr< exec::IFunction > releaseFunction()
const Operations & operations() const override
const OperandIndex & at(IOIndex set_index) const
const OperandIndexSequence & getOutputs() const override
OperandIndexSequence & getInputs()
const Param & param() const
const Object & at(const Index &index) const
Get the object that is associated with the given index.
#define VERBOSE(name, lv)
std::vector< int > dims(const std::string &src)
arm_compute::PoolingType convertPoolType(ir::operation::Pool2D::PoolType pool_type_ir)
ARMComputeAxis ToARMComputeAxis(uint32_t rank, uint32_t axis)
inline ::arm_compute::PermutationVector getARMComputePermutationVector(uint32_t rank, const std::vector< int32_t > runtime_pv)
std::unique_ptr< exec::IFunction > kernelGenLSTM(const ir::operation::LSTM &node, const ir::Operands &operands, const std::shared_ptr< T_TensorRegistry > &tensor_reg)
::arm_compute::ActivationLayerInfo asActivationLayerInfo(const ir::Activation act_code)
arm_compute::ReductionOperation convertReduceType(ir::operation::Reduce::ReduceType reduce_type_ir)
arm_compute::Coordinates asCoordinates(const ir::Operand &operand, int32_t rank)
void enableDimCorrection(IACLTensor *tensor)
arm_compute::Size2D asDilation(uint32_t dilation_width, uint32_t dilation_height)
::arm_compute::PadStrideInfo asPadStrideInfo(const ir::ExplicitPadding &padding, const ir::Stride &stride)
std::unique_ptr< AclFunction > asAclFunction(std::unique_ptr<::arm_compute::IFunction > &&layer)
std::unique_ptr< exec::IFunction > kernelGenFullyConnected(const ir::operation::FullyConnected &node, const ir::Operands &operands, const std::shared_ptr< T_TensorBuilder > &tensor_builder, const std::shared_ptr< T_TensorRegistry > &tensor_reg)
::arm_compute::DataType asDataType(const ir::DataType type)
void disableDimCorrection(IACLTensor *tensor)
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)