blob: 0930fb4117e9ffe8c9448606ac37b4126c3411b5 [file] [log] [blame]
John Richardson3c5f9492017-10-04 15:27:37 +01001/*
Gian Marco Iodicefbf3ecc2018-08-23 17:26:21 +01002 * Copyright (c) 2017-2018 ARM Limited.
John Richardson3c5f9492017-10-04 15:27:37 +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_MAGNITUDE_FIXTURE
25#define ARM_COMPUTE_TEST_MAGNITUDE_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/Globals.h"
30#include "tests/IAccessor.h"
31#include "tests/framework/Asserts.h"
32#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000033#include "tests/validation/reference/Magnitude.h"
John Richardson3c5f9492017-10-04 15:27:37 +010034
35namespace arm_compute
36{
37namespace test
38{
39namespace validation
40{
41template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
42class MagnitudeValidationFixture : public framework::Fixture
43{
44public:
45 template <typename...>
Gian Marco Iodicefbf3ecc2018-08-23 17:26:21 +010046 void setup(TensorShape shape, Format format, MagnitudeType magnitude_type)
John Richardson3c5f9492017-10-04 15:27:37 +010047 {
Gian Marco Iodicefbf3ecc2018-08-23 17:26:21 +010048 _target = compute_target(shape, format, magnitude_type);
John Richardson3c5f9492017-10-04 15:27:37 +010049 _reference = compute_reference(shape, format, magnitude_type);
50 _magnitude_type = magnitude_type;
51 }
52
53protected:
54 template <typename U>
55 void fill(U &&tensor, std::random_device::result_type seed_offset)
56 {
57 library->fill_tensor_uniform(tensor, seed_offset);
58 }
59
Gian Marco Iodicefbf3ecc2018-08-23 17:26:21 +010060 TensorType compute_target(const TensorShape &shape, Format format, MagnitudeType magnitude_type)
John Richardson3c5f9492017-10-04 15:27:37 +010061 {
62 DataType data_type = data_type_from_format(format);
63
64 // Create tensors
65 TensorType src1 = create_tensor<TensorType>(shape, data_type);
66 src1.info()->set_format(format);
67
68 TensorType src2 = create_tensor<TensorType>(shape, data_type);
69 src2.info()->set_format(format);
70
71 TensorType dst = create_tensor<TensorType>(shape, data_type);
72 dst.info()->set_format(format);
73
74 // Create and configure function
75 FunctionType magnitude;
Gian Marco Iodicefbf3ecc2018-08-23 17:26:21 +010076 magnitude.configure(&src1, &src2, &dst, magnitude_type);
John Richardson3c5f9492017-10-04 15:27:37 +010077
78 ARM_COMPUTE_EXPECT(src1.info()->is_resizable(), framework::LogLevel::ERRORS);
79 ARM_COMPUTE_EXPECT(src2.info()->is_resizable(), framework::LogLevel::ERRORS);
80 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
81
82 // Allocate tensors
83 src1.allocator()->allocate();
84 src2.allocator()->allocate();
85 dst.allocator()->allocate();
86
87 ARM_COMPUTE_EXPECT(!src1.info()->is_resizable(), framework::LogLevel::ERRORS);
88 ARM_COMPUTE_EXPECT(!src2.info()->is_resizable(), framework::LogLevel::ERRORS);
89 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
90
91 // Fill tensors
92 fill(AccessorType(src1), 0);
93 fill(AccessorType(src2), 1);
94
95 // Compute function
96 magnitude.run();
97
98 return dst;
99 }
100
101 SimpleTensor<T> compute_reference(const TensorShape &shape, Format format, MagnitudeType magnitude_type)
102 {
103 DataType data_type = data_type_from_format(format);
104
105 // Create reference
106 SimpleTensor<T> src1{ shape, data_type };
107 SimpleTensor<T> src2{ shape, data_type };
108
109 // Fill reference
110 fill(src1, 0);
111 fill(src2, 1);
112
113 return reference::magnitude<T>(src1, src2, magnitude_type);
114 }
115
116 TensorType _target{};
117 SimpleTensor<T> _reference{};
118 MagnitudeType _magnitude_type{};
119};
120} // namespace validation
121} // namespace test
122} // namespace arm_compute
123#endif /* ARM_COMPUTE_TEST_MAGNITUDE_FIXTURE */