Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +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 | |
| 25 | #include "Globals.h" |
| 26 | #include "NEON/Helper.h" |
| 27 | #include "NEON/NEAccessor.h" |
| 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 | #include "validation/ValidationUserConfiguration.h" |
| 35 | |
| 36 | #include "arm_compute/core/Helpers.h" |
| 37 | #include "arm_compute/core/Types.h" |
| 38 | #include "arm_compute/runtime/NEON/functions/NESobel3x3.h" |
| 39 | #include "arm_compute/runtime/Tensor.h" |
| 40 | #include "arm_compute/runtime/TensorAllocator.h" |
| 41 | |
| 42 | #include "PaddingCalculator.h" |
| 43 | #include "boost_wrapper.h" |
| 44 | |
| 45 | #include <random> |
| 46 | #include <string> |
| 47 | |
| 48 | using namespace arm_compute; |
| 49 | using namespace arm_compute::test; |
| 50 | using namespace arm_compute::test::neon; |
| 51 | using namespace arm_compute::test::validation; |
| 52 | |
| 53 | namespace |
| 54 | { |
| 55 | /** Compute Neon Sobel 3x3 function. |
| 56 | * |
| 57 | * @param[in] shape Shape of the input and output tensors. |
| 58 | * @param[in] border_mode BorderMode used by the input tensor |
| 59 | * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT |
| 60 | * |
| 61 | * @return Computed output tensor. |
| 62 | */ |
| 63 | std::pair<Tensor, Tensor> compute_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value) |
| 64 | { |
| 65 | // Create tensors |
| 66 | Tensor src = create_tensor(shape, DataType::U8); |
| 67 | Tensor dst_x = create_tensor(shape, DataType::S16); |
| 68 | Tensor dst_y = create_tensor(shape, DataType::S16); |
| 69 | |
| 70 | src.info()->set_format(Format::U8); |
| 71 | dst_x.info()->set_format(Format::S16); |
| 72 | dst_y.info()->set_format(Format::S16); |
| 73 | |
| 74 | // Create sobel image configure function |
| 75 | NESobel3x3 sobel_3x3; |
| 76 | sobel_3x3.configure(&src, &dst_x, &dst_y, border_mode, constant_border_value); |
| 77 | |
| 78 | // Allocate tensors |
| 79 | src.allocator()->allocate(); |
| 80 | dst_x.allocator()->allocate(); |
| 81 | dst_y.allocator()->allocate(); |
| 82 | |
| 83 | BOOST_TEST(!src.info()->is_resizable()); |
| 84 | BOOST_TEST(!dst_x.info()->is_resizable()); |
| 85 | BOOST_TEST(!dst_y.info()->is_resizable()); |
| 86 | |
| 87 | // Fill tensors |
| 88 | library->fill_tensor_uniform(NEAccessor(src), 0); |
| 89 | |
| 90 | // Compute function |
| 91 | sobel_3x3.run(); |
| 92 | |
| 93 | return std::make_pair(std::move(dst_x), std::move(dst_y)); |
| 94 | } |
| 95 | } // namespace |
| 96 | |
| 97 | #ifndef DOXYGEN_SKIP_THIS |
| 98 | BOOST_AUTO_TEST_SUITE(NEON) |
| 99 | BOOST_AUTO_TEST_SUITE(Sobel3x3) |
| 100 | |
| 101 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 102 | BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * BorderModes(), shape, border_mode) |
| 103 | { |
| 104 | // Create tensors |
| 105 | Tensor src = create_tensor(shape, DataType::U8); |
| 106 | Tensor dst_x = create_tensor(shape, DataType::S16); |
| 107 | Tensor dst_y = create_tensor(shape, DataType::S16); |
| 108 | |
| 109 | src.info()->set_format(Format::U8); |
| 110 | dst_x.info()->set_format(Format::S16); |
| 111 | dst_y.info()->set_format(Format::S16); |
| 112 | |
| 113 | BOOST_TEST(src.info()->is_resizable()); |
| 114 | BOOST_TEST(dst_x.info()->is_resizable()); |
| 115 | BOOST_TEST(dst_y.info()->is_resizable()); |
| 116 | |
| 117 | // Create sobel 3x3 configure function |
| 118 | NESobel3x3 sobel_3x3; |
| 119 | sobel_3x3.configure(&src, &dst_x, &dst_y, border_mode); |
| 120 | |
| 121 | // Validate valid region |
| 122 | const ValidRegion src_valid_region = shape_to_valid_region(shape); |
| 123 | ValidRegion dst_valid_region = shape_to_valid_region(shape); |
| 124 | if(border_mode == BorderMode::UNDEFINED) |
| 125 | { |
| 126 | dst_valid_region = shape_to_valid_region_undefined_border(shape, BorderSize(1)); |
| 127 | } |
| 128 | |
| 129 | validate(src.info()->valid_region(), src_valid_region); |
| 130 | validate(dst_x.info()->valid_region(), dst_valid_region); |
| 131 | validate(dst_y.info()->valid_region(), dst_valid_region); |
| 132 | |
| 133 | // Validate padding |
| 134 | PaddingCalculator calculator(shape.x(), 8); |
| 135 | |
| 136 | calculator.set_border_mode(border_mode); |
| 137 | calculator.set_border_size(1); |
| 138 | |
| 139 | const PaddingSize dst_padding = calculator.required_padding(); |
| 140 | |
| 141 | calculator.set_accessed_elements(16); |
| 142 | calculator.set_access_offset(-1); |
| 143 | |
| 144 | const PaddingSize src_padding = calculator.required_padding(); |
| 145 | |
| 146 | validate(src.info()->padding(), src_padding); |
| 147 | validate(dst_x.info()->padding(), dst_padding); |
| 148 | validate(dst_y.info()->padding(), dst_padding); |
| 149 | } |
| 150 | |
| 151 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 152 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * BorderModes(), shape, border_mode) |
| 153 | { |
| 154 | uint8_t constant_border_value = 0; |
| 155 | |
| 156 | // Generate a random constant value if border_mode is constant |
| 157 | if(border_mode == BorderMode::CONSTANT) |
| 158 | { |
| 159 | std::mt19937 gen(user_config.seed.get()); |
| 160 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 161 | constant_border_value = distribution(gen); |
| 162 | } |
| 163 | |
| 164 | // Compute function |
| 165 | std::pair<Tensor, Tensor> dst = compute_sobel_3x3(shape, border_mode, constant_border_value); |
| 166 | |
| 167 | // Compute reference |
| 168 | std::pair<RawTensor, RawTensor> ref_dst = Reference::compute_reference_sobel_3x3(shape, border_mode, constant_border_value); |
| 169 | |
| 170 | // Calculate valid region |
| 171 | ValidRegion valid_region = shape_to_valid_region(shape); |
| 172 | if(border_mode == BorderMode::UNDEFINED) |
| 173 | { |
| 174 | valid_region = shape_to_valid_region_undefined_border(shape, BorderSize(1)); |
| 175 | } |
| 176 | |
| 177 | // Validate output |
| 178 | validate(NEAccessor(dst.first), ref_dst.first, valid_region); |
| 179 | validate(NEAccessor(dst.second), ref_dst.second, valid_region); |
| 180 | } |
| 181 | |
| 182 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 183 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * BorderModes(), shape, border_mode) |
| 184 | { |
| 185 | uint8_t constant_border_value = 0; |
| 186 | |
| 187 | // Generate a random constant value if border_mode is constant |
| 188 | if(border_mode == BorderMode::CONSTANT) |
| 189 | { |
| 190 | std::mt19937 gen(user_config.seed.get()); |
| 191 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 192 | constant_border_value = distribution(gen); |
| 193 | } |
| 194 | |
| 195 | // Compute function |
| 196 | std::pair<Tensor, Tensor> dst = compute_sobel_3x3(shape, border_mode, constant_border_value); |
| 197 | |
| 198 | // Compute reference |
| 199 | std::pair<RawTensor, RawTensor> ref_dst = Reference::compute_reference_sobel_3x3(shape, border_mode, constant_border_value); |
| 200 | |
| 201 | // Calculate valid region |
| 202 | ValidRegion valid_region = shape_to_valid_region(shape); |
| 203 | if(border_mode == BorderMode::UNDEFINED) |
| 204 | { |
| 205 | valid_region = shape_to_valid_region_undefined_border(shape, BorderSize(1)); |
| 206 | } |
| 207 | |
| 208 | // Validate output |
| 209 | validate(NEAccessor(dst.first), ref_dst.first, valid_region); |
| 210 | validate(NEAccessor(dst.second), ref_dst.second, valid_region); |
| 211 | } |
| 212 | |
| 213 | BOOST_AUTO_TEST_SUITE_END() |
| 214 | BOOST_AUTO_TEST_SUITE_END() |
| 215 | #endif |