blob: 0ae495a4e3ea0fb72777aedf1bbdc1f1398c9065 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Barbier6ff3b192017-09-04 18:44:23 +010025#include "NEON/NEAccessor.h"
Moritz Pflanzer5b512292017-06-21 15:54:07 +010026#include "PaddingCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#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
46using namespace arm_compute;
47using namespace arm_compute::test;
48using namespace arm_compute::test::neon;
49using namespace arm_compute::test::validation;
50
51namespace
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 */
64Tensor 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 Pflanzer94450f12017-06-30 12:48:43 +010067 Tensor src1 = create_tensor<Tensor>(shape, DataType::U8);
68 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
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
92BOOST_AUTO_TEST_SUITE(NEON)
93BOOST_AUTO_TEST_SUITE(Threshold)
94
95BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
96BOOST_DATA_TEST_CASE(Configuration,
97 (SmallShapes() + LargeShapes()) * ThresholdDataset(),
98 shape, thrshConf)
99{
100 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100101 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
102 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100104 BOOST_TEST(src.info()->is_resizable());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 BOOST_TEST(dst.info()->is_resizable());
106
107 // Create and configure function
108 NEThreshold thrsh;
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100109 thrsh.configure(&src, &dst, thrshConf.threshold, thrshConf.false_value, thrshConf.true_value, thrshConf.type, thrshConf.upper);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
111 // Validate valid region
112 const ValidRegion valid_region = shape_to_valid_region(shape);
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100113 validate(src.info()->valid_region(), valid_region);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 validate(dst.info()->valid_region(), valid_region);
115
116 // Validate padding
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100117 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
118 validate(src.info()->padding(), padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 validate(dst.info()->padding(), padding);
120}
121
122BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
123BOOST_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
137BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
138BOOST_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
152BOOST_AUTO_TEST_SUITE_END()
153BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100154#endif /* DOXYGEN_SKIP_THIS */