Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
| 36 | // Space to Batch |
| 37 | template <typename T> |
| 38 | SimpleTensor<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 Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 42 | 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 Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 57 | |
| 58 | int out_pos = 0; |
Isabella Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 59 | for(int outB = 0; outB < batch_out; ++outB) |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 60 | { |
Isabella Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 61 | 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 Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 67 | { |
Isabella Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 68 | for(int outH = 0; outH < height_out; ++outH) |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 69 | { |
Isabella Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 70 | for(int outW = 0; outW < width_out; ++outW) |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 71 | { |
Isabella Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 72 | 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 Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 76 | { |
| 77 | result[out_pos] = 0; |
| 78 | } |
| 79 | else |
| 80 | { |
Isabella Gottardi | cc6129c | 2018-12-14 11:40:40 +0000 | [diff] [blame] | 81 | result[out_pos] = src[in_pos]; |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 82 | } |
| 83 | ++out_pos; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 88 | return result; |
| 89 | } |
| 90 | |
| 91 | template SimpleTensor<float> space_to_batch(const SimpleTensor<float> &src, const SimpleTensor<int32_t> &block_shape, const SimpleTensor<int32_t> &paddings, const TensorShape &dst_shape); |
| 92 | template 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 |