blob: c635d4abfd0b40666ef5f5090312f3e3f48cf9e0 [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
Isabella Gottardicc6129c2018-12-14 11:40:40 +000042 const auto width_out = static_cast<int>(dst_shape[0]);
43 const auto height_out = static_cast<int>(dst_shape[1]);
44 const auto batch_out = static_cast<int>(dst_shape[3]);
45
46 const auto width_in = static_cast<int>(src.shape()[0]);
47 const auto height_in = static_cast<int>(src.shape()[1]);
48 const auto batch_in = static_cast<int>(src.shape()[3]);
49
50 const auto channel = static_cast<int>(src.shape()[2]);
51
52 const auto block_width = block_shape[0];
53 const auto block_height = block_shape[1];
54
55 const auto padding_left = paddings[0];
56 const auto padding_top = paddings[2];
Michalis Spyrou16934a52018-08-21 18:03:58 +010057
58 int out_pos = 0;
Isabella Gottardicc6129c2018-12-14 11:40:40 +000059 for(int outB = 0; outB < batch_out; ++outB)
Michalis Spyrou16934a52018-08-21 18:03:58 +010060 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000061 unsigned int inB = outB % batch_in;
62
63 int shift_w = (outB / batch_in) % block_width;
64 int shift_h = (outB / batch_in) / block_width;
65
66 for(int c = 0; c < channel; ++c)
Michalis Spyrou16934a52018-08-21 18:03:58 +010067 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000068 for(int outH = 0; outH < height_out; ++outH)
Michalis Spyrou16934a52018-08-21 18:03:58 +010069 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000070 for(int outW = 0; outW < width_out; ++outW)
Michalis Spyrou16934a52018-08-21 18:03:58 +010071 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000072 const auto in_pos = ((inB * channel + c) * height_in + ((outH * block_height + shift_h) - padding_top)) * width_in + (outW * block_width + shift_w) - padding_left;
73
74 if(outH * block_height + shift_h < padding_top || outH * block_height + shift_h >= padding_top + height_in || outW * block_width + shift_w < padding_left
75 || outW * block_width + shift_w >= padding_left + width_in)
Michalis Spyrou16934a52018-08-21 18:03:58 +010076 {
77 result[out_pos] = 0;
78 }
79 else
80 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000081 result[out_pos] = src[in_pos];
Michalis Spyrou16934a52018-08-21 18:03:58 +010082 }
83 ++out_pos;
84 }
85 }
86 }
87 }
Michalis Spyrou16934a52018-08-21 18:03:58 +010088 return result;
89}
90
91template SimpleTensor<float> space_to_batch(const SimpleTensor<float> &src, const SimpleTensor<int32_t> &block_shape, const SimpleTensor<int32_t> &paddings, const TensorShape &dst_shape);
92template SimpleTensor<half> space_to_batch(const SimpleTensor<half> &src, const SimpleTensor<int32_t> &block_shape, const SimpleTensor<int32_t> &paddings, const TensorShape &dst_shape);
93} // namespace reference
94} // namespace validation
95} // namespace test
96} // namespace arm_compute