From 543036904d5324c4738cdc8b586c301471046c8e Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Thu, 22 Nov 2018 16:14:36 +0000 Subject: COMPMID-1726: Implement CLUnstack. Change-Id: I94b0707d19757c5f5d7ca66d9c47e378867126a3 Reviewed-on: https://review.mlplatform.org/325 Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- tests/datasets/ShapeDatasets.h | 2 + tests/validation/CL/Unstack.cpp | 126 +++++++++++++++++++++++++++++ tests/validation/fixtures/UnstackFixture.h | 118 +++++++++++++++++++++++++++ tests/validation/reference/Unstack.cpp | 108 +++++++++++++++++++++++++ tests/validation/reference/Unstack.h | 46 +++++++++++ 5 files changed, 400 insertions(+) create mode 100644 tests/validation/CL/Unstack.cpp create mode 100644 tests/validation/fixtures/UnstackFixture.h create mode 100644 tests/validation/reference/Unstack.cpp create mode 100644 tests/validation/reference/Unstack.h (limited to 'tests') 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(TensorShape(9U, 8U), 1, DataType::U8) }, std::vector{ TensorInfo(TensorShape(2U, 3U), 1, DataType::U8) }, std::vector{ TensorInfo(TensorShape(2U, 3U), 1, DataType::S32) }, + + std::vector{ TensorInfo(TensorShape(7U, 5U), 1, DataType::S32), TensorInfo(TensorShape(7U, 5U), 1, DataType::S32), TensorInfo(TensorShape(7U, 5U), 1, DataType::S32) }, std::vector{ TensorInfo(TensorShape(7U, 5U), 1, DataType::S16) }, std::vector{ 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 ti(output_info); + std::vector 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 +using CLUnstackFixture = UnstackValidationFixture; + +TEST_SUITE(F32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture, 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, 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, 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 + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class UnstackValidationFixture : public framework::Fixture +{ +public: + template + 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 + void fill(U &&tensor, int i) + { + library->fill_tensor_uniform(tensor, i); + } + + std::vector compute_target(TensorShape input_shape, int axis, unsigned int num, DataType data_type) + { + TensorType input_tensor = create_tensor(input_shape, data_type); + const unsigned int axis_u = wrap_around(axis, static_cast(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 output_slices(num_slices); + std::vector 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> compute_reference(TensorShape input_shape, int axis, unsigned int num, DataType data_type) + { + const unsigned int axis_u = wrap_around(axis, static_cast(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 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> output_tensors(num_output_tensors); + for(size_t k = 0; k < num_output_tensors; ++k) + { + output_tensors[k] = SimpleTensor(slice_shape, data_type); + } + + return reference::unstack(input_tensor, output_tensors, axis); + } + + std::vector _target{}; + std::vector> _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 +SimpleTensor get_slice(const SimpleTensor &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 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(output(id)) = *reinterpret_cast(input_tensor(input_coords)); + }); + + return output; +} +} // namespace + +template +std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis) +{ + // Wrap around negative values + const unsigned int axis_u = wrap_around(axis, static_cast(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 &output = output_tensors[k]; + const SimpleTensor kth_slice = get_slice(input_tensor, axis_u, k); + output = copy_tensor(kth_slice); + } + return output_tensors; +} + +template std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis); +template std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis); +template std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &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 + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +std::vector> unstack(const SimpleTensor &input_tensor, std::vector> &output_tensors, int axis); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_UNSTACK_H__ */ -- cgit v1.2.1