• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_
17 #define TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <cstdint>
22 
23 #include "tensorflow/lite/c/common.h"
24 
25 namespace tflite {
26 
27 // Returns number of elements in the shape array.
28 
29 int ElementCount(const TfLiteIntArray& dims);
30 
31 // Converts a float value into a quantized value.  Note that large values (close
32 // to max int and min int) may see significant error due to a lack of floating
33 // point granularity for large values.
34 template <typename T>
FloatToQuantizedType(const float value,const float scale,int zero_point)35 T FloatToQuantizedType(const float value, const float scale, int zero_point) {
36   int32_t result = round(value / scale) + zero_point;
37   result =
38       std::max(static_cast<int32_t>(std::numeric_limits<T>::min()), result);
39   result =
40       std::min(static_cast<int32_t>(std::numeric_limits<T>::max()), result);
41   return result;
42 }
43 
44 template <typename T>
FloatToSymmetricQuantizedType(const float value,const float scale)45 T FloatToSymmetricQuantizedType(const float value, const float scale) {
46   int32_t result = round(value / scale);
47   result =
48       std::max(static_cast<int32_t>(std::numeric_limits<T>::min() + 1), result);
49   result =
50       std::min(static_cast<int32_t>(std::numeric_limits<T>::max()), result);
51   return result;
52 }
53 
54 // Helper methods to quantize arrays of floats to the desired format.
55 //
56 // There are several key flavors of quantization in TfLite:
57 //        asymmetric symmetric  per channel
58 // int8_t  |     X    |    X    |     X      |
59 // uint8_t |     X    |    X    |            |
60 // int16_t |     X    |         |            |
61 // int32_t |          |    X    |     X      |
62 //
63 // The per-op quantization spec can be found here:
64 // https://www.tensorflow.org/lite/performance/quantization_spec
65 template <typename T>
Quantize(const float * input,T * output,int num_elements,float scale,int zero_point)66 void Quantize(const float* input, T* output, int num_elements, float scale,
67               int zero_point) {
68   for (int i = 0; i < num_elements; i++) {
69     output[i] = FloatToQuantizedType<T>(input[i], scale, zero_point);
70   }
71 }
72 
73 template <typename T>
SymmetricQuantize(const float * input,T * output,int num_elements,float scale)74 void SymmetricQuantize(const float* input, T* output, int num_elements,
75                        float scale) {
76   for (int i = 0; i < num_elements; i++) {
77     output[i] = FloatToSymmetricQuantizedType<T>(input[i], scale);
78   }
79 }
80 
81 template <typename T>
SymmetricPerChannelQuantize(const float * input,T * output,int num_elements,int num_channels,float * scales)82 void SymmetricPerChannelQuantize(const float* input, T* output,
83                                  int num_elements, int num_channels,
84                                  float* scales) {
85   int elements_per_channel = num_elements / num_channels;
86   for (int i = 0; i < num_channels; i++) {
87     for (int j = 0; j < elements_per_channel; j++) {
88       output[i * elements_per_channel + j] = FloatToSymmetricQuantizedType<T>(
89           input[i * elements_per_channel + j], scales[i]);
90     }
91   }
92 }
93 
94 void SignedSymmetricPerChannelQuantize(const float* values,
95                                        TfLiteIntArray* dims,
96                                        int quantized_dimension,
97                                        int8_t* quantized_values,
98                                        float* scaling_factor);
99 
100 // Quantizes inputs based on the values provided, choosing the smallest range
101 // which includes all input values.
102 template <typename T>
SymmetricQuantizeCalculateScales(const float * values,TfLiteIntArray * dims,T * output,float * scale)103 void SymmetricQuantizeCalculateScales(const float* values, TfLiteIntArray* dims,
104                                       T* output, float* scale) {
105   int input_size = ElementCount(*dims);
106 
107   float min = 0;
108   float max = 0;
109   for (int i = 0; i < input_size; i++) {
110     min = fminf(min, values[i]);
111     max = fmaxf(max, values[i]);
112   }
113   *scale = fmaxf(std::abs(min), std::abs(max)) / std::numeric_limits<T>::max();
114   for (int i = 0; i < input_size; i++) {
115     const int32_t quantized_value =
116         static_cast<int32_t>(roundf(values[i] / *scale));
117     // Clamp: just in case some odd numeric offset.
118     quantized_value = fminf(std::numeric_limits<T>::max(), quantized_value);
119     quantized_value = fmaxf(std::numeric_limits<T>::min() + 1, quantized_value);
120     output[i] = quantized_value;
121   }
122 }
123 
124 template <typename T>
Dequantize(const T * values,const int size,const float scale,int zero_point,float * dequantized_values)125 void Dequantize(const T* values, const int size, const float scale,
126                 int zero_point, float* dequantized_values) {
127   for (int i = 0; i < size; ++i) {
128     dequantized_values[i] = (values[i] - zero_point) * scale;
129   }
130 }
131 
132 }  // namespace tflite
133 
134 #endif  // TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_
135