summaryrefslogtreecommitdiff
path: root/runtimes/neurun/backend/acl_neon/KernelGenerator.cc
blob: a05fa8b305ce66b24f74e471c7fd463312d0b5b1 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
/*
 * 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/NEON/NEFunctions.h>   // Include all ARM Compute NEON functions
#include <arm_compute/runtime/NEON/NEFunctionsEx.h> // Include all ARM Compute EX NEON functions

#include <Convert.h>
#include <Swizzle.h>

#include "kernel/ConcatLayer.h"
#include "util/Padding.h"
#include "model/Index.h"
#include "model/DataType.h"
#include "model/InternalType.h"
#include "compiler/IExecutionBuilder.h"
#include "exec/NopFunction.h"
#include "util/logging.h"
#include "util/Utils.h"

using ::neurun::compiler::IExecutionBuilder;

namespace neurun
{
namespace backend
{
namespace acl_neon
{

using ::neurun::backend::acl_common::asAclFunction;

//
// ActivationBuilder
//
class ActivationBuilder
{
public:
  ActivationBuilder(IExecutionBuilder &builder) : _builder(builder)
  {
    // DO NOTHING
  }

private:
  void appendReLU(::arm_compute::ITensor *ifm_alloc);
  void appendReLU1(::arm_compute::ITensor *ifm_alloc);
  void appendReLU6(::arm_compute::ITensor *ifm_alloc);

public:
  void append(model::Activation act, ::arm_compute::ITensor *ifm_alloc);

private:
  IExecutionBuilder &_builder;
};

void ActivationBuilder::appendReLU(::arm_compute::ITensor *ifm_alloc)
{
  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::RELU};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(ifm_alloc, nullptr, act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _builder.append(std::move(acl_fn));
}

void ActivationBuilder::appendReLU1(::arm_compute::ITensor *ifm_alloc)
{
  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 1.0f, -1.0f};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(ifm_alloc, nullptr, act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _builder.append(std::move(acl_fn));
}

void ActivationBuilder::appendReLU6(::arm_compute::ITensor *ifm_alloc)
{
  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.0f, 0.0f};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(ifm_alloc, nullptr, act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _builder.append(std::move(acl_fn));
}

void ActivationBuilder::append(model::Activation act, ::arm_compute::ITensor *ifm_alloc)
{
  switch (act)
  {
    case model::Activation::NONE:
    {
      // DO NOTHING
      break;
    }
    case model::Activation::RELU:
    {
      appendReLU(ifm_alloc);
      break;
    }
    case model::Activation::RELU1:
    {
      appendReLU1(ifm_alloc);
      break;
    }
    case model::Activation::RELU6:
    {
      appendReLU6(ifm_alloc);
      break;
    }
    default:
    {
      throw std::runtime_error("Not supported, yet");
    }
  }
}

//
// KernelGenerator
//
KernelGenerator::KernelGenerator(const neurun::model::Operands &ctx,
                                 const std::shared_ptr<TensorBuilder> &tensor_builder)
    : _ctx(ctx), _tensor_builder(tensor_builder), _current_subg_layout(model::Layout::UNKNOWN)
{
  // DO NOTHING
}

void KernelGenerator::visit(const model::Subgraph &subgraph)
{
  _current_subg_layout = subgraph.getLayout();
  for (const auto &e : subgraph.operations())
  {
    const auto &node = *(e.node);
    _tensor_builder->preVisit(node);
    node.accept(*this);
    _tensor_builder->postVisit(node);
  }
}

void KernelGenerator::visit(const model::operation::AbsNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::AbsNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::ABS};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(input_alloc->handle(), output_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ArgMaxNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::ArgMaxNode::Input::INPUT)};
  const auto axis_index{node.param().axis_index};

  auto ifm_shape = _ctx.at(ifm_index).shape();
  auto ofm_shape = _ctx.at(ofm_index).shape();
  auto axis_shape = _ctx.at(axis_index).shape();

  assert(_ctx.at(axis_index).isConstant());
  // Axis rank is always 1.
  assert(axis_shape.rank() == 1);

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  const auto ifm_rank = ifm_shape.rank();
  auto frontend_layout = _current_subg_layout;
  auto backend_layout = ifm_alloc->layout();
  int32_t axis_value = _ctx.at(axis_index).asScalar<int32_t>();
  if (axis_value < 0)
  {
    axis_value += ifm_rank;
  }
  assert(axis_value >= 0 && axis_value < ifm_rank);
  const auto fixed_axis =
      acl_common::ToARMComputeAxis(ifm_rank, axis_value, frontend_layout, backend_layout).value();

  // auto fn = nnfw::cpp14::make_unique<::arm_compute::NEArgMinMaxLayer>();
  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEArgMax>();

  // NOTE
  // if (ofm_alloc->info()->data_type() == arm_compute::DataType::S32)
  //{
  ofm_alloc->info()->set_data_type(arm_compute::DataType::U32);
  //}
  fn->configure(ifm_alloc->handle(), fixed_axis, ofm_alloc->handle());
  // fn->configure(ifm_alloc->handle(), fixed_axis, ofm_alloc->handle(),
  // arm_compute::ReductionOperation::ARG_IDX_MAX);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::Conv2DNode &node)
{
  using model::operation::Conv2DNode;

  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(Conv2DNode::Input::INPUT)};
  const auto ker_index{node.getInputs().at(Conv2DNode::Input::KERNEL)};
  const auto bias_index{node.getInputs().at(Conv2DNode::Input::BIAS)};

  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  // 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 = neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape,
                                                      stride, ker_width, ker_height);
  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  auto ker_alloc = _tensor_builder->at(ker_index).get();
  auto bias_alloc = _tensor_builder->at(bias_index).get();

  const auto conv_info = acl_common::asPadStrideInfo(padding, stride);
  const auto act_info = acl_common::asActivationLayerInfo(activation);

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEConvolutionLayer>(
      _tensor_builder->acl_tensor_manager()->internal_buffer_manager());

  fn->configure(ifm_alloc->handle(), ker_alloc->handle(), bias_alloc->handle(), ofm_alloc->handle(),
                conv_info, ::arm_compute::WeightsInfo(), ::arm_compute::Size2D(1U, 1U), act_info);

  _execution_builder->append(asAclFunction(std::move(fn)));
}

void KernelGenerator::visit(const model::operation::DepthwiseConv2DNode &node)
{
  using model::operation::DepthwiseConv2DNode;

  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(DepthwiseConv2DNode::Input::INPUT)};
  const auto ker_index{node.getInputs().at(DepthwiseConv2DNode::Input::KERNEL)};
  const auto bias_index{node.getInputs().at(DepthwiseConv2DNode::Input::BIAS)};

  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  // 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 padding = neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape,
                                                      stride, ker_width, ker_height);
  const auto multiplier = node.param().multiplier;
  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  auto ker_alloc = _tensor_builder->at(ker_index).get();
  auto bias_alloc = _tensor_builder->at(bias_index).get();

  const auto conv_info = acl_common::asPadStrideInfo(padding, stride);
  const auto act_info = acl_common::asActivationLayerInfo(activation);

  if (ker_height == 3 && ker_width == 3)
  {
    auto fn = nnfw::cpp14::make_unique<::arm_compute::NEDepthwiseConvolutionLayer3x3>();

    fn->configure(ifm_alloc->handle(), ker_alloc->handle(), bias_alloc->handle(),
                  ofm_alloc->handle(), conv_info, multiplier, act_info);

    _execution_builder->append(asAclFunction(std::move(fn)));
  }
  else
  {
    auto fn = nnfw::cpp14::make_unique<::arm_compute::NEDepthwiseConvolutionLayer>();

    fn->configure(ifm_alloc->handle(), ker_alloc->handle(), bias_alloc->handle(),
                  ofm_alloc->handle(), conv_info, multiplier, act_info);

    _execution_builder->append(asAclFunction(std::move(fn)));
  }
}

void KernelGenerator::visit(const model::operation::MaxPool2DNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::MaxPool2DNode::Input::INPUT)};

  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);

  const auto kh = node.param().kh;
  const auto kw = node.param().kw;
  const auto stride = node.param().stride;
  const auto padding =
      neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, kw, kh);
  const auto activation = node.param().activation;

  VERBOSE(MaxPool2D) << "IFM_H: " << ifm_shape.H << std::endl;
  VERBOSE(MaxPool2D) << "IFM_W: " << ifm_shape.W << std::endl;
  VERBOSE(MaxPool2D) << "OFM_H: " << ofm_shape.H << std::endl;
  VERBOSE(MaxPool2D) << "OFM_W: " << ofm_shape.W << std::endl;
  VERBOSE(MaxPool2D) << "KER_H: " << kh << std::endl;
  VERBOSE(MaxPool2D) << "KER_W: " << kw << std::endl;
  VERBOSE(MaxPool2D) << "STRIDE_H: " << stride.vertical << std::endl;
  VERBOSE(MaxPool2D) << "STRIDE_W: " << stride.horizontal << std::endl;
  VERBOSE(MaxPool2D) << "PAD(T): " << padding.top << std::endl;
  VERBOSE(MaxPool2D) << "PAD(B): " << padding.bottom << std::endl;
  VERBOSE(MaxPool2D) << "PAD(L): " << padding.left << std::endl;
  VERBOSE(MaxPool2D) << "PAD(R): " << padding.right << std::endl;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  ::arm_compute::PoolingLayerInfo info{::arm_compute::PoolingType::MAX,
                                       ::arm_compute::Size2D{kw, kh},
                                       acl_common::asPadStrideInfo(padding, stride)};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEPoolingLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append((std::move(acl_fn)));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::MeanNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::MeanNode::Input::INPUT)};

  const auto axis_index{node.param().axis_index};
  const auto keep_dims{node.param().keep_dims};

  const auto ifm_shape = _ctx.at(ifm_index).shape();

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  std::set<uint32_t> axes;
  {
    const auto ifm_rank = ifm_shape.rank();
    const auto frontend_layout = _current_subg_layout;
    const auto backend_layout = ifm_alloc->layout();
    const auto axis_shape = _ctx.at(axis_index).shape();
    switch (axis_shape.rank())
    {
      case 0: // scalar
      {
        auto axis_value = _ctx.at(axis_index).asScalar<int32_t>();
        if (axis_value < 0)
        {
          axis_value += ifm_rank;
        }
        axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(ifm_rank, axis_value,
                                                                    frontend_layout, backend_layout)
                        .value());
        break;
      }
      case 1: // vector
      {
        const auto axis_base = _ctx.at(axis_index).data().base();
        const int axis_size = axis_shape.num_elements();

        // If axis's data does not exist as constant values and can be gotten as input data, we have
        // to find a way to infer output shape when sinking output.
        assert(axis_base != nullptr);
        for (int32_t n = 0; n < axis_size; ++n)
        {
          int32_t axis_value = *(reinterpret_cast<const int32_t *>(axis_base) + n);
          if (axis_value < 0)
          {
            axis_value += ifm_rank;
          }
          axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(
                          ifm_rank, axis_value, frontend_layout, backend_layout)
                          .value());
        }
        break;
      }
      default:
        throw std::runtime_error("Not supported");
    }
  }

  arm_compute::Coordinates fixed_axis;
  for (auto a : axes)
  {
    fixed_axis.set(fixed_axis.num_dimensions(), a);
  }

  // NOTE NEReduceMean has a bug that does not support NHWC layout
  //      NEReduceMean intermediate tensors are always NCHW layout
  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEReduceMeanEx>();

  fn->configure(ifm_alloc->handle(), fixed_axis, keep_dims, ofm_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::AvgPool2DNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::AvgPool2DNode::Input::INPUT)};

  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);

  const auto kh = node.param().kh;
  const auto kw = node.param().kw;
  const auto stride = node.param().stride;
  const auto padding =
      neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, kw, kh);
  const auto activation = node.param().activation;

  VERBOSE(AvgPool2D) << "IFM_H: " << ifm_shape.H << std::endl;
  VERBOSE(AvgPool2D) << "IFM_W: " << ifm_shape.W << std::endl;
  VERBOSE(AvgPool2D) << "OFM_H: " << ofm_shape.H << std::endl;
  VERBOSE(AvgPool2D) << "OFM_W: " << ofm_shape.W << std::endl;
  VERBOSE(AvgPool2D) << "KER_H: " << kh << std::endl;
  VERBOSE(AvgPool2D) << "KER_W: " << kw << std::endl;
  VERBOSE(AvgPool2D) << "STRIDE_H: " << stride.vertical << std::endl;
  VERBOSE(AvgPool2D) << "STRIDE_W: " << stride.horizontal << std::endl;
  VERBOSE(AvgPool2D) << "PAD(T): " << padding.top << std::endl;
  VERBOSE(AvgPool2D) << "PAD(B): " << padding.bottom << std::endl;
  VERBOSE(AvgPool2D) << "PAD(L): " << padding.left << std::endl;
  VERBOSE(AvgPool2D) << "PAD(R): " << padding.right << std::endl;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  ::arm_compute::PoolingLayerInfo info{
      ::arm_compute::PoolingType::AVG, ::arm_compute::Size2D{kw, kh},
      acl_common::asPadStrideInfo(padding, stride), true /* exclude_padding */};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEPoolingLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append((std::move(acl_fn)));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::ConcatNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};

  std::vector<model::OperandIndex> input_indexes;
  for (const auto &input : node.getInputs())
    input_indexes.emplace_back(input);

  const auto axis = node.param().axis;

  // If tensor allocator allocate as subtensor
  bool canEliminate = true;
  for (auto ifm_ind : input_indexes)
  {
    if (!_tensor_builder->isSubTensorOf(ofm_index, ifm_ind))
    {
      canEliminate = false;
      break;
    }
  }
  if (canEliminate)
  {
    // If concat eliminated, return a NOP IFunction
    _execution_builder->append(nnfw::cpp14::make_unique<exec::NopFunction>());
    return;
  }

  auto output_alloc = _tensor_builder->at(ofm_index).get();

  std::vector<::neurun::backend::acl_neon::operand::INETensor *> input_allocs;
  for (const auto &ifm_ind : input_indexes)
    input_allocs.emplace_back(_tensor_builder->at(ifm_ind).get());

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::acl_neon::kernel::ConcatLayer>();

  const auto rank = _ctx.at(ofm_index).shape().rank();
  const auto frontend_layout = _current_subg_layout;
  const auto backend_layout = output_alloc->layout();
  const auto fixed_axis =
      acl_common::ToARMComputeAxis(rank, axis, frontend_layout, backend_layout).value();

  fn->configure(input_allocs, fixed_axis, output_alloc);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::FloorNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::FloorNode::Input::INPUT)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEFloor>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::FullyConnectedNode &node)
{
  using model::operation::FullyConnectedNode;

  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(FullyConnectedNode::Input::INPUT)};
  const auto weight_index{node.getInputs().at(FullyConnectedNode::Input::WEIGHT)};
  const auto bias_index{node.getInputs().at(FullyConnectedNode::Input::BIAS)};

  const auto input_rank = _ctx.at(input_index).shape().rank();
  // TODO Currently we are not handling where the case is that the input's rank is 3.
  // The handling should be added in the future.
  assert(input_rank != 3);

  const auto output_size = _ctx.at(output_index).shape().dim(1);
  UNUSED_RELEASE(output_size);
  assert(_ctx.at(bias_index).shape().dim(0) == output_size);
  assert(_ctx.at(weight_index).shape().dim(0) == output_size);
  const auto batch_size = _ctx.at(output_index).shape().dim(0);
  const auto input_size = _ctx.at(weight_index).shape().dim(1);

  // Check for reshaping input's shape into rank-2
  bool needs_reshape = false;
  neurun::model::Shape reshape(2);
  if (input_rank == 4)
  {
    model::FeatureShape ifm_shape_feature =
        _ctx.at(input_index).shape().asFeature(_current_subg_layout);
    auto feature_size =
        ifm_shape_feature.N * ifm_shape_feature.C * ifm_shape_feature.H * ifm_shape_feature.W;

    UNUSED_RELEASE(feature_size);
    assert(feature_size == batch_size * input_size);

    // for reshaping
    needs_reshape = true;
    reshape.dim(0) = batch_size; /* H */
    reshape.dim(1) = input_size; /* W */
  }

  const auto activation = node.param().activation;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();
  auto weight_alloc = _tensor_builder->at(weight_index).get();
  auto bias_alloc = _tensor_builder->at(bias_index).get();
  auto acl_layout = output_alloc->handle()->info()->data_layout();

  auto fn = nnfw::cpp14::make_unique<arm_compute::NEFullyConnectedReshapingLayer>(
      _tensor_builder->acl_tensor_manager()->internal_buffer_manager());

  fn->configure(
      input_alloc->handle(), weight_alloc->handle(), bias_alloc->handle(), output_alloc->handle(),
      needs_reshape,
      ::neurun::backend::acl_common::asTensorShape(/* TODO Support NCHW frontend */
                                                   reshape, model::Layout::NHWC,
                                                   ::neurun::backend::acl_common::asRuntimeLayout(
                                                       acl_layout)));

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));

  ActivationBuilder{*_execution_builder}.append(activation, output_alloc->handle());
}

void KernelGenerator::visit(const model::operation::L2NormalizationNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::L2NormalizationNode::Input::INPUT)};

  // {CL|Neon}L2Normalization performs the reduction only along dimension 0
  // L2 Normalization always performs the reduction along the depth axis
  // Thus, we repurpose {CL|Neon}NormalizationLayers to act as depthwise L2 normalizations by
  // choosing normalization parameters as below

  const auto &ifm_shape = _ctx.at(ifm_index).shape();
  // TODO Support optional constant dimension that normalization would be performed on
  const auto normalization_axis = ifm_shape.rank() - 1;
  int32_t radius =
      2 * ifm_shape.dim(normalization_axis) + 1; // normSize = depth(last dimension) * 2 + 1
  float alpha = 1.0f;                            // In the implementation to make alpha_ become 1
  float beta = 0.5f;                             // pow(reduction, -0.5) = 1 / sqrt(reduction)
  float bias = 0.0f;                             // Don't offset the reduction.

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  const auto norm_info = ::arm_compute::NormalizationLayerInfo(::arm_compute::NormType::CROSS_MAP,
                                                               radius, alpha, beta, bias, false);

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NENormalizationLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), norm_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::L2Pool2DNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::L2Pool2DNode::Input::INPUT)};

  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);

  uint32_t kw = node.param().kw;
  uint32_t kh = node.param().kh;
  const auto stride = node.param().stride;
  const auto padding =
      neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, kw, kh);
  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  ::arm_compute::PoolingLayerInfo info{
      ::arm_compute::PoolingType::L2, ::arm_compute::Size2D{kw, kh},
      ::neurun::backend::acl_common::asPadStrideInfo(padding, stride)};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEPoolingLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::LocalResponseNormalizationNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{
      node.getInputs().at(model::operation::LocalResponseNormalizationNode::Input::INPUT)};
  const auto radius_index{node.param().radius_index};
  const auto bias_index{node.param().bias_index};
  const auto alpha_index{node.param().alpha_index};
  const auto beta_index{node.param().beta_index};

  auto radius = _ctx.at(radius_index).asScalar<int32_t>();
  auto alpha = _ctx.at(alpha_index).asScalar<float>();
  auto beta = _ctx.at(beta_index).asScalar<float>();
  auto bias = _ctx.at(bias_index).asScalar<float>();

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  const auto norm_info = ::arm_compute::NormalizationLayerInfo(
      ::arm_compute::NormType::CROSS_MAP, radius * 2 + 1, alpha, beta, bias, false);

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NENormalizationLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), norm_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::LogicalAndNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input0_index{node.getInputs().at(model::operation::LogicalAndNode::Input::INPUT0)};
  const auto input1_index{node.getInputs().at(model::operation::LogicalAndNode::Input::INPUT1)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input0_alloc = _tensor_builder->at(input0_index).get();
  auto input1_alloc = _tensor_builder->at(input1_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NELogicalAnd>();

  fn->configure(input0_alloc->handle(), input1_alloc->handle(), output_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::LogicalNotNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::LogicalNotNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEBitwiseNot>();

  fn->configure(input_alloc->handle(), output_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::LogicalOrNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input0_index{node.getInputs().at(model::operation::LogicalOrNode::Input::INPUT0)};
  const auto input1_index{node.getInputs().at(model::operation::LogicalOrNode::Input::INPUT1)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input0_alloc = _tensor_builder->at(input0_index).get();
  auto input1_alloc = _tensor_builder->at(input1_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NELogicalOr>();

  fn->configure(input0_alloc->handle(), input1_alloc->handle(), output_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::LogisticNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::LogisticNode::Input::INPUT)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::LSTMNode &node)
{
  // TODO Support dynamic rnn
  // TODO Fix subtle error in the case of non-CIFG, non-peephole and No Projection.
  const auto scratch_buffer_index{
      node.getOutputs().at(model::operation::LSTMNode::Output::SCRATCH_BUFFER)};
  const auto output_state_out_index{
      node.getOutputs().at(model::operation::LSTMNode::Output::OUTPUT_STATE_OUT)};
  const auto cell_state_out_index{
      node.getOutputs().at(model::operation::LSTMNode::Output::CELL_STATE_OUT)};
  const auto output_index{node.getOutputs().at(model::operation::LSTMNode::Output::OUTPUT)};

  const auto input_index{node.getInputs().at(model::operation::LSTMNode::Input::INPUT)};
  const auto input_to_input_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::INPUT_TO_INPUT_WEIGHTS)}; // optional
  const auto input_to_forget_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::INPUT_TO_FORGET_WEIGHTS)};
  const auto input_to_cell_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::INPUT_TO_CELL_WEIGHTS)};
  const auto input_to_output_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::INPUT_TO_OUTPUT_WEIGHTS)};
  const auto recurrent_to_input_weights_index{node.getInputs().at(
      model::operation::LSTMNode::Input::RECURRENT_TO_INPUT_WEIGHTS)}; // optional
  const auto recurrent_to_forget_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::RECURRENT_TO_FORGET_WEIGHTS)};
  const auto recurrent_to_cell_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::RECURRENT_TO_CELL_WEIGHTS)};
  const auto recurrent_to_output_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::RECURRENT_TO_OUTPUT_WEIGHTS)};
  const auto cell_to_input_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::CELL_TO_INPUT_WEIGHTS)}; // optional
  const auto cell_to_forget_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::CELL_TO_FORGET_WEIGHTS)}; // optional
  const auto cell_to_output_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::CELL_TO_OUTPUT_WEIGHTS)}; // optional
  const auto input_gate_bias_index{
      node.getInputs().at(model::operation::LSTMNode::Input::INPUT_GATE_BIAS)};
  const auto forget_gate_bias_index{
      node.getInputs().at(model::operation::LSTMNode::Input::FORGET_GATE_BIAS)};
  const auto cell_bias_index{node.getInputs().at(model::operation::LSTMNode::Input::CELL_BIAS)};
  const auto output_gate_bias_index{
      node.getInputs().at(model::operation::LSTMNode::Input::OUTPUT_GATE_BIAS)};
  const auto projection_weights_index{
      node.getInputs().at(model::operation::LSTMNode::Input::PROJECTION_WEIGHTS)}; // optional
  const auto projection_bias_index{
      node.getInputs().at(model::operation::LSTMNode::Input::PROJECTION_BIAS)}; // optional
  const auto output_state_in_index{
      node.getInputs().at(model::operation::LSTMNode::Input::OUTPUT_STATE_IN)};
  const auto cell_state_in_index{
      node.getInputs().at(model::operation::LSTMNode::Input::CELL_STATE_IN)};
  const auto cell_threshold = node.param().cell_threshold;
  const auto projection_threshold = node.param().projection_threshold;

  bool has_input_to_input_weights = _ctx.at(input_to_input_weights_index).shape().dim(0) != 0 &&
                                    _ctx.at(input_to_input_weights_index).shape().dim(1) != 0;
  bool has_recurrent_to_input_weights =
      _ctx.at(recurrent_to_input_weights_index).shape().dim(0) != 0 &&
      _ctx.at(recurrent_to_input_weights_index).shape().dim(1) != 0;
  bool has_cell_to_forget_weights = _ctx.at(cell_to_forget_weights_index).shape().dim(0) != 0;
  bool has_cell_to_output_weights = _ctx.at(cell_to_output_weights_index).shape().dim(0) != 0;
  bool has_projection_weights = _ctx.at(projection_weights_index).shape().dim(0) != 0 &&
                                _ctx.at(projection_weights_index).shape().dim(1) != 0;
  bool has_projection_bias = _ctx.at(projection_bias_index).shape().dim(0);

  // NOTE The input_to_input_weights and the recurrent_to_input_weights do not exist in CIFG.
  // true: no CIFG
  // false: CIFG
  // NOTE The cell_to_input_weights does not exist in non-peephole although regular LSTM(non-CIFG).
  bool has_cifg_param = has_input_to_input_weights && has_recurrent_to_input_weights;

  // NOTE The cell_to_forget_weights and the cell_to_output_weights exist in peephole.
  // But the cell_to_input_weights does not exist in regular CIFG although peephole.
  // true: peephole
  // false: no peephole
  bool has_peephole_param = has_cell_to_forget_weights && has_cell_to_output_weights;

  // NOTE Although the projection weights has data the projection bias may not have data.
  bool has_projection_param = has_projection_weights;

  const auto activation = node.param().activation;
  const auto cell_clip = cell_threshold;
  const auto projection_clip = projection_threshold;
  assert(cell_clip >= 0.f && projection_clip >= 0.f);

  auto scratch_buffer_alloc = _tensor_builder->at(scratch_buffer_index).get();
  auto output_state_out_alloc = _tensor_builder->at(output_state_out_index).get();
  auto cell_state_out_alloc = _tensor_builder->at(cell_state_out_index).get();
  auto output_alloc = _tensor_builder->at(output_index).get();

  auto input_alloc = _tensor_builder->at(input_index).get();

  auto input_to_forget_weights_alloc = _tensor_builder->at(input_to_forget_weights_index).get();
  auto input_to_cell_weights_alloc = _tensor_builder->at(input_to_cell_weights_index).get();
  auto input_to_output_weights_alloc = _tensor_builder->at(input_to_output_weights_index).get();
  auto recurrent_to_forget_weights_alloc =
      _tensor_builder->at(recurrent_to_forget_weights_index).get();
  auto recurrent_to_cell_weights_alloc = _tensor_builder->at(recurrent_to_cell_weights_index).get();
  auto recurrent_to_output_weights_alloc =
      _tensor_builder->at(recurrent_to_output_weights_index).get();

  auto forget_gate_bias_alloc = _tensor_builder->at(forget_gate_bias_index).get();
  auto cell_bias_alloc = _tensor_builder->at(cell_bias_index).get();
  auto output_gate_bias_alloc = _tensor_builder->at(output_gate_bias_index).get();
  auto output_state_in_alloc = _tensor_builder->at(output_state_in_index).get();
  auto cell_state_in_alloc = _tensor_builder->at(cell_state_in_index).get();

  auto act_info = ::neurun::backend::acl_common::asActivationLayerInfo(activation);

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NELSTMLayer>();

  ::arm_compute::LSTMParams<::arm_compute::ITensor> lstm_params{};
  if (has_cifg_param)
  {
    auto input_to_input_weights_alloc =
        _tensor_builder->at(input_to_input_weights_index).get(); // optional
    auto recurrent_to_input_weights_alloc =
        _tensor_builder->at(recurrent_to_input_weights_index).get(); // optional
    auto cell_to_input_weights_handle =
        has_peephole_param ? _tensor_builder->at(cell_to_input_weights_index).get()->handle()
                           : nullptr; // optional (non-cifg && peephole)
    auto input_gate_bias_alloc = _tensor_builder->at(input_gate_bias_index).get(); // optional
    lstm_params.set_cifg_params(input_to_input_weights_alloc->handle(),
                                recurrent_to_input_weights_alloc->handle(),
                                cell_to_input_weights_handle, input_gate_bias_alloc->handle());
  }
  if (has_peephole_param)
  {
    auto cell_to_forget_weights_alloc =
        _tensor_builder->at(cell_to_forget_weights_index).get(); // optional
    auto cell_to_output_weights_alloc =
        _tensor_builder->at(cell_to_output_weights_index).get(); // optional
    lstm_params.set_peephole_params(cell_to_forget_weights_alloc->handle(),
                                    cell_to_output_weights_alloc->handle());
  }
  if (has_projection_param)
  {
    auto projection_weights_alloc = _tensor_builder->at(projection_weights_index).get(); // optional
    auto projection_bias_handle = has_projection_bias
                                      ? _tensor_builder->at(projection_bias_index).get()->handle()
                                      : nullptr; // optional
    lstm_params.set_projection_params(projection_weights_alloc->handle(), projection_bias_handle);
  }

  fn->configure(
      input_alloc->handle(), input_to_forget_weights_alloc->handle(),
      input_to_cell_weights_alloc->handle(), input_to_output_weights_alloc->handle(),
      recurrent_to_forget_weights_alloc->handle(), recurrent_to_cell_weights_alloc->handle(),
      recurrent_to_output_weights_alloc->handle(), forget_gate_bias_alloc->handle(),
      cell_bias_alloc->handle(), output_gate_bias_alloc->handle(), output_state_in_alloc->handle(),
      cell_state_in_alloc->handle(), scratch_buffer_alloc->handle(),
      output_state_out_alloc->handle(), cell_state_out_alloc->handle(), output_alloc->handle(),
      lstm_params, act_info, cell_clip, projection_clip);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::MulNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto lhs_index{node.getInputs().at(model::operation::MulNode::Input::LHS)};
  const auto rhs_index{node.getInputs().at(model::operation::MulNode::Input::RHS)};

  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto lhs_alloc = _tensor_builder->at(lhs_index).get();
  auto rhs_alloc = _tensor_builder->at(rhs_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEPixelWiseMultiplication>();

  // RoundingPolicy for scale:1.0 is only allowed RoundingPolicy::TO_ZERO
  fn->configure(lhs_alloc->handle(), rhs_alloc->handle(), ofm_alloc->handle(), 1.0, // scale
                arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::PadNode &node)
{
  const auto input_index{node.getInputs().at(model::operation::PadNode::Input::INPUT)};
  const auto pad_index{node.getInputs().at(model::operation::PadNode::Input::PAD)};
  const auto output_index{node.getOutputs().at(0)};
  assert(_ctx.at(pad_index).isConstant());

  auto rank = _ctx.at(pad_index).shape().dim(0);
  auto pad_base = _ctx.at(pad_index).data().base();

  auto input = _tensor_builder->at(input_index).get()->handle();
  auto output = _tensor_builder->at(output_index).get()->handle();

  ::arm_compute::PaddingList padding_list;
  padding_list.resize(rank);
  for (int32_t n = 0; n < rank; ++n)
  {
    const int32_t *from = reinterpret_cast<const int32_t *>(pad_base) + (n * 2);

    const auto frontend_layout = _current_subg_layout;
    const auto backend_layout = _tensor_builder->at(input_index).get()->layout();
    const auto axis =
        acl_common::ToARMComputeAxis(rank, n, frontend_layout, backend_layout).value();
    padding_list[axis] = ::arm_compute::PaddingInfo{from[0], from[1]};
  }

  const auto input_type = _ctx.at(input_index).typeInfo();
  UNUSED_RELEASE(input_type);
  assert(input->info()->data_type() == acl_common::asDataType(input_type.type()));
  assert(input->info()->quantization_info() ==
         ::arm_compute::QuantizationInfo(input_type.scale(), input_type.offset()));
  const auto pixel_value =
      ::arm_compute::PixelValue(0, input->info()->data_type(), input->info()->quantization_info());

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEPadLayer>();
  fn->configure(input, output, padding_list, pixel_value);

  _execution_builder->append(asAclFunction(std::move(fn)));
}

void KernelGenerator::visit(const model::operation::PReLUNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::PReLUNode::Input::INPUT)};
  const auto alpha_index{node.getInputs().at(model::operation::PReLUNode::Input::ALPHA)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  auto alpha_alloc = _tensor_builder->at(alpha_index).get();

  std::unique_ptr<::arm_compute::IFunction> fn;

  auto l = nnfw::cpp14::make_unique<::arm_compute::NEPReLU>();

  l->configure(ifm_alloc->handle(), alpha_alloc->handle(), ofm_alloc->handle());

  fn = std::move(l);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ReduceSumNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::ReduceSumNode::Input::INPUT)};
  const auto axis_index{node.param().axis_index};

  const auto axis_base = _ctx.at(axis_index).data().base();
  const auto axis_size = _ctx.at(axis_index).shape().num_elements();
  const auto input_rank = _ctx.at(input_index).shape().rank();

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();
  const auto frontend_layout = _current_subg_layout;
  const auto backend_layout = input_alloc->layout();
  // The axis's data must exist as constant values
  assert(axis_base != nullptr);
  std::set<int32_t> axes;
  for (size_t n = 0; n < axis_size; ++n)
  {
    int32_t axis_value = *(reinterpret_cast<const int32_t *>(axis_base) + n);
    if (axis_value < 0)
    {
      axis_value += input_rank;
    }
    axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(input_rank, axis_value,
                                                                frontend_layout, backend_layout)
                    .value());
  }
  arm_compute::Coordinates fixed_axes;
  for (const auto &a : axes)
  {
    fixed_axes.set(fixed_axes.num_dimensions(), a);
  }

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEReduceSum>();

  fn->configure(input_alloc->handle(), fixed_axes, false, output_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ReLUNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::ReLUNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<arm_compute::NEActivationLayer>();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::RELU};

  fn->configure(input_alloc->handle(), output_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ReLU1Node &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::ReLU1Node::Input::INPUT)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 1.0f, -1.0f};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ReLU6Node &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::ReLU6Node::Input::INPUT)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0f};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ReshapeNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::ReshapeNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<arm_compute::NEReshapeLayer>();

  fn->configure(input_alloc->handle(), output_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ResizeBilinearNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};

  const auto ifm_index{node.getInputs().at(model::operation::ResizeBilinearNode::Input::INPUT)};
  const auto height_index{node.param().height_index};
  const auto width_index{node.param().width_index};
  (void)height_index;
  (void)width_index;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEScale>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle(),
                ::arm_compute::InterpolationPolicy::BILINEAR, ::arm_compute::BorderMode::REPLICATE,
                ::arm_compute::PixelValue(0.f), ::arm_compute::SamplingPolicy::TOP_LEFT);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::RNNNode &node)
{
  const auto output_index{node.getOutputs().at(model::operation::RNNNode::Output::OUTPUT)};
  const auto hidden_state_out_index{
      node.getOutputs().at(model::operation::RNNNode::Output::HIDDEN_STATE_OUT)};

  const auto input_index{node.getInputs().at(model::operation::RNNNode::Input::INPUT)};
  const auto weights_index{node.getInputs().at(model::operation::RNNNode::Input::WEIGHTS)};
  const auto recurrent_weights_index{
      node.getInputs().at(model::operation::RNNNode::Input::RECURRENT_WEIGHTS)};
  const auto bias_index{node.getInputs().at(model::operation::RNNNode::Input::BIAS)};
  const auto hidden_state_in_index{
      node.getInputs().at(model::operation::RNNNode::Input::HIDDEN_STATE_IN)};

  const auto activation = node.param().activation;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto hidden_state_out_alloc = _tensor_builder->at(hidden_state_out_index).get();

  auto input_alloc = _tensor_builder->at(input_index).get();
  auto weights_alloc = _tensor_builder->at(weights_index).get();
  auto recurrent_weights_alloc = _tensor_builder->at(recurrent_weights_index).get();
  auto bias_alloc = _tensor_builder->at(bias_index).get();
  auto hidden_state_in_alloc = _tensor_builder->at(hidden_state_in_index).get();
  auto act_info = ::neurun::backend::acl_common::asActivationLayerInfo(activation);

  auto copy_layer = nnfw::cpp14::make_unique<::arm_compute::NECopy>();
  copy_layer->configure(hidden_state_in_alloc->handle(), hidden_state_out_alloc->handle());
  _execution_builder->append(asAclFunction(std::move(copy_layer)));

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NERNNLayerEx>(
      _tensor_builder->acl_tensor_manager()->internal_buffer_manager());
  fn->configure(input_alloc->handle(), weights_alloc->handle(), recurrent_weights_alloc->handle(),
                bias_alloc->handle(), hidden_state_out_alloc->handle(), output_alloc->handle(),
                act_info);
  _execution_builder->append(asAclFunction(std::move(fn)));
}

void KernelGenerator::visit(const model::operation::RSQRTNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::RSQRTNode::Input::INPUT)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NERsqrtLayer>();

  fn->configure(ifm_alloc->handle(), ofm_alloc->handle());

  _execution_builder->append(asAclFunction(std::move(fn)));
}

void KernelGenerator::visit(const model::operation::SqueezeNode &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
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::SqueezeNode::Input::INPUT)};
  const auto dims_index{node.param().dims};
  (void)dims_index;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();
  auto fn = nnfw::cpp14::make_unique<arm_compute::NEReshapeLayer>();
  fn->configure(input_alloc->handle(), output_alloc->handle());
  auto acl_fn = asAclFunction(std::move(fn));
  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::TanhNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::TanhNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<arm_compute::NEActivationLayer>();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f};

  fn->configure(input_alloc->handle(), output_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::SoftmaxNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::SoftmaxNode::Input::INPUT)};
  const auto beta = node.param().beta;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NESoftmaxLayer>(
      _tensor_builder->acl_tensor_manager()->internal_buffer_manager());

  fn->configure(input_alloc->handle(), output_alloc->handle(), beta);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::SplitNode &node)
{
  // TODO Support this op by SubTensor
  const auto ifm_index{node.getInputs().at(model::operation::SplitNode::Input::INPUT)};
  const auto axis_index{node.param().axis_index};
  const auto num_of_splits_index{node.param().num_of_splits_index};

  assert(_ctx.at(num_of_splits_index).asScalar<unsigned int>() == node.getOutputs().size());

  const auto ifm_rank = _ctx.at(ifm_index).shape().rank();
  std::vector<model::OperandIndex> output_indexes;
  for (const auto &output : node.getOutputs())
    output_indexes.emplace_back(output);

  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  std::vector<arm_compute::ITensor *> output_allocs;
  for (const auto &ofm_ind : output_indexes)
    output_allocs.emplace_back(_tensor_builder->at(ofm_ind).get()->handle());

  const auto frontend_layout = _current_subg_layout;
  const auto backend_layout = ifm_alloc->layout();
  auto axis = _ctx.at(axis_index).asScalar<int32_t>();
  if (axis < 0)
    axis += ifm_rank;
  axis = acl_common::ToARMComputeAxis(ifm_rank, axis, frontend_layout, backend_layout).value();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NESplit>();

  fn->configure(ifm_alloc->handle(), output_allocs, axis);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::SQRTNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::SQRTNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  const ::arm_compute::ActivationLayerInfo act_info{
      ::arm_compute::ActivationLayerInfo::ActivationFunction::SQRT};

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEActivationLayer>();

  fn->configure(input_alloc->handle(), output_alloc->handle(), act_info);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::SquaredDifferenceNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto lhs_index{node.getInputs().at(model::operation::SquaredDifferenceNode::Input::LHS)};
  const auto rhs_index{node.getInputs().at(model::operation::SquaredDifferenceNode::Input::RHS)};

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto lhs_alloc = _tensor_builder->at(lhs_index).get();
  auto rhs_alloc = _tensor_builder->at(rhs_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEElementwiseSquaredDiff>();

  fn->configure(lhs_alloc->handle(), rhs_alloc->handle(), ofm_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::SubNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto lhs_index{node.getInputs().at(model::operation::SubNode::Input::LHS)};
  const auto rhs_index{node.getInputs().at(model::operation::SubNode::Input::RHS)};

  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto lhs_alloc = _tensor_builder->at(lhs_index).get();
  auto rhs_alloc = _tensor_builder->at(rhs_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEArithmeticSubtraction>();

  fn->configure(lhs_alloc->handle(), rhs_alloc->handle(), ofm_alloc->handle(),
                arm_compute::ConvertPolicy::SATURATE);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::StridedSliceNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::StridedSliceNode::Input::INPUT)};
  const auto startData_index{node.param().startData_index};
  const auto endData_index{node.param().endData_index};
  const auto stridesData_index{node.param().stridesData_index};
  const auto beginMask_index{node.param().beginMask_index};
  const auto endMask_index{node.param().endMask_index};
  const auto shrinkAxisMask_index{node.param().shrinkAxisMask_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);
  {
    auto input_shape = _ctx.at(input_index).shape();
    auto startData_base = _ctx.at(startData_index).data().base();
    auto endData_base = _ctx.at(endData_index).data().base();
    auto stridesData_base = _ctx.at(stridesData_index).data().base();
    const int startData_size = _ctx.at(startData_index).shape().num_elements();
    const int endData_size = _ctx.at(endData_index).shape().num_elements();
    const int stridesData_size = _ctx.at(stridesData_index).shape().num_elements();

    using neurun::model::DataType;

    UNUSED_RELEASE(startData_size);
    UNUSED_RELEASE(endData_size);
    UNUSED_RELEASE(stridesData_size);

    assert(_ctx.at(startData_index).typeInfo().type() == DataType::INT32);
    assert(_ctx.at(endData_index).typeInfo().type() == DataType::INT32);
    assert(_ctx.at(stridesData_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 = ::neurun::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 beginMask = ::neurun::backend::acl_common::ReorderBits<int32_t>(
      _ctx.at(beginMask_index).asScalar<int32_t>(), input_rank);
  const auto endMask = ::neurun::backend::acl_common::ReorderBits<int32_t>(
      _ctx.at(endMask_index).asScalar<int32_t>(), input_rank);
  const auto shrinkAxisMask = ::neurun::backend::acl_common::ReorderBits<int32_t>(
      _ctx.at(shrinkAxisMask_index).asScalar<int32_t>(), input_rank);

  auto outputData_alloc = _tensor_builder->at(output_index).get();
  auto inputData_alloc = _tensor_builder->at(input_index).get();

  ::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]);
  }

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEStridedSlice>();

  fn->configure(inputData_alloc->handle(), outputData_alloc->handle(), starts_set, ends_set,
                strides_set, beginMask, endMask, shrinkAxisMask);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::TransposeConvNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto output_shape_index{
      node.getInputs().at(model::operation::TransposeConvNode::Input::OUTPUT_SHAPE)};
  const auto ker_index{node.getInputs().at(model::operation::TransposeConvNode::Input::KERNEL)};
  const auto ifm_index{node.getInputs().at(model::operation::TransposeConvNode::Input::INPUT)};

  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ker_shape = _ctx.at(ker_index).shape().asFeature(_current_subg_layout);

  const auto stride = node.param().stride;

  assert((node.param().padding.type == model::PaddingType::SAME) ||
         (node.param().padding.type == model::PaddingType::VALID));
  auto padding = neurun::util::calculatePadding(node.param().padding, ofm_shape, ifm_shape, stride,
                                                ker_shape.W, ker_shape.H);

  uint32_t invalid_horizontal = 0;
  uint32_t invalid_vertical = 0;
  if (node.param().padding.type == model::PaddingType::VALID)
  {
    invalid_horizontal =
        ofm_shape.W - (1 + (ifm_shape.W - 1) * stride.horizontal) - (ker_shape.W - 1);
    invalid_vertical = ofm_shape.H - (1 + (ifm_shape.H - 1) * stride.vertical) - (ker_shape.H - 1);
  }

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();
  auto ker_alloc = _tensor_builder->at(ker_index).get();

  const auto tconv_info = acl_common::asPadStrideInfo(padding, stride);

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NETransposeConvLayer>();

  fn->configure(ifm_alloc->handle(), ker_alloc->handle(), nullptr, ofm_alloc->handle(), tconv_info,
                invalid_horizontal, invalid_vertical);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::TransposeNode &node)
{
  const auto ofm_idx{node.getOutputs().at(0)};
  const auto ifm_idx{node.getInputs().at(model::operation::TransposeNode::Input::INPUT)};
  const auto perm{node.param().perm};

  const auto rank = _ctx.at(ifm_idx).shape().rank();
  std::vector<int32_t> pv;
  const auto perm_base = _ctx.at(perm).data().base();
  const int perm_size = _ctx.at(perm).shape().num_elements();

  assert(perm_base != nullptr);
  for (int32_t n = 0; n < perm_size; ++n)
  {
    const int32_t perm_value = *(reinterpret_cast<const int32_t *>(perm_base) + n);
    assert(perm_value < rank);
    pv.emplace_back(perm_value);
  }

  auto ofm_alloc = _tensor_builder->at(ofm_idx).get();
  const auto ifm_alloc = _tensor_builder->at(ifm_idx).get();
  const auto frontend_layout = _current_subg_layout;
  const auto backend_layout = ifm_alloc->layout();

  auto backend_pv = ::neurun::backend::acl_common::getARMComputePermutationVector(
      rank, pv, frontend_layout, backend_layout);

  std::unique_ptr<::arm_compute::IFunction> fn;

  if (ifm_alloc->num_dimensions() <= 2 && ofm_alloc->num_dimensions() <= 2)
  {
    auto l = nnfw::cpp14::make_unique<::arm_compute::NETranspose>();

    l->configure(ifm_alloc->handle(), ofm_alloc->handle());

    fn = std::move(l);
  }
  else
  {
    auto l = nnfw::cpp14::make_unique<::arm_compute::NEPermute>();

    l->configure(ifm_alloc->handle(), ofm_alloc->handle(), backend_pv);

    fn = std::move(l);
  }

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::UnpackNode &node)
{
  const auto input_index{node.getInputs().at(model::operation::UnpackNode::Input::INPUT)};
  auto axis{node.param().axis};

  const auto input_rank = _ctx.at(input_index).shape().rank();

  std::vector<model::OperandIndex> output_indexes;
  for (const auto &output_index : node.getOutputs())
    output_indexes.emplace_back(output_index);

  auto input = _tensor_builder->at(input_index).get()->handle();
  std::vector<arm_compute::ITensor *> outputs;
  for (const auto &output_index : output_indexes)
    outputs.emplace_back(_tensor_builder->at(output_index)->handle());

  const auto frontend_layout = _current_subg_layout;
  const auto backend_layout = _tensor_builder->at(input_index).get()->layout();
  if (axis < 0)
    axis += input_rank;
  axis = acl_common::ToARMComputeAxis(input_rank, axis, frontend_layout, backend_layout).value();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEUnstack>();

  fn->configure(input, outputs, axis);

  _execution_builder->append(asAclFunction(std::move(fn)));
}

void KernelGenerator::visit(const model::operation::AddNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto lhs_index{node.getInputs().at(model::operation::AddNode::Input::LHS)};
  const auto rhs_index{node.getInputs().at(model::operation::AddNode::Input::RHS)};

  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto lhs_alloc = _tensor_builder->at(lhs_index).get();
  auto rhs_alloc = _tensor_builder->at(rhs_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEArithmeticAddition>();

  fn->configure(lhs_alloc->handle(), rhs_alloc->handle(), ofm_alloc->handle(),
                arm_compute::ConvertPolicy::SATURATE);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::DivNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto lhs_index{node.getInputs().at(model::operation::DivNode::Input::LHS)};
  const auto rhs_index{node.getInputs().at(model::operation::DivNode::Input::RHS)};

  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto lhs_alloc = _tensor_builder->at(lhs_index).get();
  auto rhs_alloc = _tensor_builder->at(rhs_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEElementwiseDivision>();

  fn->configure(lhs_alloc->handle(), rhs_alloc->handle(), ofm_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));

  ActivationBuilder{*_execution_builder}.append(activation, ofm_alloc->handle());
}

void KernelGenerator::visit(const model::operation::ExpNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::ExpNode::Input::INPUT)};

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEExpLayer>();

  fn->configure(input_alloc->handle(), output_alloc->handle());

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

void KernelGenerator::visit(const model::operation::ReduceMaxNode &node)
{
  (void)node;
  throw std::runtime_error("Not supported, yet");
}

void KernelGenerator::visit(const model::operation::ComparisonNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input0_index{node.getInputs().at(model::operation::ComparisonNode::Input::INPUT0)};
  const auto input1_index{node.getInputs().at(model::operation::ComparisonNode::Input::INPUT1)};

  const auto comparison_type = node.param().comparison_type;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input0_alloc = _tensor_builder->at(input0_index).get();
  auto input1_alloc = _tensor_builder->at(input1_index).get();

  auto fn = nnfw::cpp14::make_unique<::arm_compute::NEElementwiseComparison>();

  fn->configure(input0_alloc->handle(), input1_alloc->handle(), output_alloc->handle(),
                (arm_compute::ComparisonOperation)comparison_type);

  auto acl_fn = asAclFunction(std::move(fn));

  _execution_builder->append(std::move(acl_fn));
}

} // namespace acl_neon
} // namespace backend
} // namespace neurun