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 "dataset/ThresholdDataset.h" |
| 32 | #include "validation/Datasets.h" |
| 33 | #include "validation/Reference.h" |
| 34 | #include "validation/Validation.h" |
| 35 | |
| 36 | #include "arm_compute/core/Helpers.h" |
| 37 | #include "arm_compute/core/Types.h" |
| 38 | #include "arm_compute/runtime/NEON/functions/NEThreshold.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 Threshold function. |
| 55 | * |
| 56 | * @param[in] shape Shape of the input and output tensors. |
| 57 | * @param[in] threshold Threshold. When the threshold type is RANGE, this is used as the lower threshold. |
| 58 | * @param[in] false_value value to set when the condition is not respected. |
| 59 | * @param[in] true_value value to set when the condition is respected. |
| 60 | * @param[in] type Thresholding type. Either RANGE or BINARY. |
| 61 | * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE. |
| 62 | * |
| 63 | * @return Computed output tensor. |
| 64 | */ |
| 65 | Tensor compute_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper) |
| 66 | { |
| 67 | // Create tensors |
| 68 | Tensor src1 = create_tensor(shape, DataType::U8); |
| 69 | Tensor dst = create_tensor(shape, DataType::U8); |
| 70 | |
| 71 | // Create and configure function |
| 72 | NEThreshold thrsh; |
| 73 | thrsh.configure(&src1, &dst, threshold, false_value, true_value, type, upper); |
| 74 | |
| 75 | // Allocate tensors |
| 76 | src1.allocator()->allocate(); |
| 77 | dst.allocator()->allocate(); |
| 78 | |
| 79 | BOOST_TEST(!src1.info()->is_resizable()); |
| 80 | BOOST_TEST(!dst.info()->is_resizable()); |
| 81 | |
| 82 | // Fill tensors |
| 83 | library->fill_tensor_uniform(NEAccessor(src1), 0); |
| 84 | |
| 85 | // Compute function |
| 86 | thrsh.run(); |
| 87 | |
| 88 | return dst; |
| 89 | } |
| 90 | } // namespace |
| 91 | |
| 92 | #ifndef DOXYGEN_SKIP_THIS |
| 93 | BOOST_AUTO_TEST_SUITE(NEON) |
| 94 | BOOST_AUTO_TEST_SUITE(Threshold) |
| 95 | |
| 96 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 97 | BOOST_DATA_TEST_CASE(Configuration, |
| 98 | (SmallShapes() + LargeShapes()) * ThresholdDataset(), |
| 99 | shape, thrshConf) |
| 100 | { |
| 101 | // Create tensors |
| 102 | Tensor src1 = create_tensor(shape, DataType::U8); |
| 103 | Tensor dst = create_tensor(shape, DataType::U8); |
| 104 | |
| 105 | BOOST_TEST(src1.info()->is_resizable()); |
| 106 | BOOST_TEST(dst.info()->is_resizable()); |
| 107 | |
| 108 | // Create and configure function |
| 109 | NEThreshold thrsh; |
| 110 | thrsh.configure(&src1, &dst, thrshConf.threshold, thrshConf.false_value, thrshConf.true_value, thrshConf.type, thrshConf.upper); |
| 111 | |
| 112 | // Validate valid region |
| 113 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 114 | validate(src1.info()->valid_region(), valid_region); |
| 115 | validate(dst.info()->valid_region(), valid_region); |
| 116 | |
| 117 | // Validate padding |
Moritz Pflanzer | 5b51229 | 2017-06-21 15:54:07 +0100 | [diff] [blame] | 118 | const PaddingSize padding(0, PaddingCalculator(shape.x(), 16).required_padding(), 0, 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | validate(src1.info()->padding(), padding); |
| 120 | validate(dst.info()->padding(), padding); |
| 121 | } |
| 122 | |
| 123 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 124 | BOOST_DATA_TEST_CASE(RunSmall, |
| 125 | SmallShapes() * ThresholdDataset(), |
| 126 | shape, thrshConf) |
| 127 | { |
| 128 | // Compute function |
| 129 | Tensor dst = compute_threshold(shape, thrshConf.threshold, thrshConf.false_value, thrshConf.true_value, thrshConf.type, thrshConf.upper); |
| 130 | |
| 131 | // Compute reference |
| 132 | RawTensor ref_dst = Reference::compute_reference_threshold(shape, thrshConf.threshold, thrshConf.false_value, thrshConf.true_value, thrshConf.type, thrshConf.upper); |
| 133 | |
| 134 | // Validate output |
| 135 | validate(NEAccessor(dst), ref_dst); |
| 136 | } |
| 137 | |
| 138 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 139 | BOOST_DATA_TEST_CASE(RunLarge, |
| 140 | LargeShapes() * ThresholdDataset(), |
| 141 | shape, thrshConf) |
| 142 | { |
| 143 | // Compute function |
| 144 | Tensor dst = compute_threshold(shape, thrshConf.threshold, thrshConf.false_value, thrshConf.true_value, thrshConf.type, thrshConf.upper); |
| 145 | |
| 146 | // Compute reference |
| 147 | RawTensor ref_dst = Reference::compute_reference_threshold(shape, thrshConf.threshold, thrshConf.false_value, thrshConf.true_value, thrshConf.type, thrshConf.upper); |
| 148 | |
| 149 | // Validate output |
| 150 | validate(NEAccessor(dst), ref_dst); |
| 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_SUITE_END() |
| 154 | BOOST_AUTO_TEST_SUITE_END() |
| 155 | #endif |