Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / acl_cl / ConstantInitializer.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <AclActivationBuilder.h>
18 #include <AclFunction.h>
19 #include <Convert.h>
20 #include <Swizzle.h>
21
22 #include "ConstantInitializer.h"
23
24 namespace onert
25 {
26 namespace backend
27 {
28 namespace acl_cl
29 {
30
31 ConstantInitializer::ConstantInitializer(const ir::Operands &operands,
32                                          const std::shared_ptr<ITensorRegistry> &tensor_reg)
33     : acl_common::AclConstantInitializer{operands, tensor_reg}
34 {
35   // DO NOTHING
36 }
37
38 void ConstantInitializer::visit(const ir::operation::EmbeddingLookup &node)
39 {
40   copyInputInitialize(node, ir::operation::EmbeddingLookup::LOOKUPS);
41 }
42
43 void ConstantInitializer::visit(const ir::operation::Gather &node)
44 {
45   copyInputInitialize(node, ir::operation::Gather::INDICES);
46 }
47
48 void ConstantInitializer::visit(const ir::operation::HashtableLookup &node)
49 {
50   copyInputInitialize(node, ir::operation::HashtableLookup::LOOKUPS);
51   copyInputInitialize(node, ir::operation::HashtableLookup::KEYS);
52 }
53
54 void ConstantInitializer::visit(const ir::operation::SpaceToBatchND &node)
55 {
56   const auto &block_size_index = node.getInputs().at(ir::operation::SpaceToBatchND::BLOCK_SIZE);
57   const auto &block_size_obj = _operands.at(block_size_index);
58
59   if (block_size_obj.isConstant())
60   {
61     _init_map[block_size_index] = [](const ir::Operand &model_obj, backend::ITensor &obj) {
62       assert(model_obj.data());
63       const auto &shape = model_obj.shape();
64       const auto base = reinterpret_cast<const int32_t *>(model_obj.data()->base());
65       assert(model_obj.shape().rank() == 1);
66       obj.access([&](ITensor &tensor) {
67         for (size_t i = 0; i < shape.num_elements(); ++i)
68         {
69           const int32_t value = base[shape.num_elements() - i - 1];
70           int32_t *into = reinterpret_cast<int32_t *>(tensor.buffer() +
71                                                       tensor.calcOffset({static_cast<int32_t>(i)}));
72           *into = value;
73         }
74       });
75     };
76   }
77
78   const auto &paddings_index = node.getInputs().at(ir::operation::SpaceToBatchND::PADDINGS);
79   const auto &paddings_obj = _operands.at(paddings_index);
80   if (paddings_obj.isConstant())
81   {
82     _init_map[paddings_index] = [](const ir::Operand &model_obj, backend::ITensor &obj) {
83       assert(model_obj.data());
84       const auto &shape = model_obj.shape();
85       const auto base = reinterpret_cast<const int32_t *>(model_obj.data()->base());
86       assert(model_obj.shape().rank() == 2);
87       assert(obj.dimension(0) == 2);
88       obj.access([&](ITensor &tensor) {
89         for (auto i = 0; i < shape.dim(0); ++i)
90         {
91           for (auto j = 0; j < shape.dim(1); ++j)
92           {
93             const int32_t value = base[i * 2 + j];
94             int32_t *into = reinterpret_cast<int32_t *>(
95                 tensor.buffer() + tensor.calcOffset({shape.dim(0) - i - 1, j}));
96             *into = value;
97           }
98         }
99       });
100     };
101   }
102 }
103
104 void ConstantInitializer::visit(const ir::operation::Reverse &node)
105 {
106   const auto &output_index = node.getOutputs().at(0);
107
108   const auto &input_index = node.getInputs().at(ir::operation::Reverse::Input::INPUT);
109   const auto &input_obj = _operands.at(input_index);
110
111   const auto &axis_index = node.getInputs().at(ir::operation::Reverse::Input::AXIS);
112   const auto &axis_obj = _operands.at(axis_index);
113
114   const auto ifm_rank = input_obj.shape().rank();
115   const auto frontend_layout = this->_current_layout;
116
117   auto output_tensor = this->_tensor_reg->getITensor(output_index);
118   const auto backend_layout = output_tensor->layout();
119
120   if (axis_obj.isConstant())
121   {
122     _init_map[axis_index] = [ifm_rank, frontend_layout, backend_layout](const ir::Operand &operand,
123                                                                         backend::ITensor &obj) {
124       assert(operand.data());
125
126       const auto axis_value = *(reinterpret_cast<const int32_t *>(operand.data()->base()));
127       int32_t axis_tmp = axis_value;
128       if (axis_tmp < 0)
129       {
130         axis_tmp = axis_tmp + ifm_rank;
131       }
132
133       auto axis =
134           acl_common::ToARMComputeAxis(ifm_rank, axis_tmp, frontend_layout, backend_layout).value();
135
136       obj.access([&](ITensor &tensor) {
137         int32_t *into = reinterpret_cast<int32_t *>(tensor.buffer());
138         *into = (int32_t)axis;
139       });
140     };
141   }
142 }
143
144 } // namespace acl_cl
145 } // namespace backend
146 } // namespace onert