| 36 | // Space to Batch |
| 37 | template <typename T> |
| 38 | SimpleTensor<T> space_to_batch(const SimpleTensor<T> &src, |
| 39 | const SimpleTensor<int32_t> &block_shape, |
| 40 | const SimpleTensor<int32_t> &paddings, |
| 41 | const TensorShape &dst_shape) |
| 42 | { |
| 43 | SimpleTensor<T> result(dst_shape, src.data_type(), 1, src.quantization_info()); |
| 44 | |
| 45 | const auto width_out = static_cast<int>(dst_shape[0]); |
| 46 | const auto height_out = static_cast<int>(dst_shape[1]); |
| 47 | const auto batch_out = static_cast<int>(dst_shape[3]); |
| 48 | |
| 49 | const auto width_in = static_cast<int>(src.shape()[0]); |
| 50 | const auto height_in = static_cast<int>(src.shape()[1]); |
| 51 | const auto batch_in = static_cast<int>(src.shape()[3]); |
| 52 | |
| 53 | const auto channel = static_cast<int>(src.shape()[2]); |
| 54 | |
| 55 | const auto block_width = block_shape[0]; |
| 56 | const auto block_height = block_shape[1]; |
| 57 | |
| 58 | const auto padding_left = paddings[0]; |
| 59 | const auto padding_top = paddings[2]; |
| 60 | |
| 61 | // Pad value must be logic zero |
| 62 | const auto pad_value = is_data_type_quantized(src.data_type()) ? src.quantization_info().uniform().offset : 0; |
| 63 | |
| 64 | int out_pos = 0; |
| 65 | for (int outB = 0; outB < batch_out; ++outB) |
| 66 | { |
| 67 | unsigned int inB = outB % batch_in; |
| 68 | |
| 69 | int shift_w = (outB / batch_in) % block_width; |
| 70 | int shift_h = (outB / batch_in) / block_width; |
| 71 | |
| 72 | for (int c = 0; c < channel; ++c) |
| 73 | { |
| 74 | for (int outH = 0; outH < height_out; ++outH) |
| 75 | { |
| 76 | for (int outW = 0; outW < width_out; ++outW) |
| 77 | { |
| 78 | const auto in_pos = |
| 79 | ((inB * channel + c) * height_in + ((outH * block_height + shift_h) - padding_top)) * width_in + |
| 80 | (outW * block_width + shift_w) - padding_left; |
| 81 | |
| 82 | if (outH * block_height + shift_h < padding_top || |
| 83 | outH * block_height + shift_h >= padding_top + height_in || |
| 84 | outW * block_width + shift_w < padding_left || |
| 85 | outW * block_width + shift_w >= padding_left + width_in) |
| 86 | { |
| 87 | result[out_pos] = pad_value; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | result[out_pos] = src[in_pos]; |
| 92 | } |
| 93 | ++out_pos; |
| 94 | } |
| 95 | } |
no test coverage detected