18#ifndef LUCI_INTERPRETER_KERNELS_TESTUTILS_H
19#define LUCI_INTERPRETER_KERNELS_TESTUTILS_H
21#include "luci_interpreter/core/Tensor.h"
27#include <gtest/gtest.h>
28#include <gmock/gmock.h>
38std::vector<T>
quantize(
const float *
data,
size_t num_elements,
float scale, int32_t zero_point);
65 Tensor tensor(
DT, shape, {{scale}, {zero_point}},
"");
86 const std::vector<int32_t> &zero_points,
int quantized_dimension,
90 assert(quantized_dimension < shape.
num_dims());
91 Tensor tensor(
DT, shape, {scales, zero_points, quantized_dimension},
"");
102 for (
int i = 0;
i < quantized_dimension; ++
i)
104 for (
int i = quantized_dimension + 1;
i < shape.
num_dims(); ++
i)
114 int32_t zero_point = zero_points[channel];
115 float scale = scales[channel];
136 if (std::is_same<T, float>::value)
137 return DataType::FLOAT32;
138 if (std::is_same<T, double>::value)
139 return DataType::FLOAT64;
140 if (std::is_same<T, uint8_t>::value)
142 if (std::is_same<T, uint16_t>::value)
143 return DataType::U16;
144 if (std::is_same<T, uint32_t>::value)
145 return DataType::U32;
146 if (std::is_same<T, uint64_t>::value)
147 return DataType::U64;
148 if (std::is_same<T, int8_t>::value)
150 if (std::is_same<T, int16_t>::value)
151 return DataType::S16;
152 if (std::is_same<T, int32_t>::value)
153 return DataType::S32;
154 if (std::is_same<T, int64_t>::value)
155 return DataType::S64;
156 if (std::is_same<T, bool>::value)
157 return DataType::BOOL;
158 return DataType::Unknown;
163 const auto *
data_ptr = tensor.data<T>();
170::testing::Matcher<std::vector<float>>
FloatArrayNear(
const std::vector<float> &values,
174std::vector<T>
quantize(
const float *
data,
size_t num_elements,
float scale, int32_t zero_point)
176 static_assert(std::is_integral<T>::value,
"Integral type expected.");
179 if (std::is_signed<T>::value)
181 q_min = -std::numeric_limits<T>::max();
182 q_max = std::numeric_limits<T>::max();
187 q_max = std::numeric_limits<T>::max();
191 for (
size_t i = 0;
i < num_elements; ++
i)
193 const auto &f =
data[
i];
194 q.push_back(
static_cast<T
>(
195 std::max<float>(
q_min, std::min<float>(
q_max, std::round(zero_point + (f / scale))))));
201std::vector<float>
dequantize(
const T *
data,
size_t num_elements,
float scale, int32_t zero_point)
203 static_assert(std::is_integral<T>::value,
"Integral type expected.");
204 std::vector<float> f;
205 for (
size_t i = 0;
i < num_elements; ++
i)
207 const T &q =
data[
i];
208 f.push_back(scale * (q - zero_point));
216 static_assert(std::is_integral<T>::value,
"Integral type expected.");
217 int32_t zero_point = 0;
219 const T
qmin = std::numeric_limits<T>::lowest();
220 const T
qmax = std::numeric_limits<T>::max();
232 return {scale, zero_point};
285 return {scale, zero_point};
int32_t num_elements() const
__global uchar * offset(const Image *img, int x, int y)
const T * data(const std::vector< T, Alloc > &v)
Tensor makeOutputTensor(DataType element_type)
std::vector< float > dequantize(const T *data, size_t num_elements, float scale, int32_t zero_point)
std::vector< int32_t > extractTensorShape(const Tensor &tensor)
float getTolerance(float min, float max, int quantize_steps)
constexpr DataType getElementType()
std::vector< float > dequantizeTensorData(const Tensor &tensor)
Matcher< std::vector< float > > FloatArrayNear(const std::vector< float > &values, float max_abs_error)
std::vector< T > quantize(const float *data, size_t num_elements, float scale, int32_t zero_point)
std::pair< float, int32_t > quantizationParams(float f_min, float f_max)
std::vector< T > extractTensorData(const Tensor &tensor)
Tensor makeInputTensor(const Shape &shape, const std::vector< typename DataTypeImpl< DT >::Type > &data, IMemoryManager *memory_manager)
DataType
"scalar" value type
T must_cast(loco::Node *node)
C++ scalar type corresponding to each DataType.