Compute Library
 23.11
NEStridedSliceKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021, 2023 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  */
25 
28 #include "arm_compute/core/Types.h"
32 
33 #include "src/core/CPP/Validate.h"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
42 Status validate_arguments(const ITensorInfo *input,
43  const ITensorInfo *output,
44  const Coordinates &starts,
45  const Coordinates &ends,
46  const BiStrides &strides,
47  int32_t begin_mask,
48  int32_t end_mask,
49  int32_t shrink_axis_mask)
50 {
53 
54  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
55  ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions());
56  ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions());
57  ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
59  std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i) { return i == 0; }));
60 
61  // Get expected output shape
62  const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(
63  *input, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
64  ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
65 
66  // Checks output if configured
67  if (output->total_size() != 0)
68  {
69  const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
70  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
72  }
73 
74  return Status{};
75 }
76 
77 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input,
78  ITensorInfo *output,
79  const Coordinates &starts,
80  const Coordinates &ends,
81  const BiStrides &strides,
82  int32_t begin_mask,
83  int32_t end_mask,
84  int32_t shrink_axis_mask)
85 {
86  // Output tensor auto initialization if not yet initialized
88  *input, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
89  auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
90 
91  // Create window
92  Window win = calculate_max_window(*output, Steps());
93 
94  return std::make_pair(Status{}, win);
95 }
96 } // namespace
97 
98 NEStridedSliceKernel::NEStridedSliceKernel() : _starts_abs(), _final_strides(), _shrink_mask()
99 {
100 }
101 
103  ITensorInfo *output,
104  const Coordinates &starts,
105  const Coordinates &ends,
106  const BiStrides &strides,
107  int32_t begin_mask,
108  int32_t end_mask,
109  int32_t shrink_axis_mask)
110 {
113  validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
114  _shrink_mask = shrink_axis_mask;
115  const TensorShape &input_shape = input->tensor_shape();
116  Coordinates ends_abs;
117  std::tie(_starts_abs, ends_abs, _final_strides) =
119  begin_mask, end_mask, shrink_axis_mask);
120  // Configure kernel window
121  auto win_config =
122  validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
123  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
124  INEKernel::configure(win_config.second);
125 }
126 
128  const ITensorInfo *output,
129  const Coordinates &starts,
130  const Coordinates &ends,
131  const BiStrides &strides,
132  int32_t begin_mask,
133  int32_t end_mask,
134  int32_t shrink_axis_mask)
135 {
137  validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
138  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), starts, ends,
139  strides, begin_mask, end_mask, shrink_axis_mask)
140  .first);
141 
142  return Status{};
143 }
144 
145 void NEStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
146 {
150 
152  const ITensor *output = tensors.get_tensor(TensorType::ACL_DST);
153 
154  size_t width_size = input->info()->element_size();
155 
156  const bool is_shrink_x = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 0);
157  const bool is_shrink_y = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 1);
158  const bool is_shrink_z = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 2);
159  const bool is_shrink_w = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 3);
160 
161  unsigned int index = 0;
162  const int idx_x = is_shrink_x ? 0 : index++;
163  const int idx_y = is_shrink_y ? 0 : index++;
164  const int idx_z = is_shrink_z ? 0 : index++;
165  const int idx_w = is_shrink_w ? 0 : index;
166 
167  BiStrides shrinked_strides;
168  shrinked_strides.set(0, is_shrink_x ? 0 : _final_strides[0]);
169  shrinked_strides.set(1, is_shrink_y ? 0 : _final_strides[1]);
170  shrinked_strides.set(2, is_shrink_z ? 0 : _final_strides[2]);
171  shrinked_strides.set(3, is_shrink_w ? 0 : _final_strides[3]);
172 
173  Window win = window;
174 
175  size_t length_x = win.shape()[0];
176 
177  if (_final_strides[0] == 1 && !is_shrink_x)
178  {
179  win.set(Window::DimX, Window::Dimension(0, 1, 1));
180  width_size = width_size * length_x;
181  }
182 
183  Iterator output_it(output, win);
184 
185  const int start_0 = _starts_abs[0];
186  const int start_1 = _starts_abs[1];
187  const int start_2 = _starts_abs[2];
188  const int start_3 = _starts_abs[3];
189 
190  const int shrinked_stride_0 = shrinked_strides[0];
191  const int shrinked_stride_1 = shrinked_strides[1];
192  const int shrinked_stride_2 = shrinked_strides[2];
193  const int shrinked_stride_3 = shrinked_strides[3];
194 
195  const int byte_increment_0 = static_cast<int>(input->info()->strides_in_bytes()[0]);
196  const int byte_increment_1 = static_cast<int>(input->info()->strides_in_bytes()[1]);
197  const int byte_increment_2 = static_cast<int>(input->info()->strides_in_bytes()[2]);
198  const int byte_increment_3 = static_cast<int>(input->info()->strides_in_bytes()[3]);
199 
200  uint8_t *input_base = input->ptr_to_element(Coordinates(0, 0, 0, 0));
201  uint8_t *cur_ptr;
202 
204  win,
205  [&](const Coordinates &id)
206  {
207  cur_ptr = input_base;
208  cur_ptr += (start_0 + (id[idx_x] * shrinked_stride_0)) * byte_increment_0;
209  cur_ptr += (start_1 + (id[idx_y] * shrinked_stride_1)) * byte_increment_1;
210  cur_ptr += (start_2 + (id[idx_z] * shrinked_stride_2)) * byte_increment_2;
211  cur_ptr += (start_3 + (id[idx_w] * shrinked_stride_3)) * byte_increment_3;
212 
213  std::copy_n(cur_ptr, width_size, output_it.ptr());
214  },
215  output_it);
216 }
217 } // namespace arm_compute
arm_compute::misc::shape_calculator::compute_strided_slice_shape
TensorShape compute_strided_slice_shape(const ITensorInfo &input, const Coordinates &starts, const Coordinates &ends, const Coordinates &strides, int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
Calculate the strided slice output shape of a tensor.
Definition: ShapeCalculator.h:1161
arm_compute::BiStrides
Coordinates BiStrides
Bidirectional strides.
Definition: Types.h:81
arm_compute::Dimensions::set
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:75
arm_compute::NEStridedSliceKernel::configure
void configure(const ITensorInfo *input, ITensorInfo *output, const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
Configure kernel.
Definition: NEStridedSliceKernel.cpp:102
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:29
arm_compute::test::validation::output_shape
TensorShape output_shape
Definition: LSTMLayerQuantized.cpp:469
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::cpu::kernels::validate_arguments
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Definition: CpuDirectConv2dKernel.cpp:57
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1079
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:574
Types.h
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Window.h
arm_compute::NEStridedSliceKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
Static function to check if given info will lead to a valid configuration of NEStridedSliceKernel.
Definition: NEStridedSliceKernel.cpp:127
TensorInfo.h
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::ITensorPack::get_tensor
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:63
arm_compute::ACL_SRC_0
@ ACL_SRC_0
Definition: Types.h:45
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:677
NEStridedSliceKernel.h
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::Iterator::ptr
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:147
arm_compute::ITensorPack::get_const_tensor
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:53
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::ITensorPack
Tensor packing service.
Definition: ITensorPack.h:39
arm_compute::execute_window_loop
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:74
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
arm_compute::auto_init_if_empty
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Definition: AutoConfiguration.h:43
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::helpers::bit_ops::is_bit_set
bool is_bit_set(T v, unsigned int idx)
Checks if the idx-th bit is set in an integral type.
Definition: bit_ops.h:45
arm_compute::Window::shape
TensorShape shape() const
Return the shape of the window in number of steps.
Definition: Window.inl:298
WindowHelpers.h
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:203
bit_ops.h
arm_compute::cpu::kernels::validate_and_configure_window
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
Definition: CpuDirectConv2dKernel.cpp:92
arm_compute::NEStridedSliceKernel::NEStridedSliceKernel
NEStridedSliceKernel()
Default constructor.
Definition: NEStridedSliceKernel.cpp:98
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
arm_compute::NEStridedSliceKernel::run_op
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Definition: NEStridedSliceKernel.cpp:145
AutoConfiguration.h
arm_compute::test::validation::input_shape
TensorShape input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
Definition: LSTMLayerQuantized.cpp:466
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::ThreadInfo
Information about executing thread and CPU.
Definition: CPPTypes.h:180
arm_compute::misc::ICloneable::clone
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
ShapeCalculator.h
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
Validate.h
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::helpers::tensor_transform::calculate_strided_slice_coords
std::tuple< Coordinates, Coordinates, Coordinates > calculate_strided_slice_coords(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides, int32_t begin_mask=0, int32_t end_mask=0, int32_t shrink_axis_mask=0)
Calculate start, end and stride coordinates for a strided slice.
Definition: tensor_transform.cpp:125
tensor_transform.h
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
ITensor.h
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486