blob: ef94ea83b02c1747727f1ebf08b37def316d0b5a [file] [log] [blame]
Gian Marco Iodicebf179552017-09-05 13:51:21 +01001/*
Giorgio Arena156fcf32018-03-09 15:30:43 +00002 * Copyright (c) 2017-2018 ARM Limited.
Gian Marco Iodicebf179552017-09-05 13:51:21 +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_FLATTEN_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_FLATTEN_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Utils.h"
Giorgio Arena156fcf32018-03-09 15:30:43 +000030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodicebf179552017-09-05 13:51:21 +010031#include "arm_compute/runtime/Tensor.h"
32#include "tests/AssetsLibrary.h"
33#include "tests/Globals.h"
34#include "tests/IAccessor.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000037#include "tests/validation/reference/FlattenLayer.h"
Gian Marco Iodicebf179552017-09-05 13:51:21 +010038
39#include <random>
40
41namespace arm_compute
42{
43namespace test
44{
45namespace validation
46{
Giorgio Arena156fcf32018-03-09 15:30:43 +000047using namespace arm_compute::misc::shape_calculator;
48
Gian Marco Iodicebf179552017-09-05 13:51:21 +010049template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
50class FlattenLayerValidationFixture : public framework::Fixture
51{
52public:
53 template <typename...>
54 void setup(TensorShape shape, DataType data_type)
55 {
56 _fractional_bits = is_data_type_fixed_point(data_type) ? 4 : 0;
Giorgio Arena156fcf32018-03-09 15:30:43 +000057
58 TensorShape shape_flatten;
59 TensorInfo input_info(shape, 1, data_type, _fractional_bits);
60 shape_flatten = compute_im2col_flatten_shape(&input_info);
61
62 _target = compute_target(shape, shape_flatten, data_type);
63 _reference = compute_reference(shape, shape_flatten, data_type);
Gian Marco Iodicebf179552017-09-05 13:51:21 +010064 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape());
65 }
66
67protected:
68 template <typename U>
69 void fill(U &&tensor)
70 {
71 if(_fractional_bits == 0)
72 {
73 std::uniform_real_distribution<> distribution(-1.f, 1.f);
74 library->fill(tensor, distribution, 0);
75 }
76 else
77 {
78 const int one_fixed = 1 << _fractional_bits;
79 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
80 library->fill(tensor, distribution, 0);
81 }
82 }
83
Giorgio Arena156fcf32018-03-09 15:30:43 +000084 TensorType compute_target(const TensorShape &shape, const TensorShape &shape_flatten, DataType data_type)
Gian Marco Iodicebf179552017-09-05 13:51:21 +010085 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +010086 // Create tensors
87 TensorType src = create_tensor<TensorType>(shape, data_type, 1, _fractional_bits);
88 TensorType dst = create_tensor<TensorType>(shape_flatten, data_type, 1, _fractional_bits);
89
90 // Create and configure function
91 FunctionType flatten_layer;
92 flatten_layer.configure(&src, &dst);
93
94 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
96
97 // Allocate tensors
98 src.allocator()->allocate();
99 dst.allocator()->allocate();
100
101 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
103
104 // Fill tensors
105 fill(AccessorType(src));
106
107 // Compute function
108 flatten_layer.run();
109
110 return dst;
111 }
112
Giorgio Arena156fcf32018-03-09 15:30:43 +0000113 SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &shape_flatten, DataType data_type)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100114 {
115 // Create reference
116 SimpleTensor<T> src{ shape, data_type, 1, _fractional_bits };
117
118 // Fill reference
119 fill(src);
120
Giorgio Arena156fcf32018-03-09 15:30:43 +0000121 return reference::flatten_layer<T>(src, shape_flatten);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100122 }
123
124 TensorType _target{};
125 SimpleTensor<T> _reference{};
126 int _fractional_bits{};
127};
128} // namespace validation
129} // namespace test
130} // namespace arm_compute
131#endif /* ARM_COMPUTE_TEST_FLATTEN_LAYER_FIXTURE */