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