blob: 7e7ddf46055fef2967a8f58564e8bf659315787f [file] [log] [blame]
Isabella Gottardib797fa22017-06-23 15:02:11 +01001/*
2 * Copyright (c) 2017 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 */
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010024#include "AssetsLibrary.h"
Isabella Gottardib797fa22017-06-23 15:02:11 +010025#include "CL/CLAccessor.h"
26#include "CL/CLLutAccessor.h"
27#include "Globals.h"
28#include "PaddingCalculator.h"
29#include "RawLutAccessor.h"
Isabella Gottardib797fa22017-06-23 15:02:11 +010030#include "TypePrinter.h"
31#include "Utils.h"
32#include "validation/Datasets.h"
33#include "validation/Helpers.h"
34#include "validation/Reference.h"
35#include "validation/Validation.h"
36
37#include "arm_compute/core/Helpers.h"
38#include "arm_compute/core/Types.h"
39#include "arm_compute/runtime/CL/CLTensor.h"
40#include "arm_compute/runtime/CL/CLTensorAllocator.h"
41#include "arm_compute/runtime/CL/functions/CLTableLookup.h"
42
43#include "boost_wrapper.h"
44
45#include <map>
46#include <random>
47#include <string>
48
49using namespace arm_compute;
50using namespace arm_compute::test;
51using namespace arm_compute::test::cl;
52using namespace arm_compute::test::validation;
53
54namespace
55{
56/** Compute Table Lookup function.
57 *
58 * @param[in] shape Shape of the input tensors
59 * @param[in] data_type Type of the input/output tensor
60 * @param[in] lut The input LUT.
61 *
62 * @return Computed output cl tensor.
63 */
64CLTensor compute_table_lookup(const TensorShape &shape, DataType data_type, CLLut &lut)
65{
66 // Create tensors
67 CLTensor src = create_tensor<CLTensor>(shape, data_type);
68 CLTensor dst = create_tensor<CLTensor>(shape, data_type);
69
70 // Create and configure function
71 CLTableLookup table_lookup;
72 table_lookup.configure(&src, &lut, &dst);
73
74 // Allocate tensors
75 src.allocator()->allocate();
76 dst.allocator()->allocate();
77
78 BOOST_TEST(!src.info()->is_resizable());
79 BOOST_TEST(!dst.info()->is_resizable());
80
81 // Fill tensors
82 library->fill_tensor_uniform(CLAccessor(src), 0);
83
84 // Compute function
85 table_lookup.run();
86
87 return dst;
88}
89} // namespace
90
91#ifndef DOXYGEN_SKIP_THIS
92BOOST_AUTO_TEST_SUITE(CL)
93BOOST_AUTO_TEST_SUITE(TableLookup)
94
95BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
96BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * boost::unit_test::data::make({ DataType::U8, DataType::S16 }),
97 shape, data_type)
98{
99 //Create Lut
100 const int num_elem = (data_type == DataType::U8) ? std::numeric_limits<uint8_t>::max() + 1 : std::numeric_limits<int16_t>::max() - std::numeric_limits<int16_t>::lowest() + 1;
101 CLLut cllut(num_elem, data_type);
102
103 if(data_type == DataType::U8)
104 {
105 fill_lookuptable(CLLutAccessor<uint8_t>(cllut));
106 }
107 else
108 {
109 fill_lookuptable(CLLutAccessor<int16_t>(cllut));
110 }
111
112 // Create tensors
113 CLTensor src = create_tensor<CLTensor>(shape, data_type);
114 CLTensor dst = create_tensor<CLTensor>(shape, data_type);
115
116 BOOST_TEST(src.info()->is_resizable());
117 BOOST_TEST(dst.info()->is_resizable());
118
119 // Create and configure function
120 CLTableLookup table_lookup;
121 table_lookup.configure(&src, &cllut, &dst);
122
123 // Validate valid region
124 const ValidRegion valid_region = shape_to_valid_region(shape);
125 validate(src.info()->valid_region(), valid_region);
126 validate(dst.info()->valid_region(), valid_region);
127
128 // Validate padding
129 const PaddingSize padding = PaddingCalculator(shape.x(), 8).required_padding();
130 validate(src.info()->padding(), padding);
131 validate(dst.info()->padding(), padding);
132}
133
134BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
135BOOST_DATA_TEST_CASE(RunSmall,
136 SmallShapes() * boost::unit_test::data::make({ DataType::U8, DataType::S16 }),
137 shape, data_type)
138{
139 //Create Lut
140 const int num_elem = (data_type == DataType::U8) ? std::numeric_limits<uint8_t>::max() + 1 : std::numeric_limits<int16_t>::max() - std::numeric_limits<int16_t>::lowest() + 1;
141 CLLut cllut(num_elem, data_type);
142
143 if(data_type == DataType::U8)
144 {
145 //Create rawLut
146 std::map<uint8_t, uint8_t> rawlut;
147
148 //Fill the Lut
149 fill_lookuptable(CLLutAccessor<uint8_t>(cllut));
150 fill_lookuptable(RawLutAccessor<uint8_t>(rawlut));
151
152 // Compute function
153 CLTensor dst = compute_table_lookup(shape, data_type, cllut);
154
155 // Compute reference
156 RawTensor ref_dst = Reference::compute_reference_table_lookup(shape, data_type, rawlut);
157
158 // Validate output
159 validate(CLAccessor(dst), ref_dst);
160 }
161 else
162 {
163 //Create rawLut
164 std::map<int16_t, int16_t> rawlut;
165
166 //Fill the Lut
167 fill_lookuptable(CLLutAccessor<int16_t>(cllut));
168 fill_lookuptable(RawLutAccessor<int16_t>(rawlut));
169
170 // Compute function
171 CLTensor dst = compute_table_lookup(shape, data_type, cllut);
172
173 // Compute reference
174 RawTensor ref_dst = Reference::compute_reference_table_lookup(shape, data_type, rawlut);
175
176 // Validate output
177 validate(CLAccessor(dst), ref_dst);
178 }
179}
180
181BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
182BOOST_DATA_TEST_CASE(RunLarge,
183 LargeShapes() * boost::unit_test::data::make({ DataType::U8, DataType::S16 }),
184 shape, data_type)
185{
186 //Create Lut
187 const int num_elem = (data_type == DataType::U8) ? std::numeric_limits<uint8_t>::max() + 1 : std::numeric_limits<int16_t>::max() - std::numeric_limits<int16_t>::lowest() + 1;
188 CLLut cllut(num_elem, data_type);
189
190 if(data_type == DataType::U8)
191 {
192 //Create rawLut
193 std::map<uint8_t, uint8_t> rawlut;
194
195 //Fill the Lut
196 fill_lookuptable(CLLutAccessor<uint8_t>(cllut));
197 fill_lookuptable(RawLutAccessor<uint8_t>(rawlut));
198
199 // Compute function
200 CLTensor dst = compute_table_lookup(shape, data_type, cllut);
201
202 // Compute reference
203 RawTensor ref_dst = Reference::compute_reference_table_lookup(shape, data_type, rawlut);
204
205 // Validate output
206 validate(CLAccessor(dst), ref_dst);
207 }
208 else
209 {
210 //Create rawLut
211 std::map<int16_t, int16_t> rawlut;
212
213 //Fill the Lut
214 fill_lookuptable(CLLutAccessor<int16_t>(cllut));
215 fill_lookuptable(RawLutAccessor<int16_t>(rawlut));
216
217 // Compute function
218 CLTensor dst = compute_table_lookup(shape, data_type, cllut);
219
220 // Compute reference
221 RawTensor ref_dst = Reference::compute_reference_table_lookup(shape, data_type, rawlut);
222
223 // Validate output
224 validate(CLAccessor(dst), ref_dst);
225 }
226}
227
228BOOST_AUTO_TEST_SUITE_END()
229BOOST_AUTO_TEST_SUITE_END()
230#endif /* DOXYGEN_SKIP_THIS */