Imported Upstream version 1.9.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 "ConstantInitializer.h"
18
19 namespace onert
20 {
21 namespace backend
22 {
23 namespace acl_cl
24 {
25
26 ConstantInitializer::ConstantInitializer(const ir::Operands &operands,
27                                          const std::shared_ptr<ITensorRegistry> &tensor_reg)
28     : acl_common::AclConstantInitializer{operands, tensor_reg}
29 {
30   // DO NOTHING
31 }
32
33 void ConstantInitializer::visit(const ir::operation::EmbeddingLookup &node)
34 {
35   copyInputInitialize(node, ir::operation::EmbeddingLookup::LOOKUPS);
36 }
37
38 void ConstantInitializer::visit(const ir::operation::Gather &node)
39 {
40   copyInputInitialize(node, ir::operation::Gather::INDICES);
41 }
42
43 void ConstantInitializer::visit(const ir::operation::HashtableLookup &node)
44 {
45   copyInputInitialize(node, ir::operation::HashtableLookup::LOOKUPS);
46   copyInputInitialize(node, ir::operation::HashtableLookup::KEYS);
47 }
48
49 void ConstantInitializer::visit(const ir::operation::SpaceToBatchND &node)
50 {
51   const auto &block_size_index = node.getInputs().at(ir::operation::SpaceToBatchND::BLOCK_SIZE);
52   const auto &block_size_obj = _operands.at(block_size_index);
53
54   if (block_size_obj.isConstant())
55   {
56     _init_map[block_size_index] = [](const ir::Operand &model_obj, backend::ITensor &obj) {
57       assert(model_obj.data());
58       const auto &shape = model_obj.shape();
59       const auto base = reinterpret_cast<const int32_t *>(model_obj.data()->base());
60       assert(model_obj.shape().rank() == 1);
61       obj.access([&](ITensor &tensor) {
62         for (size_t i = 0; i < shape.num_elements(); ++i)
63         {
64           const int32_t value = base[shape.num_elements() - i - 1];
65           int32_t *into = reinterpret_cast<int32_t *>(tensor.buffer() +
66                                                       tensor.calcOffset({static_cast<int32_t>(i)}));
67           *into = value;
68         }
69       });
70     };
71   }
72
73   const auto &paddings_index = node.getInputs().at(ir::operation::SpaceToBatchND::PADDINGS);
74   const auto &paddings_obj = _operands.at(paddings_index);
75   if (paddings_obj.isConstant())
76   {
77     _init_map[paddings_index] = [](const ir::Operand &model_obj, backend::ITensor &obj) {
78       assert(model_obj.data());
79       const auto &shape = model_obj.shape();
80       const auto base = reinterpret_cast<const int32_t *>(model_obj.data()->base());
81       assert(model_obj.shape().rank() == 2);
82       assert(obj.dimension(0) == 2);
83       obj.access([&](ITensor &tensor) {
84         for (auto i = 0; i < shape.dim(0); ++i)
85         {
86           for (auto j = 0; j < shape.dim(1); ++j)
87           {
88             const int32_t value = base[i * 2 + j];
89             int32_t *into = reinterpret_cast<int32_t *>(
90                 tensor.buffer() + tensor.calcOffset({shape.dim(0) - i - 1, j}));
91             *into = value;
92           }
93         }
94       });
95     };
96   }
97 }
98
99 } // namespace acl_cl
100 } // namespace backend
101 } // namespace onert