blob: e9d624e6f344e4d7281875ffa246c06154b31fe0 [file] [log] [blame]
Georgios Pinitas303f0db2018-11-19 11:56:51 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 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:
Georgios Pinitas303f0db2018-11-19 11:56:51 +000039 void setup(TensorShape shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
40 {
41 _target = compute_target(shape, dt_in, dt_out, policy);
42 _reference = compute_reference(shape, dt_in, dt_out, policy);
43 }
44
45protected:
46 template <typename U>
47 void fill(U &&tensor, int i, DataType dt_in, DataType dt_out)
48 {
49 // Restricting range to avoid inf values
50 if(dt_out == DataType::F16)
51 {
52 const int signed_min = -32000;
53 const int signed_max = 32000;
54 const int unsigned_min = 0;
55 const int unsigned_max = 65000;
56
57 switch(dt_in)
58 {
59 case DataType::U8:
60 case DataType::QASYMM8:
SiCong Li2d10f182020-06-08 16:20:15 +010061 case DataType::QASYMM8_SIGNED:
Georgios Pinitas303f0db2018-11-19 11:56:51 +000062 case DataType::S8:
63 case DataType::F32:
64 {
65 library->fill_tensor_uniform(tensor, i);
66 break;
67 }
68 case DataType::U16:
69 {
70 library->fill_tensor_uniform(tensor, i, static_cast<uint16_t>(unsigned_min), static_cast<uint16_t>(unsigned_max));
71 break;
72 }
73 case DataType::S16:
74 {
75 library->fill_tensor_uniform(tensor, i, static_cast<int16_t>(signed_min), static_cast<int16_t>(signed_max));
76 break;
77 }
78 case DataType::U32:
79 {
80 library->fill_tensor_uniform(tensor, i, static_cast<uint32_t>(unsigned_min), static_cast<uint32_t>(unsigned_max));
81 break;
82 }
83 case DataType::S32:
84 {
85 library->fill_tensor_uniform(tensor, i, static_cast<int32_t>(signed_min), static_cast<int32_t>(signed_max));
86 break;
87 }
Pablo Marquez Tello205ba242023-07-12 14:29:58 +010088 case DataType::U64:
89 {
90 library->fill_tensor_uniform(tensor, i, static_cast<uint64_t>(unsigned_min), static_cast<uint64_t>(unsigned_max));
91 break;
92 }
93 case DataType::S64:
94 {
95 library->fill_tensor_uniform(tensor, i, static_cast<int64_t>(signed_min), static_cast<int64_t>(signed_max));
96 break;
97 }
Georgios Pinitas303f0db2018-11-19 11:56:51 +000098 default:
99 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
100 }
101 }
102 else
103 {
104 library->fill_tensor_uniform(tensor, i);
105 }
106 }
107
108 TensorType compute_target(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
109 {
110 // Create tensors
111 TensorType src = create_tensor<TensorType>(shape, dt_in, 1);
112 TensorType dst = create_tensor<TensorType>(shape, dt_out, 1);
113
114 // Create and configure function
115 FunctionType cast;
116 cast.configure(&src, &dst, policy);
117
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100118 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
119 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitas303f0db2018-11-19 11:56:51 +0000120
121 // Allocate tensors
122 src.allocator()->allocate();
123 dst.allocator()->allocate();
124
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100125 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
126 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitas303f0db2018-11-19 11:56:51 +0000127
128 // Fill tensors
129 fill(AccessorType(src), 0, dt_in, dt_out);
130
131 // Compute function
132 cast.run();
133
134 return dst;
135 }
136
137 SimpleTensor<T2> compute_reference(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
138 {
139 // Create reference
140 SimpleTensor<T1> src{ shape, dt_in, 1 };
141
142 // Fill reference
143 fill(src, 0, dt_in, dt_out);
144
145 return reference::depth_convert<T1, T2>(src, dt_out, policy, 0);
146 }
147
148 TensorType _target{};
149 SimpleTensor<T2> _reference{};
150};
151} // namespace validation
152} // namespace test
153} // namespace arm_compute
154#endif /* ARM_COMPUTE_TEST_CAST_FIXTURE */