Compute Library
 23.08
NEStridedSliceKernel Class Reference

Interface for the kernel to perform tensor strided slicing. More...

#include <NEStridedSliceKernel.h>

Collaboration diagram for NEStridedSliceKernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NEStridedSliceKernel ()
 Default constructor. More...
 
 NEStridedSliceKernel (const NEStridedSliceKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEStridedSliceKerneloperator= (const NEStridedSliceKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEStridedSliceKernel (NEStridedSliceKernel &&)=default
 Allow instances of this class to be moved. More...
 
NEStridedSliceKerneloperator= (NEStridedSliceKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NEStridedSliceKernel ()=default
 Default destructor. More...
 
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. More...
 
void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
 
virtual void run (const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. More...
 
virtual void run_nd (const Window &window, const ThreadInfo &info, const Window &thread_locator)
 legacy compatibility layer for implemantions which do not support thread_locator In these cases we simply narrow the interface down the legacy version More...
 
virtual size_t get_mws (const CPUInfo &platform, size_t thread_count) const
 Return minimum workload size of the relevant kernel. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...
 

Static Public Member Functions

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. More...
 

Additional Inherited Members

- Static Public Attributes inherited from ICPPKernel
static constexpr size_t default_mws = 1
 

Detailed Description

Interface for the kernel to perform tensor strided slicing.

Definition at line 38 of file NEStridedSliceKernel.h.

Constructor & Destructor Documentation

◆ NEStridedSliceKernel() [1/3]

Default constructor.

Definition at line 91 of file NEStridedSliceKernel.cpp.

92  : _starts_abs(), _final_strides(), _shrink_mask()
93 {
94 }

◆ NEStridedSliceKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ NEStridedSliceKernel() [3/3]

Allow instances of this class to be moved.

◆ ~NEStridedSliceKernel()

~NEStridedSliceKernel ( )
default

Default destructor.

Member Function Documentation

◆ 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.

Note
Supported tensor rank: up to 4
Parameters
[in]inputSource tensor info. Data type supported: All
[out]outputDestination tensor info. Data type supported: Same as input
[in]startsThe starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
[in]endsThe ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
[in]stridesThe strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
[in]begin_maskIf the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
[in]end_maskIf the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
[in]shrink_axis_maskIf the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1. A slice of size 1 starting from starts[i] in the dimension must be preserved.

Definition at line 96 of file NEStridedSliceKernel.cpp.

99 {
101  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
102  _shrink_mask = shrink_axis_mask;
103  const TensorShape &input_shape = input->tensor_shape();
104  Coordinates ends_abs;
105  std::tie(_starts_abs, ends_abs, _final_strides) = arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(
106  input_shape,
107  starts, ends, strides,
108  begin_mask, end_mask, shrink_axis_mask);
109  // Configure kernel window
110  auto win_config = validate_and_configure_window(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
111  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
112  INEKernel::configure(win_config.second);
113 }

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::helpers::tensor_transform::calculate_strided_slice_coords(), arm_compute::test::validation::input, arm_compute::test::validation::input_shape, arm_compute::cpu::kernels::validate_and_configure_window(), and arm_compute::cpu::kernels::validate_arguments().

◆ name()

const char* name ( ) const
inlineoverridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 41 of file NEStridedSliceKernel.h.

42  {
43  return "NEStridedSliceKernel";
44  }

◆ operator=() [1/2]

NEStridedSliceKernel& operator= ( const NEStridedSliceKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

NEStridedSliceKernel& operator= ( NEStridedSliceKernel &&  )
default

Allow instances of this class to be moved.

◆ run_op()

void run_op ( ITensorPack tensors,
const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
Parameters
[in]tensorsA vector containing the tensors to operate on.
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Reimplemented from ICPPKernel.

Definition at line 127 of file NEStridedSliceKernel.cpp.

128 {
132 
133  const ITensor *input = tensors.get_const_tensor(TensorType::ACL_SRC_0);
134  const ITensor *output = tensors.get_tensor(TensorType::ACL_DST);
135 
136  size_t width_size = input->info()->element_size();
137 
138  const bool is_shrink_x = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 0);
139  const bool is_shrink_y = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 1);
140  const bool is_shrink_z = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 2);
141  const bool is_shrink_w = arm_compute::helpers::bit_ops::is_bit_set(_shrink_mask, 3);
142 
143  unsigned int index = 0;
144  const int idx_x = is_shrink_x ? 0 : index++;
145  const int idx_y = is_shrink_y ? 0 : index++;
146  const int idx_z = is_shrink_z ? 0 : index++;
147  const int idx_w = is_shrink_w ? 0 : index;
148 
149  BiStrides shrinked_strides;
150  shrinked_strides.set(0, is_shrink_x ? 0 : _final_strides[0]);
151  shrinked_strides.set(1, is_shrink_y ? 0 : _final_strides[1]);
152  shrinked_strides.set(2, is_shrink_z ? 0 : _final_strides[2]);
153  shrinked_strides.set(3, is_shrink_w ? 0 : _final_strides[3]);
154 
155  Window win = window;
156 
157  size_t length_x = win.shape()[0];
158 
159  if(_final_strides[0] == 1 && !is_shrink_x)
160  {
161  win.set(Window::DimX, Window::Dimension(0, 1, 1));
162  width_size = width_size * length_x;
163  }
164 
165  Iterator output_it(output, win);
166 
167  const int start_0 = _starts_abs[0];
168  const int start_1 = _starts_abs[1];
169  const int start_2 = _starts_abs[2];
170  const int start_3 = _starts_abs[3];
171 
172  const int shrinked_stride_0 = shrinked_strides[0];
173  const int shrinked_stride_1 = shrinked_strides[1];
174  const int shrinked_stride_2 = shrinked_strides[2];
175  const int shrinked_stride_3 = shrinked_strides[3];
176 
177  const int byte_increment_0 = static_cast<int>(input->info()->strides_in_bytes()[0]);
178  const int byte_increment_1 = static_cast<int>(input->info()->strides_in_bytes()[1]);
179  const int byte_increment_2 = static_cast<int>(input->info()->strides_in_bytes()[2]);
180  const int byte_increment_3 = static_cast<int>(input->info()->strides_in_bytes()[3]);
181 
182  uint8_t *input_base = input->ptr_to_element(Coordinates(0, 0, 0, 0));
183  uint8_t *cur_ptr;
184 
186  win, [&](const Coordinates & id)
187  {
188  cur_ptr = input_base;
189  cur_ptr += (start_0 + (id[idx_x] * shrinked_stride_0)) * byte_increment_0;
190  cur_ptr += (start_1 + (id[idx_y] * shrinked_stride_1)) * byte_increment_1;
191  cur_ptr += (start_2 + (id[idx_z] * shrinked_stride_2)) * byte_increment_2;
192  cur_ptr += (start_3 + (id[idx_w] * shrinked_stride_3)) * byte_increment_3;
193 
194  std::copy_n(cur_ptr, width_size, output_it.ptr());
195  },
196  output_it);
197 }

References arm_compute::ACL_DST, arm_compute::ACL_SRC_0, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, Window::DimX, arm_compute::execute_window_loop(), ITensorPack::get_const_tensor(), ITensorPack::get_tensor(), arm_compute::test::validation::info, arm_compute::test::validation::input, arm_compute::helpers::bit_ops::is_bit_set(), Iterator::ptr(), Dimensions< T >::set(), Window::set(), Window::shape(), and IKernel::window().

◆ validate()

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

Static function to check if given info will lead to a valid configuration of NEStridedSliceKernel.

Note
Supported tensor rank: up to 4
Parameters
[in]inputSource tensor info. Data type supported: All
[in]outputDestination tensor info. Data type supported: Same as input
[in]startsThe starts of the dimensions of the input tensor to be sliced. The length must be of rank(input).
[in]endsThe ends of the dimensions of the input tensor to be sliced. The length must be of rank(input).
[in]stridesThe strides of the dimensions of the input tensor to be sliced. The length must be of rank(input).
[in]begin_maskIf the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
[in]end_maskIf the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
[in]shrink_axis_maskIf the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1. A slice of size 1 starting from starts[i] in the dimension must be preserved.

Definition at line 115 of file NEStridedSliceKernel.cpp.

118 {
119  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask));
120  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(),
121  starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)
122  .first);
123 
124  return Status{};
125 }

References ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), arm_compute::test::validation::input, arm_compute::cpu::kernels::validate_and_configure_window(), and arm_compute::cpu::kernels::validate_arguments().

Referenced by NESlice::validate(), and NEStridedSlice::validate().


The documentation for this class was generated from the following files:
arm_compute::BiStrides
Coordinates BiStrides
Bidirectional strides.
Definition: Types.h:82
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:76
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:60
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1004
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
arm_compute::ACL_SRC_0
@ ACL_SRC_0
Definition: Types.h:45
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:204
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:456
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
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:284
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
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_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::test::validation::input_shape
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
Definition: ConvolutionLayer.cpp:408
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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:120
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
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:77
arm_compute::TensorShape::set
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486