diff options
-rw-r--r-- | arm_compute/core/utils/misc/ShapeCalculator.h | 7 | ||||
-rw-r--r-- | arm_compute/runtime/CL/CLFunctions.h | 1 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLUnstack.h | 76 | ||||
-rw-r--r-- | src/runtime/CL/functions/CLUnstack.cpp | 120 | ||||
-rw-r--r-- | tests/datasets/ShapeDatasets.h | 2 | ||||
-rw-r--r-- | tests/validation/CL/Unstack.cpp | 126 | ||||
-rw-r--r-- | tests/validation/fixtures/UnstackFixture.h | 118 | ||||
-rw-r--r-- | tests/validation/reference/Unstack.cpp | 108 | ||||
-rw-r--r-- | tests/validation/reference/Unstack.h | 46 |
9 files changed, 604 insertions, 0 deletions
diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index adf5309ea5..4756ff4f97 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -803,6 +803,13 @@ inline TensorShape extract_shape(const TensorShape *data) return *data; } +inline TensorShape calculate_unstack_shape(TensorShape input_shape, unsigned int axis) +{ + ARM_COMPUTE_ERROR_ON(axis > input_shape.num_dimensions()); + input_shape.remove_dimension(axis); + return input_shape; +} + template <typename T> inline TensorShape calculate_depth_concatenate_shape(const std::vector<T *> &inputs_vector) { diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index 303a8989a9..e0cf65b923 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -134,6 +134,7 @@ #include "arm_compute/runtime/CL/functions/CLThreshold.h" #include "arm_compute/runtime/CL/functions/CLTile.h" #include "arm_compute/runtime/CL/functions/CLTranspose.h" +#include "arm_compute/runtime/CL/functions/CLUnstack.h" #include "arm_compute/runtime/CL/functions/CLUpsampleLayer.h" #include "arm_compute/runtime/CL/functions/CLWarpAffine.h" #include "arm_compute/runtime/CL/functions/CLWarpPerspective.h" diff --git a/arm_compute/runtime/CL/functions/CLUnstack.h b/arm_compute/runtime/CL/functions/CLUnstack.h new file mode 100644 index 0000000000..feed430c58 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLUnstack.h @@ -0,0 +1,76 @@ +/* + * 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_CLUNSTACK_H__ +#define __ARM_COMPUTE_CLUNSTACK_H__ + +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/IFunction.h" + +#include "arm_compute/runtime/CL/functions/CLStridedSlice.h" + +#include <memory> + +namespace arm_compute +{ +class ICLTensor; + +/** Basic function to unpack a rank-R tensor into rank-(R-1) tensors. This function calls the following functions: + * + * -# @ref CLStridedSlice + * + */ +class CLUnstack : public IFunction +{ +public: + /** Default constructor */ + CLUnstack(); + /** Set the input, output and unstacking axis. + * + * @param[in] input A tensor to be unstacked. Data type supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in,out] output_vector A vector of tensors. Data types supported: Same as @p input. + * Note: The number of elements of the vector will be used as the number of slices to be taken from the axis. + * @param[in] axis The axis to unstack along. Valid values are [-R,R) where R is the input's rank. Negative values wrap around. + * + */ + void configure(const ICLTensor *input, const std::vector<ICLTensor *> &output_vector, int axis); + /** Static function to check if given info will lead to a valid configuration of @ref CLUnstack + * + * @param[in] input Input tensor info. Data type supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] output_vector Vector of output tensors' info. Data types supported: Same as @p input. + * @param[in] axis The axis to unstack along. Valid values are [-R,R) where R is the input's rank. Negative values wrap around. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &output_vector, int axis); + + // Inherited methods overridden: + void run() override; + +private: + unsigned int _num_slices; + std::unique_ptr<CLStridedSlice[]> _strided_slice_vector; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLUNSTACK_H__ */ diff --git a/src/runtime/CL/functions/CLUnstack.cpp b/src/runtime/CL/functions/CLUnstack.cpp new file mode 100644 index 0000000000..2c8a310043 --- /dev/null +++ b/src/runtime/CL/functions/CLUnstack.cpp @@ -0,0 +1,120 @@ +/* + * 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/CLUnstack.h" + +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Error.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<int>(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); +} + +inline const std::vector<ITensorInfo *> get_tensor_info(const std::vector<ICLTensor *> &output_vector) +{ + // given a vector of tensors this function returns a vector to their ITensorInfo* + std::vector<ITensorInfo *> out_info(output_vector.size()); + std::transform(output_vector.begin(), output_vector.end(), out_info.begin(), [](ICLTensor * t) + { + return t->info(); + }); + return out_info; +} +} // namespace + +CLUnstack::CLUnstack() // NOLINT + : _num_slices(0), + _strided_slice_vector() +{ +} + +void CLUnstack::configure(const ICLTensor *input, const std::vector<ICLTensor *> &output_vector, int axis) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input); + ARM_COMPUTE_ERROR_THROW_ON(CLUnstack::validate(input->info(), get_tensor_info(output_vector), axis)); + // Wrap around negative values + const unsigned int axis_u = wrap_axis(axis, input->info()); + _num_slices = std::min(output_vector.size(), input->info()->dimension(axis_u)); + _strided_slice_vector = arm_compute::support::cpp14::make_unique<CLStridedSlice[]>(_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 CLUnstack::validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &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<int>(input->tensor_shape().num_dimensions()))); + ARM_COMPUTE_RETURN_ERROR_ON(axis >= static_cast<int>(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(CLStridedSlice::validate(input, output_vector[k], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << wrap_axis(axis, input)))); + } + return Status{}; +} + +void CLUnstack::run() +{ + for(unsigned i = 0; i < _num_slices; ++i) + { + _strided_slice_vector[i].run(); + } +} + +} // namespace arm_compute diff --git a/tests/datasets/ShapeDatasets.h b/tests/datasets/ShapeDatasets.h index 875f7e8ea1..629c47d54c 100644 --- a/tests/datasets/ShapeDatasets.h +++ b/tests/datasets/ShapeDatasets.h @@ -118,6 +118,8 @@ public: : ShapeDataset("Shape", { TensorShape{ 1U, 7U, 7U }, + TensorShape{ 2U, 5U, 4U }, + TensorShape{ 7U, 7U, 5U }, TensorShape{ 27U, 13U, 37U }, TensorShape{ 128U, 64U, 21U } diff --git a/tests/validation/CL/Unstack.cpp b/tests/validation/CL/Unstack.cpp new file mode 100644 index 0000000000..13b8872ce4 --- /dev/null +++ b/tests/validation/CL/Unstack.cpp @@ -0,0 +1,126 @@ +/* + * 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/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLUnstack.h" + +#include "tests/CL/CLAccessor.h" +#include "tests/datasets/ShapeDatasets.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/UnstackFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +const auto unstack_axis_dataset = framework::dataset::make("Axis", -3, 3); +const auto unstack_num_dataset = framework::dataset::make("Num", 1, 3); // The length of the dimension axis +const auto unstack_dataset_small = datasets::Small3DShapes() * unstack_axis_dataset * unstack_num_dataset; +} //namespace + +TEST_SUITE(CL) +TEST_SUITE(Unstack) + +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("InputInfo", +{ + TensorInfo(TensorShape(1U, 9U, 8U), 1, DataType::U8), // Passes, 1 slice on x axis + TensorInfo(TensorShape(1U, 2U, 3U), 1, DataType::U8), // fails because axis > input's rank + TensorInfo(TensorShape(1U, 2U, 3U), 1, DataType::S32), // fails axis < (- input's rank) + TensorInfo(TensorShape(3U, 7U, 5U), 1, DataType::S32), // passes, 3 slices along X + TensorInfo(TensorShape(13U, 7U, 5U), 1, DataType::S16), // fails, too few output slices + TensorInfo(TensorShape(1U, 2U, 3U), 1, DataType::U8), // fails mismatching data types +}), +framework::dataset::make("OutputInfo", +{ + std::vector<TensorInfo>{ TensorInfo(TensorShape(9U, 8U), 1, DataType::U8) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(2U, 3U), 1, DataType::U8) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(2U, 3U), 1, DataType::S32) }, + + std::vector<TensorInfo>{ TensorInfo(TensorShape(7U, 5U), 1, DataType::S32), TensorInfo(TensorShape(7U, 5U), 1, DataType::S32), TensorInfo(TensorShape(7U, 5U), 1, DataType::S32) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(7U, 5U), 1, DataType::S16) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(9U, 8U), 1, DataType::S32) }, +})), +framework::dataset::make("Axis", { -3, 3, -4, -3, 1, 1 })), +framework::dataset::make("Num", { 1, 1, 1, 1, 0, 1 })), +framework::dataset::make("Expected", { true, false, false, true, false, false })), +input_info, output_info, axis, num, expected) +{ + std::vector<TensorInfo> ti(output_info); + std::vector<ITensorInfo *> vec(num); + for(size_t j = 0; j < vec.size(); ++j) + { + vec[j] = &ti[j]; + } + ARM_COMPUTE_EXPECT(bool(CLUnstack::validate(&input_info.clone()->set_is_resizable(false), vec, axis)) == expected, framework::LogLevel::ERRORS); +} + +template <typename T> +using CLUnstackFixture = UnstackValidationFixture<CLTensor, ICLTensor, CLAccessor, CLUnstack, T>; + +TEST_SUITE(F32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture<float>, framework::DatasetMode::PRECOMMIT, unstack_dataset_small * framework::dataset::make("DataType", { DataType::F32 })) +{ + ARM_COMPUTE_ERROR_ON(_target.size() != _reference.size()); + // Validate output + for(size_t k = 0; k < _target.size(); ++k) + { + validate(CLAccessor(_target[k]), _reference[k]); + } +} +TEST_SUITE_END() // F32 + +TEST_SUITE(F16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture<half>, framework::DatasetMode::PRECOMMIT, unstack_dataset_small * framework::dataset::make("DataType", { DataType::F16 })) +{ + ARM_COMPUTE_ERROR_ON(_target.size() != _reference.size()); + // Validate output + for(size_t k = 0; k < _target.size(); ++k) + { + validate(CLAccessor(_target[k]), _reference[k]); + } +} +TEST_SUITE_END() // F16 + +TEST_SUITE(Quantized) +FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, unstack_dataset_small * framework::dataset::make("DataType", { DataType::QASYMM8 })) +{ + ARM_COMPUTE_ERROR_ON(_target.size() != _reference.size()); + // Validate output + for(size_t k = 0; k < _target.size(); ++k) + { + validate(CLAccessor(_target[k]), _reference[k]); + } +} +TEST_SUITE_END() // QASYMM8 + +TEST_SUITE_END() // Unstack +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/UnstackFixture.h b/tests/validation/fixtures/UnstackFixture.h new file mode 100644 index 0000000000..f20a128415 --- /dev/null +++ b/tests/validation/fixtures/UnstackFixture.h @@ -0,0 +1,118 @@ +/* + * 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_UNSTACK_FIXTURE +#define ARM_COMPUTE_TEST_UNSTACK_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/Unstack.h" + +#include <random> + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T> +class UnstackValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape input_shape, int axis, int num, DataType data_type) + { + _target = compute_target(input_shape, axis, num, data_type); + _reference = compute_reference(input_shape, axis, num, data_type); + } + +protected: + template <typename U> + void fill(U &&tensor, int i) + { + library->fill_tensor_uniform(tensor, i); + } + + std::vector<TensorType> compute_target(TensorShape input_shape, int axis, unsigned int num, DataType data_type) + { + TensorType input_tensor = create_tensor<TensorType>(input_shape, data_type); + const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_shape.num_dimensions())); + const unsigned int axis_size = input_shape[axis_u]; + const unsigned int num_slices = std::min(num, axis_size); + std::vector<TensorType> output_slices(num_slices); + std::vector<ITensorType *> output_ptrs(num_slices); + for(size_t k = 0; k < num_slices; ++k) + { + output_ptrs[k] = &output_slices[k]; + } + // Create and configure function + FunctionType unstack; + unstack.configure(&input_tensor, output_ptrs, axis); + // Allocate tensors + for(auto &out : output_slices) + { + out.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!out.info()->is_resizable(), framework::LogLevel::ERRORS); + } + input_tensor.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!input_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); + fill(AccessorType(input_tensor), 0); + // Compute function + unstack.run(); + return output_slices; + } + + std::vector<SimpleTensor<T>> compute_reference(TensorShape input_shape, int axis, unsigned int num, DataType data_type) + { + const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_shape.num_dimensions())); + const unsigned int axis_size = input_shape[axis_u]; + const unsigned int num_output_tensors = (num == 0) ? axis_size : std::min(axis_size, num); + // create and fill input tensor + SimpleTensor<T> input_tensor{ input_shape, data_type }; + fill(input_tensor, 0); + // create output tensors + const TensorShape slice_shape = arm_compute::misc::shape_calculator::calculate_unstack_shape(input_shape, axis_u); + std::vector<SimpleTensor<T>> output_tensors(num_output_tensors); + for(size_t k = 0; k < num_output_tensors; ++k) + { + output_tensors[k] = SimpleTensor<T>(slice_shape, data_type); + } + + return reference::unstack<T>(input_tensor, output_tensors, axis); + } + + std::vector<TensorType> _target{}; + std::vector<SimpleTensor<T>> _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_UNSTACK_FIXTURE */ diff --git a/tests/validation/reference/Unstack.cpp b/tests/validation/reference/Unstack.cpp new file mode 100644 index 0000000000..3474c15173 --- /dev/null +++ b/tests/validation/reference/Unstack.cpp @@ -0,0 +1,108 @@ +/* + * 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 "Unstack.h" + +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +inline Coordinates expand_coordinates(Coordinates in_coord, size_t axis, size_t slice, size_t num_dimensions) +{ + /* + Reconstruct input_coord to read the corresponding value from the correct slice. This is done by adding an extra dimension + to the coordinates and shuffling around the values based on the info below. + + For example, if input tensor shape is (X, Y, Z, W); + + If axis == 0, each slice will have the shape (Y, Z, W) and there will be X slices + + If axis == 1, each slice will have the shape (X, Z, W) and there will be Y slices. + */ + Coordinates expanded_coord; + expanded_coord.set_num_dimensions(num_dimensions); + expanded_coord.set(axis, slice); + for(size_t k = 0; k < axis; ++k) + { + expanded_coord.set(k, in_coord[k]); + } + for(size_t k = axis + 1; k < num_dimensions; ++k) + { + expanded_coord.set(k, in_coord[k - 1]); + } + return expanded_coord; +} + +template <typename T> +SimpleTensor<T> get_slice(const SimpleTensor<T> &input_tensor, size_t axis, size_t slice) +{ + TensorShape out_shape = input_tensor.shape(); + out_shape.remove_dimension(axis); + + const size_t unpacked_num_dimensions(input_tensor.shape().num_dimensions()); + + SimpleTensor<T> output{ out_shape, input_tensor.data_type() }; + + Window win; + win.use_tensor_dimensions(out_shape); + execute_window_loop(win, [&](const Coordinates & id) + { + const Coordinates input_coords = expand_coordinates(id, axis, slice, unpacked_num_dimensions); + *reinterpret_cast<T *>(output(id)) = *reinterpret_cast<const T *>(input_tensor(input_coords)); + }); + + return output; +} +} // namespace + +template <typename T> +std::vector<SimpleTensor<T>> unstack(const SimpleTensor<T> &input_tensor, std::vector<SimpleTensor<T>> &output_tensors, int axis) +{ + // Wrap around negative values + const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_tensor.shape().num_dimensions())); + ARM_COMPUTE_ERROR_ON(axis_u >= input_tensor.shape().num_dimensions()); + for(size_t k = 0; k < output_tensors.size(); ++k) + { + SimpleTensor<T> &output = output_tensors[k]; + const SimpleTensor<T> kth_slice = get_slice(input_tensor, axis_u, k); + output = copy_tensor<T>(kth_slice); + } + return output_tensors; +} + +template std::vector<SimpleTensor<float>> unstack(const SimpleTensor<float> &input_tensor, std::vector<SimpleTensor<float>> &output_tensors, int axis); +template std::vector<SimpleTensor<half>> unstack(const SimpleTensor<half> &input_tensor, std::vector<SimpleTensor<half>> &output_tensors, int axis); +template std::vector<SimpleTensor<uint8_t>> unstack(const SimpleTensor<uint8_t> &input_tensor, std::vector<SimpleTensor<uint8_t>> &output_tensors, int axis); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/Unstack.h b/tests/validation/reference/Unstack.h new file mode 100644 index 0000000000..56e37784cc --- /dev/null +++ b/tests/validation/reference/Unstack.h @@ -0,0 +1,46 @@ +/* + * 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_UNSTACK_H__ +#define __ARM_COMPUTE_TEST_UNSTACK_H__ + +#include "tests/SimpleTensor.h" + +#include <vector> + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +std::vector<SimpleTensor<T>> unstack(const SimpleTensor<T> &input_tensor, std::vector<SimpleTensor<T>> &output_tensors, int axis); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_UNSTACK_H__ */ |