blob: 979ab94b335c58742431c4d71d5b6bbb67eb7a76 [file] [log] [blame]
Michalis Spyrou16934a52018-08-21 18:03:58 +01001/*
2 * Copyright (c) 2018 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#include "SpaceToBatch.h"
25
26#include "tests/validation/Helpers.h"
27
28namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
36// Space to Batch
37template <typename T>
38SimpleTensor<T> space_to_batch(const SimpleTensor<T> &src, const SimpleTensor<int32_t> &block_shape, const SimpleTensor<int32_t> &paddings, const TensorShape &dst_shape)
39{
40 SimpleTensor<T> result(dst_shape, src.data_type());
41
42 auto width_out = static_cast<int>(dst_shape[0]);
43 auto height_out = static_cast<int>(dst_shape[1]);
44 auto z_out = static_cast<int>(dst_shape[2]);
45
46 int out_pos = 0;
47 for(int batch = 0; batch < static_cast<int>(dst_shape[3]); ++batch)
48 {
49 for(int z = 0; z < z_out; ++z)
50 {
51 for(int y = 0; y < height_out; ++y)
52 {
53 for(int x = 0; x < width_out; ++x)
54 {
55 if(x < paddings[0] || x > width_out - paddings[1] - 1
56 || y < paddings[2] || y > height_out - paddings[3] - 1)
57 {
58 result[out_pos] = 0;
59 }
60 else
61 {
62 const int r = dst_shape[3] / (block_shape[0] * block_shape[1]);
63 const int in_x = (block_shape[0] * (x - paddings[0]) + (batch / r) % block_shape[0]);
64 const int in_y = (block_shape[1] * (y - paddings[2]) + (batch / r) / block_shape[0]);
65 int in_pos = in_x + src.shape()[0] * in_y + z * src.shape()[0] * src.shape()[1] + (batch % r) * src.shape()[0] * src.shape()[1] * src.shape()[2];
66 result[out_pos] = src[in_pos];
67 }
68 ++out_pos;
69 }
70 }
71 }
72 }
73
74 return result;
75}
76
77template SimpleTensor<float> space_to_batch(const SimpleTensor<float> &src, const SimpleTensor<int32_t> &block_shape, const SimpleTensor<int32_t> &paddings, const TensorShape &dst_shape);
78template SimpleTensor<half> space_to_batch(const SimpleTensor<half> &src, const SimpleTensor<int32_t> &block_shape, const SimpleTensor<int32_t> &paddings, const TensorShape &dst_shape);
79} // namespace reference
80} // namespace validation
81} // namespace test
82} // namespace arm_compute