Compute Library
 22.05
NEStridedSliceKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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 #include "src/core/CPP/Validate.h"
36 
37 namespace arm_compute
38 {
39 namespace
40 {
41 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
42  const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
43  int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
44 {
46  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
47 
48  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
49  ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions());
50  ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions());
51  ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
52  ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
53  {
54  return i == 0;
55  }));
56 
57  // Get expected output shape
58  const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input,
59  starts, ends, strides,
60  begin_mask, end_mask, shrink_axis_mask);
61  ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0);
62 
63  // Checks output if configured
64  if(output->total_size() != 0)
65  {
66  const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
67  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
69  }
70 
71  return Status{};
72 }
73 
74 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output,
75  const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
76  int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
77 {
78  // Output tensor auto initialization if not yet initialized
80  starts, ends, strides,
81  begin_mask, end_mask, shrink_axis_mask);
82  auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape));
83 
84  // Create window
85  Window win = calculate_max_window(*output, Steps());
86 
87  return std::make_pair(Status{}, win);
88 }
89 
90 void strided_slice_generic(const ITensor *input, ITensor *output,
91  const Coordinates &starts, const BiStrides &strides, int32_t shrink_axis_mask,
92  const Window &window)
93 {
94  Iterator output_it(output, window);
95  const size_t width_size = input->info()->element_size();
96 
97  const bool is_shrink_w = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 0);
98  const bool is_shrink_h = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 1);
99  const bool is_shrink_c = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 2);
100  const bool is_shrink_n = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, 3);
101 
102  unsigned int index = 0;
103  const int idx_w = is_shrink_w ? 0 : index++;
104  const int idx_h = is_shrink_h ? 0 : index++;
105  const int idx_c = is_shrink_c ? 0 : index++;
106  const int idx_n = is_shrink_n ? 0 : index;
107 
108  BiStrides shrinked_strides;
109  shrinked_strides.set(0, is_shrink_w ? 0 : strides[0]);
110  shrinked_strides.set(1, is_shrink_h ? 0 : strides[1]);
111  shrinked_strides.set(2, is_shrink_c ? 0 : strides[2]);
112  shrinked_strides.set(3, is_shrink_n ? 0 : strides[3]);
113 
114  execute_window_loop(window, [&](const Coordinates & id)
115  {
116  const int w_coord = starts[0] + (id[idx_w] * shrinked_strides[0]);
117  const int h_coord = starts[1] + (id[idx_h] * shrinked_strides[1]);
118  const int c_coord = starts[2] + (id[idx_c] * shrinked_strides[2]);
119  const int n_coord = starts[3] + (id[idx_n] * shrinked_strides[3]);
120 
121  Coordinates in_coords(w_coord, h_coord, c_coord, n_coord);
122  std::copy_n(input->ptr_to_element(in_coords), width_size, output_it.ptr());
123  },
124  output_it);
125 }
126 } // namespace
127 
129  : _starts_abs(), _final_strides(), _shrink_mask()
130 {
131 }
132 
134  const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
135  int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
136 {
137  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
138  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
139 
140  _shrink_mask = shrink_axis_mask;
141 
142  const TensorShape &input_shape = input->tensor_shape();
143 
144  Coordinates ends_abs;
145  std::tie(_starts_abs, ends_abs, _final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
146  input_shape,
147  starts, ends, strides,
148  begin_mask, end_mask, shrink_axis_mask);
149 
150  // Configure kernel window
151  auto win_config = validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
152  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
153  INEKernel::configure(win_config.second);
154 }
155 
157  const Coordinates &starts, const Coordinates &ends, const BiStrides &strides,
158  int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
159 {
160  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
162  starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)
163  .first);
164 
165  return Status{};
166 }
167 
168 void NEStridedSliceKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
169 {
170  ARM_COMPUTE_UNUSED(info);
173 
174  // Dispatch kernel
175  strided_slice_generic(tensors.get_const_tensor(TensorType::ACL_SRC_0),
177  _starts_abs, _final_strides, _shrink_mask, window);
178 }
179 } // namespace arm_compute
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:76
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.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
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.
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
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
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Coordinates BiStrides
Bidirectional strides.
Definition: Types.h:53
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Copyright (c) 2017-2022 Arm Limited.
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Coordinates of an item.
Definition: Coordinates.h:37
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...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
Information about executing thread and CPU.
Definition: CPPTypes.h:169
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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:77
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.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201