2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <AclActivationBuilder.h>
18 #include <AclFunction.h>
22 #include "ConstantInitializer.h"
31 ConstantInitializer::ConstantInitializer(const ir::Operands &operands,
32 const std::shared_ptr<ITensorRegistry> &tensor_reg)
33 : acl_common::AclConstantInitializer{operands, tensor_reg}
38 void ConstantInitializer::visit(const ir::operation::EmbeddingLookup &node)
40 copyInputInitialize(node, ir::operation::EmbeddingLookup::LOOKUPS);
43 void ConstantInitializer::visit(const ir::operation::Gather &node)
45 copyInputInitialize(node, ir::operation::Gather::INDICES);
48 void ConstantInitializer::visit(const ir::operation::HashtableLookup &node)
50 copyInputInitialize(node, ir::operation::HashtableLookup::LOOKUPS);
51 copyInputInitialize(node, ir::operation::HashtableLookup::KEYS);
54 void ConstantInitializer::visit(const ir::operation::SpaceToBatchND &node)
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);
59 if (block_size_obj.isConstant())
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)
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)}));
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())
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)
91 for (auto j = 0; j < shape.dim(1); ++j)
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}));
104 void ConstantInitializer::visit(const ir::operation::Reverse &node)
106 const auto &output_index = node.getOutputs().at(0);
108 const auto &input_index = node.getInputs().at(ir::operation::Reverse::Input::INPUT);
109 const auto &input_obj = _operands.at(input_index);
111 const auto &axis_index = node.getInputs().at(ir::operation::Reverse::Input::AXIS);
112 const auto &axis_obj = _operands.at(axis_index);
114 const auto ifm_rank = input_obj.shape().rank();
115 const auto frontend_layout = this->_current_op_seq_layout;
117 auto output_tensor = this->_tensor_reg->getITensor(output_index);
118 const auto backend_layout = output_tensor->layout();
120 if (axis_obj.isConstant())
122 _init_map[axis_index] = [ifm_rank, frontend_layout, backend_layout](const ir::Operand &operand,
123 backend::ITensor &obj) {
124 assert(operand.data());
126 const auto axis_value = *(reinterpret_cast<const int32_t *>(operand.data()->base()));
127 int32_t axis_tmp = axis_value;
130 axis_tmp = axis_tmp + ifm_rank;
134 acl_common::ToARMComputeAxis(ifm_rank, axis_tmp, frontend_layout, backend_layout).value();
136 obj.access([&](ITensor &tensor) {
137 int32_t *into = reinterpret_cast<int32_t *>(tensor.buffer());
138 *into = (int32_t)axis;
144 } // namespace acl_cl
145 } // namespace backend