Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Dequantize.cpp
1 /*
2  * Copyright (c) 2022 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 "kernels/Dequantize.h"
18 #include "kernels/Utils.h"
19 #include "PALDequantize.h"
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25
26 Dequantize::Dequantize(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
27
28 void Dequantize::configure()
29 {
30   LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::S8 ||
31                          input()->element_type() == DataType::U8 ||
32                          input()->element_type() == DataType::S16);
33
34   LUCI_INTERPRETER_CHECK(input()->scales().size() == 1);
35
36   if (input()->element_type() == DataType::S16)
37     LUCI_INTERPRETER_CHECK(input()->zero_point() == 0);
38
39   LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::FLOAT32);
40
41   // TODO: enable it only if kernel with dynamic shapes
42   output()->resize(input()->shape());
43 }
44
45 void Dequantize::execute() const
46 {
47   tflite::DequantizationParams op_params;
48   op_params.zero_point = input()->zero_point();
49   op_params.scale = input()->scale();
50
51   switch (input()->element_type())
52   {
53     case DataType::U8:
54     {
55       luci_interpreter_pal::Dequantize(op_params, getTensorShape(input()),
56                                        getTensorData<uint8_t>(input()), getTensorShape(output()),
57                                        getTensorData<float>(output()));
58       break;
59     }
60     case DataType::S8:
61     {
62       luci_interpreter_pal::Dequantize(op_params, getTensorShape(input()),
63                                        getTensorData<int8_t>(input()), getTensorShape(output()),
64                                        getTensorData<float>(output()));
65       break;
66     }
67     case DataType::S16:
68     {
69       luci_interpreter_pal::Dequantize(op_params, getTensorShape(input()),
70                                        getTensorData<int16_t>(input()), getTensorShape(output()),
71                                        getTensorData<float>(output()));
72       break;
73     }
74     default:
75       assert(false && "Unsupported type.");
76   }
77 }
78
79 } // namespace kernels
80 } // namespace luci_interpreter