318ec0e41509c19143bcd861f779637769d06bfb
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / include / luci_interpreter / core / Tensor.h
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 #ifndef LUCI_INTERPRETER_CORE_TENSOR_H
18 #define LUCI_INTERPRETER_CORE_TENSOR_H
19
20 #include "luci_interpreter/core/DataType.h"
21 #include "luci_interpreter/core/reader/CircleMicroReader.h"
22
23 #include <cassert>
24 #include <cstddef>
25 #include <cstdint>
26 #include <memory>
27 #include <string>
28 #include <vector>
29
30 namespace luci_interpreter
31 {
32
33 class Tensor
34 {
35 public:
36 #ifndef DIS_QUANT
37   static float scale(const circle::Tensor *circle_tensor)
38   {
39     const auto *quant_params = circle_tensor->quantization();
40     if (quant_params == nullptr)
41     {
42       assert(false && "There is no quantization params");
43       return 0;
44     }
45
46     return *quant_params->scale()->cbegin();
47   }
48
49   static int32_t zero_point(const circle::Tensor *circle_tensor)
50   {
51     const auto *quant_params = circle_tensor->quantization();
52     if (quant_params == nullptr)
53     {
54       assert(false && "There is no quantization params");
55       return 0;
56     }
57
58     return *quant_params->zero_point()->cbegin();
59   }
60
61   static const std::vector<float> scales(const circle::Tensor *circle_tensor)
62   {
63     const auto *quant_params = circle_tensor->quantization();
64     if (quant_params == nullptr)
65     {
66       assert(false && "There is no quantization params");
67       return {};
68     }
69     assert(quant_params->scale() != nullptr);
70     std::vector<float> scales(quant_params->scale()->cbegin(), quant_params->scale()->cend());
71
72     return scales;
73   }
74
75   static const std::vector<int32_t> zero_points(const circle::Tensor *circle_tensor)
76   {
77     const auto *quant_params = circle_tensor->quantization();
78     if (quant_params == nullptr)
79     {
80       assert(false && "There is no quantization params");
81       return {};
82     }
83     assert(quant_params->zero_point() != nullptr);
84     std::vector<int32_t> zero_points(quant_params->zero_point()->cbegin(),
85                                      quant_params->zero_point()->cend());
86
87     return zero_points;
88   }
89
90   static int32_t quantized_dimension(const circle::Tensor *circle_tensor)
91   {
92     const auto *quant_params = circle_tensor->quantization();
93     if (quant_params == nullptr)
94     {
95       assert(false && "There is no quantization params");
96       return 0;
97     }
98     return quant_params->quantized_dimension();
99   }
100 #endif
101
102   static DataType element_type(const circle::Tensor *circle_tensor)
103   {
104     return luci_datatype(circle_tensor->type());
105   }
106
107   static int num_dims(const circle::Tensor *circle_tensor)
108   {
109     // TODO check removing of wrap
110     auto const &const_dims = wrap(circle_tensor->shape());
111     return const_dims.size();
112   }
113
114   static int32_t dim(const circle::Tensor *circle_tensor, int i)
115   {
116     // TODO check removing of wrap
117     assert(i >= 0);
118     auto const &const_dims = wrap(circle_tensor->shape());
119     assert(i < const_dims.size());
120
121     return const_dims[i];
122   }
123
124   static int32_t num_elements(const circle::Tensor *circle_tensor)
125   {
126     int32_t result = 1;
127     auto const &const_dims = wrap(circle_tensor->shape());
128     for (const int32_t dim : const_dims)
129     {
130       result *= dim;
131     }
132     return result;
133   }
134 };
135
136 } // namespace luci_interpreter
137
138 #endif // LUCI_INTERPRETER_CORE_TENSOR_H