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