blob: d7ce4902097093f24ad56730176930da9ac4bb94 [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "QuantizationLayer.h"
25
Georgios Pinitasbb3be392017-09-20 18:36:03 +010026#include <cmath>
27
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010028namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010036template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
37SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<T> &src)
38{
39 // Create reference
40 SimpleTensor<uint8_t> dst{ src.shape(), DataType::U8 };
41
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010042 const int width = src.shape().x();
43 const int height = src.shape().y();
44 const int depth = src.shape().z();
45 const int stride_w = width * height * depth;
46 const int num_batches = src.shape().total_size_upper(3);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010047
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010048 for(int k = 0; k < num_batches; ++k)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010049 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010050 // Compute min and max of the 3D tensor
Georgios Pinitasbb3be392017-09-20 18:36:03 +010051 float min = src[k * stride_w];
52 float max = src[k * stride_w];
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010053
54 // Look for min and max values
55 for(int i = 1; i < stride_w; ++i)
56 {
57 float val = src[i + k * stride_w];
Georgios Pinitasbb3be392017-09-20 18:36:03 +010058 min = std::min(min, val);
59 max = std::max(max, val);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010060 }
61
62 // Saturate the result in case min = max
63 if(min == max)
64 {
65 min = 0.0f;
66 max = 1.0f;
67 }
68
69 const float range = max - min;
70
71 for(int i = 0; i < stride_w; ++i)
72 {
73 // map values to range [0.0, 1.0]
74 float val = src[i + k * stride_w];
75 const float normalized = (val - min) / range;
76 dst[i + k * stride_w] = static_cast<uint8_t>(std::min(255.0f, normalized * 256.0f));
77 }
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010078 }
79
80 return dst;
81}
82
83template SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<float> &src);
84} // namespace reference
85} // namespace validation
86} // namespace test
87} // namespace arm_compute