1 /*
2 * Copyright (C) 2018 The Android Open Source Project
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 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
18
19 Licensed under the Apache License, Version 2.0 (the "License");
20 you may not use this file except in compliance with the License.
21 You may obtain a copy of the License at
22
23 http://www.apache.org/licenses/LICENSE-2.0
24
25 Unless required by applicable law or agreed to in writing, software
26 distributed under the License is distributed on an "AS IS" BASIS,
27 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 See the License for the specific language governing permissions and
29 limitations under the License.
30 ==============================================================================*/
31 #ifndef TENSORFLOW_MODELS_SEQUENCE_PROJECTION_TFLITE_OPS_QUANTIZATION_UTIL_H_
32 #define TENSORFLOW_MODELS_SEQUENCE_PROJECTION_TFLITE_OPS_QUANTIZATION_UTIL_H_
33
34 #include <algorithm>
35 #include <cmath>
36
37 #include "tensorflow/lite/context.h"
38
39 namespace seq_flow_lite {
40
41 // Returns the original (dequantized) value of 8bit value.
PodDequantizeValue(const TfLiteTensor & tensor,uint8_t value)42 inline float PodDequantizeValue(const TfLiteTensor& tensor, uint8_t value) {
43 const int32_t zero_point = tensor.params.zero_point;
44 const float scale = tensor.params.scale;
45 return (static_cast<int32_t>(value) - zero_point) * scale;
46 }
47
48 // Returns the original (dequantized) value of the 'index'-th element of
49 // 'tensor.
PodDequantize(const TfLiteTensor & tensor,int index)50 inline float PodDequantize(const TfLiteTensor& tensor, int index) {
51 return PodDequantizeValue(tensor, tensor.data.uint8[index]);
52 }
53
54 // Quantizes 'value' to 8bit, given the quantization bias (zero_point) and
55 // factor (inverse_scale).
PodQuantize(float value,int32_t zero_point,float inverse_scale)56 inline uint8_t PodQuantize(float value, int32_t zero_point,
57 float inverse_scale) {
58 const float integer_value_in_float = value * inverse_scale;
59 const float offset = (integer_value_in_float >= 0.0) ? 0.5f : -0.5f;
60 // NOTE(sfeuz): This assumes value * inverse_scale is within [INT_MIN,
61 // INT_MAX].
62 int32_t integer_value =
63 static_cast<int32_t>(integer_value_in_float + offset) + zero_point;
64 return static_cast<uint8_t>(std::max(std::min(255, integer_value), 0));
65 }
66
67 } // namespace seq_flow_lite
68
69 #endif // TENSORFLOW_MODELS_SEQUENCE_PROJECTION_TFLITE_OPS_QUANTIZATION_UTIL_H_
70