Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 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 | */ |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 24 | #include "SliceOperations.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | namespace reference |
| 36 | { |
| 37 | template <typename T> |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 38 | SimpleTensor<T> slice(const SimpleTensor<T> &src, Coordinates starts, Coordinates ends) |
| 39 | { |
| 40 | using namespace arm_compute::helpers::tensor_transform; |
| 41 | |
| 42 | // Validation checks |
| 43 | ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4); |
| 44 | ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions()); |
| 45 | ARM_COMPUTE_ERROR_ON(std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i) |
| 46 | { |
| 47 | return i < 0; |
| 48 | })); |
| 49 | ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions()); |
| 50 | |
| 51 | // Get source shape |
| 52 | const TensorShape &src_shape = src.shape(); |
| 53 | |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 54 | // Get destination shape |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 55 | TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_slice_shape(src_shape, starts, ends); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 56 | |
| 57 | // Create destination tensor |
| 58 | SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 }; |
| 59 | |
| 60 | // Perform slice |
| 61 | Window win; |
| 62 | win.use_tensor_dimensions(dst_shape); |
| 63 | execute_window_loop(win, [&](const Coordinates & id) |
| 64 | { |
| 65 | Coordinates offset; |
| 66 | for(unsigned int i = 0; i < id.num_dimensions(); ++i) |
| 67 | { |
| 68 | offset.set(i, starts[i] + id[i]); |
| 69 | } |
| 70 | *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset)); |
| 71 | }); |
| 72 | |
| 73 | return dst; |
| 74 | } |
| 75 | |
| 76 | template SimpleTensor<float> slice(const SimpleTensor<float> &src, Coordinates starts, Coordinates ends); |
| 77 | template SimpleTensor<half_float::half> slice(const SimpleTensor<half_float::half> &src, Coordinates starts, Coordinates ends); |
| 78 | |
| 79 | template <typename T> |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 80 | SimpleTensor<T> strided_slice(const SimpleTensor<T> &src, |
| 81 | Coordinates starts, Coordinates ends, BiStrides strides, |
| 82 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 83 | { |
| 84 | using namespace arm_compute::helpers::tensor_transform; |
| 85 | |
| 86 | // Validation checks |
| 87 | ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4); |
| 88 | ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions()); |
| 89 | ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions()); |
| 90 | ARM_COMPUTE_ERROR_ON(strides.num_dimensions() > src.shape().num_dimensions()); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 91 | ARM_COMPUTE_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i) |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 92 | { |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 93 | return i == 0; |
| 94 | })); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 95 | |
| 96 | // Get source shape |
| 97 | const TensorShape &src_shape = src.shape(); |
| 98 | |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 99 | // Get destination shape |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 100 | const TensorShape dst_shape = compute_strided_slice_output_shape(src_shape, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 101 | |
| 102 | // Create destination tensor |
| 103 | SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 }; |
| 104 | |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 105 | // Get coordinates |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 106 | Coordinates starts_abs{}; |
| 107 | Coordinates ends_abs{}; |
| 108 | Coordinates final_strides{}; |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 109 | std::tie(starts_abs, ends_abs, final_strides) = calculate_strided_slice_coords(src_shape, |
| 110 | starts, ends, strides, |
| 111 | begin_mask, end_mask, shrink_axis_mask); |
| 112 | |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 113 | // Perform strided slice |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 114 | unsigned int idx = 0; |
| 115 | Window win; |
| 116 | win.use_tensor_dimensions(compute_strided_slice_output_shape(src_shape, |
| 117 | starts, ends, strides, |
| 118 | begin_mask, end_mask, shrink_axis_mask, true)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 119 | execute_window_loop(win, [&](const Coordinates & id) |
| 120 | { |
| 121 | Coordinates offset; |
| 122 | for(unsigned int i = 0; i < id.num_dimensions(); ++i) |
| 123 | { |
| 124 | offset.set(i, starts_abs[i] + id[i] * final_strides[i]); |
| 125 | } |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 126 | dst.data()[idx++] = *reinterpret_cast<const T *>(src(offset)); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 127 | }); |
| 128 | |
| 129 | return dst; |
| 130 | } |
| 131 | |
| 132 | template SimpleTensor<float> strided_slice(const SimpleTensor<float> &src, |
| 133 | Coordinates starts, Coordinates ends, BiStrides strides, |
| 134 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask); |
| 135 | template SimpleTensor<half_float::half> strided_slice(const SimpleTensor<half_float::half> &src, |
| 136 | Coordinates starts, Coordinates ends, BiStrides strides, |
| 137 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask); |
| 138 | } // namespace reference |
| 139 | } // namespace validation |
| 140 | } // namespace test |
| 141 | } // namespace arm_compute |