Imported Upstream version 1.11.1
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / cpu_common / ConstantInitializerBase.cc
1 /*
2  * Copyright (c) 2020 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 "backend/cpu_common/ConstantInitializerBase.h"
18
19 #include <Half.h>
20
21 using float16 = Half;
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu_common
28 {
29
30 void ConstantInitializerBase::registerCopyInitializer(const ir::OperandIndex &index,
31                                                       const ir::Operand &obj)
32 {
33   // For only CONSTANTS
34   // TODO Add to check if tensor has been allocated
35   if (!obj.isConstant())
36     return;
37
38   const auto type = obj.typeInfo().type();
39   using ir::DataType;
40
41   switch (type)
42   {
43     case DataType::FLOAT32:
44       _init_map[index] = copyInit<float>;
45       break;
46     case DataType::INT32:
47       _init_map[index] = copyInit<int32_t>;
48       break;
49     case DataType::UINT32:
50       _init_map[index] = copyInit<uint32_t>;
51       break;
52     case DataType::BOOL8:
53     case DataType::QUANT_UINT8_ASYMM:
54       _init_map[index] = copyInit<uint8_t>;
55       break;
56     case DataType::QUANT_INT8_SYMM:
57     case DataType::QUANT_INT8_ASYMM:
58       _init_map[index] = copyInit<int8_t>;
59       break;
60     case DataType::FLOAT16:
61       _init_map[index] = copyInit<float16>;
62       break;
63     case DataType::INT64:
64       _init_map[index] = copyInit<int64_t>;
65       break;
66     default:
67       throw std::runtime_error("Not supported, yet");
68       break;
69   }
70 }
71
72 void ConstantInitializerBase::registerPermuteInitializer(const ir::OperandIndex &index,
73                                                          const ir::Operand &obj)
74 {
75   // For only CONSTANTS
76   // TODO Add to check if tensor has been allocated
77   if (!obj.isConstant())
78     return;
79
80   const auto type = obj.typeInfo().type();
81   using ir::DataType;
82   using namespace std::placeholders;
83
84   switch (type)
85   {
86     case DataType::FLOAT32:
87       _init_map[index] = std::bind(permuteInit<float>, _1, _2, _current_layout);
88       break;
89     case DataType::INT32:
90       _init_map[index] = std::bind(permuteInit<int32_t>, _1, _2, _current_layout);
91       break;
92     case DataType::UINT32:
93       _init_map[index] = std::bind(permuteInit<uint32_t>, _1, _2, _current_layout);
94       break;
95     case DataType::BOOL8:
96     case DataType::QUANT_UINT8_ASYMM:
97       _init_map[index] = std::bind(permuteInit<uint8_t>, _1, _2, _current_layout);
98       break;
99     case DataType::QUANT_INT8_SYMM:
100     case DataType::QUANT_INT8_ASYMM:
101       _init_map[index] = std::bind(permuteInit<int8_t>, _1, _2, _current_layout);
102       break;
103     case DataType::FLOAT16:
104       _init_map[index] = std::bind(permuteInit<float16>, _1, _2, _current_layout);
105       break;
106     case DataType::INT64:
107       _init_map[index] = std::bind(permuteInit<int64_t>, _1, _2, _current_layout);
108       break;
109     default:
110       throw std::runtime_error("Not supported, yet");
111       break;
112   }
113 }
114
115 } // namespace cpu_common
116 } // namespace backend
117 } // namespace onert