forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernelGenerator.cc
1551 lines (1259 loc) · 60.9 KB
/
KernelGenerator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "KernelGenerator.h"
#include <arm_compute/runtime/CL/CLFunctions.h> // Include all ARM Compute CL functions
#include <arm_compute/runtime/CL/CLFunctionsEx.h> // Include all ARM Compute EX CL functions
#include <AclActivationBuilder.h>
#include <AclFunction.h>
#include <Convert.h>
#include <Swizzle.h>
#include "ir/Index.h"
#include "ir/DataType.h"
#include "ir/InternalType.h"
#include "exec/NopFunction.h"
#include "exec/FunctionSequence.h"
#include "util/logging.h"
#include "AclKernelGen.h"
namespace onert
{
namespace backend
{
namespace acl_cl
{
using ::onert::backend::acl_common::asAclFunction;
using ActivationBuilder = ::onert::backend::acl_common::AclActivationBuilder<
::arm_compute::ICLTensor, ::arm_compute::CLActivationLayer, acl_common::AclFunction>;
KernelGenerator::KernelGenerator(
const ir::Graph &graph, const std::shared_ptr<TensorBuilder> &tensor_builder,
const std::shared_ptr<acl_common::AclTensorRegistry<TensorManager>> &tensor_reg)
: basic::KernelGeneratorBase{graph}, _ctx(graph.operands()), _operations_ctx(graph.operations()),
_tensor_builder(tensor_builder), _tensor_reg(tensor_reg)
{
// DO NOTHING
}
std::unique_ptr<exec::FunctionSequence> KernelGenerator::generate(ir::OperationIndex ind)
{
auto ret = std::make_unique<exec::FunctionSequence>();
ret->enableDynamicShapeInferer(false);
const auto &op = _graph.operations().at(ind);
op.accept(*this);
ret->append(releaseFunction());
return ret;
}
void KernelGenerator::visit(const ir::operation::BatchToSpaceND &node)
{
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(ir::operation::BatchToSpaceND::Input::INPUT)};
const auto block_size_index{
node.getInputs().at(ir::operation::BatchToSpaceND::Input::BLOCK_SIZE)};
const auto NNApiInputs = 2;
if (node.getInputs().size() != NNApiInputs)
{
const auto crops_index{node.getInputs().at(ir::operation::BatchToSpaceND::Input::CROPS_DATA)};
if (!_ctx.at(crops_index).isConstant())
{
throw std::runtime_error("Non-constant crops NYI for acl_cl backend BatchToSpaceND");
}
auto crops = _ctx.at(crops_index).asVector<int32_t>();
for (auto &&crop : crops)
{
if (crop != 0)
{
throw std::runtime_error("Non-zero crops NYI for acl_cl backend BatchToSpaceND");
}
}
}
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
if (!_ctx.at(block_size_index).data())
throw std::runtime_error("ACL CL does not support dynamic block size for BatchToSpaceND");
auto block = _ctx.at(block_size_index).asVector<int32_t>();
int32_t height = block[0];
int32_t width = block[1];
auto fn = acl_common::generateLayer<arm_compute::CLBatchToSpaceLayer>(
ifm_tensor->handle(), width, height, ofm_tensor->handle());
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::BinaryArithmetic &node)
{
const auto ofm_index{node.getOutputs().at(0)};
const auto lhs_index{node.getInputs().at(ir::operation::BinaryArithmetic::Input::LHS)};
const auto rhs_index{node.getInputs().at(ir::operation::BinaryArithmetic::Input::RHS)};
const auto activation = node.param().activation;
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
const auto act_info = acl_common::asActivationLayerInfo(activation);
std::unique_ptr<arm_compute::IFunction> fn;
switch (node.param().arithmetic_type)
{
case ir::operation::BinaryArithmetic::ArithmeticType::ADD:
{
arm_compute::CLArithmeticAddition::validate(lhs_tensor->info(), rhs_tensor->info(),
ofm_tensor->info(),
arm_compute::ConvertPolicy::SATURATE, act_info)
.throw_if_error();
fn = acl_common::generateLayer<arm_compute::CLArithmeticAddition>(
lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
arm_compute::ConvertPolicy::SATURATE, act_info);
break;
}
case ir::operation::BinaryArithmetic::ArithmeticType::SUB:
{
arm_compute::CLArithmeticSubtraction::validate(lhs_tensor->info(), rhs_tensor->info(),
ofm_tensor->info(),
arm_compute::ConvertPolicy::SATURATE, act_info)
.throw_if_error();
fn = acl_common::generateLayer<arm_compute::CLArithmeticSubtraction>(
lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
arm_compute::ConvertPolicy::SATURATE, act_info);
break;
}
case ir::operation::BinaryArithmetic::ArithmeticType::MUL:
{
arm_compute::CLPixelWiseMultiplication::validate(
lhs_tensor->info(), rhs_tensor->info(), ofm_tensor->info(), 1.0,
arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_NEAREST_EVEN,
act_info)
.throw_if_error();
fn = acl_common::generateLayer<arm_compute::CLPixelWiseMultiplication>(
lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(), 1.0, // scale
arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_NEAREST_EVEN,
act_info);
break;
}
case ir::operation::BinaryArithmetic::ArithmeticType::DIV:
{
arm_compute::CLArithmeticDivision::validate(lhs_tensor->info(), rhs_tensor->info(),
ofm_tensor->info(), act_info)
.throw_if_error();
fn = acl_common::generateLayer<arm_compute::CLArithmeticDivision>(
lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(), act_info);
break;
}
default:
assert(false && "The BinaryArithmetic operation supports only binary arithmetic operations");
break;
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Conv2D &node)
{
using ir::operation::Conv2D;
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(Conv2D::Input::INPUT)};
const auto ker_index{node.getInputs().at(Conv2D::Input::KERNEL)};
const auto bias_index{node.getInputs().at(Conv2D::Input::BIAS)};
const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature();
const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature();
// Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
const auto &ker_shape = _ctx.at(ker_index).shape();
const auto ker_height = ker_shape.dim(1);
const auto ker_width = ker_shape.dim(2);
const auto stride = node.param().stride;
const auto padding =
ir::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, ker_width, ker_height);
const auto activation = node.param().activation;
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
const auto conv_info = acl_common::asPadStrideInfo(padding, stride);
const auto act_info = acl_common::asActivationLayerInfo(activation);
auto fn = acl_common::generateLayer<arm_compute::CLConvolutionLayer>(
_tensor_builder->acl_tensor_manager()->internal_buffer_manager(), ifm_tensor->handle(),
ker_tensor->handle(), bias_tensor->handle(), ofm_tensor->handle(), conv_info,
::arm_compute::WeightsInfo(), ::arm_compute::Size2D(1U, 1U), act_info);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::DepthwiseConv2D &node)
{
using ir::operation::DepthwiseConv2D;
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(DepthwiseConv2D::Input::INPUT)};
const auto ker_index{node.getInputs().at(DepthwiseConv2D::Input::KERNEL)};
const auto bias_index{node.getInputs().at(DepthwiseConv2D::Input::BIAS)};
const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature();
const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature();
// Kernel format is [1, kernel_height, kernel_width, depth_out].
const auto &ker_shape = _ctx.at(ker_index).shape();
const auto ker_height = ker_shape.dim(1);
const auto ker_width = ker_shape.dim(2);
const auto stride = node.param().stride;
const auto dilation = node.param().dilation;
const auto padding =
ir::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, ker_width, ker_height,
dilation.width_factor, dilation.height_factor);
const auto multiplier = node.param().multiplier;
const auto activation = node.param().activation;
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
auto ker_tensor = _tensor_reg->getAclTensor(ker_index);
auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
const auto conv_info = acl_common::asPadStrideInfo(padding, stride);
const auto act_info = acl_common::asActivationLayerInfo(activation);
const auto dilation_info = acl_common::asDilation(dilation.width_factor, dilation.height_factor);
auto fn = acl_common::generateLayer<arm_compute::CLDepthwiseConvolutionLayer>(
ifm_tensor->handle(), ker_tensor->handle(), bias_tensor->handle(), ofm_tensor->handle(),
conv_info, multiplier, act_info, dilation_info);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Concat &node)
{
const auto ofm_index{node.getOutputs().at(0)};
std::vector<ir::OperandIndex> input_indexes;
for (const auto &input : node.getInputs())
input_indexes.emplace_back(input);
const auto axis = node.param().axis;
// Concat elimination check
bool eliminated = _tensor_builder->areSubTensorsOf(ofm_index, node.getInputs());
if (eliminated)
{
// If concat eliminated, return a NOP IFunction
VERBOSE(acl_cl_KernelGenerator_Concat) << "Concat eliminated" << std::endl;
_return_fn = std::make_unique<exec::NopFunction>();
return;
}
auto output_tensor = _tensor_reg->getAclTensor(ofm_index);
std::vector<const ::arm_compute::ICLTensor *> input_tensors;
for (const auto &ifm_ind : input_indexes)
input_tensors.emplace_back(_tensor_reg->getAclTensor(ifm_ind)->handle());
std::unique_ptr<::arm_compute::IFunction> fn;
if (input_indexes.size() < 2)
{
::arm_compute::ICLTensor *input_tesor =
_tensor_reg->getAclTensor(input_indexes.at(0))->handle();
fn = acl_common::generateLayer<arm_compute::CLCopy>(input_tesor, output_tensor->handle());
}
else
{
const auto rank = _ctx.at(ofm_index).shape().rank();
const auto fixed_axis = acl_common::ToARMComputeAxis(rank, axis).value();
fn = acl_common::generateLayer<::arm_compute::CLConcatenateLayer>(
input_tensors, output_tensor->handle(), fixed_axis);
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::FullyConnected &node)
{
const auto output_index{node.getOutputs().at(0)};
auto output_tensor = _tensor_reg->getAclTensor(output_index);
const auto activation = node.param().activation;
if (node.param().weights_format == ir::FullyConnectedWeightsFormat::Shuffled16x1Float32)
throw std::runtime_error(
"KernelGenerator(acl_cl): FullyConnected 16x1Float32 weights is not supported.");
auto fn = acl_common::kernelGenFullyConnected<acl_common::AclFunction, ::arm_compute::ICLTensor,
::arm_compute::CLFullyConnectedReshapingLayer>(
node, _ctx, _tensor_builder, _tensor_reg);
_return_fn = std::make_unique<exec::FunctionSequence>(
std::move(fn), ActivationBuilder::generate(activation, output_tensor->handle()));
}
void KernelGenerator::visit(const ir::operation::Reduce &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::Reduce::Input::INPUT)};
const auto axes_index{node.getInputs().at(ir::operation::Reduce::Input::AXES)};
const auto keep_dims{node.param().keep_dims};
const auto reduce_type = node.param().reduce_type;
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
// Convert to ACL axes taking into account negative values and possible duplicates.
const auto &axes = _ctx.at(axes_index);
const auto input_rank = _ctx.at(input_index).shape().rank();
std::unique_ptr<arm_compute::IFunction> fn;
if (reduce_type == ir::operation::Reduce::ReduceType::MEAN)
{
const auto acl_axes = acl_common::asCoordinates(axes, input_rank);
fn = acl_common::generateLayer<arm_compute::CLReduceMean>(input_tensor->handle(), acl_axes,
keep_dims, output_tensor->handle());
}
else
{
const auto acl_axes = acl_common::asSet(axes, input_rank);
fn = acl_common::generateLayer<arm_compute::CLReduceOperation>(
_tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
output_tensor->handle(), acl_axes, keep_dims, acl_common::convertReduceType(reduce_type));
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Reshape &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::Reshape::Input::INPUT)};
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
auto fn = acl_common::generateLayer<arm_compute::CLReshapeLayer>(input_tensor->handle(),
output_tensor->handle());
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Squeeze &node)
{
// Squeeze is identical to reshape except that it has an optional dimensions input.
// In addition, optional dims_index is ignored since output tensor already has squeezed shape
// by freezer and toco
// TODO Support multi-layout for frontend and backend
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::Squeeze::Input::INPUT)};
const auto dims{node.param().dims};
const auto ndim{node.param().ndim};
(void)dims;
(void)ndim;
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
auto fn = acl_common::generateLayer<arm_compute::CLReshapeLayer>(input_tensor->handle(),
output_tensor->handle());
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Softmax &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::Softmax::Input::INPUT)};
const auto beta = node.param().beta;
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
auto fn = acl_common::generateLayer<arm_compute::CLSoftmaxLayer>(
_tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
output_tensor->handle(), beta);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Slice &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::Slice::Input::INPUT)};
const auto begins_index{node.getInputs().at(ir::operation::Slice::Input::BEGINS)};
const auto sizes_index{node.getInputs().at(ir::operation::Slice::Input::SIZES)};
auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
// Set initializers for indices data such as order of inputData
int input_rank = _ctx.at(input_index).shape().rank();
std::vector<int32_t> starts;
std::vector<int32_t> ends;
starts.resize(input_rank, 0);
ends.resize(input_rank, 0);
{
assert(_ctx.at(begins_index).data());
assert(_ctx.at(sizes_index).data());
auto beginData_base = _ctx.at(begins_index).data()->base();
auto sizeData_base = _ctx.at(sizes_index).data()->base();
[[maybe_unused]] const int beginData_size = _ctx.at(begins_index).shape().num_elements();
[[maybe_unused]] const int sizeData_size = _ctx.at(sizes_index).shape().num_elements();
using ir::DataType;
assert(_ctx.at(begins_index).typeInfo().type() == DataType::INT32);
assert(_ctx.at(sizes_index).typeInfo().type() == DataType::INT32);
assert(beginData_size == input_rank);
assert(sizeData_size == input_rank);
assert(beginData_base != nullptr);
for (int n = 0; n < input_rank; ++n)
{
auto axis = ::onert::backend::acl_common::ToARMComputeAxis(input_rank, n).value();
int32_t begin_value = *(reinterpret_cast<const int32_t *>(beginData_base) + n);
starts[axis] = begin_value;
int32_t size_value = *(reinterpret_cast<const int32_t *>(sizeData_base) + n);
ends[axis] = begin_value + size_value;
}
}
::arm_compute::Coordinates starts_set;
::arm_compute::Coordinates ends_set;
for (size_t i = 0; i < starts.size(); ++i)
{
starts_set.set(i, starts[i]);
ends_set.set(i, ends[i]);
}
auto fn = acl_common::generateLayer<arm_compute::CLSlice>(
inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::StridedSlice &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::StridedSlice::Input::INPUT)};
const auto starts_index{node.getInputs().at(ir::operation::StridedSlice::Input::STARTS)};
const auto ends_index{node.getInputs().at(ir::operation::StridedSlice::Input::ENDS)};
const auto strides_index{node.getInputs().at(ir::operation::StridedSlice::Input::STRIDES)};
auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
// Set initializers for indices data such as order of inputData
int input_rank = _ctx.at(input_index).shape().rank();
std::vector<int32_t> starts;
std::vector<int32_t> ends;
std::vector<int32_t> strides;
starts.resize(input_rank, 0);
ends.resize(input_rank, 0);
strides.resize(input_rank, 0);
{
assert(_ctx.at(starts_index).data());
assert(_ctx.at(ends_index).data());
assert(_ctx.at(strides_index).data());
auto startData_base = _ctx.at(starts_index).data()->base();
auto endData_base = _ctx.at(ends_index).data()->base();
auto stridesData_base = _ctx.at(strides_index).data()->base();
[[maybe_unused]] const int startData_size = _ctx.at(starts_index).shape().num_elements();
[[maybe_unused]] const int endData_size = _ctx.at(ends_index).shape().num_elements();
[[maybe_unused]] const int stridesData_size = _ctx.at(strides_index).shape().num_elements();
using ir::DataType;
assert(_ctx.at(starts_index).typeInfo().type() == DataType::INT32);
assert(_ctx.at(ends_index).typeInfo().type() == DataType::INT32);
assert(_ctx.at(strides_index).typeInfo().type() == DataType::INT32);
assert(startData_size == input_rank);
assert(endData_size == input_rank);
assert(stridesData_size == input_rank);
assert(startData_base != nullptr);
for (int n = 0; n < input_rank; ++n)
{
auto axis = ::onert::backend::acl_common::ToARMComputeAxis(input_rank, n).value();
int32_t start_value = *(reinterpret_cast<const int32_t *>(startData_base) + n);
starts[axis] = start_value;
int32_t end_value = *(reinterpret_cast<const int32_t *>(endData_base) + n);
ends[axis] = end_value;
int32_t strides_value = *(reinterpret_cast<const int32_t *>(stridesData_base) + n);
strides[axis] = strides_value;
}
}
// Set mask bits such as order of inputData
const auto begin_mask = acl_common::ReorderBits<int32_t>(node.param().begin_mask, input_rank);
const auto end_mask = acl_common::ReorderBits<int32_t>(node.param().end_mask, input_rank);
const auto shrink_axis_mask =
acl_common::ReorderBits<int32_t>(node.param().shrink_axis_mask, input_rank);
::arm_compute::Coordinates starts_set;
::arm_compute::Coordinates ends_set;
::arm_compute::BiStrides strides_set;
for (size_t i = 0; i < starts.size(); ++i)
{
starts_set.set(i, starts[i]);
ends_set.set(i, ends[i]);
strides_set.set(i, strides[i]);
}
// Disable applied dim_correction
if (inputData_tensor->num_dimensions() != inputData_tensor->info()->num_dimensions())
{
// This means that high dimension's value is 1 and input tensor is applied dim_correction
acl_common::disableDimCorrection(inputData_tensor);
}
auto fn = acl_common::generateLayer<arm_compute::CLStridedSlice>(
inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set, strides_set,
begin_mask, end_mask, shrink_axis_mask);
// Revert disabling applied dim_correction
if (inputData_tensor->dimension(0) == 1)
{
acl_common::enableDimCorrection(inputData_tensor);
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Transpose &node)
{
const auto ofm_idx{node.getOutputs().at(0)};
const auto ifm_idx{node.getInputs().at(ir::operation::Transpose::Input::INPUT)};
const auto perm_idx{node.getInputs().at(ir::operation::Transpose::Input::PERMUTATION)};
const auto rank = _ctx.at(ifm_idx).shape().rank();
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_idx);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_idx);
const auto &perms = _ctx.at(perm_idx);
std::vector<int32_t> pv;
if (perms.shape() == ir::Shape{0})
{
pv.resize(rank);
std::iota(pv.begin(), pv.end(), 0);
std::reverse(pv.begin(), pv.end());
}
else
{
pv = _ctx.at(perm_idx).asVector<int32_t>();
}
std::unique_ptr<arm_compute::IFunction> fn;
if (rank == 1)
{
fn = acl_common::generateLayer<arm_compute::CLCopy>(ifm_tensor->handle(), ofm_tensor->handle());
}
else if (rank == 2)
{
assert(pv.size() == 2 && pv.at(0) == 1 && pv.at(1) == 0);
fn = acl_common::generateLayer<arm_compute::CLTranspose>(ifm_tensor->handle(),
ofm_tensor->handle());
}
else
{
auto backend_pv = acl_common::getARMComputePermutationVector(rank, pv);
fn = acl_common::generateLayer<arm_compute::CLPermute>(ifm_tensor->handle(),
ofm_tensor->handle(), backend_pv);
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::ElementwiseActivation &node)
{
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(ir::operation::ElementwiseActivation::Input::INPUT)};
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
const ::arm_compute::ActivationLayerInfo act_info =
acl_common::asActivationLayerInfo(node.param().op_type, node.param().alpha, node.param().beta);
auto fn = acl_common::generateLayer<arm_compute::CLActivationLayer>(
ifm_tensor->handle(), ofm_tensor->handle(), act_info);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::ElementwiseBinary &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto lhs_index{node.getInputs().at(ir::operation::ElementwiseBinary::Input::LHS)};
const auto rhs_index{node.getInputs().at(ir::operation::ElementwiseBinary::Input::RHS)};
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
std::unique_ptr<arm_compute::IFunction> fn;
switch (node.param().op_type)
{
case ir::operation::ElementwiseBinary::ElementwiseBinaryType::LOGICAL_AND:
{
fn = acl_common::generateLayer<arm_compute::CLBinaryLogicalOp>(
lhs_tensor->handle(), rhs_tensor->handle(), output_tensor->handle(),
arm_compute::BinaryLogicalOperation::AND);
break;
}
case ir::operation::ElementwiseBinary::ElementwiseBinaryType::LOGICAL_OR:
{
fn = acl_common::generateLayer<arm_compute::CLBitwiseOr>(
lhs_tensor->handle(), rhs_tensor->handle(), output_tensor->handle());
break;
}
case ir::operation::ElementwiseBinary::ElementwiseBinaryType::MAX:
{
fn = acl_common::generateLayer<arm_compute::CLElementwiseMax>(
lhs_tensor->handle(), rhs_tensor->handle(), output_tensor->handle());
break;
}
case ir::operation::ElementwiseBinary::ElementwiseBinaryType::MIN:
{
fn = acl_common::generateLayer<arm_compute::CLElementwiseMin>(
lhs_tensor->handle(), rhs_tensor->handle(), output_tensor->handle());
break;
}
default:
{
std::string err_msg("acl_cl KernelGenerator : " + node.name() +
"is not elementwise-binary operations");
assert(false && err_msg.c_str());
break;
}
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::ElementwiseUnary &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::ElementwiseUnary::Input::INPUT)};
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
std::unique_ptr<arm_compute::IFunction> fn;
switch (node.param().op_type)
{
case ir::operation::ElementwiseUnary::Type::ABS:
{
const ::arm_compute::ActivationLayerInfo act_info{
::arm_compute::ActivationLayerInfo::ActivationFunction::ABS};
fn = acl_common::generateLayer<arm_compute::CLActivationLayer>(
input_tensor->handle(), output_tensor->handle(), act_info);
break;
}
case ir::operation::ElementwiseUnary::Type::CAST:
{
if (input_tensor->data_type() == output_tensor->data_type())
{
fn = acl_common::generateLayer<arm_compute::CLCopy>(input_tensor->handle(),
output_tensor->handle());
}
else if (_ctx.at(input_index).typeInfo().type() == ir::DataType::BOOL8)
{
fn = acl_common::generateLayer<arm_compute::CLCastBool>(input_tensor->handle(),
output_tensor->handle());
}
else
{
// TODO Support converting float to int32 as round down
fn = acl_common::generateLayer<arm_compute::CLCast>(
input_tensor->handle(), output_tensor->handle(), arm_compute::ConvertPolicy::SATURATE);
}
break;
}
case ir::operation::ElementwiseUnary::Type::DEQUANTIZE:
{
fn = acl_common::generateLayer<arm_compute::CLDequantizationLayer>(input_tensor->handle(),
output_tensor->handle());
break;
}
case ir::operation::ElementwiseUnary::Type::EXP:
{
fn = acl_common::generateLayer<arm_compute::CLExpLayer>(input_tensor->handle(),
output_tensor->handle());
break;
}
case ir::operation::ElementwiseUnary::Type::FLOOR:
{
fn = acl_common::generateLayer<arm_compute::CLFloor>(input_tensor->handle(),
output_tensor->handle());
break;
}
case ir::operation::ElementwiseUnary::Type::LOGICAL_NOT:
{
fn = acl_common::generateLayer<arm_compute::CLBitwiseNot>(input_tensor->handle(),
output_tensor->handle());
break;
}
case ir::operation::ElementwiseUnary::Type::NEG:
{
fn = acl_common::generateLayer<arm_compute::CLNeg>(input_tensor->handle(),
output_tensor->handle());
break;
}
case ir::operation::ElementwiseUnary::Type::RSQRT:
{
fn = acl_common::generateLayer<arm_compute::CLRsqrtLayer>(input_tensor->handle(),
output_tensor->handle());
break;
}
case ir::operation::ElementwiseUnary::Type::SQRT:
{
const ::arm_compute::ActivationLayerInfo act_info{
::arm_compute::ActivationLayerInfo::ActivationFunction::SQRT};
fn = acl_common::generateLayer<arm_compute::CLActivationLayer>(
input_tensor->handle(), output_tensor->handle(), act_info);
break;
}
default:
{
throw std::runtime_error("acl_cl KernelGenerator : " + node.name() + "is not supported yet");
break;
}
}
auto acl_fn = asAclFunction(std::move(fn));
_return_fn = std::move(acl_fn);
}
void KernelGenerator::visit(const ir::operation::ExpandDims &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::ExpandDims::Input::INPUT)};
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
auto fn = acl_common::generateLayer<arm_compute::CLReshapeLayer>(input_tensor->handle(),
output_tensor->handle());
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::InstanceNorm &node)
{
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(ir::operation::InstanceNorm::Input::INPUT)};
const auto gamma_index{node.getInputs().at(ir::operation::InstanceNorm::Input::GAMMA)};
const auto beta_index{node.getInputs().at(ir::operation::InstanceNorm::Input::BETA)};
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
auto gamma_tensor = _tensor_reg->getAclTensor(gamma_index);
auto beta_tensor = _tensor_reg->getAclTensor(beta_index);
auto epsilon = node.param().epsilon;
auto activation = node.param().activation;
auto fn = acl_common::generateLayer<arm_compute::CLInstanceNormalizationLayerEx>(
ifm_tensor->handle(), ofm_tensor->handle(), gamma_tensor->handle(), beta_tensor->handle(),
epsilon);
_return_fn = std::make_unique<exec::FunctionSequence>(
asAclFunction(std::move(fn)), ActivationBuilder::generate(activation, ofm_tensor->handle()));
}
void KernelGenerator::visit(const ir::operation::LSTM &node)
{
_return_fn = acl_common::kernelGenLSTM<acl_common::AclFunction, ::arm_compute::ICLTensor,
::arm_compute::CLLSTMLayer>(node, _ctx, _tensor_reg);
}
void KernelGenerator::visit(const ir::operation::Comparison &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input0_index{node.getInputs().at(ir::operation::Comparison::Input::INPUT0)};
const auto input1_index{node.getInputs().at(ir::operation::Comparison::Input::INPUT1)};
const auto comparison_type = node.param().comparison_type;
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto input0_tensor = _tensor_reg->getAclTensor(input0_index);
auto input1_tensor = _tensor_reg->getAclTensor(input1_index);
auto fn = acl_common::generateLayer<arm_compute::CLComparison>(
input0_tensor->handle(), input1_tensor->handle(), output_tensor->handle(),
(arm_compute::ComparisonOperation)comparison_type);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::OneHot &node)
{
const auto output_idx{node.getOutputs().at(0)};
const auto indices_idx{node.getInputs().at(ir::operation::OneHot::Input::INDICES)};
const auto depth_idx{node.getInputs().at(ir::operation::OneHot::Input::DEPTH)};
const auto onvalue_idx{node.getInputs().at(ir::operation::OneHot::Input::ON_VALUE)};
const auto offvalue_idx{node.getInputs().at(ir::operation::OneHot::Input::OFF_VALUE)};
const auto depth = _ctx.at(depth_idx).asScalar<int32_t>();
assert(depth > 0);
auto output_tensor = _tensor_reg->getAclTensor(output_idx);
auto indices_tensor = _tensor_reg->getAclTensor(indices_idx);
auto onvalue_tensor = _tensor_reg->getAclTensor(onvalue_idx);
const size_t output_rank = _ctx.at(output_idx).shape().rank();
int32_t axis = node.param().axis == -1 ? output_rank - 1 : node.param().axis;
axis = acl_common::ToARMComputeAxis(output_rank, axis).value();
if (output_tensor->num_dimensions() != output_tensor->info()->num_dimensions())
{
// This means that high dimension's value is 1 and output_tensor is applied dim_correction
acl_common::disableDimCorrection(output_tensor);
}
std::unique_ptr<::arm_compute::IFunction> fn;
const auto &offvalue = _ctx.at(offvalue_idx);
if (offvalue.isConstant())
{
fn = acl_common::generateLayer<arm_compute::CLOneHot>(
indices_tensor->handle(), onvalue_tensor->handle(), output_tensor->handle(),
acl_common::asPixelValue(offvalue), static_cast<uint32_t>(depth), axis);
}
else
{
auto offvalue_tensor = _tensor_reg->getAclTensor(offvalue_idx);
fn = acl_common::generateLayer<arm_compute::CLOneHot>(
indices_tensor->handle(), onvalue_tensor->handle(), offvalue_tensor->handle(),
output_tensor->handle(), static_cast<uint32_t>(depth), axis);
}
if (output_tensor->dimension(0) == 1)
{
acl_common::enableDimCorrection(output_tensor);
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Pack &node)
{
const auto output_index{node.getOutputs().at(0)};
auto axis{node.param().axis};
const auto output_rank = _ctx.at(output_index).shape().rank();
std::vector<ir::OperandIndex> input_indexes;
for (const auto &input_index : node.getInputs())
input_indexes.emplace_back(input_index);
auto output = _tensor_reg->getAclTensor(output_index)->handle();
std::vector<arm_compute::ICLTensor *> inputs;
for (const auto &input_index : input_indexes)
inputs.emplace_back(_tensor_reg->getAclTensor(input_index)->handle());
if (axis < 0)
axis += output_rank;
axis = acl_common::ToARMComputeAxis(output_rank, axis).value();
// Disable applied dim_correction
for (const auto &input_index : input_indexes)
{
const auto &input_tensor = _tensor_reg->getAclTensor(input_index);
if (input_tensor->num_dimensions() != input_tensor->info()->num_dimensions())
{
// This means that high dimension's value is 1 and input tensor is applied dim_correction
acl_common::disableDimCorrection(input_tensor);
}
}
auto fn = acl_common::generateLayer<arm_compute::CLStackLayer>(inputs, axis, output);
// Revert disabling applied dim_correction
for (const auto &input_index : input_indexes)
{
const auto &input_tensor = _tensor_reg->getAclTensor(input_index);
if (input_tensor->dimension(0) == 1)
{
acl_common::enableDimCorrection(input_tensor);
}
}
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::Pool2D &node)
{
auto raw_fn = acl_common::kernelGenPool2D<::arm_compute::CLPoolingLayer>(
node, _ctx, _tensor_reg, acl_common::convertPoolType(node.param().op_type));
const auto ofm_index{node.getOutputs().at(0)};
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
const auto activation = node.param().activation;
_return_fn = std::make_unique<exec::FunctionSequence>(
asAclFunction(std::move(raw_fn)),
ActivationBuilder::generate(activation, ofm_tensor->handle()));
}
void KernelGenerator::visit(const ir::operation::ResizeBilinear &node)
{
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(ir::operation::ResizeBilinear::Input::INPUT)};
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
auto fn = acl_common::generateLayer<arm_compute::CLScale>(
ifm_tensor->handle(), ofm_tensor->handle(),
::arm_compute::ScaleKernelInfo{
::arm_compute::InterpolationPolicy::BILINEAR, ::arm_compute::BorderMode::REPLICATE,
::arm_compute::PixelValue(0.f), ::arm_compute::SamplingPolicy::TOP_LEFT});
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::ResizeNearestNeighbor &node)
{
const auto ofm_index{node.getOutputs().at(0)};
const auto ifm_index{node.getInputs().at(ir::operation::ResizeNearestNeighbor::Input::INPUT)};
auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
auto ifm_tensor = _tensor_reg->getAclTensor(ifm_index);
auto fn = acl_common::generateLayer<arm_compute::CLScale>(
ifm_tensor->handle(), ofm_tensor->handle(),
::arm_compute::ScaleKernelInfo{
::arm_compute::InterpolationPolicy::NEAREST_NEIGHBOR, ::arm_compute::BorderMode::REPLICATE,
::arm_compute::PixelValue(0.f), ::arm_compute::SamplingPolicy::TOP_LEFT});
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::RNN &node)
{
const auto output_index{node.getOutputs().at(ir::operation::RNN::Output::OUTPUT)};
const auto hidden_state_out_index{
node.getOutputs().at(ir::operation::RNN::Output::HIDDEN_STATE_OUT)};
const auto input_index{node.getInputs().at(ir::operation::RNN::Input::INPUT)};
const auto weights_index{node.getInputs().at(ir::operation::RNN::Input::WEIGHTS)};
const auto recurrent_weights_index{
node.getInputs().at(ir::operation::RNN::Input::RECURRENT_WEIGHTS)};
const auto bias_index{node.getInputs().at(ir::operation::RNN::Input::BIAS)};
const auto hidden_state_in_index{node.getInputs().at(ir::operation::RNN::Input::HIDDEN_STATE_IN)};
const auto activation = node.param().activation;
auto output_tensor = _tensor_reg->getAclTensor(output_index);
auto hidden_state_out_tensor = _tensor_reg->getAclTensor(hidden_state_out_index);
auto input_tensor = _tensor_reg->getAclTensor(input_index);
auto weights_tensor = _tensor_reg->getAclTensor(weights_index);
auto recurrent_weights_tensor = _tensor_reg->getAclTensor(recurrent_weights_index);
auto bias_tensor = _tensor_reg->getAclTensor(bias_index);
auto hidden_state_in_tensor = _tensor_reg->getAclTensor(hidden_state_in_index);
auto act_info = ::onert::backend::acl_common::asActivationLayerInfo(activation);
auto copy_layer = acl_common::generateLayer<arm_compute::CLCopy>(
hidden_state_in_tensor->handle(), hidden_state_out_tensor->handle());
_return_fn = asAclFunction(std::move(copy_layer));
auto fn = acl_common::generateLayer<arm_compute::CLRNNLayer>(
_tensor_builder->acl_tensor_manager()->internal_buffer_manager(), input_tensor->handle(),
weights_tensor->handle(), recurrent_weights_tensor->handle(), bias_tensor->handle(),
hidden_state_out_tensor->handle(), output_tensor->handle(), act_info);
_return_fn = asAclFunction(std::move(fn));
}
void KernelGenerator::visit(const ir::operation::SpaceToBatchND &node)
{