From 22f917cfd2282052c447068b188eee0c59f737fd Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Tue, 21 May 2019 13:30:10 +0100 Subject: COMPMID-2240 Implement DEPTH_TO_SPACE for NEON Change-Id: I705aa0f804093c3628c691e46cca475f2819dc65 Signed-off-by: Michalis Spyrou Reviewed-on: https://review.mlplatform.org/c/1198 Comments-Addressed: Arm Jenkins Reviewed-by: Giuseppe Rossini Tested-by: Arm Jenkins --- tests/validation/NEON/DepthToSpaceLayer.cpp | 115 +++++++++++++++++++++ .../validation/fixtures/DepthToSpaceLayerFixture.h | 111 ++++++++++++++++++++ tests/validation/reference/DepthToSpaceLayer.cpp | 75 ++++++++++++++ tests/validation/reference/DepthToSpaceLayer.h | 44 ++++++++ 4 files changed, 345 insertions(+) create mode 100644 tests/validation/NEON/DepthToSpaceLayer.cpp create mode 100644 tests/validation/fixtures/DepthToSpaceLayerFixture.h create mode 100644 tests/validation/reference/DepthToSpaceLayer.cpp create mode 100644 tests/validation/reference/DepthToSpaceLayer.h (limited to 'tests/validation') diff --git a/tests/validation/NEON/DepthToSpaceLayer.cpp b/tests/validation/NEON/DepthToSpaceLayer.cpp new file mode 100644 index 0000000000..abc8c4f266 --- /dev/null +++ b/tests/validation/NEON/DepthToSpaceLayer.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 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/core/Types.h" +#include "arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/DepthToSpaceDataset.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/DepthToSpaceLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(NEON) +TEST_SUITE(DepthToSpaceLayer) + +template +using NEDepthToSpaceLayerFixture = DepthToSpaceLayerValidationFixture; + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(16U, 8U, 4U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 8U, 4U, 4U), 1, DataType::F32), // block < 2 + TensorInfo(TensorShape(16U, 8U, 2U, 4U), 1, DataType::F32), // Mismatching data types + TensorInfo(TensorShape(16U, 8U, 2U, 4U), 1, DataType::F32), // Negative block shape + TensorInfo(TensorShape(32U, 16U, 2U, 4U, 4U), 1, DataType::F32), // Wrong tensor shape + }), + framework::dataset::make("BlockShape", { 2, 1, 2, 2, 2 })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 16U, 1U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 16U, 1U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 16U, 2U, 1U), 1, DataType::F16), + TensorInfo(TensorShape(32U, 16U, 2U, 1U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 8U, 2U, 1U), 1, DataType::F32), + })), + framework::dataset::make("Expected", { true, false, false, false, false})), + input_info, block_shape, output_info, expected) +{ + bool has_error = bool(NEDepthToSpaceLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), block_shape)); + ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NEDepthToSpaceLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEDepthToSpaceLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() + +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEDepthToSpaceLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEDepthToSpaceLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() // DepthToSpace +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/DepthToSpaceLayerFixture.h b/tests/validation/fixtures/DepthToSpaceLayerFixture.h new file mode 100644 index 0000000000..b42bed52a7 --- /dev/null +++ b/tests/validation/fixtures/DepthToSpaceLayerFixture.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 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. + */ +#ifndef ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_FIXTURE +#define ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_FIXTURE + +#include "tests/Globals.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/reference/DepthToSpaceLayer.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class DepthToSpaceLayerValidationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape input_shape, int32_t block_shape, TensorShape output_shape, DataType data_type, DataLayout data_layout) + { + _target = compute_target(input_shape, block_shape, output_shape, data_type, data_layout); + _reference = compute_reference(input_shape, block_shape, output_shape, data_type); + } + +protected: + template + void fill(U &&tensor, int i) + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + } + TensorType compute_target(TensorShape input_shape, int32_t block_shape, TensorShape output_shape, + DataType data_type, DataLayout data_layout) + { + if(data_layout == DataLayout::NHWC) + { + permute(input_shape, PermutationVector(2U, 0U, 1U)); + permute(output_shape, PermutationVector(2U, 0U, 1U)); + } + + // Create tensors + TensorType input = create_tensor(input_shape, data_type, 1, QuantizationInfo(), data_layout); + TensorType output = create_tensor(output_shape, data_type, 1, QuantizationInfo(), data_layout); + + // Create and configure function + FunctionType depth_to_space; + depth_to_space.configure(&input, &output, block_shape); + + ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + input.allocator()->allocate(); + output.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(input), 0); + + // Compute function + depth_to_space.run(); + + return output; + } + + SimpleTensor compute_reference(const TensorShape &input_shape, int32_t block_shape, + const TensorShape &output_shape, DataType data_type) + { + // Create reference + SimpleTensor input{ input_shape, data_type }; + + // Fill reference + fill(input, 0); + + // Compute reference + return reference::depth_to_space(input, output_shape, block_shape); + } + + TensorType _target{}; + SimpleTensor _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_FIXTURE */ diff --git a/tests/validation/reference/DepthToSpaceLayer.cpp b/tests/validation/reference/DepthToSpaceLayer.cpp new file mode 100644 index 0000000000..4135ce5471 --- /dev/null +++ b/tests/validation/reference/DepthToSpaceLayer.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 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 "DepthToSpaceLayer.h" + +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +// Batch to Space +template +SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape) +{ + ARM_COMPUTE_ERROR_ON(block_shape <= 0); + SimpleTensor result(dst_shape, src.data_type()); + + int in_pos = 0; + const auto width_in = static_cast(src.shape()[0]); + const auto height_in = static_cast(src.shape()[1]); + const auto channel_in = static_cast(src.shape()[2]); + const auto batch_in = static_cast(src.shape()[3]); + const int r = channel_in / (block_shape * block_shape); + + for(int b = 0; b < batch_in; ++b) + { + for(int z = 0; z < channel_in; ++z) + { + for(int y = 0; y < height_in; ++y) + { + for(int x = 0; x < width_in; ++x) + { + const int out_x = (block_shape * x + (z / r) % block_shape); + const int out_y = (block_shape * y + (z / r) / block_shape); + const int out_pos = out_x + dst_shape[0] * out_y + (z % r) * dst_shape[0] * dst_shape[1] + b * dst_shape[0] * dst_shape[1] * dst_shape[2]; + result[out_pos] = src[in_pos]; + ++in_pos; + } + } + } + } + + return result; +} +template SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape); +template SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/DepthToSpaceLayer.h b/tests/validation/reference/DepthToSpaceLayer.h new file mode 100644 index 0000000000..3989401c08 --- /dev/null +++ b/tests/validation/reference/DepthToSpaceLayer.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 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. + */ +#ifndef __ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_H__ +#define __ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_H__ */ -- cgit v1.2.1