From 6d9d6f49aa05ad033676a219c1a98150fb8401f4 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Mon, 24 Dec 2018 16:10:47 +0000 Subject: COMPMID-1769: Add support for NEON StridedSlice,Split,Slice,Unstack Change-Id: I7d3c5e6858fed090410720f76947327e39bc72f8 Reviewed-on: https://review.mlplatform.org/450 Reviewed-by: Giuseppe Rossini Tested-by: Arm Jenkins Reviewed-by: Michalis Spyrou Reviewed-by: VidhyaSudhan Loganathan --- src/runtime/NEON/functions/NESlice.cpp | 63 ++++++++++++ src/runtime/NEON/functions/NESplit.cpp | 138 ++++++++++++++++++++++++++ src/runtime/NEON/functions/NEStridedSlice.cpp | 47 +++++++++ src/runtime/NEON/functions/NEUnstack.cpp | 119 ++++++++++++++++++++++ 4 files changed, 367 insertions(+) create mode 100644 src/runtime/NEON/functions/NESlice.cpp create mode 100644 src/runtime/NEON/functions/NESplit.cpp create mode 100644 src/runtime/NEON/functions/NEStridedSlice.cpp create mode 100644 src/runtime/NEON/functions/NEUnstack.cpp (limited to 'src/runtime') diff --git a/src/runtime/NEON/functions/NESlice.cpp b/src/runtime/NEON/functions/NESlice.cpp new file mode 100644 index 0000000000..03c2053b9b --- /dev/null +++ b/src/runtime/NEON/functions/NESlice.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018-2019 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/NEON/functions/NESlice.h" + +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/NEON/kernels/NEStridedSliceKernel.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/helpers/tensor_transform.h" + +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +void NESlice::configure(const ITensor *input, ITensor *output, const Coordinates &starts, const Coordinates &ends) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input); + + // Get absolute end coordinates + const int32_t slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(ends); + + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(input, output, starts, ends, BiStrides(), 0, slice_end_mask, 0); + _kernel = std::move(k); +} + +Status NESlice::validate(const ITensorInfo *input, const ITensorInfo *output, const Coordinates &starts, const Coordinates &ends) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); + + // Check start dimensions for being non-negative + ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i) + { + return i < 0; + })); + + // Get absolute end coordinates + const int32_t slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(ends); + + return NEStridedSliceKernel::validate(input, output, starts, ends, BiStrides(), 0, slice_end_mask, 0); +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NESplit.cpp b/src/runtime/NEON/functions/NESplit.cpp new file mode 100644 index 0000000000..e947657934 --- /dev/null +++ b/src/runtime/NEON/functions/NESplit.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2018-2019 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/NEON/functions/NESplit.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" + +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +NESplit::NESplit() + : _outputs_vector(), _slice_functions(), _num_outputs(0) +{ +} + +void NESplit::configure(const ITensor *input, const std::vector &outputs, unsigned int axis) +{ + // Create Slice functions + _num_outputs = outputs.size(); + _slice_functions = arm_compute::support::cpp14::make_unique(_num_outputs); + + // Get output shape + const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_split_shape(input->info(), axis, _num_outputs); + + // Extract output tensor info + std::vector outputs_info; + for(auto &output : outputs) + { + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + outputs_info.emplace_back(output->info()); + } + + // Validate + ARM_COMPUTE_ERROR_THROW_ON(NESplit::validate(input->info(), outputs_info, axis)); + + const size_t axis_split_step = output_shape[axis]; + unsigned int axis_offset = 0; + + // Start/End coordinates + Coordinates start_coords; + Coordinates end_coords; + for(unsigned int d = 0; d < output_shape.num_dimensions(); ++d) + { + end_coords.set(d, -1); + } + + for(unsigned int i = 0; i < _num_outputs; i++) + { + // Update coordinate on axis + start_coords.set(axis, axis_offset); + end_coords.set(axis, axis_offset + axis_split_step); + + // Configure slice function + _slice_functions[i].configure(input, outputs[i], start_coords, end_coords); + + // Set valid region from shape + outputs[i]->info()->set_valid_region(ValidRegion(Coordinates(), output_shape)); + + // Update axis offset + axis_offset += axis_split_step; + } +} + +Status NESplit::validate(const ITensorInfo *input, const std::vector &outputs, unsigned int axis) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); + ARM_COMPUTE_RETURN_ERROR_ON(axis >= input->num_dimensions()); + ARM_COMPUTE_RETURN_ERROR_ON(outputs.size() < 2); + + // Get output shape + const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_split_shape(input, axis, outputs.size()); + ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() == 0); + + const size_t axis_split_step = output_shape[axis]; + unsigned int axis_offset = 0; + + // Start/End coordinates + Coordinates start_coords; + Coordinates end_coords; + for(unsigned int d = 0; d < output_shape.num_dimensions(); ++d) + { + end_coords.set(d, -1); + } + + // Validate output tensors + for(const auto &output : outputs) + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); + + // Output auto inizialitation if not yet initialized + TensorInfo tmp_output_info = *output->clone(); + auto_init_if_empty(tmp_output_info, input->clone()->set_is_resizable(true).set_tensor_shape(output_shape)); + + // Update coordinate on axis + start_coords.set(axis, axis_offset); + end_coords.set(axis, axis_offset + axis_split_step); + + ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(input, output, start_coords, end_coords)); + axis_offset += axis_split_step; + } + + return Status{}; +} + +void NESplit::run() +{ + for(unsigned i = 0; i < _num_outputs; ++i) + { + _slice_functions[i].run(); + } +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEStridedSlice.cpp b/src/runtime/NEON/functions/NEStridedSlice.cpp new file mode 100644 index 0000000000..53eb2b03b0 --- /dev/null +++ b/src/runtime/NEON/functions/NEStridedSlice.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018-2019 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/NEON/functions/NEStridedSlice.h" + +#include "arm_compute/core/NEON/kernels/NEStridedSliceKernel.h" +#include "arm_compute/core/Types.h" +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +void NEStridedSlice::configure(const ITensor *input, ITensor *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(); + k->configure(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); + _kernel = std::move(k); +} + +Status NEStridedSlice::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 NEStridedSliceKernel::validate(input, output, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEUnstack.cpp b/src/runtime/NEON/functions/NEUnstack.cpp new file mode 100644 index 0000000000..7532020973 --- /dev/null +++ b/src/runtime/NEON/functions/NEUnstack.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2018-2019 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/NEON/functions/NEUnstack.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" + +namespace arm_compute +{ +namespace +{ +inline unsigned int wrap_axis(int axis, const ITensorInfo *const tensor) +{ + return wrap_around(axis, static_cast(tensor->num_dimensions())); +} + +inline void setup_slice_coordinates_and_mask(Coordinates &slice_start, int32_t &slice_end_mask, const unsigned int input_num_dimensions) +{ + // Setups up coordinates to slice the input tensor: start coordinates to all 0s and the unstacking axis of both Start/End to slice just one 2d tensor at a time. + Coordinates slice_end; + slice_start.set_num_dimensions(input_num_dimensions); + slice_end.set_num_dimensions(input_num_dimensions); + for(size_t k = 0; k < input_num_dimensions; ++k) + { + slice_start.set(k, 0); + slice_end.set(k, -1); + } + slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(slice_end); +} +} // namespace + +NEUnstack::NEUnstack() // NOLINT + : _num_slices(0), + _strided_slice_vector() +{ +} + +void NEUnstack::configure(const ITensor *input, const std::vector &output_vector, int axis) +{ + std::vector outputs_vector_info(output_vector.size()); + std::transform(output_vector.begin(), output_vector.end(), outputs_vector_info.begin(), [](ITensor * t) + { + ARM_COMPUTE_ERROR_ON_NULLPTR(t); + return t->info(); + }); + + ARM_COMPUTE_ERROR_ON_NULLPTR(input); + ARM_COMPUTE_ERROR_THROW_ON(NEUnstack::validate(input->info(), outputs_vector_info, axis)); + + // Wrap around negative values + const unsigned int axis_u = wrap_axis(axis, input->info()); + _num_slices = std::min(outputs_vector_info.size(), input->info()->dimension(axis_u)); + _strided_slice_vector = arm_compute::support::cpp14::make_unique(_num_slices); + + Coordinates slice_start; + int32_t slice_end_mask; + setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->info()->tensor_shape().num_dimensions()); + for(unsigned int slice = 0; slice < _num_slices; ++slice) + { + // Adjusts start and end coordinates to take a 2D slice at a time + slice_start.set(axis_u, slice); + _strided_slice_vector[slice].configure(input, output_vector[slice], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << axis_u)); + } +} + +Status NEUnstack::validate(const ITensorInfo *input, const std::vector &output_vector, int axis) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); + ARM_COMPUTE_RETURN_ERROR_ON(output_vector.empty()); + ARM_COMPUTE_RETURN_ERROR_ON(axis < (-static_cast(input->tensor_shape().num_dimensions()))); + ARM_COMPUTE_RETURN_ERROR_ON(axis >= static_cast(input->tensor_shape().num_dimensions())); + + const unsigned int num_slices = std::min(output_vector.size(), input->dimension(wrap_axis(axis, input))); + ARM_COMPUTE_RETURN_ERROR_ON(num_slices > input->dimension(wrap_axis(axis, input))); + ARM_COMPUTE_RETURN_ERROR_ON(num_slices > output_vector.size()); + + Coordinates slice_start; + int32_t slice_end_mask; + for(size_t k = 0; k < num_slices; ++k) + { + slice_start.set(wrap_axis(axis, input), k); + setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->tensor_shape().num_dimensions()); + ARM_COMPUTE_RETURN_ON_ERROR(NEStridedSlice::validate(input, output_vector[k], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << wrap_axis(axis, input)))); + } + return Status{}; +} + +void NEUnstack::run() +{ + for(unsigned i = 0; i < _num_slices; ++i) + { + _strided_slice_vector[i].run(); + } +} +} // namespace arm_compute -- cgit v1.2.1