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