140ab4d2c12ccf514f4304596554911f014990e2
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / LogisticLayer.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 "LogisticLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/Logistic.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 LogisticLayer::LogisticLayer() : _input(nullptr), _output(nullptr)
33 {
34   // DO NOTHING
35 }
36
37 void LogisticLayer::populateLookupTable()
38 {
39   const auto input_scale = static_cast<double>(_input->data_scale());
40   const auto input_zero_point = static_cast<int32_t>(_input->data_offset());
41   const auto output_scale = static_cast<double>(_output->data_scale());
42   const auto output_zero_point = static_cast<int32_t>(_output->data_offset());
43   const float inverse_scale = 1 / output_scale;
44   int32_t maxval = std::numeric_limits<uint8_t>::max();
45   int32_t minval = std::numeric_limits<uint8_t>::min();
46   for (int32_t val = minval; val <= maxval; ++val)
47   {
48     const float dequantized = input_scale * (val - input_zero_point);
49     const float transformed = 1.0f / (1.0f + std::exp(-dequantized));
50     const float rescaled = std::round(transformed * inverse_scale);
51     const int32_t quantized = static_cast<int32_t>(rescaled + output_zero_point);
52     _table[val] = static_cast<uint8_t>(std::max(std::min(maxval, quantized), minval));
53   }
54 }
55
56 void LogisticLayer::logisticFloat32()
57 {
58   nnfw::cker::Logistic(getTensorShape(_input), reinterpret_cast<const float *>(_input->buffer()),
59                        getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()));
60 }
61
62 void LogisticLayer::logisticQuant8()
63 {
64   const int size = MatchingFlatSize(getTensorShape(_input), getTensorShape(_output));
65   const uint8_t *input_data = reinterpret_cast<const uint8_t *>(_input->buffer());
66   uint8_t *output_data = reinterpret_cast<uint8_t *>(_output->buffer());
67
68   for (int i = 0; i < size; ++i)
69   {
70     output_data[i] = _table[input_data[i]];
71   }
72 }
73
74 void LogisticLayer::configure(const IPortableTensor *input, IPortableTensor *output)
75 {
76   _input = input;
77   _output = output;
78
79   if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
80   {
81     if (_output->data_scale() != 1.f / 256)
82     {
83       throw std::runtime_error{"incorrect scale for output"};
84     }
85     populateLookupTable();
86   }
87 }
88
89 void LogisticLayer::run()
90 {
91   if (_input->data_type() == OperandType::FLOAT32)
92   {
93     logisticFloat32();
94   }
95   else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
96   {
97     logisticQuant8();
98   }
99   else
100   {
101     throw std::runtime_error{"Logistic: unsupported data type"};
102   }
103 }
104
105 } // namespace ops
106 } // namespace cpu
107 } // namespace backend
108 } // namespace onert