blob: 7349bdfae2b64c540d26dc507e9dc8abbb9836c2 [file] [log] [blame]
Isabella Gottardi02aabcc2017-10-12 17:28:51 +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#ifndef ARM_COMPUTE_TEST_HISTOGRAM_FIXTURE
25#define ARM_COMPUTE_TEST_HISTOGRAM_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000034#include "tests/validation/reference/Histogram.h"
Isabella Gottardi02aabcc2017-10-12 17:28:51 +010035#include "utils/Utils.h"
36
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename DistributionType>
46class HistogramValidationFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape, DataType data_type)
51 {
52 std::mt19937 gen(library->seed());
53 std::uniform_int_distribution<size_t> distribution_size_t(1, 30);
54 const size_t num_bins = distribution_size_t(gen);
55 std::uniform_int_distribution<int32_t> distribution_int32_t(0, 125);
56 const size_t offset = distribution_int32_t(gen);
57 std::uniform_int_distribution<uint32_t> distribution_uint32_t(1, 255 - offset);
58 const size_t range = distribution_uint32_t(gen);
59
60 _target = compute_target(shape, data_type, num_bins, offset, range);
61 _reference = compute_reference(shape, data_type, num_bins, offset, range);
62 }
63
64protected:
65 template <typename U>
66 void fill(U &&tensor)
67 {
68 library->fill_tensor_uniform(tensor, 0);
69 }
70
71 TensorType compute_target(const TensorShape &shape, DataType data_type, size_t num_bins, int32_t offset, uint32_t range)
72 {
73 // Create tensors
74 TensorType src = create_tensor<TensorType>(shape, data_type);
75 TensorType dst = create_tensor<TensorType>(TensorShape(num_bins), DataType::U32);
76 DistributionType distribution_dst(num_bins, offset, range);
77
78 // Create and configure function
79 FunctionType histogram;
80 histogram.configure(&src, &distribution_dst);
81 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
82 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
83
84 // Allocate tensors
85 src.allocator()->allocate();
86 dst.allocator()->allocate();
87
88 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
89 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
90
91 // Fill tensors
92 fill(AccessorType(src));
93
94 // Compute function
95 histogram.run();
96
97 // Copy the distribution in a tensor
98 arm_compute::utils::map(distribution_dst, true);
99 AccessorType accessor_dst = AccessorType(dst);
100 uint32_t *dst_data = static_cast<uint32_t *>(accessor_dst.data());
101
102 ARM_COMPUTE_EXPECT(accessor_dst.size() <= dst.info()->total_size(), framework::LogLevel::ERRORS);
103
104 std::copy_n(distribution_dst.buffer(), num_bins, dst_data);
105 arm_compute::utils::unmap(distribution_dst);
106 return dst;
107 }
108
109 SimpleTensor<uint32_t> compute_reference(const TensorShape &shape, DataType data_type, size_t num_bins, int32_t offset, uint32_t range)
110 {
111 ARM_COMPUTE_ERROR_ON(data_type != DataType::U8);
112
113 // Create reference
114 SimpleTensor<T> src{ shape, data_type };
115
116 // Fill reference
117 fill(src);
118
119 // Compute reference
120 return reference::histogram<T>(src, num_bins, offset, range);
121 }
122
123 TensorType _target{};
124 SimpleTensor<uint32_t> _reference{};
125};
126} // namespace validation
127} // namespace test
128} // namespace arm_compute
129#endif /* ARM_COMPUTE_TEST_HISTOGRAM_FIXTURE */