|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_ |
|
|
#define TENSORFLOW_LITE_MICRO_MICRO_UTILS_H_ |
|
|
|
|
|
|
|
|
|
|
|
#ifdef abs |
|
|
#undef abs |
|
|
#endif |
|
|
|
|
|
#include <algorithm> |
|
|
#include <cmath> |
|
|
#include <cstdint> |
|
|
#include <limits> |
|
|
|
|
|
#include "edge-impulse-sdk/tensorflow/lite/c/common.h" |
|
|
|
|
|
namespace tflite { |
|
|
|
|
|
|
|
|
|
|
|
int ElementCount(const TfLiteIntArray& dims); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
T FloatToQuantizedType(const float value, const float scale, int zero_point) { |
|
|
int32_t result = round(value / scale) + zero_point; |
|
|
result = |
|
|
std::max(static_cast<int32_t>(std::numeric_limits<T>::min()), result); |
|
|
result = |
|
|
std::min(static_cast<int32_t>(std::numeric_limits<T>::max()), result); |
|
|
return result; |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
T FloatToSymmetricQuantizedType(const float value, const float scale) { |
|
|
int32_t result = round(value / scale); |
|
|
result = |
|
|
std::max(static_cast<int32_t>(std::numeric_limits<T>::min() + 1), result); |
|
|
result = |
|
|
std::min(static_cast<int32_t>(std::numeric_limits<T>::max()), result); |
|
|
return result; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
void Quantize(const float* input, T* output, int num_elements, float scale, |
|
|
int zero_point) { |
|
|
for (int i = 0; i < num_elements; i++) { |
|
|
output[i] = FloatToQuantizedType<T>(input[i], scale, zero_point); |
|
|
} |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void SymmetricQuantize(const float* input, T* output, int num_elements, |
|
|
float scale) { |
|
|
for (int i = 0; i < num_elements; i++) { |
|
|
output[i] = FloatToSymmetricQuantizedType<T>(input[i], scale); |
|
|
} |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void SymmetricPerChannelQuantize(const float* input, T* output, |
|
|
int num_elements, int num_channels, |
|
|
float* scales) { |
|
|
int elements_per_channel = num_elements / num_channels; |
|
|
for (int i = 0; i < num_channels; i++) { |
|
|
for (int j = 0; j < elements_per_channel; j++) { |
|
|
output[i * elements_per_channel + j] = FloatToSymmetricQuantizedType<T>( |
|
|
input[i * elements_per_channel + j], scales[i]); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
void SignedSymmetricPerChannelQuantize(const float* values, |
|
|
TfLiteIntArray* dims, |
|
|
int quantized_dimension, |
|
|
int8_t* quantized_values, |
|
|
float* scaling_factor); |
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
void SymmetricQuantizeCalculateScales(const float* values, TfLiteIntArray* dims, |
|
|
T* output, float* scale) { |
|
|
int input_size = ElementCount(*dims); |
|
|
|
|
|
float min = 0; |
|
|
float max = 0; |
|
|
for (int i = 0; i < input_size; i++) { |
|
|
min = fminf(min, values[i]); |
|
|
max = fmaxf(max, values[i]); |
|
|
} |
|
|
*scale = fmaxf(std::abs(min), std::abs(max)) / std::numeric_limits<T>::max(); |
|
|
for (int i = 0; i < input_size; i++) { |
|
|
const int32_t quantized_value = |
|
|
static_cast<int32_t>(roundf(values[i] / *scale)); |
|
|
|
|
|
quantized_value = fminf(std::numeric_limits<T>::max(), quantized_value); |
|
|
quantized_value = fmaxf(std::numeric_limits<T>::min() + 1, quantized_value); |
|
|
output[i] = quantized_value; |
|
|
} |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
void Dequantize(const T* values, const int size, const float scale, |
|
|
int zero_point, float* dequantized_values) { |
|
|
for (int i = 0; i < size; ++i) { |
|
|
dequantized_values[i] = (values[i] - zero_point) * scale; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
#endif |
|
|
|