blob: 09b9e0ef1ab7cd8fec7fa60f03f22aa9114f7d6d [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +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_POOLING_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/Tensor.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
35#include "tests/validation/CPP/PoolingLayer.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class PoolingLayerValidationFixedPointFixture : public framework::Fixture
47{
48public:
49 template <typename...>
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000050 void setup(TensorShape shape, PoolingType pool_type, int pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, int fractional_bits)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010051 {
52 _fractional_bits = fractional_bits;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000053 PoolingLayerInfo info(pool_type, pool_size, pad_stride_info, exclude_padding);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010054
55 _target = compute_target(shape, info, data_type, fractional_bits);
56 _reference = compute_reference(shape, info, data_type, fractional_bits);
57 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor)
62 {
63 if(_fractional_bits == 0)
64 {
65 std::uniform_real_distribution<> distribution(-1.f, 1.f);
66 library->fill(tensor, distribution, 0);
67 }
68 else
69 {
70 const int one_fixed = 1 << _fractional_bits;
71 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
72 library->fill(tensor, distribution, 0);
73 }
74 }
75
76 TensorType compute_target(const TensorShape &shape, PoolingLayerInfo info, DataType data_type, int fixed_point_position = 0)
77 {
78 // Create tensors
79 TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position);
80 TensorType dst;
81
82 // Create and configure function
83 FunctionType pool_layer;
84 pool_layer.configure(&src, &dst, info);
85
86 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
87 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
88
89 // Allocate tensors
90 src.allocator()->allocate();
91 dst.allocator()->allocate();
92
93 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
95
96 // Fill tensors
97 fill(AccessorType(src));
98
99 // Compute function
100 pool_layer.run();
101
102 return dst;
103 }
104
105 SimpleTensor<T> compute_reference(const TensorShape &shape, PoolingLayerInfo info, DataType data_type, int fixed_point_position = 0)
106 {
107 // Create reference
108 SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position };
109
110 // Fill reference
111 fill(src);
112
113 return reference::pooling_layer<T>(src, info);
114 }
115
116 TensorType _target{};
117 SimpleTensor<T> _reference{};
118 int _fractional_bits{};
119};
120
121template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
122class PoolingLayerValidationFixture : public PoolingLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
123{
124public:
125 template <typename...>
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000126 void setup(TensorShape shape, PoolingType pool_type, int pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100127 {
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000128 PoolingLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, pool_type, pool_size, pad_stride_info, exclude_padding, data_type, 0);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100129 }
130};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100131
132template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
133class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
134{
135public:
136 template <typename...>
137 void setup(TensorShape shape, PoolingType pool_type, DataType data_type)
138 {
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000139 PoolingLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, pool_type, shape.x(), PadStrideInfo(1, 1, 0, 0), true, data_type, 0);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100140 }
141};
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100142} // namespace validation
143} // namespace test
144} // namespace arm_compute
145#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */