forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.cpp
More file actions
141 lines (125 loc) · 4.48 KB
/
TestUtils.cpp
File metadata and controls
141 lines (125 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "kernels/TestUtils.h"
#include <stdexcept>
namespace luci_interpreter
{
namespace kernels
{
namespace testing
{
using ::testing::FloatNear;
using ::testing::DoubleNear;
using ::testing::Matcher;
Tensor makeOutputTensor(DataType element_type) { return Tensor(element_type, {}, {}, ""); }
Tensor makeOutputTensor(DataType element_type, float scale, int32_t zero_point)
{
return Tensor(element_type, {}, {{scale}, {zero_point}}, "");
}
std::vector<float> dequantizeTensorData(const Tensor &tensor)
{
if (tensor.element_type() == DataType::U8)
{
std::vector<uint8_t> data = extractTensorData<uint8_t>(tensor);
return dequantize(data.data(), data.size(), tensor.scale(), tensor.zero_point());
}
if (tensor.element_type() == DataType::S8)
{
std::vector<int8_t> data = extractTensorData<int8_t>(tensor);
return dequantize(data.data(), data.size(), tensor.scale(), tensor.zero_point());
}
else if (tensor.element_type() == DataType::S16)
{
// S16 quantization is symmetric, so zero point should be zero.
for (auto zp : tensor.zero_points())
{
(void)zp;
assert(zp == 0);
}
std::vector<int16_t> data = extractTensorData<int16_t>(tensor);
if (tensor.scales().size() == 1)
{
return dequantize(data.data(), data.size(), tensor.scale(), 0);
}
// quantize_dimension breaks shape into two parts:
// inner dimensions that contains continuous data with one quantization type
// outer dimensions that contains other dimensions
const Shape shape = tensor.shape();
const int32_t quantized_dimension = tensor.quantized_dimension();
assert(quantized_dimension < shape.num_dims());
size_t outer_dims_size = 1;
int32_t quant_dim_size = shape.dim(quantized_dimension);
size_t inner_dims_size = 1;
assert(quant_dim_size == tensor.scales().size());
for (int i = 0; i < quantized_dimension; ++i)
outer_dims_size *= shape.dim(i);
for (int i = quantized_dimension + 1; i < shape.num_dims(); ++i)
inner_dims_size *= shape.dim(i);
assert(shape.num_elements() == outer_dims_size * quant_dim_size * inner_dims_size);
std::vector<float> dequantized_data;
dequantized_data.reserve(shape.num_elements());
for (size_t outer_it = 0; outer_it < outer_dims_size; ++outer_it)
for (int32_t channel = 0; channel < quant_dim_size; ++channel)
{
float scale = tensor.scales()[channel];
size_t offset = inner_dims_size * (quant_dim_size * outer_it + channel);
std::vector<float> part_dequantized_data =
dequantize(data.data() + offset, inner_dims_size, scale, 0);
dequantized_data.insert(dequantized_data.end(), part_dequantized_data.begin(),
part_dequantized_data.end());
}
return dequantized_data;
}
else
{
throw std::runtime_error("Unsupported type.");
}
}
Matcher<std::vector<float>> FloatArrayNear(const std::vector<float> &values, float max_abs_error)
{
std::vector<Matcher<float>> matchers;
matchers.reserve(values.size());
for (const float v : values)
{
matchers.emplace_back(FloatNear(v, max_abs_error));
}
return ElementsAreArray(matchers);
}
Matcher<std::vector<double>> DoubleArrayNear(const std::vector<double> &values,
double max_abs_error)
{
std::vector<Matcher<double>> matchers;
matchers.reserve(values.size());
for (const double v : values)
{
matchers.emplace_back(DoubleNear(v, max_abs_error));
}
return ElementsAreArray(matchers);
}
std::vector<int32_t> extractTensorShape(const Tensor &tensor)
{
std::vector<int32_t> result;
int dims = tensor.shape().num_dims();
for (int i = 0; i < dims; i++)
{
result.push_back(tensor.shape().dim(i));
}
return result;
}
} // namespace testing
} // namespace kernels
} // namespace luci_interpreter