blob: 60b4c2b314983b24a681ba46afa7212e035c69c9 [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 }
88 default:
89 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
90 }
91 }
92 else
93 {
94 library->fill_tensor_uniform(tensor, i);
95 }
96 }
97
98 TensorType compute_target(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
99 {
100 // Create tensors
101 TensorType src = create_tensor<TensorType>(shape, dt_in, 1);
102 TensorType dst = create_tensor<TensorType>(shape, dt_out, 1);
103
104 // Create and configure function
105 FunctionType cast;
106 cast.configure(&src, &dst, policy);
107
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100108 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
109 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Georgios Pinitas303f0db2018-11-19 11:56:51 +0000110
111 // Allocate tensors
112 src.allocator()->allocate();
113 dst.allocator()->allocate();
114
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100115 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
116 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Georgios Pinitas303f0db2018-11-19 11:56:51 +0000117
118 // Fill tensors
119 fill(AccessorType(src), 0, dt_in, dt_out);
120
121 // Compute function
122 cast.run();
123
124 return dst;
125 }
126
127 SimpleTensor<T2> compute_reference(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy)
128 {
129 // Create reference
130 SimpleTensor<T1> src{ shape, dt_in, 1 };
131
132 // Fill reference
133 fill(src, 0, dt_in, dt_out);
134
135 return reference::depth_convert<T1, T2>(src, dt_out, policy, 0);
136 }
137
138 TensorType _target{};
139 SimpleTensor<T2> _reference{};
140};
141} // namespace validation
142} // namespace test
143} // namespace arm_compute
144#endif /* ARM_COMPUTE_TEST_CAST_FIXTURE */