Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
| 24 | #include "Globals.h" |
| 25 | #include "NEON/Helper.h" |
| 26 | #include "NEON/NEAccessor.h" |
Moritz Pflanzer | 5b51229 | 2017-06-21 15:54:07 +0100 | [diff] [blame] | 27 | #include "PaddingCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "TensorLibrary.h" |
| 29 | #include "TypePrinter.h" |
| 30 | #include "Utils.h" |
| 31 | #include "validation/Datasets.h" |
| 32 | #include "validation/Reference.h" |
| 33 | #include "validation/Validation.h" |
| 34 | |
| 35 | #include "arm_compute/core/Helpers.h" |
| 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "arm_compute/runtime/NEON/functions/NEBitwiseAnd.h" |
| 38 | #include "arm_compute/runtime/SubTensor.h" |
| 39 | #include "arm_compute/runtime/Tensor.h" |
| 40 | #include "arm_compute/runtime/TensorAllocator.h" |
| 41 | |
| 42 | #include "boost_wrapper.h" |
| 43 | |
| 44 | #include <random> |
| 45 | #include <string> |
| 46 | |
| 47 | using namespace arm_compute; |
| 48 | using namespace arm_compute::test; |
| 49 | using namespace arm_compute::test::neon; |
| 50 | using namespace arm_compute::test::validation; |
| 51 | |
| 52 | namespace |
| 53 | { |
| 54 | /** Compute Neon bitwise and function. |
| 55 | * |
| 56 | * @param[in] shape Shape of the input and output tensors. |
| 57 | * |
| 58 | * @return Computed output tensor. |
| 59 | */ |
| 60 | Tensor compute_bitwise_and(const TensorShape &shape) |
| 61 | { |
| 62 | // Create tensors |
| 63 | Tensor src1 = create_tensor(shape, DataType::U8); |
| 64 | Tensor src2 = create_tensor(shape, DataType::U8); |
| 65 | Tensor dst = create_tensor(shape, DataType::U8); |
| 66 | |
| 67 | // Create and configure function |
| 68 | NEBitwiseAnd band; |
| 69 | band.configure(&src1, &src2, &dst); |
| 70 | |
| 71 | // Allocate tensors |
| 72 | src1.allocator()->allocate(); |
| 73 | src2.allocator()->allocate(); |
| 74 | dst.allocator()->allocate(); |
| 75 | |
| 76 | BOOST_TEST(!src1.info()->is_resizable()); |
| 77 | BOOST_TEST(!src2.info()->is_resizable()); |
| 78 | BOOST_TEST(!dst.info()->is_resizable()); |
| 79 | |
| 80 | // Fill tensors |
| 81 | library->fill_tensor_uniform(NEAccessor(src1), 0); |
| 82 | library->fill_tensor_uniform(NEAccessor(src2), 1); |
| 83 | |
| 84 | // Compute function |
| 85 | band.run(); |
| 86 | |
| 87 | return dst; |
| 88 | } |
| 89 | |
| 90 | /** Compute Neon bitwise and function that splits the input and output in two subtensor. |
| 91 | * |
| 92 | * @param[in] shape Shape of the input and output tensors. |
| 93 | * |
| 94 | * @return Computed output tensor. |
| 95 | */ |
| 96 | Tensor compute_bitwise_and_subtensor(const TensorShape &shape) |
| 97 | { |
| 98 | // Create tensors |
| 99 | Tensor src1 = create_tensor(shape, DataType::U8); |
| 100 | Tensor src2 = create_tensor(shape, DataType::U8); |
| 101 | Tensor dst = create_tensor(shape, DataType::U8); |
| 102 | |
| 103 | // Create SubTensors |
| 104 | int coord_z = shape.z() / 2; |
| 105 | TensorShape sub_shape = shape; |
| 106 | sub_shape.set(2, coord_z); |
| 107 | |
| 108 | SubTensor src1_sub1(&src1, sub_shape, Coordinates()); |
| 109 | SubTensor src1_sub2(&src1, sub_shape, Coordinates(0, 0, coord_z)); |
| 110 | SubTensor src2_sub1(&src2, sub_shape, Coordinates()); |
| 111 | SubTensor src2_sub2(&src2, sub_shape, Coordinates(0, 0, coord_z)); |
| 112 | SubTensor dst_sub1(&dst, sub_shape, Coordinates()); |
| 113 | SubTensor dst_sub2(&dst, sub_shape, Coordinates(0, 0, coord_z)); |
| 114 | |
| 115 | // Create and configure function |
| 116 | NEBitwiseAnd band1, band2; |
| 117 | band1.configure(&src1_sub1, &src2_sub1, &dst_sub1); |
| 118 | band2.configure(&src1_sub2, &src2_sub2, &dst_sub2); |
| 119 | |
| 120 | // Allocate tensors |
| 121 | src1.allocator()->allocate(); |
| 122 | src2.allocator()->allocate(); |
| 123 | dst.allocator()->allocate(); |
| 124 | |
| 125 | BOOST_TEST(!src1.info()->is_resizable()); |
| 126 | BOOST_TEST(!src2.info()->is_resizable()); |
| 127 | BOOST_TEST(!dst.info()->is_resizable()); |
| 128 | |
| 129 | // Fill tensors |
| 130 | std::uniform_int_distribution<> distribution(0, 255); |
| 131 | library->fill(NEAccessor(src1), distribution, 0); |
| 132 | library->fill(NEAccessor(src2), distribution, 1); |
| 133 | |
| 134 | // Compute function |
| 135 | band1.run(); |
| 136 | band2.run(); |
| 137 | |
| 138 | return dst; |
| 139 | } |
| 140 | } // namespace |
| 141 | |
| 142 | #ifndef DOXYGEN_SKIP_THIS |
| 143 | BOOST_AUTO_TEST_SUITE(NEON) |
| 144 | BOOST_AUTO_TEST_SUITE(BitwiseAnd) |
| 145 | |
| 146 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 147 | BOOST_DATA_TEST_CASE(Configuration, SmallShapes() + LargeShapes(), shape) |
| 148 | { |
| 149 | // Create tensors |
| 150 | Tensor src1 = create_tensor(shape, DataType::U8); |
| 151 | Tensor src2 = create_tensor(shape, DataType::U8); |
| 152 | Tensor dst = create_tensor(shape, DataType::U8); |
| 153 | |
| 154 | BOOST_TEST(src1.info()->is_resizable()); |
| 155 | BOOST_TEST(src2.info()->is_resizable()); |
| 156 | BOOST_TEST(dst.info()->is_resizable()); |
| 157 | |
| 158 | // Create and configure function |
| 159 | NEBitwiseAnd band; |
| 160 | band.configure(&src1, &src2, &dst); |
| 161 | |
| 162 | // Validate valid region |
| 163 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 164 | validate(src1.info()->valid_region(), valid_region); |
| 165 | validate(src2.info()->valid_region(), valid_region); |
| 166 | validate(dst.info()->valid_region(), valid_region); |
| 167 | |
| 168 | // Validate padding |
Moritz Pflanzer | 2509fba | 2017-06-23 14:15:03 +0100 | [diff] [blame] | 169 | const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | validate(src1.info()->padding(), padding); |
| 171 | validate(src2.info()->padding(), padding); |
| 172 | validate(dst.info()->padding(), padding); |
| 173 | } |
| 174 | |
| 175 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 176 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes(), shape) |
| 177 | { |
| 178 | // Compute function |
| 179 | Tensor dst = compute_bitwise_and(shape); |
| 180 | |
| 181 | // Compute reference |
| 182 | RawTensor ref_dst = Reference::compute_reference_bitwise_and(shape); |
| 183 | |
| 184 | // Validate output |
| 185 | validate(NEAccessor(dst), ref_dst); |
| 186 | } |
| 187 | |
| 188 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 189 | BOOST_AUTO_TEST_CASE(RunSubTensor) |
| 190 | { |
| 191 | // Create shape |
| 192 | TensorShape shape(27U, 35U, 8U, 2U); |
| 193 | |
| 194 | // Compute function |
| 195 | Tensor dst = compute_bitwise_and_subtensor(shape); |
| 196 | |
| 197 | // Compute reference |
| 198 | RawTensor ref_dst = Reference::compute_reference_bitwise_and(shape); |
| 199 | |
| 200 | // Validate output |
| 201 | validate(NEAccessor(dst), ref_dst); |
| 202 | } |
| 203 | |
| 204 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 205 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes(), shape) |
| 206 | { |
| 207 | // Compute function |
| 208 | Tensor dst = compute_bitwise_and(shape); |
| 209 | |
| 210 | // Compute reference |
| 211 | RawTensor ref_dst = Reference::compute_reference_bitwise_and(shape); |
| 212 | |
| 213 | // Validate output |
| 214 | validate(NEAccessor(dst), ref_dst); |
| 215 | } |
| 216 | |
| 217 | BOOST_AUTO_TEST_SUITE_END() |
| 218 | BOOST_AUTO_TEST_SUITE_END() |
| 219 | #endif |