1 /* Copyright 2018 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 #ifndef TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZATION_UTILS_H_ 16 #define TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZATION_UTILS_H_ 17 18 #include <cstdint> 19 20 #include "tensorflow/lite/context.h" 21 #include "tensorflow/lite/schema/schema_generated.h" 22 23 namespace tflite { 24 namespace optimize { 25 namespace utils { 26 27 // Returns the number of elements in the given tensor. 28 TfLiteStatus NumElements(const TensorT& tensor, uint64_t* num_elements); 29 30 // Populates the scale and zero point for quantization parameters. 31 // 32 // Nudges min and max so that floating point 0 falls exactly on a quantized 33 // value, returning the nudges scale and zero_point. 34 void GetAsymmetricQuantizationParams( 35 float min, float max, const int quant_min, const int quant_max, 36 QuantizationParametersT* quantization_params); 37 38 // Per-channel quantize a tensor at the given index and returns both scales and 39 // quantized values. 40 // Parameters: 41 // - input is the float input data to be quantized. 42 // - dimension is the dimension of the input data. Only supports dimension of 43 // size 4. 44 // - channel_dim_index is the channel index within "dimension". 45 // dimension[channel_dim_index] gives the number of channels. 46 // - output_scale is the output scale, the size of which equals the number of 47 // channels. 48 // - output_value is the output data, the size of which equals the number of 49 // inputs. 50 void SymmetricPerChannelQuantization(const float* const input, 51 const std::vector<int>& dimension, 52 int32_t channel_dim_index, 53 std::vector<float>* output_scales, 54 std::vector<int8_t>* output_value); 55 56 // Quantize the values given an array of scales. 57 void SymmetricPerChannelQuantizeValues(const float* const input, 58 const std::vector<float>& scales_inv, 59 const std::vector<int>& dimension, 60 int32_t channel_dim_index, 61 std::vector<int8_t>* output_value); 62 63 // Quantizes tensor using symmetric quantization with the min and max elements 64 // of the tensor. 65 TfLiteStatus SymmetricQuantizeTensor(ModelT* model, TensorT* tensor); 66 67 } // namespace utils 68 } // namespace optimize 69 } // namespace tflite 70 71 #endif // TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZATION_UTILS_H_ 72