blob: c76d7af5b95fe793aa3620c5fdc92c21d25ff1e0 [file] [log] [blame]
John Richardson338b1952017-08-24 12:03:42 +01001/*
Giorgio Arena4bdd1772020-12-17 16:47:07 +00002 * Copyright (c) 2017-2020 Arm Limited.
John Richardson338b1952017-08-24 12:03:42 +01003 *
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_MEAN_STD_DEV_FIXTURE
25#define ARM_COMPUTE_TEST_MEAN_STD_DEV_FIXTURE
26
John Richardson338b1952017-08-24 12:03:42 +010027#include "tests/Globals.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010028#include "tests/framework/Asserts.h"
29#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000030#include "tests/validation/reference/MeanStdDev.h"
John Richardson338b1952017-08-24 12:03:42 +010031
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
39class MeanStdDevValidationFixture : public framework::Fixture
40{
41public:
42 template <typename...>
43 void setup(TensorShape shape, DataType data_type)
44 {
45 _target = compute_target(shape, data_type);
46 _reference = compute_reference(shape, data_type);
47 }
48
49protected:
50 template <typename U>
51 void fill(U &&tensor)
52 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000053 if(tensor.data_type() == DataType::F32)
Michalis Spyroud1794eb2018-06-15 16:15:26 +010054 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000055 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
56 library->fill(tensor, distribution, 0);
57 }
58 else if(tensor.data_type() == DataType::F16)
59 {
60 arm_compute::utils::uniform_real_distribution_fp16 distribution{ half(-1.0f), half(1.0f) };
Michalis Spyroud1794eb2018-06-15 16:15:26 +010061 library->fill(tensor, distribution, 0);
62 }
63 else
64 {
65 library->fill_tensor_uniform(tensor, 0);
66 }
John Richardson338b1952017-08-24 12:03:42 +010067 }
68
69 std::pair<float, float> compute_target(const TensorShape &shape, DataType data_type)
70 {
71 // Create tensors
72 TensorType src = create_tensor<TensorType>(shape, data_type);
73
74 // Create output variables
75 float mean = 0.0f;
76 float std_dev = 0.0f;
77
78 // Create and configure function
79 FunctionType mean_std_dev;
80 mean_std_dev.configure(&src, &mean, &std_dev);
81
82 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
83
84 // Allocate tensors
85 src.allocator()->allocate();
86 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
87
88 // Fill tensors
89 fill(AccessorType(src));
90
91 // Compute function
92 mean_std_dev.run();
93
94 return std::make_pair(mean, std_dev);
95 }
96
97 std::pair<float, float> compute_reference(const TensorShape &shape, DataType data_type)
98 {
99 // Create reference
100 SimpleTensor<T> src{ shape, data_type };
101
102 // Fill reference
103 fill(src);
104
105 // Compute reference
106 return reference::mean_and_standard_deviation<T>(src);
107 }
108
109 std::pair<float, float> _target{};
110 std::pair<float, float> _reference{};
111};
112} // namespace validation
113} // namespace test
114} // namespace arm_compute
115#endif /* ARM_COMPUTE_TEST_MEAN_STD_DEV_FIXTURE */