diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2018-08-21 14:41:35 +0100 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:54:54 +0000 |
commit | 77589b58d55a181d49d5d94126c7e204af2835bc (patch) | |
tree | 58664c368f170a270a684e137f1acd7aaa5e2a42 | |
parent | 2aa0ec46622a77c65d7ebb6defe70ea45d72cc17 (diff) | |
download | ComputeLibrary-77589b58d55a181d49d5d94126c7e204af2835bc.tar.gz |
COMPMID-1235: Implements StridedSlice for CL
Change-Id: If2b44da31fae528c76be742b4b3a21fb0eb06b49
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/145284
Reviewed-by: Giuseppe Rossini <giuseppe.rossini@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
-rw-r--r-- | arm_compute/core/CL/CLKernels.h | 1 | ||||
-rw-r--r-- | arm_compute/core/CL/kernels/CLStridedSliceKernel.h | 97 | ||||
-rw-r--r-- | arm_compute/core/Types.h | 2 | ||||
-rw-r--r-- | arm_compute/core/utils/helpers/tensor_transform.h | 84 | ||||
-rw-r--r-- | arm_compute/core/utils/misc/ShapeCalculator.h | 18 | ||||
-rw-r--r-- | arm_compute/runtime/CL/CLFunctions.h | 1 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLStridedSlice.h | 75 | ||||
-rw-r--r-- | src/core/CL/CLKernelLibrary.cpp | 5 | ||||
-rw-r--r-- | src/core/CL/cl_kernels/strided_slice.cl | 85 | ||||
-rw-r--r-- | src/core/CL/kernels/CLStridedSliceKernel.cpp | 189 | ||||
-rw-r--r-- | src/core/utils/helpers/tensor_transform.cpp | 142 | ||||
-rw-r--r-- | src/runtime/CL/functions/CLStridedSlice.cpp | 47 | ||||
-rw-r--r-- | tests/datasets/StridedSliceDataset.h | 181 | ||||
-rw-r--r-- | tests/validation/CL/StridedSlice.cpp | 136 | ||||
-rw-r--r-- | tests/validation/fixtures/StridedSliceFixture.h | 119 | ||||
-rw-r--r-- | tests/validation/reference/StridedSlice.cpp | 92 | ||||
-rw-r--r-- | tests/validation/reference/StridedSlice.h | 45 |
17 files changed, 1319 insertions, 0 deletions
diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h index ea942bde5d..ff16394a8a 100644 --- a/arm_compute/core/CL/CLKernels.h +++ b/arm_compute/core/CL/CLKernels.h @@ -109,6 +109,7 @@ #include "arm_compute/core/CL/kernels/CLSobel5x5Kernel.h" #include "arm_compute/core/CL/kernels/CLSobel7x7Kernel.h" #include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h" +#include "arm_compute/core/CL/kernels/CLStridedSliceKernel.h" #include "arm_compute/core/CL/kernels/CLTableLookupKernel.h" #include "arm_compute/core/CL/kernels/CLThresholdKernel.h" #include "arm_compute/core/CL/kernels/CLTransposeKernel.h" diff --git a/arm_compute/core/CL/kernels/CLStridedSliceKernel.h b/arm_compute/core/CL/kernels/CLStridedSliceKernel.h new file mode 100644 index 0000000000..e104dcfdd7 --- /dev/null +++ b/arm_compute/core/CL/kernels/CLStridedSliceKernel.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CL_STRIDED_SLICE_KERNEL_H__ +#define __ARM_COMPUTE_CL_STRIDED_SLICE_KERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" +#include "arm_compute/core/Types.h" + +#include <cstdint> + +namespace arm_compute +{ +// Forward declarations +class ICLTensor; + +/** Interface for the kernel to perform tensor strided slicing */ +class CLStridedSliceKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLStridedSliceKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLStridedSliceKernel(const CLStridedSliceKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLStridedSliceKernel &operator=(const CLStridedSliceKernel &) = delete; + /** Allow instances of this class to be moved */ + CLStridedSliceKernel(CLStridedSliceKernel &&) = default; + /** Allow instances of this class to be moved */ + CLStridedSliceKernel &operator=(CLStridedSliceKernel &&) = default; + /** Default destructor */ + ~CLStridedSliceKernel() = default; + /** Configure kernel + * + * @note Supported tensor rank: up to 4 + * + * @param[in] input Source tensor. Data type supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[out] output Destination tensor. Data type supported: Same as @p input + * @param[in] starts The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] ends The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] strides The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] begin_mask If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] end_mask If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] shrink_axis_mask If 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. + */ + void configure(const ICLTensor *input, ICLTensor *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 @ref CLStridedSliceKernel + * + * @note Supported tensor rank: up to 4 + * + * @param[in] input Source tensor. Data type supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] output Destination tensor. Data type supported: Same as @p input + * @param[in] starts The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] ends The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] strides The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] begin_mask If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] end_mask If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] shrink_axis_mask If 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. + */ + 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); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_input; /**< Source tensor */ + ICLTensor *_output; /**< Destination tensor */ +}; +} // namespace arm_compute +#endif /*__ARM_COMPUTE_CL_STRIDED_SLICE_KERNEL_H__ */ diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 37a8850237..31f2c88278 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -45,6 +45,8 @@ using half = half_float::half; /** Permutation vector */ using PermutationVector = Strides; +/** Bidirectional strides */ +using BiStrides = Coordinates; /** Image colour formats */ enum class Format diff --git a/arm_compute/core/utils/helpers/tensor_transform.h b/arm_compute/core/utils/helpers/tensor_transform.h new file mode 100644 index 0000000000..e5b32a5372 --- /dev/null +++ b/arm_compute/core/utils/helpers/tensor_transform.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H__ +#define __ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H__ + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace helpers +{ +namespace tensor_transform +{ +/** Returns the absolute start coordinates of strided slice + * + * @param[in] input_shape Input tensor shape + * @param[in] starts Start coordinates + * @param[in] strides Slice strides + * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, begin[i] is ignored and + * the fullest possible range in that dimension is used instead. + * + * @return Absolute start coordinates + */ +Coordinates strided_slice_absolute_start_coords(TensorShape input_shape, Coordinates starts, Coordinates strides, int32_t begin_mask = 0); + +/** Returns the absolute ends coordinates of strided slice + * + * @param[in] input_shape Input tensor shape + * @param[in] starts_abs Absolute start coordinates + * @param[in] ends End coordinates + * @param[in] strides Slice strides + * @param[in] end_mask (Optional) If the ith bit of end_mask is set, end[i] is ignored and + * the fullest possible range in that dimension is used instead. + * @param[in] shrink_axis_mask (Optional) If 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 begin[i] in the dimension must be preserved. + * + * @return Absolute end coordinates + */ +Coordinates strided_slice_absolute_end_coords(TensorShape input_shape, Coordinates starts_abs, Coordinates ends, Coordinates strides, + int32_t end_mask = 0, int32_t shrink_axis_mask = 0); +/** Returns the final strides of strided slice + * + * @param[in] input_shape Input tensor shape + * @param[in] strides Slice strides + * + * @return The final strides need by strided slice + */ +Coordinates strided_slice_strides(TensorShape input_shape, Coordinates strides); + +/** Computes output shape of a strided slice + * + * @param[in] input_shape Input tensor shape + * @param[in] starts_abs Absolute start coordinates + * @param[in] ends_abs Absolute end coordinates + * @param[in] final_strides Slice strides + * + * @return The output tensor shape + */ +TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts_abs, Coordinates ends_abs, Coordinates final_strides); +} // namespace tensor_tranform +} // namespace helpers +} // namespace arm_compute +#endif /* __ARM_COMPUTE_UTILS_HELPERS_TENSOR_TRANSFORM_H__ */ diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index 09f558d8b0..e0b6ce639f 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -28,6 +28,8 @@ #include "arm_compute/core/ITensorInfo.h" #include "arm_compute/core/Utils.h" +#include "arm_compute/core/utils/helpers/tensor_transform.h" + #include <cmath> namespace arm_compute @@ -432,6 +434,22 @@ inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo return output_shape; } +inline 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) +{ + using namespace arm_compute::helpers::tensor_transform; + + const TensorShape &input_shape = input.tensor_shape(); + + // Get actual start, end coordinates and strides + const Coordinates final_strides = strided_slice_strides(input_shape, strides); + const Coordinates starts_abs = strided_slice_absolute_start_coords(input_shape, starts, final_strides, begin_mask); + const Coordinates ends_abs = strided_slice_absolute_end_coords(input_shape, starts_abs, ends, final_strides, end_mask, shrink_axis_mask); + + return compute_strided_slice_output_shape(input_shape, starts_abs, ends_abs, final_strides); +} + template <typename T> inline TensorShape extract_shape(T *data) { diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index 5e42715c2f..f81a1babf9 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -109,6 +109,7 @@ #include "arm_compute/runtime/CL/functions/CLSobel5x5.h" #include "arm_compute/runtime/CL/functions/CLSobel7x7.h" #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h" +#include "arm_compute/runtime/CL/functions/CLStridedSlice.h" #include "arm_compute/runtime/CL/functions/CLTableLookup.h" #include "arm_compute/runtime/CL/functions/CLThreshold.h" #include "arm_compute/runtime/CL/functions/CLTranspose.h" diff --git a/arm_compute/runtime/CL/functions/CLStridedSlice.h b/arm_compute/runtime/CL/functions/CLStridedSlice.h new file mode 100644 index 0000000000..4a336f6fdc --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLStridedSlice.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CL_STRIDED_SLICE_H__ +#define __ARM_COMPUTE_CL_STRIDED_SLICE_H__ + +#include "arm_compute/runtime/CL/ICLSimpleFunction.h" + +namespace arm_compute +{ +// Forward Declarations +class ICLTensor; + +/** Basic function to run @ref CLStridedSliceKernel */ +class CLStridedSlice : public ICLSimpleFunction +{ +public: + /** Configure kernel + * + * @note Supported tensor rank: up to 4 + * + * @param[in] input Source tensor. Data type supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[out] output Destination tensor. Data type supported: Same as @p input + * @param[in] starts The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] ends The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] strides The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] end_mask (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] shrink_axis_mask (Optional) If 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. + */ + void configure(const ICLTensor *input, ICLTensor *output, + const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, + int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0); + + /** Static function to check if given info will lead to a valid configuration of @ref CLStridedSlice + * + * @note Supported tensor rank: up to 4 + * + * @param[in] input Source tensor. Data type supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] output Destination tensor. Data type supported: Same as @p input + * @param[in] starts The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] ends The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] strides The strides of the dimensions of the input tensor to be sliced. The length must be of rank(input). + * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] end_mask (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead. + * @param[in] shrink_axis_mask (Optional) If 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. + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, + const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, + int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CL_STRIDED_SLICE_H__ */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 3c92257642..6f45756c12 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -359,6 +359,7 @@ const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map = { "softmax_layer_max_shift_exp_sum_quantized_parallel", "softmax_layer_quantized.cl" }, { "softmax_layer_max_shift_exp_sum_serial", "softmax_layer.cl" }, { "softmax_layer_max_shift_exp_sum_parallel", "softmax_layer.cl" }, + { "strided_slice", "strided_slice.cl" }, { "suppress_non_maximum", "canny.cl" }, { "tablelookup_U8", "tablelookup.cl" }, { "tablelookup_S16", "tablelookup.cl" }, @@ -735,6 +736,10 @@ const std::map<std::string, std::string> CLKernelLibrary::_program_source_map = #include "./cl_kernels/softmax_layer_quantized.clembed" }, { + "strided_slice.cl", +#include "./cl_kernels/strided_slice.clembed" + }, + { "tablelookup.cl", #include "./cl_kernels/tablelookup.clembed" }, diff --git a/src/core/CL/cl_kernels/strided_slice.cl b/src/core/CL/cl_kernels/strided_slice.cl new file mode 100644 index 0000000000..7c68fb9a07 --- /dev/null +++ b/src/core/CL/cl_kernels/strided_slice.cl @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "helpers.h" + +/** Perform a strided slice operation on a given input. + * + * @attention Supported tensor rank: up to 4 + * + * @attention Data type can be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float + * @attention Input and output tensor dephts should be given as a preprocessor arguments using -DSRC_DEPTH=size. and -DDST_DEPTH=size + * @attention Absolute start coordinates for each dimension should be given as preprocessor -DSTART_index=value e.g. -DSTART_0=2 + * @attention Strides for each dimension should be given as preprocessor -DSTRIDE_index=value e.g. -DSTRIDE_1=1 + * + * @param[in] input_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 + * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes) + * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes) + * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr + * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) + * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] output_stride_w Stride of the destination tensor in W dimension (in bytes) + * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes) + * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor + */ +__kernel void strided_slice( + TENSOR4D_DECLARATION(input), + TENSOR4D_DECLARATION(output)) +{ + // Get pixels pointer + Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, SRC_DEPTH); + Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DST_DEPTH); + + int offset_0 = 0; + int offset_1 = 0; + int offset_2 = 0; + int offset_3 = 0; + + // Calculate offset +#if defined(START_0) && defined(STRIDE_0) + offset_0 = (int)START_0 + (int)get_global_id(0) * (int)STRIDE_0; +#endif // defined(START_0) && defined(STRIDE_0) +#if defined(START_1) && defined(STRIDE_1) + offset_1 = (int)START_1 + (int)get_global_id(1) * (int)STRIDE_1; +#endif // defined(START_1) && defined(STRIDE_1) +#if defined(START_2) && defined(STRIDE_2) + offset_2 = (int)START_2 + ((int)get_global_id(2) % (int)DST_DEPTH) * (int)STRIDE_2; +#endif // defined(START_2) && defined(STRIDE_2) +#if defined(START_3) && defined(STRIDE_3) + offset_3 = (int)START_3 + ((int)get_global_id(2) / (int)DST_DEPTH) * (int)STRIDE_3; +#endif // defined(START_2) && defined(STRIDE_2) + + // Store result + *((__global DATA_TYPE *)(output.ptr)) = *((__global DATA_TYPE *)tensor4D_offset(&input, offset_0, offset_1, offset_2, offset_3)); +} diff --git a/src/core/CL/kernels/CLStridedSliceKernel.cpp b/src/core/CL/kernels/CLStridedSliceKernel.cpp new file mode 100644 index 0000000000..f07436ac60 --- /dev/null +++ b/src/core/CL/kernels/CLStridedSliceKernel.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/CL/kernels/CLStridedSliceKernel.h" + +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/CLValidate.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/IAccessWindow.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Window.h" + +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/helpers/tensor_transform.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(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) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, + DataType::U8, DataType::S8, DataType::QASYMM8, + DataType::U16, DataType::S16, + DataType::U32, DataType::S32, + DataType::F16, DataType::F32); + + ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4); + ARM_COMPUTE_RETURN_ERROR_ON(starts.num_dimensions() > input->num_dimensions()); + ARM_COMPUTE_RETURN_ERROR_ON(ends.num_dimensions() > input->num_dimensions()); + ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions()); + for(unsigned int i = 0; i < strides.num_dimensions(); ++i) + { + ARM_COMPUTE_RETURN_ERROR_ON(strides[i] == 0); + } + + // Get expected output shape + const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input, + starts, ends, strides, + begin_mask, end_mask, shrink_axis_mask); + ARM_COMPUTE_RETURN_ERROR_ON(exp_output_shape.total_size() == 0); + + // Checks output if configured + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape() != exp_output_shape); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + } + + return Status{}; +} + +std::pair<Status, Window> validate_and_configure_window(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) +{ + // Output tensor auto initialization if not yet initialized + const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(*input, + starts, ends, strides, + begin_mask, end_mask, shrink_axis_mask); + auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape)); + + // Create window + const unsigned int num_elems_processed_per_iteration = 1; + + Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); + output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); + + return std::make_pair(Status{}, win); +} +} // namespace + +CLStridedSliceKernel::CLStridedSliceKernel() + : _input(nullptr), _output(nullptr) +{ +} + +void CLStridedSliceKernel::configure(const ICLTensor *input, ICLTensor *output, + const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); + + _input = input; + _output = output; + + const TensorShape &input_shape = input->info()->tensor_shape(); + + const Coordinates final_strides = arm_compute::helpers::tensor_transform::strided_slice_strides(input_shape, strides); + const Coordinates starts_abs = arm_compute::helpers::tensor_transform::strided_slice_absolute_start_coords(input_shape, starts, final_strides, begin_mask); + const Coordinates ends_abs = arm_compute::helpers::tensor_transform::strided_slice_absolute_end_coords(input_shape, starts_abs, ends, final_strides, end_mask, shrink_axis_mask); + + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), output->info(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + ICLKernel::configure_internal(win_config.second); + + // Create build options + CLBuildOptions build_opts; + build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); + for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) + { + build_opts.add_option("-DSTART_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(starts_abs[i])); + build_opts.add_option("-DSTRIDE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(final_strides[i])); + } + build_opts.add_option_if_else(input_shape.num_dimensions() > 2, + "-DSRC_DEPTH=" + support::cpp11::to_string(input_shape.z()), + "-DSRC_DEPTH=1"); + build_opts.add_option_if_else(_output->info()->num_dimensions() > 2, + "-DDST_DEPTH=" + support::cpp11::to_string(_output->info()->tensor_shape().z()), + "-DDST_DEPTH=1"); + + // Create kernel + _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("strided_slice", build_opts.options())); + + // Set config_id for enabling LWS tuning + _config_id = "strided_slice"; + _config_id += "_"; + _config_id += lower_string(string_from_data_type(input->info()->data_type())); + for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) + { + _config_id += "_"; + _config_id += support::cpp11::to_string(input->info()->dimension(i)); + _config_id += "_"; + _config_id += support::cpp11::to_string(starts_abs[i]); + _config_id += "_"; + _config_id += support::cpp11::to_string(ends_abs[i]); + _config_id += "_"; + _config_id += support::cpp11::to_string(final_strides[i]); + } +} + +Status CLStridedSliceKernel::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) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), + starts, ends, strides, begin_mask, end_mask, shrink_axis_mask) + .first); + + return Status{}; +} + +void CLStridedSliceKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + + Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); + Window slice = window_collapsed.first_slice_window_4D(); + + do + { + unsigned int idx = 0; + add_4D_tensor_argument(idx, _input, slice); + add_4D_tensor_argument(idx, _output, slice); + enqueue(queue, *this, slice, lws_hint()); + } + while(window_collapsed.slide_window_slice_4D(slice)); +} +} // namespace arm_compute diff --git a/src/core/utils/helpers/tensor_transform.cpp b/src/core/utils/helpers/tensor_transform.cpp new file mode 100644 index 0000000000..5c83a8bdb5 --- /dev/null +++ b/src/core/utils/helpers/tensor_transform.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/utils/helpers/tensor_transform.h" + +namespace arm_compute +{ +namespace helpers +{ +namespace tensor_transform +{ +Coordinates strided_slice_absolute_start_coords(TensorShape input_shape, Coordinates starts, Coordinates strides, int32_t begin_mask) +{ + Coordinates starts_abs; + for(unsigned int i = 0; i < starts.num_dimensions(); ++i) + { + // Get start index + int start_i = starts[i]; + + // Reset in case of begin mask present + if((begin_mask & 1 << i) != 0) + { + start_i = strides[i] > 0 ? std::numeric_limits<int>::lowest() : std::numeric_limits<int>::max(); + } + + // Account negative start points + const int dim_size = input_shape[i]; + if(start_i < 0) + { + start_i += dim_size; + } + + // Final clamp + start_i = utility::clamp(start_i, 0, dim_size - 1); + starts_abs.set(i, start_i); + } + + // Fill remaining + for(unsigned int i = starts_abs.num_dimensions(); i < input_shape.num_dimensions(); ++i) + { + starts_abs.set(i, 0); + } + + return starts_abs; +} + +Coordinates strided_slice_absolute_end_coords(TensorShape input_shape, Coordinates starts_abs, Coordinates ends, Coordinates strides, + int32_t end_mask, int32_t shrink_axis_mask) +{ + Coordinates ends_abs; + for(unsigned int i = 0; i < ends.num_dimensions(); ++i) + { + // Get end index + int stop_i = ends[i]; + + // Shrink dimension + if((shrink_axis_mask & (1 << i)) != 0) + { + stop_i = starts_abs[i] + 1; + } + + // Reset in case of begin mask present + if((end_mask & 1 << i) != 0) + { + stop_i = (strides[i] > 0) ? std::numeric_limits<int>::max() : std::numeric_limits<int>::lowest(); + } + + // Account negative end points + const int dim_size = input_shape[i]; + if(stop_i < 0) + { + stop_i += dim_size; + } + + // Final clamp + stop_i = (strides[i] > 0) ? utility::clamp(stop_i, 0, dim_size) : utility::clamp(stop_i, -1, dim_size - 1); + ends_abs.set(i, stop_i); + } + + // Fill remaining ends + for(unsigned int i = ends_abs.num_dimensions(); i < input_shape.num_dimensions(); ++i) + { + ends_abs.set(i, input_shape[i]); + } + + return ends_abs; +} + +Coordinates strided_slice_strides(TensorShape input_shape, Coordinates strides) +{ + for(unsigned int i = strides.num_dimensions(); i < input_shape.num_dimensions(); ++i) + { + strides.set(i, 1); + } + return strides; +} + +TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts_abs, Coordinates ends_abs, Coordinates final_strides) +{ + TensorShape output_shape = input_shape; + for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) + { + const int stride_i = final_strides[i]; + const int range = ends_abs[i] - starts_abs[i]; + if((range == 0) || // Zero range + (range < 0 && stride_i >= 0) || // Negative range with positive stride + (range > 0 && stride_i <= 0)) // Positive range with negative stride + { + output_shape.set(i, 0); + return output_shape; + } + else + { + int dim = range / stride_i + (range % stride_i != 0 ? 1 : 0); + output_shape.set(i, dim); + } + } + return output_shape; +} +} // namespace tensor_transform +} // namespace helpers +} // namespace arm_compute diff --git a/src/runtime/CL/functions/CLStridedSlice.cpp b/src/runtime/CL/functions/CLStridedSlice.cpp new file mode 100644 index 0000000000..e34f653652 --- /dev/null +++ b/src/runtime/CL/functions/CLStridedSlice.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLStridedSlice.h" + +#include "arm_compute/core/CL/kernels/CLStridedSliceKernel.h" +#include "arm_compute/core/Types.h" +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +void CLStridedSlice::configure(const ICLTensor *input, ICLTensor *output, + const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) +{ + auto k = arm_compute::support::cpp14::make_unique<CLStridedSliceKernel>(); + k->configure(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); + _kernel = std::move(k); +} + +Status CLStridedSlice::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) +{ + return CLStridedSliceKernel::validate(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); +} +} // namespace arm_compute diff --git a/tests/datasets/StridedSliceDataset.h b/tests/datasets/StridedSliceDataset.h new file mode 100644 index 0000000000..00f19920b8 --- /dev/null +++ b/tests/datasets/StridedSliceDataset.h @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_STRIDED_SLICE_DATASET +#define ARM_COMPUTE_TEST_STRIDED_SLICE_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class StridedSliceDataset +{ +public: + using type = std::tuple<TensorShape, Coordinates, Coordinates, BiStrides, int32_t, int32_t, int32_t>; + + struct iterator + { + iterator(std::vector<TensorShape>::const_iterator tensor_shapes_it, + std::vector<Coordinates>::const_iterator starts_values_it, + std::vector<Coordinates>::const_iterator ends_values_it, + std::vector<BiStrides>::const_iterator strides_values_it, + std::vector<int32_t>::const_iterator begin_mask_values_it, + std::vector<int32_t>::const_iterator end_mask_values_it, + std::vector<int32_t>::const_iterator shrink_mask_values_it) + : _tensor_shapes_it{ std::move(tensor_shapes_it) }, + _starts_values_it{ std::move(starts_values_it) }, + _ends_values_it{ std::move(ends_values_it) }, + _strides_values_it{ std::move(strides_values_it) }, + _begin_mask_values_it{ std::move(begin_mask_values_it) }, + _end_mask_values_it{ std::move(end_mask_values_it) }, + _shrink_mask_values_it{ std::move(shrink_mask_values_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "Shape=" << *_tensor_shapes_it << ":"; + description << "Starts=" << *_starts_values_it << ":"; + description << "Ends=" << *_ends_values_it << ":"; + description << "Strides=" << *_strides_values_it << ":"; + description << "BeginMask=" << *_begin_mask_values_it << ":"; + description << "EndMask=" << *_end_mask_values_it << ":"; + description << "ShrinkMask=" << *_shrink_mask_values_it << ":"; + return description.str(); + } + + StridedSliceDataset::type operator*() const + { + return std::make_tuple(*_tensor_shapes_it, + *_starts_values_it, *_ends_values_it, *_strides_values_it, + *_begin_mask_values_it, *_end_mask_values_it, *_shrink_mask_values_it); + } + + iterator &operator++() + { + ++_tensor_shapes_it; + ++_starts_values_it; + ++_ends_values_it; + ++_strides_values_it; + ++_begin_mask_values_it; + ++_end_mask_values_it; + ++_shrink_mask_values_it; + + return *this; + } + + private: + std::vector<TensorShape>::const_iterator _tensor_shapes_it; + std::vector<Coordinates>::const_iterator _starts_values_it; + std::vector<Coordinates>::const_iterator _ends_values_it; + std::vector<BiStrides>::const_iterator _strides_values_it; + std::vector<int32_t>::const_iterator _begin_mask_values_it; + std::vector<int32_t>::const_iterator _end_mask_values_it; + std::vector<int32_t>::const_iterator _shrink_mask_values_it; + }; + + iterator begin() const + { + return iterator(_tensor_shapes.begin(), + _starts_values.begin(), _ends_values.begin(), _strides_values.begin(), + _begin_mask_values.begin(), _end_mask_values.begin(), _shrink_mask_values.begin()); + } + + int size() const + { + return std::min(_tensor_shapes.size(), std::min(_starts_values.size(), std::min(_ends_values.size(), _strides_values.size()))); + } + + void add_config(TensorShape shape, + Coordinates starts, Coordinates ends, BiStrides strides, + int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_mask = 0) + { + _tensor_shapes.emplace_back(std::move(shape)); + _starts_values.emplace_back(std::move(starts)); + _ends_values.emplace_back(std::move(ends)); + _strides_values.emplace_back(std::move(strides)); + _begin_mask_values.emplace_back(std::move(begin_mask)); + _end_mask_values.emplace_back(std::move(end_mask)); + _shrink_mask_values.emplace_back(std::move(shrink_mask)); + } + +protected: + StridedSliceDataset() = default; + StridedSliceDataset(StridedSliceDataset &&) = default; + +private: + std::vector<TensorShape> _tensor_shapes{}; + std::vector<Coordinates> _starts_values{}; + std::vector<Coordinates> _ends_values{}; + std::vector<BiStrides> _strides_values{}; + std::vector<int32_t> _begin_mask_values{}; + std::vector<int32_t> _end_mask_values{}; + std::vector<int32_t> _shrink_mask_values{}; +}; + +class SmallStridedSliceDataset final : public StridedSliceDataset +{ +public: + SmallStridedSliceDataset() + { + // 1D + add_config(TensorShape(15U), Coordinates(0), Coordinates(5), BiStrides(2)); + add_config(TensorShape(15U), Coordinates(-1), Coordinates(-8), BiStrides(-2)); + // 2D + add_config(TensorShape(15U, 16U), Coordinates(0, 1), Coordinates(5, -1), BiStrides(2, 1)); + add_config(TensorShape(15U, 16U), Coordinates(4, 1), Coordinates(12, -1), BiStrides(2, 1), 1); + // 3D + add_config(TensorShape(15U, 16U, 4U), Coordinates(0, 1, 2), Coordinates(5, -1, 4), BiStrides(2, 1, 2)); + add_config(TensorShape(15U, 16U, 4U), Coordinates(0, 1, 2), Coordinates(5, -1, 4), BiStrides(2, 1, 2), 0, 1); + // 4D + add_config(TensorShape(15U, 16U, 4U, 12U), Coordinates(0, 1, 2, 2), Coordinates(5, -1, 4, 5), BiStrides(2, 1, 2, 3)); + } +}; + +class LargeStridedSliceDataset final : public StridedSliceDataset +{ +public: + LargeStridedSliceDataset() + { + // 1D + add_config(TensorShape(1025U), Coordinates(128), Coordinates(-100), BiStrides(20)); + // 2D + add_config(TensorShape(372U, 68U), Coordinates(128U, 7U), Coordinates(368U, -30), BiStrides(10, 7)); + // 3D + add_config(TensorShape(372U, 68U, 12U), Coordinates(128U, 7U, -1), Coordinates(368U, -30, -5), BiStrides(14, 7, -2)); + // 4D + add_config(TensorShape(372U, 68U, 7U, 4U), Coordinates(128U, 7U, 2U), Coordinates(368U, -30, 5U), BiStrides(20, 7, 2), 1, 1); + } +}; + +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_STRIDED_SLICE_DATASET */ diff --git a/tests/validation/CL/StridedSlice.cpp b/tests/validation/CL/StridedSlice.cpp new file mode 100644 index 0000000000..824f7b1cf3 --- /dev/null +++ b/tests/validation/CL/StridedSlice.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2017-2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLStridedSlice.h" + +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/StridedSliceDataset.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/StridedSliceFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(CL) +TEST_SUITE(StridedSlice) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 3U, 2U, 5U, 3U), 1, DataType::F32), // Invalid input shape + TensorInfo(TensorShape(27U, 3U, 2U), 1, DataType::F32), // Zero stride + TensorInfo(TensorShape(27U, 3U, 2U), 1, DataType::F32), // Big number of coordinates + TensorInfo(TensorShape(27U, 3U, 2U), 1, DataType::F32), // Invalid Coords/Strides + TensorInfo(TensorShape(27U, 3U, 2U), 1, DataType::F32) + }), + framework::dataset::make("Starts", { Coordinates(3, 1, 0), Coordinates(3, 1, 0), Coordinates(3, 1, 0), Coordinates(3, 1, 0), Coordinates(3, 1, 0) })), + framework::dataset::make("Ends", { Coordinates(13, 3, 0), Coordinates(13, 3, 1), Coordinates(13, 3, 1, 1), Coordinates(13, -1, 1), Coordinates(13, 3, 1) })), + framework::dataset::make("Strides", { BiStrides(2, 1, 1), BiStrides(2, 0, 1), BiStrides(2, 1, 1), BiStrides(2, -1, 1), BiStrides(2, 1, 1) })), + framework::dataset::make("Expected", { false, false, false, false, true })), + input_info, starts, ends, strides, expected) +{ + TensorInfo output_info; + ARM_COMPUTE_EXPECT(bool(CLStridedSlice::validate(&input_info.clone()->set_is_resizable(false), &output_info, starts, ends, strides)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +DATA_TEST_CASE(Configuration, + framework::DatasetMode::ALL, + combine(arm_compute::test::datasets::SmallStridedSliceDataset(), framework::dataset::make("DataType", { DataType::F16, DataType::F32 })), + shape, starts, ends, strides, begin_mask, end_mask, shrink_mask, data_type) +{ + // Create tensors + CLTensor src = create_tensor<CLTensor>(shape, data_type); + CLTensor dst; + + // Create and Configure function + CLStridedSlice strided_slice; + strided_slice.configure(&src, &dst, starts, ends, strides, begin_mask, end_mask, shrink_mask); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(dst.info()->tensor_shape()); + validate(dst.info()->valid_region(), valid_region); +} + +template <typename T> +using CLStridedSliceFixture = StridedSliceFixture<CLTensor, CLAccessor, CLStridedSlice, T>; + +TEST_SUITE(Float) +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, + CLStridedSliceFixture<half>, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallStridedSliceDataset(), framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, + CLStridedSliceFixture<half>, + framework::DatasetMode::NIGHTLY, + combine(datasets::LargeStridedSliceDataset(), framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // FP16 + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, + CLStridedSliceFixture<float>, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallStridedSliceDataset(), framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, + CLStridedSliceFixture<float>, + framework::DatasetMode::NIGHTLY, + combine(datasets::LargeStridedSliceDataset(), framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // FP32 +TEST_SUITE_END() // Float + +TEST_SUITE_END() // StridedSlice +TEST_SUITE_END() // CL + +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/StridedSliceFixture.h b/tests/validation/fixtures/StridedSliceFixture.h new file mode 100644 index 0000000000..26442ad834 --- /dev/null +++ b/tests/validation/fixtures/StridedSliceFixture.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_STRIDED_SLICE_FIXTURE +#define ARM_COMPUTE_TEST_STRIDED_SLICE_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/RawLutAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/StridedSlice.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> +class StridedSliceFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape shape, + Coordinates starts, Coordinates ends, BiStrides strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_mask, + DataType data_type) + { + _target = compute_target(shape, starts, ends, strides, begin_mask, end_mask, shrink_mask, data_type); + _reference = compute_reference(shape, starts, ends, strides, begin_mask, end_mask, shrink_mask, data_type); + } + +protected: + template <typename U> + void fill(U &&tensor, int i) + { + library->fill_tensor_uniform(tensor, i); + } + + TensorType compute_target(const TensorShape &shape, + const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_mask, + DataType data_type) + { + // Create tensors + TensorType src = create_tensor<TensorType>(shape, data_type); + TensorType dst; + + // Create and configure function + FunctionType strided_slice; + strided_slice.configure(&src, &dst, starts, ends, strides, begin_mask, end_mask, shrink_mask); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), 0); + fill(AccessorType(dst), 1); + + // Compute function + strided_slice.run(); + + return dst; + } + + SimpleTensor<T> compute_reference(const TensorShape &shape, + const Coordinates &starts, const Coordinates &ends, const BiStrides &strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_mask, + DataType data_type) + { + // Create reference + SimpleTensor<T> src{ shape, data_type }; + + // Fill reference + fill(src, 0); + + return reference::strided_slice(src, starts, ends, strides, begin_mask, end_mask, shrink_mask); + } + + TensorType _target{}; + SimpleTensor<T> _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_STRIDED_SLICE_FIXTURE */ diff --git a/tests/validation/reference/StridedSlice.cpp b/tests/validation/reference/StridedSlice.cpp new file mode 100644 index 0000000000..ac02ce1e71 --- /dev/null +++ b/tests/validation/reference/StridedSlice.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "StridedSlice.h" + +#include "arm_compute/core/utils/helpers/tensor_transform.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +SimpleTensor<T> strided_slice(const SimpleTensor<T> &src, + Coordinates starts, Coordinates ends, BiStrides strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) +{ + using namespace arm_compute::helpers::tensor_transform; + + // Validation checks + ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4); + ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions()); + ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions()); + ARM_COMPUTE_ERROR_ON(strides.num_dimensions() > src.shape().num_dimensions()); + for(unsigned int i = 0; i < strides.num_dimensions(); ++i) + { + ARM_COMPUTE_ERROR_ON(strides[i] == 0); + } + + // Get source shape + const TensorShape &src_shape = src.shape(); + + // Get actual start, end coordinates and strides + const Coordinates final_strides = strided_slice_strides(src_shape, strides); + const Coordinates starts_abs = strided_slice_absolute_start_coords(src_shape, starts, final_strides, begin_mask); + const Coordinates ends_abs = strided_slice_absolute_end_coords(src_shape, starts_abs, ends, final_strides, end_mask, shrink_axis_mask); + + // Get destination shape + const TensorShape dst_shape = compute_strided_slice_output_shape(src_shape, starts_abs, ends_abs, final_strides); + + // Create destination tensor + SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 }; + + // Perform strided slice + Window win; + win.use_tensor_dimensions(dst_shape); + execute_window_loop(win, [&](const Coordinates & id) + { + Coordinates offset; + for(unsigned int i = 0; i < id.num_dimensions(); ++i) + { + offset.set(i, starts_abs[i] + id[i] * final_strides[i]); + } + *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset)); + }); + + return dst; +} + +template SimpleTensor<float> strided_slice(const SimpleTensor<float> &src, + Coordinates starts, Coordinates ends, BiStrides strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask); +template SimpleTensor<half_float::half> strided_slice(const SimpleTensor<half_float::half> &src, + Coordinates starts, Coordinates ends, BiStrides strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/StridedSlice.h b/tests/validation/reference/StridedSlice.h new file mode 100644 index 0000000000..805b7e65da --- /dev/null +++ b/tests/validation/reference/StridedSlice.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_STRIDED_SLICE_H__ +#define __ARM_COMPUTE_TEST_STRIDED_SLICE_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +SimpleTensor<T> strided_slice(const SimpleTensor<T> &src, + Coordinates starts, Coordinates ends, BiStrides strides, + int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_STRIDED_SLICE_H__ */ |