blob: f55d20bf3eff022d23ae392287b79775009e7fde [file] [log] [blame]
Sanghoon Lee24486d62017-09-04 15:51:21 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2017-2023 Arm Limited.
Sanghoon Lee24486d62017-09-04 15:51:21 +01003 *
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#ifndef ARM_COMPUTE_TEST_DEPTH_CONVERT_FIXTURE
25#define ARM_COMPUTE_TEST_DEPTH_CONVERT_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Sanghoon Lee24486d62017-09-04 15:51:21 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/DepthConvertLayer.h"
Sanghoon Lee24486d62017-09-04 15:51:21 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
Usama Arif9e631c22019-05-14 17:10:40 +010043/* This function ignores the scale and zeroPoint of quanized tensors, i.e. QASYMM8 input is treated as uint8 values.*/
Sanghoon Lee24486d62017-09-04 15:51:21 +010044template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
Michalis Spyroue2588182018-12-13 18:31:18 +000045class DepthConvertLayerValidationBaseFixture : public framework::Fixture
Sanghoon Lee24486d62017-09-04 15:51:21 +010046{
47public:
Michalis Spyroue2588182018-12-13 18:31:18 +000048 void setup(TensorShape shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift, QuantizationInfo quantization_info)
Sanghoon Lee24486d62017-09-04 15:51:21 +010049 {
Michalis Spyroue2588182018-12-13 18:31:18 +000050 _shift = shift;
51 _quantization_info = quantization_info;
52 _target = compute_target(shape, dt_in, dt_out, policy, shift);
53 _reference = compute_reference(shape, dt_in, dt_out, policy, shift);
Sanghoon Lee24486d62017-09-04 15:51:21 +010054 }
55
56protected:
57 template <typename U>
Usama Arif9e631c22019-05-14 17:10:40 +010058 void fill(U &&tensor, int i, DataType dt_in, DataType dt_out)
Sanghoon Lee24486d62017-09-04 15:51:21 +010059 {
Michalis Spyroue2588182018-12-13 18:31:18 +000060 if(is_data_type_quantized(tensor.data_type()))
61 {
62 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
Pablo Tello29cab362022-03-10 17:05:34 +000063 std::uniform_int_distribution<uint32_t> distribution(bounds.first, bounds.second);
Michalis Spyroue2588182018-12-13 18:31:18 +000064
65 library->fill(tensor, distribution, i);
66 }
67 else
68 {
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000069 // When converting S32 to F16, both reference and Compute Library implementations are + or - infinity outside the F16 range.
Sheri Zhangac6499a2021-02-10 15:32:38 +000070 if(dt_in == DataType::S32 && dt_out == DataType::F16)
Usama Arif9e631c22019-05-14 17:10:40 +010071 {
72 std::uniform_int_distribution<int32_t> distribution_s32(-65504, 65504);
73 library->fill(tensor, distribution_s32, i);
74 }
75 else
76 {
77 library->fill_tensor_uniform(tensor, i);
78 }
Michalis Spyroue2588182018-12-13 18:31:18 +000079 }
Sanghoon Lee24486d62017-09-04 15:51:21 +010080 }
81
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010082 TensorType compute_target(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift)
Sanghoon Lee24486d62017-09-04 15:51:21 +010083 {
84 // Create tensors
Michalis Spyroue2588182018-12-13 18:31:18 +000085 TensorType src = create_tensor<TensorType>(shape, dt_in, 1, _quantization_info);
86 TensorType dst = create_tensor<TensorType>(shape, dt_out, 1, _quantization_info);
Sanghoon Lee24486d62017-09-04 15:51:21 +010087
88 // Create and configure function
89 FunctionType depth_convert;
90 depth_convert.configure(&src, &dst, policy, shift);
91
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010092 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
93 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Sanghoon Lee24486d62017-09-04 15:51:21 +010094
95 // Allocate tensors
96 src.allocator()->allocate();
97 dst.allocator()->allocate();
98
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010099 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
100 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Sanghoon Lee24486d62017-09-04 15:51:21 +0100101
102 // Fill tensors
Usama Arif9e631c22019-05-14 17:10:40 +0100103 fill(AccessorType(src), 0, dt_in, dt_out);
Sanghoon Lee24486d62017-09-04 15:51:21 +0100104
105 // Compute function
106 depth_convert.run();
107
108 return dst;
109 }
110
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100111 SimpleTensor<T2> compute_reference(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift)
Sanghoon Lee24486d62017-09-04 15:51:21 +0100112 {
113 // Create reference
Michalis Spyroue2588182018-12-13 18:31:18 +0000114 SimpleTensor<T1> src{ shape, dt_in, 1, _quantization_info };
Sanghoon Lee24486d62017-09-04 15:51:21 +0100115
116 // Fill reference
Usama Arif9e631c22019-05-14 17:10:40 +0100117 fill(src, 0, dt_in, dt_out);
Sanghoon Lee24486d62017-09-04 15:51:21 +0100118
119 return reference::depth_convert<T1, T2>(src, dt_out, policy, shift);
120 }
121
122 TensorType _target{};
123 SimpleTensor<T2> _reference{};
Sanghoon Lee24486d62017-09-04 15:51:21 +0100124 int _shift{};
Michalis Spyroue2588182018-12-13 18:31:18 +0000125 QuantizationInfo _quantization_info{};
126};
127
128template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
129class DepthConvertLayerValidationFixture : public DepthConvertLayerValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2>
130{
131public:
Michalis Spyroue2588182018-12-13 18:31:18 +0000132 void setup(TensorShape shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift)
133 {
134 DepthConvertLayerValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, dt_in, dt_out, policy,
135 shift, QuantizationInfo());
136 }
137};
138
139template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
140class DepthConvertLayerValidationQuantizedFixture : public DepthConvertLayerValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2>
141{
142public:
Michalis Spyroue2588182018-12-13 18:31:18 +0000143 void setup(TensorShape shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift, QuantizationInfo quantization_info)
144 {
145 DepthConvertLayerValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, dt_in, dt_out, policy,
146 shift, quantization_info);
147 }
Sanghoon Lee24486d62017-09-04 15:51:21 +0100148};
Sanghoon Lee24486d62017-09-04 15:51:21 +0100149} // namespace validation
150} // namespace test
151} // namespace arm_compute
152#endif /* ARM_COMPUTE_TEST_DEPTH_CONVERT_FIXTURE */