blob: 3a6efa22afc1e524d4e3e6ac67a1dbb32101a25f [file] [log] [blame]
Georgios Pinitas303f0db2018-11-19 11:56:51 +00001/*
2 * Copyright (c) 2018 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#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:
62 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
108 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
109 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
110
111 // Allocate tensors
112 src.allocator()->allocate();
113 dst.allocator()->allocate();
114
115 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
116 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
117
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 */