blob: e72487c7cf9491dd925c3343c3a8ca985335e470 [file] [log] [blame]
Gian Marco Iodicebf179552017-09-05 13:51:21 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2017-2021, 2023 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:
Gian Marco Iodicebf179552017-09-05 13:51:21 +010053 void setup(TensorShape shape, DataType data_type)
54 {
Giorgio Arena156fcf32018-03-09 15:30:43 +000055 TensorShape shape_flatten;
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010056 TensorInfo input_info(shape, 1, data_type);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010057 shape_flatten = compute_flatten_shape(&input_info);
Giorgio Arena156fcf32018-03-09 15:30:43 +000058
59 _target = compute_target(shape, shape_flatten, data_type);
60 _reference = compute_reference(shape, shape_flatten, data_type);
Gian Marco Iodicebf179552017-09-05 13:51:21 +010061 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape());
62 }
63
64protected:
65 template <typename U>
66 void fill(U &&tensor)
67 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000068 static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported.");
Giorgio Arena33b103b2021-01-08 10:37:15 +000069 using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
Giorgio Arena4bdd1772020-12-17 16:47:07 +000070
71 DistributionType distribution{ T(-1.0f), T(1.0f) };
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010072 library->fill(tensor, distribution, 0);
Gian Marco Iodicebf179552017-09-05 13:51:21 +010073 }
74
Giorgio Arena156fcf32018-03-09 15:30:43 +000075 TensorType compute_target(const TensorShape &shape, const TensorShape &shape_flatten, DataType data_type)
Gian Marco Iodicebf179552017-09-05 13:51:21 +010076 {
Gian Marco Iodicebf179552017-09-05 13:51:21 +010077 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010078 TensorType src = create_tensor<TensorType>(shape, data_type, 1);
79 TensorType dst = create_tensor<TensorType>(shape_flatten, data_type, 1);
Gian Marco Iodicebf179552017-09-05 13:51:21 +010080
81 // Create and configure function
82 FunctionType flatten_layer;
83 flatten_layer.configure(&src, &dst);
84
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010085 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
86 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Gian Marco Iodicebf179552017-09-05 13:51:21 +010087
88 // Allocate tensors
89 src.allocator()->allocate();
90 dst.allocator()->allocate();
91
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010092 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
93 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Gian Marco Iodicebf179552017-09-05 13:51:21 +010094
95 // Fill tensors
96 fill(AccessorType(src));
97
98 // Compute function
99 flatten_layer.run();
100
101 return dst;
102 }
103
Giorgio Arena156fcf32018-03-09 15:30:43 +0000104 SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &shape_flatten, DataType data_type)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100105 {
106 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100107 SimpleTensor<T> src{ shape, data_type, 1 };
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100108
109 // Fill reference
110 fill(src);
111
Giorgio Arena156fcf32018-03-09 15:30:43 +0000112 return reference::flatten_layer<T>(src, shape_flatten);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100113 }
114
115 TensorType _target{};
116 SimpleTensor<T> _reference{};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100117};
118} // namespace validation
119} // namespace test
120} // namespace arm_compute
121#endif /* ARM_COMPUTE_TEST_FLATTEN_LAYER_FIXTURE */