blob: 222b1463dde10a08c389ea9f781b83d04ae552b7 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitas77589b52018-08-21 14:41:35 +01003 *
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 Pinitasc1a72452018-08-24 11:25:32 +010024#include "SliceOperations.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010025
26#include "arm_compute/core/utils/helpers/tensor_transform.h"
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010028
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T>
Georgios Pinitasc1a72452018-08-24 11:25:32 +010038SimpleTensor<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 Pinitasc1a72452018-08-24 11:25:32 +010054 // Get destination shape
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000055 TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_slice_shape(src_shape, starts, ends);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010056
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
76template SimpleTensor<float> slice(const SimpleTensor<float> &src, Coordinates starts, Coordinates ends);
77template SimpleTensor<half_float::half> slice(const SimpleTensor<half_float::half> &src, Coordinates starts, Coordinates ends);
78
79template <typename T>
Georgios Pinitas77589b52018-08-21 14:41:35 +010080SimpleTensor<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 Pinitasc1a72452018-08-24 11:25:32 +010091 ARM_COMPUTE_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
Georgios Pinitas77589b52018-08-21 14:41:35 +010092 {
Georgios Pinitasc1a72452018-08-24 11:25:32 +010093 return i == 0;
94 }));
Georgios Pinitas77589b52018-08-21 14:41:35 +010095
96 // Get source shape
97 const TensorShape &src_shape = src.shape();
98
Georgios Pinitas77589b52018-08-21 14:41:35 +010099 // Get destination shape
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000100 const TensorShape dst_shape = compute_strided_slice_output_shape(src_shape, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100101
102 // Create destination tensor
103 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
104
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000105 // Get coordinates
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100106 Coordinates starts_abs{};
107 Coordinates ends_abs{};
108 Coordinates final_strides{};
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000109 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 Pinitas77589b52018-08-21 14:41:35 +0100113 // Perform strided slice
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000114 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 Pinitas77589b52018-08-21 14:41:35 +0100119 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 Pinitasb4af2c62018-12-10 18:45:35 +0000126 dst.data()[idx++] = *reinterpret_cast<const T *>(src(offset));
Georgios Pinitas77589b52018-08-21 14:41:35 +0100127 });
128
129 return dst;
130}
131
132template 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);
135template 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