19#include <arm_compute/runtime/NEON/NEFunctions.h>
28#include "ir/DataType.h"
41using ::onert::backend::acl_common::asAclFunction;
46 const ir::Graph &graph,
const std::shared_ptr<TensorBuilder> &tensor_builder,
48 : basic::KernelGeneratorBase{graph}, _ctx(graph.operands()), _operations_ctx(graph.operations()),
49 _tensor_builder(tensor_builder), _tensor_reg(tensor_reg)
56 auto ret = std::make_unique<exec::FunctionSequence>();
57 ret->enableDynamicShapeInferer(
false);
71 const auto ifm_rank = _ctx.
at(ifm_index).shape().rank();
73 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
74 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
76 int axis_value = _ctx.
at(axis_index).asScalar<int32_t>();
79 axis_value += ifm_rank;
81 assert(axis_value >= 0 && axis_value < ifm_rank);
83 auto reduce_type = node.
param().
is_arg_max ? ::arm_compute::ReductionOperation::ARG_IDX_MAX
84 : ::arm_compute::ReductionOperation::ARG_IDX_MIN;
86 auto fn = acl_common::generateLayer<arm_compute::NEArgMinMaxLayer>(
87 ifm_tensor->handle(), fixed_axis, ofm_tensor->handle(), reduce_type);
92void KernelGenerator::visit(
const ir::operation::BatchToSpaceND &node)
94 const auto ofm_index{node.getOutputs().at(0)};
96 const auto block_size_index{
99 const auto NNApiInputs = 2;
100 if (node.getInputs().size() != NNApiInputs)
103 if (!_ctx.
at(crops_index).isConstant())
105 throw std::runtime_error(
"Non-constant crops NYI for acl_neon backend BatchToSpaceND");
108 auto crops = _ctx.
at(crops_index).asVector<int32_t>();
109 for (
auto &&crop : crops)
113 throw std::runtime_error(
"Non-zero crops NYI for acl_neon backend BatchToSpaceND");
118 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
119 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
121 if (!_ctx.
at(block_size_index).data())
122 throw std::runtime_error(
"ACL NEON does not support dynamic block size for BatchToSpaceND");
124 auto block = _ctx.
at(block_size_index).asVector<int32_t>();
125 int32_t height = block[0];
126 int32_t width = block[1];
128 auto fn = acl_common::generateLayer<arm_compute::NEBatchToSpaceLayer>(
129 ifm_tensor->handle(), width, height, ofm_tensor->handle());
134void KernelGenerator::visit(
const ir::operation::BinaryArithmetic &node)
136 const auto ofm_index{node.getOutputs().at(0)};
140 const auto activation = node.param().activation;
142 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
143 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
144 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
146 std::unique_ptr<arm_compute::IFunction> fn;
147 switch (node.param().arithmetic_type)
151 arm_compute::NEArithmeticAddition::validate(lhs_tensor->info(), rhs_tensor->info(),
153 arm_compute::ConvertPolicy::SATURATE)
155 fn = acl_common::generateLayer<arm_compute::NEArithmeticAddition>(
156 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
157 arm_compute::ConvertPolicy::SATURATE);
162 arm_compute::NEArithmeticSubtraction::validate(lhs_tensor->info(), rhs_tensor->info(),
164 arm_compute::ConvertPolicy::SATURATE)
166 fn = acl_common::generateLayer<arm_compute::NEArithmeticSubtraction>(
167 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
168 arm_compute::ConvertPolicy::SATURATE);
173 arm_compute::NEPixelWiseMultiplication::validate(
174 lhs_tensor->info(), rhs_tensor->info(), ofm_tensor->info(), 1.0,
175 arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO)
178 fn = acl_common::generateLayer<arm_compute::NEPixelWiseMultiplication>(
179 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(), 1.0,
180 arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO);
185 arm_compute::NEElementwiseDivision::validate(lhs_tensor->info(), rhs_tensor->info(),
188 fn = acl_common::generateLayer<arm_compute::NEElementwiseDivision>(
189 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle());
193 assert(
false &&
"The BinaryArithmetic operation supports only binary arithmetic operations");
196 _return_fn = std::make_unique<exec::FunctionSequence>(
200void KernelGenerator::visit(
const ir::operation::Conv2D &node)
202 using ir::operation::Conv2D;
204 const auto ofm_index{node.getOutputs().at(0)};
205 const auto ifm_index{node.getInputs().at(Conv2D::Input::INPUT)};
206 const auto ker_index{node.getInputs().at(Conv2D::Input::KERNEL)};
207 const auto bias_index{node.getInputs().at(Conv2D::Input::BIAS)};
209 const auto ofm_shape = _ctx.
at(ofm_index).shape().asFeature();
210 const auto ifm_shape = _ctx.
at(ifm_index).shape().asFeature();
212 const auto &ker_shape = _ctx.
at(ker_index).shape();
213 const auto ker_height = ker_shape.dim(1);
214 const auto ker_width = ker_shape.dim(2);
216 const auto stride = node.param().stride;
218 ir::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, ker_width, ker_height);
219 const auto activation = node.param().activation;
221 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
222 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
223 auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
224 auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
229 auto fn = acl_common::generateLayer<arm_compute::NEConvolutionLayer>(
230 _tensor_builder->acl_tensor_manager()->internal_buffer_manager(), ifm_tensor->handle(),
231 ker_tensor->handle(), bias_tensor->handle(), ofm_tensor->handle(), conv_info,
232 ::arm_compute::WeightsInfo(), ::arm_compute::Size2D(1U, 1U), act_info);
237void KernelGenerator::visit(
const ir::operation::DepthToSpace &node)
239 const auto output_index{node.getOutputs().at(0)};
242 auto block_size = node.param().block_size;
243 assert(block_size > 0);
245 auto output_tensor = _tensor_reg->getAclTensor(output_index);
246 auto input_tensor = _tensor_reg->getAclTensor(input_index);
248 auto fn = acl_common::generateLayer<arm_compute::NEDepthToSpaceLayer>(
249 input_tensor->handle(),
output_tensor->handle(), block_size);
254void KernelGenerator::visit(
const ir::operation::DepthwiseConv2D &node)
256 using ir::operation::DepthwiseConv2D;
258 const auto ofm_index{node.getOutputs().at(0)};
259 const auto ifm_index{node.getInputs().at(DepthwiseConv2D::Input::INPUT)};
260 const auto ker_index{node.getInputs().at(DepthwiseConv2D::Input::KERNEL)};
261 const auto bias_index{node.getInputs().at(DepthwiseConv2D::Input::BIAS)};
263 const auto ifm_shape = _ctx.
at(ifm_index).shape().asFeature();
264 const auto ofm_shape = _ctx.
at(ofm_index).shape().asFeature();
266 const auto &ker_shape = _ctx.
at(ker_index).shape();
267 const auto ker_height = ker_shape.dim(1);
268 const auto ker_width = ker_shape.dim(2);
270 const auto stride = node.param().stride;
271 const auto dilation = node.param().dilation;
274 dilation.width_factor, dilation.height_factor);
275 const auto multiplier = node.param().multiplier;
276 const auto activation = node.param().activation;
278 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
279 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
280 auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
281 auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
287 auto fn = acl_common::generateLayer<arm_compute::NEDepthwiseConvolutionLayer>(
288 ifm_tensor->handle(), ker_tensor->handle(), bias_tensor->handle(), ofm_tensor->handle(),
289 conv_info, multiplier, act_info, dilation_info);
294void KernelGenerator::visit(
const ir::operation::Concat &node)
296 const auto ofm_index{node.getOutputs().at(0)};
298 std::vector<ir::OperandIndex> input_indexes;
299 for (
const auto &input : node.getInputs())
300 input_indexes.emplace_back(
input);
302 const auto axis = node.param().axis;
305 bool eliminated = _tensor_builder->areSubTensorsOf(ofm_index, node.getInputs());
309 VERBOSE(acl_neon_KernelGenerator_Concat) <<
"Concat eliminated" << std::endl;
310 _return_fn = std::make_unique<exec::NopFunction>();
315 std::vector<const ::arm_compute::ITensor *> input_tensors;
316 for (
const auto &ifm_ind : input_indexes)
317 input_tensors.emplace_back(_tensor_reg->getAclTensor(ifm_ind)->handle());
319 std::unique_ptr<::arm_compute::IFunction> fn;
320 if (input_indexes.size() < 2)
322 ::arm_compute::ITensor *input_tesor = _tensor_reg->getAclTensor(input_indexes.at(0))->handle();
323 fn = acl_common::generateLayer<arm_compute::NECopy>(input_tesor,
output_tensor->handle());
327 const auto rank = _ctx.
at(ofm_index).shape().rank();
329 fn = acl_common::generateLayer<arm_compute::NEConcatenateLayer>(
336void KernelGenerator::visit(
const ir::operation::ElementwiseActivation &node)
338 const auto ofm_index{node.getOutputs().at(0)};
341 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
342 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
344 const ::arm_compute::ActivationLayerInfo act_info =
347 std::unique_ptr<arm_compute::IFunction> fn =
348 acl_common::generateLayer<arm_compute::NEActivationLayer>(ifm_tensor->handle(),
349 ofm_tensor->handle(), act_info);
354void KernelGenerator::visit(
const ir::operation::ElementwiseBinary &node)
356 const auto output_index{node.getOutputs().at(0)};
360 auto output_tensor = _tensor_reg->getAclTensor(output_index);
361 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
362 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
364 std::unique_ptr<arm_compute::IFunction> fn;
365 switch (node.param().op_type)
369 fn = acl_common::generateLayer<arm_compute::NELogicalAnd>(
370 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
375 fn = acl_common::generateLayer<arm_compute::NELogicalOr>(
376 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
381 fn = acl_common::generateLayer<arm_compute::NEElementwiseMax>(
382 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
387 fn = acl_common::generateLayer<arm_compute::NEElementwiseMin>(
388 lhs_tensor->handle(), rhs_tensor->handle(),
output_tensor->handle());
393 std::string err_msg(
"acl_neon KernelGenerator : " + node.name() +
394 "is not elementwise-binary operations");
395 assert(
false && err_msg.c_str());
402void KernelGenerator::visit(
const ir::operation::ElementwiseUnary &node)
404 const auto output_index{node.getOutputs().at(0)};
407 auto output_tensor = _tensor_reg->getAclTensor(output_index);
408 auto input_tensor = _tensor_reg->getAclTensor(input_index);
410 std::unique_ptr<arm_compute::IFunction> fn;
411 switch (node.param().op_type)
415 const ::arm_compute::ActivationLayerInfo act_info{
416 ::arm_compute::ActivationLayerInfo::ActivationFunction::ABS};
418 fn = acl_common::generateLayer<arm_compute::NEActivationLayer>(
426 fn = acl_common::generateLayer<arm_compute::NECopy>(input_tensor->handle(),
429 else if (_ctx.
at(input_index).typeInfo().
type() == ir::DataType::BOOL8)
431 fn = acl_common::generateLayer<arm_compute::NECastBool>(input_tensor->handle(),
436 fn = acl_common::generateLayer<arm_compute::NECast>(
437 input_tensor->handle(),
output_tensor->handle(), arm_compute::ConvertPolicy::SATURATE);
443 fn = acl_common::generateLayer<arm_compute::NEDequantizationLayer>(input_tensor->handle(),
449 fn = acl_common::generateLayer<arm_compute::NEExpLayer>(input_tensor->handle(),
455 fn = acl_common::generateLayer<arm_compute::NEFloor>(input_tensor->handle(),
461 fn = acl_common::generateLayer<arm_compute::NEBitwiseNot>(input_tensor->handle(),
467 fn = acl_common::generateLayer<arm_compute::NENegLayer>(input_tensor->handle(),
473 fn = acl_common::generateLayer<arm_compute::NERsqrtLayer>(input_tensor->handle(),
479 const ::arm_compute::ActivationLayerInfo act_info{
480 ::arm_compute::ActivationLayerInfo::ActivationFunction::SQRT};
482 fn = acl_common::generateLayer<arm_compute::NEActivationLayer>(
488 throw std::runtime_error(
"acl_neon KernelGenerator : " + node.name() +
489 "is not supported yet");
496void KernelGenerator::visit(
const ir::operation::EmbeddingLookup &node)
498 const auto output_index{node.getOutputs().at(0)};
502 auto output_tensor = _tensor_reg->getAclTensor(output_index);
503 auto lookups_tensor = _tensor_reg->getAclTensor(lookups_index);
504 auto values_tensor = _tensor_reg->getAclTensor(values_index);
506 auto fn = acl_common::generateLayer<arm_compute::NEEmbeddingLookup>(
507 values_tensor->handle(),
output_tensor->handle(), lookups_tensor->handle());
512void KernelGenerator::visit(
const ir::operation::FullyConnected &node)
514 const auto output_index{node.getOutputs().at(0)};
515 auto output_tensor = _tensor_reg->getAclTensor(output_index);
516 const auto activation = node.param().activation;
518 throw std::runtime_error(
519 "KernelGenerator(acl_neon): FullyConnected 16x1Float32 weights is not supported.");
523 node, _ctx, _tensor_builder, _tensor_reg);
524 _return_fn = std::make_unique<exec::FunctionSequence>(
528void KernelGenerator::visit(
const ir::operation::HashtableLookup &node)
537 auto output_tensor = _tensor_reg->getAclTensor(output_index);
538 auto hits_tensor = _tensor_reg->getAclTensor(hits_index);
540 auto lookups_tensor = _tensor_reg->getAclTensor(lookups_index);
541 auto keys_tensor = _tensor_reg->getAclTensor(keys_index);
542 auto values_tensor = _tensor_reg->getAclTensor(values_index);
544 auto fn = acl_common::generateLayer<arm_compute::NEHashtableLookup>(
545 lookups_tensor->handle(), keys_tensor->handle(), values_tensor->handle(),
551void KernelGenerator::visit(
const ir::operation::Gather &node)
553 const auto ofm_index{node.getOutputs().at(0)};
558 const auto ifm_rank = _ctx.at(ifm_index).shape().rank();
559 const auto axis_raw = node.param().axis;
560 const auto axis_value = (axis_raw < 0 ? (ifm_rank + axis_raw) : axis_raw);
564 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
565 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
566 auto indices_tensor = _tensor_reg->getAclTensor(indices_index);
570 assert(n == ifm_tensor->num_dimensions());
571 size_t k = _ctx.at(indices_index).shape().rank();
572 assert(k == indices_tensor->num_dimensions());
575 if (n != ifm_tensor->info()->num_dimensions())
580 if (k != indices_tensor->info()->num_dimensions())
586 auto fn = acl_common::generateLayer<arm_compute::NEGatherEx>(
587 ifm_tensor->handle(), indices_tensor->handle(), ofm_tensor->handle(), axis);
590 if (ifm_tensor->dimension(0) == 1)
594 if (indices_tensor->dimension(0) == 1)
602void KernelGenerator::visit(
const ir::operation::InstanceNorm &node)
604 const auto ofm_index{node.getOutputs().at(0)};
609 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
610 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
611 auto gamma_tensor = _tensor_reg->getAclTensor(gamma_index);
612 auto beta_tensor = _tensor_reg->getAclTensor(beta_index);
613 auto epsilon = node.param().epsilon;
614 auto activation = node.param().activation;
616 auto fn = acl_common::generateLayer<arm_compute::NEInstanceNormalizationLayerEx>(
617 ifm_tensor->handle(), ofm_tensor->handle(), gamma_tensor->handle(), beta_tensor->handle(),
620 _return_fn = std::make_unique<exec::FunctionSequence>(
624void KernelGenerator::visit(
const ir::operation::L2Normalization &node)
626 const auto ofm_index{node.getOutputs().at(0)};
634 const auto &ifm_shape = _ctx.at(ifm_index).shape();
636 const auto normalization_axis = _ctx.at(ifm_index).shape().rank() - 1;
638 2 * ifm_shape.dim(normalization_axis) + 1;
643 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
644 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
646 const auto norm_info = ::arm_compute::NormalizationLayerInfo(::arm_compute::NormType::CROSS_MAP,
647 radius, alpha, beta, bias,
false);
649 auto fn = acl_common::generateLayer<arm_compute::NENormalizationLayer>(
650 ifm_tensor->handle(), ofm_tensor->handle(), norm_info);
655void KernelGenerator::visit(
const ir::operation::LocalResponseNormalization &node)
657 const auto ofm_index{node.getOutputs().at(0)};
658 const auto ifm_index{
661 auto radius = node.param().radius;
662 auto alpha = node.param().alpha;
663 auto beta = node.param().beta;
664 auto bias = node.param().bias;
666 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
667 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
669 const auto norm_info = ::arm_compute::NormalizationLayerInfo(
670 ::arm_compute::NormType::CROSS_MAP, radius * 2 + 1, alpha, beta, bias,
false);
672 auto fn = acl_common::generateLayer<arm_compute::NENormalizationLayer>(
673 ifm_tensor->handle(), ofm_tensor->handle(), norm_info);
678void KernelGenerator::visit(
const ir::operation::LSTM &node)
681 ::arm_compute::NELSTMLayer>(node, _ctx, _tensor_reg);
684void KernelGenerator::visit(
const ir::operation::Pack &node)
686 const auto output_index{node.getOutputs().at(0)};
687 auto axis{node.param().axis};
689 const auto output_rank = _ctx.at(output_index).shape().rank();
691 std::vector<ir::OperandIndex> input_indexes;
692 for (
const auto &input_index : node.getInputs())
695 auto output = _tensor_reg->getAclTensor(output_index)->handle();
696 std::vector<arm_compute::ITensor *>
inputs;
697 for (
const auto &input_index : input_indexes)
705 for (
const auto &input_index : input_indexes)
707 const auto &input_tensor = _tensor_reg->getAclTensor(input_index);
708 if (input_tensor->num_dimensions() != input_tensor->info()->num_dimensions())
715 auto fn = acl_common::generateLayer<arm_compute::NEStackLayer>(inputs, axis, output);
718 for (
const auto &input_index : input_indexes)
720 const auto &input_tensor = _tensor_reg->getAclTensor(input_index);
721 if (input_tensor->dimension(0) == 1)
730void KernelGenerator::visit(
const ir::operation::Pad &node)
734 const auto output_index{node.getOutputs().at(0)};
735 assert(_ctx.at(pad_index).data());
737 auto rank = _ctx.at(input_index).shape().rank();
738 auto pad_base = _ctx.at(pad_index).data()->base();
740 auto input = _tensor_reg->getAclTensor(input_index)->handle();
741 auto output = _tensor_reg->getAclTensor(output_index)->handle();
743 ::arm_compute::PaddingList padding_list;
744 padding_list.resize(rank);
745 for (int32_t n = 0; n < rank; ++n)
747 const int32_t *from =
reinterpret_cast<const int32_t *
>(pad_base) + (n * 2);
750 padding_list[axis] = ::arm_compute::PaddingInfo{from[0], from[1]};
753 [[maybe_unused]]
const auto input_type = _ctx.at(input_index).typeInfo();
755 assert(
input->info()->quantization_info() ==
756 ::arm_compute::QuantizationInfo(input_type.scale(), input_type.zero_point()));
757 const auto pixel_value =
758 ::arm_compute::PixelValue(0,
input->info()->data_type(),
input->info()->quantization_info());
761 acl_common::generateLayer<arm_compute::NEPadLayer>(input, output, padding_list, pixel_value);
766void KernelGenerator::visit(
const ir::operation::Pool2D &node)
768 auto raw_fn = acl_common::kernelGenPool2D<::arm_compute::NEPoolingLayer>(
771 const auto ofm_index{node.getOutputs().at(0)};
772 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
773 const auto activation = node.param().activation;
774 _return_fn = std::make_unique<exec::FunctionSequence>(
779void KernelGenerator::visit(
const ir::operation::PReLU &node)
781 const auto ofm_index{node.getOutputs().at(0)};
785 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
786 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
787 auto alpha_tensor = _tensor_reg->getAclTensor(alpha_index);
789 auto fn = acl_common::generateLayer<arm_compute::NEPReluLayer>(
790 ifm_tensor->handle(), alpha_tensor->handle(), ofm_tensor->handle());
795void KernelGenerator::visit(
const ir::operation::Reduce &node)
797 const auto output_index{node.getOutputs().at(0)};
801 auto output_tensor = _tensor_reg->getAclTensor(output_index);
802 auto input_tensor = _tensor_reg->getAclTensor(input_index);
805 const auto &axes = _ctx.at(axes_index);
806 const auto input_rank = _ctx.at(input_index).shape().rank();
808 const auto reduce_type = node.param().reduce_type;
809 const auto keep_dims = node.param().keep_dims;
811 std::unique_ptr<::arm_compute::IFunction> fn;
814 fn = acl_common::generateLayer<arm_compute::NEReduceMean>(input_tensor->handle(), reduce_axes,
819 fn = acl_common::generateLayer<arm_compute::NEReduceSum>(input_tensor->handle(), reduce_axes,
824 fn = acl_common::generateLayer<arm_compute::NEReduceOperation>(
825 input_tensor->handle(), reduce_axes, keep_dims,
output_tensor->handle(),
831void KernelGenerator::visit(
const ir::operation::Reshape &node)
833 const auto output_index{node.getOutputs().at(0)};
836 auto output_tensor = _tensor_reg->getAclTensor(output_index);
837 auto input_tensor = _tensor_reg->getAclTensor(input_index);
839 auto fn = acl_common::generateLayer<arm_compute::NEReshapeLayer>(input_tensor->handle(),
845void KernelGenerator::visit(
const ir::operation::ResizeBilinear &node)
847 const auto ofm_index{node.getOutputs().at(0)};
850 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
851 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
853 auto fn = acl_common::generateLayer<arm_compute::NEScale>(
854 ifm_tensor->handle(), ofm_tensor->handle(),
855 ::arm_compute::ScaleKernelInfo{::arm_compute::InterpolationPolicy::BILINEAR,
856 ::arm_compute::BorderMode::REPLICATE,
857 ::arm_compute::PixelValue(0.f),
858 ::arm_compute::SamplingPolicy::TOP_LEFT, false });
863void KernelGenerator::visit(
const ir::operation::RNN &node)
866 const auto hidden_state_out_index{
871 const auto recurrent_weights_index{
876 const auto activation = node.param().activation;
878 auto output_tensor = _tensor_reg->getAclTensor(output_index);
879 auto hidden_state_out_tensor = _tensor_reg->getAclTensor(hidden_state_out_index);
881 auto input_tensor = _tensor_reg->getAclTensor(input_index);
882 auto weights_tensor = _tensor_reg->getAclTensor(weights_index);
883 auto recurrent_weights_tensor = _tensor_reg->getAclTensor(recurrent_weights_index);
884 auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
885 auto hidden_state_in_tensor = _tensor_reg->getAclTensor(hidden_state_in_index);
888 auto copy_layer = acl_common::generateLayer<arm_compute::NECopy>(
889 hidden_state_in_tensor->handle(), hidden_state_out_tensor->handle());
892 auto fn = acl_common::generateLayer<arm_compute::NERNNLayer>(
893 _tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
894 weights_tensor->handle(), recurrent_weights_tensor->handle(), bias_tensor->handle(),
895 hidden_state_out_tensor->handle(),
output_tensor->handle(), act_info);
899void KernelGenerator::visit(
const ir::operation::Squeeze &node)
904 const auto output_index{node.getOutputs().at(0)};
906 const auto dims{node.param().dims};
907 const auto ndim{node.param().ndim};
911 auto output_tensor = _tensor_reg->getAclTensor(output_index);
912 auto input_tensor = _tensor_reg->getAclTensor(input_index);
913 auto fn = acl_common::generateLayer<arm_compute::NEReshapeLayer>(input_tensor->handle(),
918void KernelGenerator::visit(
const ir::operation::Softmax &node)
920 const auto output_index{node.getOutputs().at(0)};
922 const auto beta = node.param().beta;
924 auto output_tensor = _tensor_reg->getAclTensor(output_index);
925 auto input_tensor = _tensor_reg->getAclTensor(input_index);
928 auto fn = acl_common::generateLayer<arm_compute::NESoftmaxLayer>(
929 _tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
935void KernelGenerator::visit(
const ir::operation::SpaceToBatchND &node)
937 const auto ofm_index{node.getOutputs().at(0)};
939 const auto block_size_index{
943 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
944 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
945 auto block_size_tensor = _tensor_reg->getAclTensor(block_size_index);
946 auto paddings_tensor = _tensor_reg->getAclTensor(paddings_index);
948 assert(_ctx.at(block_size_index).data());
949 assert(_ctx.at(paddings_index).data());
951 auto fn = acl_common::generateLayer<arm_compute::NESpaceToBatchLayer>(
952 ifm_tensor->handle(), block_size_tensor->handle(), paddings_tensor->handle(),
953 ofm_tensor->handle());
958void KernelGenerator::visit(
const ir::operation::SpaceToDepth &node)
960 const auto ofm_index{node.getOutputs().at(0)};
963 auto block_size = node.param().block_size;
965 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
966 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
968 auto fn = acl_common::generateLayer<arm_compute::NESpaceToDepthLayer>(
969 ifm_tensor->handle(), ofm_tensor->handle(), block_size);
974void KernelGenerator::visit(
const ir::operation::Split &node)
980 assert(node.param().num_splits ==
static_cast<int>(node.getOutputs().size()));
981 if (!_ctx.at(axis_index).isConstant())
983 throw std::runtime_error(
"Non-constant axis_index NYI for acl_neon backend");
986 const auto ifm_rank = _ctx.at(ifm_index).shape().rank();
987 std::vector<ir::OperandIndex> output_indexes;
988 for (
const auto &output : node.getOutputs())
989 output_indexes.emplace_back(
output);
991 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
992 std::vector<arm_compute::ITensor *> output_tensors;
993 for (
const auto &ofm_ind : output_indexes)
994 output_tensors.emplace_back(_tensor_reg->getAclTensor(ofm_ind)->handle());
996 auto axis = _ctx.at(axis_index).asScalar<int32_t>();
1002 acl_common::generateLayer<arm_compute::NESplit>(ifm_tensor->handle(), output_tensors, axis);
1007void KernelGenerator::visit(
const ir::operation::SquaredDifference &node)
1009 const auto ofm_index{node.getOutputs().at(0)};
1013 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
1014 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
1015 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
1017 auto fn = acl_common::generateLayer<arm_compute::NEElementwiseSquaredDiff>(
1018 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle());
1023void KernelGenerator::visit(
const ir::operation::Slice &node)
1025 const auto output_index{node.getOutputs().at(0)};
1030 auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
1031 auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
1034 int input_rank = _ctx.at(input_index).shape().rank();
1035 std::vector<int32_t> starts;
1036 std::vector<int32_t> ends;
1037 starts.resize(input_rank, 0);
1038 ends.resize(input_rank, 0);
1040 auto beginData_base = _ctx.at(begins_index).data()->base();
1041 auto sizeData_base = _ctx.at(sizes_index).data()->base();
1042 [[maybe_unused]]
const int beginData_size = _ctx.at(begins_index).shape().num_elements();
1043 [[maybe_unused]]
const int sizeData_size = _ctx.at(sizes_index).shape().num_elements();
1047 assert(_ctx.at(begins_index).typeInfo().type() == DataType::INT32);
1048 assert(_ctx.at(sizes_index).typeInfo().type() == DataType::INT32);
1049 assert(beginData_size == input_rank);
1050 assert(sizeData_size == input_rank);
1052 assert(beginData_base !=
nullptr);
1053 for (
int n = 0; n < input_rank; ++n)
1057 int32_t begin_value = *(
reinterpret_cast<const int32_t *
>(beginData_base) + n);
1058 starts[axis] = begin_value;
1060 int32_t size_value = *(
reinterpret_cast<const int32_t *
>(sizeData_base) + n);
1061 ends[axis] = begin_value + size_value;
1065 ::arm_compute::Coordinates starts_set;
1066 ::arm_compute::Coordinates ends_set;
1068 for (
size_t i = 0; i < starts.size(); ++i)
1070 starts_set.set(i, starts[i]);
1071 ends_set.set(i, ends[i]);
1074 auto fn = acl_common::generateLayer<arm_compute::NESlice>(
1075 inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set);
1080void KernelGenerator::visit(
const ir::operation::StridedSlice &node)
1082 const auto output_index{node.getOutputs().at(0)};
1088 auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
1089 auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
1092 int input_rank = _ctx.at(input_index).shape().rank();
1093 std::vector<int32_t> starts;
1094 std::vector<int32_t> ends;
1095 std::vector<int32_t> strides;
1096 starts.resize(input_rank, 0);
1097 ends.resize(input_rank, 0);
1098 strides.resize(input_rank, 0);
1100 auto startData_base = _ctx.at(starts_index).data()->base();
1101 auto endData_base = _ctx.at(ends_index).data()->base();
1102 auto stridesData_base = _ctx.at(strides_index).data()->base();
1103 [[maybe_unused]]
const int startData_size = _ctx.at(starts_index).shape().num_elements();
1104 [[maybe_unused]]
const int endData_size = _ctx.at(ends_index).shape().num_elements();
1105 [[maybe_unused]]
const int stridesData_size = _ctx.at(strides_index).shape().num_elements();
1109 assert(_ctx.at(starts_index).typeInfo().type() == DataType::INT32);
1110 assert(_ctx.at(ends_index).typeInfo().type() == DataType::INT32);
1111 assert(_ctx.at(strides_index).typeInfo().type() == DataType::INT32);
1112 assert(startData_size == input_rank);
1113 assert(endData_size == input_rank);
1114 assert(stridesData_size == input_rank);
1116 assert(startData_base !=
nullptr);
1117 for (
int n = 0; n < input_rank; ++n)
1121 int32_t start_value = *(
reinterpret_cast<const int32_t *
>(startData_base) + n);
1122 starts[axis] = start_value;
1124 int32_t end_value = *(
reinterpret_cast<const int32_t *
>(endData_base) + n);
1125 ends[axis] = end_value;
1127 int32_t strides_value = *(
reinterpret_cast<const int32_t *
>(stridesData_base) + n);
1128 strides[axis] = strides_value;
1134 const auto begin_mask = acl_common::ReorderBits<int32_t>(node.param().begin_mask, input_rank);
1135 const auto end_mask = acl_common::ReorderBits<int32_t>(node.param().end_mask, input_rank);
1136 const auto shrink_axis_mask =
1137 acl_common::ReorderBits<int32_t>(node.param().shrink_axis_mask, input_rank);
1139 ::arm_compute::Coordinates starts_set;
1140 ::arm_compute::Coordinates ends_set;
1141 ::arm_compute::BiStrides strides_set;
1143 for (
size_t i = 0; i < starts.size(); ++i)
1145 starts_set.set(i, starts[i]);
1146 ends_set.set(i, ends[i]);
1147 strides_set.set(i, strides[i]);
1151 if (
static_cast<size_t>(inputData_tensor->getShape().rank()) !=
1152 inputData_tensor->info()->num_dimensions())
1158 auto fn = acl_common::generateLayer<arm_compute::NEStridedSlice>(
1159 inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set, strides_set,
1160 begin_mask, end_mask, shrink_axis_mask);
1163 if (inputData_tensor->getShape().dim(0) == 1)
1171void KernelGenerator::visit(
const ir::operation::TransposeConv &node)
1173 const auto ofm_index{node.getOutputs().at(0)};
1177 const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature();
1178 const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature();
1179 const auto ker_shape = _ctx.at(ker_index).shape().asFeature();
1181 const auto stride = node.param().stride;
1186 ker_shape.W, ker_shape.H);
1188 uint32_t invalid_horizontal = 0;
1189 uint32_t invalid_vertical = 0;
1192 invalid_horizontal =
1193 ofm_shape.W - (1 + (ifm_shape.W - 1) * stride.horizontal) - (ker_shape.W - 1);
1194 invalid_vertical = ofm_shape.H - (1 + (ifm_shape.H - 1) * stride.vertical) - (ker_shape.H - 1);
1197 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
1198 auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
1199 auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
1203 auto fn = acl_common::generateLayer<arm_compute::NETransposeConvLayer>(
1204 ifm_tensor->handle(), ker_tensor->handle(),
nullptr, ofm_tensor->handle(), tconv_info,
1205 invalid_horizontal, invalid_vertical);
1210void KernelGenerator::visit(
const ir::operation::Transpose &node)
1212 const auto ofm_idx{node.getOutputs().at(0)};
1216 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_idx);
1217 const auto ifm_tensor = _tensor_reg->getAclTensor(ifm_idx);
1218 const auto rank = _ctx.at(ifm_idx).shape().rank();
1220 const auto &perms = _ctx.at(perm_idx);
1221 std::vector<int32_t> pv;
1222 if (perms.shape() == ir::Shape{0})
1225 std::iota(pv.begin(), pv.end(), 0);
1226 std::reverse(pv.begin(), pv.end());
1230 pv = _ctx.at(perm_idx).asVector<int32_t>();
1233 std::unique_ptr<arm_compute::IFunction> fn;
1236 fn = acl_common::generateLayer<arm_compute::NECopy>(ifm_tensor->handle(), ofm_tensor->handle());
1240 assert(pv.size() == 2 && pv.at(0) == 1 && pv.at(1) == 0);
1241 fn = acl_common::generateLayer<arm_compute::NETranspose>(ifm_tensor->handle(),
1242 ofm_tensor->handle());
1248 fn = acl_common::generateLayer<arm_compute::NEPermute>(ifm_tensor->handle(),
1249 ofm_tensor->handle(), backend_pv);
1254void KernelGenerator::visit(
const ir::operation::Unpack &node)
1257 auto axis{node.param().axis};
1259 const auto input_rank = _ctx.at(input_index).shape().rank();
1261 std::vector<ir::OperandIndex> output_indexes;
1262 for (
const auto &output_index : node.getOutputs())
1263 output_indexes.emplace_back(output_index);
1265 auto input_tensor = _tensor_reg->getAclTensor(input_index);
1266 std::vector<arm_compute::ITensor *> outputs;
1267 for (
const auto &output_index : output_indexes)
1268 outputs.emplace_back(_tensor_reg->getAclTensor(output_index)->handle());
1275 if (
static_cast<size_t>(input_tensor->getShape().rank()) !=
1276 input_tensor->info()->num_dimensions())
1283 acl_common::generateLayer<arm_compute::NEUnstack>(input_tensor->handle(), outputs, axis);
1286 if (input_tensor->getShape().dim(0) == 1)
1294void KernelGenerator::visit(
const ir::operation::ExpandDims &node)
1296 const auto output_index{node.getOutputs().at(0)};
1299 auto output_tensor = _tensor_reg->getAclTensor(output_index);
1300 auto input_tensor = _tensor_reg->getAclTensor(input_index);
1302 auto fn = acl_common::generateLayer<arm_compute::NEReshapeLayer>(input_tensor->handle(),
1308void KernelGenerator::visit(
const ir::operation::Comparison &node)
1310 const auto output_index{node.getOutputs().at(0)};
1314 const auto comparison_type = node.param().comparison_type;
1316 auto output_tensor = _tensor_reg->getAclTensor(output_index);
1317 auto input0_tensor = _tensor_reg->getAclTensor(input0_index);
1318 auto input1_tensor = _tensor_reg->getAclTensor(input1_index);
1320 auto fn = acl_common::generateLayer<arm_compute::NEElementwiseComparison>(
1321 input0_tensor->handle(), input1_tensor->handle(),
output_tensor->handle(),
1322 (arm_compute::ComparisonOperation)comparison_type);
1327void KernelGenerator::visit(
const ir::operation::OneHot &node)
1329 const auto out_idx{node.getOutputs().at(0)};
1336 auto indices_tensor = _tensor_reg->getAclTensor(indices_idx);
1337 auto depth_tensor = _tensor_reg->getAclTensor(depth_idx);
1338 auto onvalue_tensor = _tensor_reg->getAclTensor(onvalue_idx);
1339 auto offvalue_tensor = _tensor_reg->getAclTensor(offvalue_idx);
1341 const size_t output_rank = _ctx.at(out_idx).shape().rank();
1342 int32_t axis = node.param().axis == -1 ? output_rank - 1 : node.param().axis;
1345 auto fn = acl_common::generateLayer<arm_compute::NEOneHot>(
1346 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)
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)