blob: 13b2cb5a103241e2316dba3280ccf0d9f868094b [file] [log] [blame]
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +00003 *
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 "arm_compute/core/NEON/kernels/NEStridedSliceKernel.h"
25
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000026#include "arm_compute/core/IAccessWindow.h"
27#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000029#include "arm_compute/core/Types.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "arm_compute/core/Window.h"
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000031#include "arm_compute/core/utils/helpers/tensor_transform.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CPP/Validate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
36#include "src/core/utils/helpers/bit_ops.h"
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000037
38namespace arm_compute
39{
40namespace
41{
42Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
43 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
44 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000048
49 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
50 ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions());
51 ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions());
52 ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
53 ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
54 {
55 return i == 0;
56 }));
57
58 // Get expected output shape
59 const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
60 starts, ends, strides,
61 begin_mask, end_mask, shrink_axis_mask);
62 ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
63
64 // Checks output if configured
65 if(output->total_size() != 0)
66 {
67 const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
70 }
71
72 return Status{};
73}
74
Michalis Spyrou1732da82020-06-19 12:40:46 +010075std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output,
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000076 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
77 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
78{
79 // Output tensor auto initialization if not yet initialized
80 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
81 starts, ends, strides,
82 begin_mask, end_mask, shrink_axis_mask);
83 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
84
85 // Create window
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010086 Window win = calculate_max_window(*output, Steps());
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +000087 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
88
89 return std::make_pair(Status{}, win);
90}
91
92void strided_slice_generic(const ITensor *input, ITensor *output,
93 const Coordinates &starts, const BiStrides &strides, int32_t shrink_axis_mask,
94 const Window &window)
95{
96 Iterator output_it(output, window);
97 const size_t width_size = input->info()->element_size();
98
99 const bool is_shrink_w = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
100 const bool is_shrink_h = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 1);
101 const bool is_shrink_c = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 2);
102 const bool is_shrink_n = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 3);
103
104 unsigned int index = 0;
105 const int idx_w = is_shrink_w ? 0 : index++;
106 const int idx_h = is_shrink_h ? 0 : index++;
107 const int idx_c = is_shrink_c ? 0 : index++;
108 const int idx_n = is_shrink_n ? 0 : index;
109
110 BiStrides shrinked_strides;
111 shrinked_strides.set(0, is_shrink_w ? 0 : strides[0]);
112 shrinked_strides.set(1, is_shrink_h ? 0 : strides[1]);
113 shrinked_strides.set(2, is_shrink_c ? 0 : strides[2]);
114 shrinked_strides.set(3, is_shrink_n ? 0 : strides[3]);
115
116 execute_window_loop(window, [&](const Coordinates & id)
117 {
118 const int w_coord = starts[0] + (id[idx_w] * shrinked_strides[0]);
119 const int h_coord = starts[1] + (id[idx_h] * shrinked_strides[1]);
120 const int c_coord = starts[2] + (id[idx_c] * shrinked_strides[2]);
121 const int n_coord = starts[3] + (id[idx_n] * shrinked_strides[3]);
122
123 Coordinates in_coords(w_coord, h_coord, c_coord, n_coord);
124 std::copy_n(input->ptr_to_element(in_coords), width_size, output_it.ptr());
125 },
126 output_it);
127}
128} // namespace
129
130NEStridedSliceKernel::NEStridedSliceKernel()
Michalis Spyrou1732da82020-06-19 12:40:46 +0100131 : _starts_abs(), _final_strides(), _shrink_mask()
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000132{
133}
134
Michalis Spyrou1732da82020-06-19 12:40:46 +0100135void NEStridedSliceKernel::configure(const ITensorInfo *input, ITensorInfo *output,
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000136 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
137 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
138{
139 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyrou1732da82020-06-19 12:40:46 +0100140 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000141
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000142 _shrink_mask = shrink_axis_mask;
143
Michalis Spyrou1732da82020-06-19 12:40:46 +0100144 const TensorShape &input_shape = input->tensor_shape();
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000145
146 Coordinates ends_abs;
147 std::tie(_starts_abs, ends_abs, _final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
148 input_shape,
149 starts, ends, strides,
150 begin_mask, end_mask, shrink_axis_mask);
151
152 // Configure kernel window
Michalis Spyrou1732da82020-06-19 12:40:46 +0100153 auto win_config = validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000154 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
155 INEKernel::configure(win_config.second);
156}
157
158Status NEStridedSliceKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
159 const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
160 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
161{
162 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
163 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(),
164 starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)
165 .first);
166
167 return Status{};
168}
169
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100170void NEStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000171{
172 ARM_COMPUTE_UNUSED(info);
173 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
174 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
175
176 // Dispatch kernel
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100177 strided_slice_generic(tensors.get_const_tensor(TensorType::ACL_SRC_0),
178 tensors.get_tensor(TensorType::ACL_DST),
179 _starts_abs, _final_strides, _shrink_mask, window);
Georgios Pinitas6d9d6f42018-12-24 16:10:47 +0000180}
181} // namespace arm_compute