blob: 2670af50dfd8d080bbaeff182847621d06d2792c [file] [log] [blame]
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +01001/*
SiCong Li5a7d1572023-03-21 12:00:15 +00002 * Copyright (c) 2018-2019, 2023 Arm Limited.
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +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_BATCH_TO_SPACE_LAYER_DATASET
25#define ARM_COMPUTE_TEST_BATCH_TO_SPACE_LAYER_DATASET
26
27#include "utils/TypePrinter.h"
28
29#include "arm_compute/core/TensorShape.h"
30#include "arm_compute/core/Types.h"
31
32namespace arm_compute
33{
34namespace test
35{
36namespace datasets
37{
38class BatchToSpaceLayerDataset
39{
40public:
SiCong Li5a7d1572023-03-21 12:00:15 +000041 using type = std::tuple<TensorShape, std::vector<int32_t>, CropInfo, TensorShape>;
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010042
43 struct iterator
44 {
SiCong Li5a7d1572023-03-21 12:00:15 +000045 iterator(std::vector<TensorShape>::const_iterator src_it,
46 std::vector<std::vector<int32_t>>::const_iterator block_shape_it,
47 std::vector<CropInfo>::const_iterator crop_info_it,
48 std::vector<TensorShape>::const_iterator dst_it)
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010049 : _src_it{ std::move(src_it) },
50 _block_shape_it{ std::move(block_shape_it) },
SiCong Li5a7d1572023-03-21 12:00:15 +000051 _crop_info_it{ std::move(crop_info_it) },
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010052 _dst_it{ std::move(dst_it) }
53 {
54 }
55
56 std::string description() const
57 {
58 std::stringstream description;
59 description << "In=" << *_src_it << ":";
60 description << "BlockShape=" << *_block_shape_it << ":";
SiCong Li5a7d1572023-03-21 12:00:15 +000061 description << "CropInfo=" << *_crop_info_it << ":";
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010062 description << "Out=" << *_dst_it;
63 return description.str();
64 }
65
66 BatchToSpaceLayerDataset::type operator*() const
67 {
SiCong Li5a7d1572023-03-21 12:00:15 +000068 return std::make_tuple(*_src_it, *_block_shape_it, *_crop_info_it, *_dst_it);
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010069 }
70
71 iterator &operator++()
72 {
73 ++_src_it;
74 ++_block_shape_it;
SiCong Li5a7d1572023-03-21 12:00:15 +000075 ++_crop_info_it;
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010076 ++_dst_it;
77
78 return *this;
79 }
80
81 private:
SiCong Li5a7d1572023-03-21 12:00:15 +000082 std::vector<TensorShape>::const_iterator _src_it;
83 std::vector<std::vector<int32_t>>::const_iterator _block_shape_it;
84 std::vector<CropInfo>::const_iterator _crop_info_it;
85 std::vector<TensorShape>::const_iterator _dst_it;
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010086 };
87
88 iterator begin() const
89 {
SiCong Li5a7d1572023-03-21 12:00:15 +000090 return iterator(_src_shapes.begin(), _block_shapes.begin(), _crop_infos.begin(), _dst_shapes.begin());
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010091 }
92
93 int size() const
94 {
SiCong Li5a7d1572023-03-21 12:00:15 +000095 return std::min(std::min(std::min(_src_shapes.size(), _block_shapes.size()), _crop_infos.size()), _dst_shapes.size());
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010096 }
97
SiCong Li5a7d1572023-03-21 12:00:15 +000098 void add_config(const TensorShape &src, const std::vector<int32_t> &block_shape, const CropInfo &crop_info, const TensorShape &dst)
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +010099 {
100 _src_shapes.emplace_back(std::move(src));
SiCong Li5a7d1572023-03-21 12:00:15 +0000101 _block_shapes.emplace_back(std::move(block_shape));
102 _crop_infos.emplace_back(std::move(crop_info));
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100103 _dst_shapes.emplace_back(std::move(dst));
104 }
105
106protected:
107 BatchToSpaceLayerDataset() = default;
108 BatchToSpaceLayerDataset(BatchToSpaceLayerDataset &&) = default;
109
110private:
SiCong Li5a7d1572023-03-21 12:00:15 +0000111 std::vector<TensorShape> _src_shapes{};
112 std::vector<std::vector<int32_t>> _block_shapes{};
113 std::vector<CropInfo> _crop_infos{};
114 std::vector<TensorShape> _dst_shapes{};
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100115};
116
SiCong Li5a7d1572023-03-21 12:00:15 +0000117/** Follow NCHW data layout across all datasets. I.e.
118 * TensorShape(Width(X), Height(Y), Channel(Z), Batch(W))
119 */
120
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100121class SmallBatchToSpaceLayerDataset final : public BatchToSpaceLayerDataset
122{
123public:
124 SmallBatchToSpaceLayerDataset()
125 {
SiCong Li5a7d1572023-03-21 12:00:15 +0000126 // Block size = 1 (effectively no batch to space)
127 add_config(TensorShape(1U, 1U, 1U, 4U), { 1U, 1U }, CropInfo(), TensorShape(1U, 1U, 1U, 4U));
128 add_config(TensorShape(8U, 2U, 4U, 3U), { 1U, 1U }, CropInfo(), TensorShape(8U, 2U, 4U, 3U));
129 // Same block size in both x and y
130 add_config(TensorShape(3U, 2U, 1U, 4U), { 2U, 2U }, CropInfo(), TensorShape(6U, 4U, 1U, 1U));
131 add_config(TensorShape(1U, 3U, 2U, 9U), { 3U, 3U }, CropInfo(), TensorShape(3U, 9U, 2U, 1U));
132 // Different block size in x and y
133 add_config(TensorShape(5U, 7U, 7U, 4U), { 2U, 1U }, CropInfo(), TensorShape(10U, 7U, 7U, 2U));
134 add_config(TensorShape(3U, 3U, 1U, 8U), { 1U, 2U }, CropInfo(), TensorShape(3U, 6U, 1U, 4U));
135 add_config(TensorShape(5U, 2U, 2U, 6U), { 3U, 2U }, CropInfo(), TensorShape(15U, 4U, 2U, 1U));
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100136 }
137};
138
SiCong Li5a7d1572023-03-21 12:00:15 +0000139/** Relative small shapes that are still large enough to leave room for testing cropping of the output shape
140 */
141class SmallBatchToSpaceLayerWithCroppingDataset final : public BatchToSpaceLayerDataset
142{
143public:
144 SmallBatchToSpaceLayerWithCroppingDataset()
145 {
146 // Crop in both dims
147 add_config(TensorShape(5U, 3U, 2U, 8U), { 2U, 2U }, CropInfo(1U, 1U, 2U, 1U), TensorShape(8U, 3U, 2U, 2U));
148 // Left crop in x dim
149 add_config(TensorShape(1U, 1U, 1U, 20U), { 4U, 5U }, CropInfo(2U, 1U, 0U, 2U), TensorShape(1U, 3U, 1U, 1U));
150 // Left crop in y dim
151 add_config(TensorShape(3U, 1U, 1U, 8U), { 2U, 4U }, CropInfo(0U, 0U, 2U, 1U), TensorShape(6U, 1U, 1U, 1U));
152 }
153};
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100154class LargeBatchToSpaceLayerDataset final : public BatchToSpaceLayerDataset
155{
156public:
157 LargeBatchToSpaceLayerDataset()
158 {
SiCong Li5a7d1572023-03-21 12:00:15 +0000159 // Same block size in both x and y
160 add_config(TensorShape(64U, 32U, 2U, 4U), { 2U, 2U }, CropInfo(), TensorShape(128U, 64U, 2U, 1U));
161 add_config(TensorShape(128U, 16U, 2U, 18U), { 3U, 3U }, CropInfo(), TensorShape(384U, 48U, 2U, 2U));
162 // Different block size in x and y
163 add_config(TensorShape(16U, 8U, 2U, 8U), { 4U, 1U }, CropInfo(), TensorShape(64U, 8U, 2U, 2U));
164 add_config(TensorShape(8U, 16U, 2U, 8U), { 2U, 4U }, CropInfo(), TensorShape(16U, 64U, 2U, 1U));
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100165 }
166};
167} // namespace datasets
168} // namespace test
169} // namespace arm_compute
170#endif /* ARM_COMPUTE_TEST_BATCH_TO_SPACE_LAYER_DATASET */