blob: 056f6642612442b26cef3972906057bad821fc00 [file] [log] [blame]
Georgios Pinitas303f0db2018-11-19 11:56:51 +00001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitas303f0db2018-11-19 11:56:51 +00003 *
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_CAST_FIXTURE
25#define ARM_COMPUTE_TEST_CAST_FIXTURE
26
27#include "tests/validation/fixtures/DepthConvertLayerFixture.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
36class CastValidationFixture : public framework::Fixture
37{
38public:
39 template <typename...>
40 void setup(TensorShape shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
41 {
42 _target = compute_target(shape, dt_in, dt_out, policy);
43 _reference = compute_reference(shape, dt_in, dt_out, policy);
44 }
45
46protected:
47 template <typename U>
48 void fill(U &&tensor, int i, DataType dt_in, DataType dt_out)
49 {
50 // Restricting range to avoid inf values
51 if(dt_out == DataType::F16)
52 {
53 const int signed_min = -32000;
54 const int signed_max = 32000;
55 const int unsigned_min = 0;
56 const int unsigned_max = 65000;
57
58 switch(dt_in)
59 {
60 case DataType::U8:
61 case DataType::QASYMM8:
SiCong Li2d10f182020-06-08 16:20:15 +010062 case DataType::QASYMM8_SIGNED:
Georgios Pinitas303f0db2018-11-19 11:56:51 +000063 case DataType::S8:
64 case DataType::F32:
65 {
66 library->fill_tensor_uniform(tensor, i);
67 break;
68 }
69 case DataType::U16:
70 {
71 library->fill_tensor_uniform(tensor, i, static_cast<uint16_t>(unsigned_min), static_cast<uint16_t>(unsigned_max));
72 break;
73 }
74 case DataType::S16:
75 {
76 library->fill_tensor_uniform(tensor, i, static_cast<int16_t>(signed_min), static_cast<int16_t>(signed_max));
77 break;
78 }
79 case DataType::U32:
80 {
81 library->fill_tensor_uniform(tensor, i, static_cast<uint32_t>(unsigned_min), static_cast<uint32_t>(unsigned_max));
82 break;
83 }
84 case DataType::S32:
85 {
86 library->fill_tensor_uniform(tensor, i, static_cast<int32_t>(signed_min), static_cast<int32_t>(signed_max));
87 break;
88 }
89 default:
90 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
91 }
92 }
93 else
94 {
95 library->fill_tensor_uniform(tensor, i);
96 }
97 }
98
99 TensorType compute_target(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
100 {
101 // Create tensors
102 TensorType src = create_tensor<TensorType>(shape, dt_in, 1);
103 TensorType dst = create_tensor<TensorType>(shape, dt_out, 1);
104
105 // Create and configure function
106 FunctionType cast;
107 cast.configure(&src, &dst, policy);
108
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100109 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
110 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitas303f0db2018-11-19 11:56:51 +0000111
112 // Allocate tensors
113 src.allocator()->allocate();
114 dst.allocator()->allocate();
115
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100116 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
117 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitas303f0db2018-11-19 11:56:51 +0000118
119 // Fill tensors
120 fill(AccessorType(src), 0, dt_in, dt_out);
121
122 // Compute function
123 cast.run();
124
125 return dst;
126 }
127
128 SimpleTensor<T2> compute_reference(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
129 {
130 // Create reference
131 SimpleTensor<T1> src{ shape, dt_in, 1 };
132
133 // Fill reference
134 fill(src, 0, dt_in, dt_out);
135
136 return reference::depth_convert<T1, T2>(src, dt_out, policy, 0);
137 }
138
139 TensorType _target{};
140 SimpleTensor<T2> _reference{};
141};
142} // namespace validation
143} // namespace test
144} // namespace arm_compute
145#endif /* ARM_COMPUTE_TEST_CAST_FIXTURE */