diff options
author | Michele Di Giorgio <michele.digiorgio@arm.com> | 2018-05-01 16:52:00 +0100 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:50:48 +0000 |
commit | 721756352e68602fe6aaf90b3ca031c947cd2176 (patch) | |
tree | 33921f257424524e82d8aeaec62447f4dbc5b440 /tests | |
parent | c0f54434383f945d95f95549c1c4b0d5f5d2caff (diff) | |
download | ComputeLibrary-721756352e68602fe6aaf90b3ca031c947cd2176.tar.gz |
COMPMID-1107: Add support for ChannelShuffle in CL
Change-Id: I56d2a02b316f0c69ff1fd7220e732f775414fe69
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/129709
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Giorgio Arena <giorgio.arena@arm.com>
Reviewed-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/datasets/ChannelShuffleLayerDataset.h | 128 | ||||
-rw-r--r-- | tests/validation/CL/ChannelShuffle.cpp | 117 | ||||
-rw-r--r-- | tests/validation/fixtures/ChannelShuffleLayerFixture.h | 108 | ||||
-rw-r--r-- | tests/validation/reference/ChannelShuffle.cpp | 82 | ||||
-rw-r--r-- | tests/validation/reference/ChannelShuffle.h | 43 |
5 files changed, 478 insertions, 0 deletions
diff --git a/tests/datasets/ChannelShuffleLayerDataset.h b/tests/datasets/ChannelShuffleLayerDataset.h new file mode 100644 index 0000000000..a47513958a --- /dev/null +++ b/tests/datasets/ChannelShuffleLayerDataset.h @@ -0,0 +1,128 @@ +/* + * 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_CHANNEL_SHUFFLE_LAYER_DATASET +#define ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_LAYER_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class ChannelShuffleLayerDataset +{ +public: + using type = std::tuple<TensorShape, int>; + + struct iterator + { + iterator(std::vector<TensorShape>::const_iterator tensor_it, + std::vector<int>::const_iterator num_groups_it) + : _tensor_it{ std::move(tensor_it) }, + _num_groups_it{ std::move(num_groups_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_tensor_it << ":"; + description << "NumGroups=" << *_num_groups_it; + return description.str(); + } + + ChannelShuffleLayerDataset::type operator*() const + { + return std::make_tuple(*_tensor_it, *_num_groups_it); + } + + iterator &operator++() + { + ++_tensor_it; + ++_num_groups_it; + + return *this; + } + + private: + std::vector<TensorShape>::const_iterator _tensor_it; + std::vector<int>::const_iterator _num_groups_it; + }; + + iterator begin() const + { + return iterator(_tensor_shapes.begin(), _num_groups.begin()); + } + + int size() const + { + return std::min(_tensor_shapes.size(), _num_groups.size()); + } + + void add_config(TensorShape tensor, int num_groups) + { + _tensor_shapes.emplace_back(std::move(tensor)); + _num_groups.emplace_back(std::move(num_groups)); + } + +protected: + ChannelShuffleLayerDataset() = default; + ChannelShuffleLayerDataset(ChannelShuffleLayerDataset &&) = default; + +private: + std::vector<TensorShape> _tensor_shapes{}; + std::vector<int> _num_groups{}; +}; + +class SmallRandomChannelShuffleLayerDataset final : public ChannelShuffleLayerDataset +{ +public: + SmallRandomChannelShuffleLayerDataset() + { + add_config(TensorShape(15U, 16U, 4U, 12U), 2); + add_config(TensorShape(21U, 11U, 12U, 7U), 4); + add_config(TensorShape(21U, 11U, 12U, 7U), 6); + add_config(TensorShape(7U, 3U, 6U, 11U), 3); + } +}; + +class LargeRandomChannelShuffleLayerDataset final : public ChannelShuffleLayerDataset +{ +public: + LargeRandomChannelShuffleLayerDataset() + { + add_config(TensorShape(210U, 43U, 20U, 3U), 5); + add_config(TensorShape(283U, 213U, 15U, 3U), 3); + add_config(TensorShape(500U, 115U, 16U, 2U), 4); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_LAYER_DATASET */ diff --git a/tests/validation/CL/ChannelShuffle.cpp b/tests/validation/CL/ChannelShuffle.cpp new file mode 100644 index 0000000000..41813c4ffd --- /dev/null +++ b/tests/validation/CL/ChannelShuffle.cpp @@ -0,0 +1,117 @@ +/* + * 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/CLChannelShuffleLayer.h" +#include "tests/CL/CLAccessor.h" +#include "tests/datasets/ChannelShuffleLayerDataset.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/ChannelShuffleLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(CL) +TEST_SUITE(ChannelShuffle) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallRandomChannelShuffleLayerDataset(), framework::dataset::make("DataType", { DataType::S8, DataType::U8, DataType::S16, DataType::U16, DataType::U32, DataType::S32, DataType::F16, DataType::F32 })), + shape, num_groups, data_type) +{ + // Create tensors + CLTensor ref_src = create_tensor<CLTensor>(shape, data_type); + CLTensor dst = create_tensor<CLTensor>(shape, data_type); + + // Create and Configure function + CLChannelShuffleLayer channel_shuffle_func; + channel_shuffle_func.configure(&ref_src, &dst, num_groups); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); +} + +template <typename T> +using CLChannelShuffleLayerFixture = ChannelShuffleLayerValidationFixture<CLTensor, CLAccessor, CLChannelShuffleLayer, T>; + +TEST_SUITE(U8) +FIXTURE_DATA_TEST_CASE(RunSmall, CLChannelShuffleLayerFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallRandomChannelShuffleLayerDataset(), + framework::dataset::make("DataType", + DataType::U8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLChannelShuffleLayerFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeRandomChannelShuffleLayerDataset(), framework::dataset::make("DataType", + DataType::U8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() + +TEST_SUITE(Float) +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLChannelShuffleLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallRandomChannelShuffleLayerDataset(), framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLChannelShuffleLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeRandomChannelShuffleLayerDataset(), framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLChannelShuffleLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallRandomChannelShuffleLayerDataset(), framework::dataset::make("DataType", + DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLChannelShuffleLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeRandomChannelShuffleLayerDataset(), framework::dataset::make("DataType", + DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/ChannelShuffleLayerFixture.h b/tests/validation/fixtures/ChannelShuffleLayerFixture.h new file mode 100644 index 0000000000..9746480829 --- /dev/null +++ b/tests/validation/fixtures/ChannelShuffleLayerFixture.h @@ -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. + */ +#ifndef ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_FIXTURE +#define ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_FIXTURE + +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/Tensor.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/reference/ChannelShuffle.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> +class ChannelShuffleLayerValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape shape, unsigned int num_groups, DataType data_type) + { + _target = compute_target(shape, data_type, num_groups); + _reference = compute_reference(shape, data_type, num_groups); + } + +protected: + template <typename U> + void fill(U &&tensor) + { + library->fill_tensor_uniform(tensor, 0); + } + + TensorType compute_target(const TensorShape &shape, DataType data_type, unsigned int num_groups) + { + // Create tensors + TensorType src = create_tensor<TensorType>(shape, data_type); + TensorType dst = create_tensor<TensorType>(shape, data_type); + + // Create and configure function + FunctionType channel_shuffle_func; + channel_shuffle_func.configure(&src, &dst, num_groups); + + 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)); + + // Compute function + channel_shuffle_func.run(); + + return dst; + } + + SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, unsigned int num_groups) + { + // Create reference + SimpleTensor<T> src{ shape, data_type }; + + // Fill reference + fill(src); + + return reference::channel_shuffle<T>(src, num_groups); + } + + TensorType _target{}; + SimpleTensor<T> _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_FIXTURE */ diff --git a/tests/validation/reference/ChannelShuffle.cpp b/tests/validation/reference/ChannelShuffle.cpp new file mode 100644 index 0000000000..790eff53a0 --- /dev/null +++ b/tests/validation/reference/ChannelShuffle.cpp @@ -0,0 +1,82 @@ +/* + * 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 "ChannelShuffle.h" + +#include "arm_compute/core/Types.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +// Refence implementation for channel shuffle taken from https://github.com/pytorch/pytorch/blob/master/caffe2/operators/channel_shuffle_op.h +template <typename T> +SimpleTensor<T> channel_shuffle(const SimpleTensor<T> &src, int num_groups) +{ + // Create reference + SimpleTensor<T> dst{ src.shape(), src.data_type(), src.num_channels(), src.fixed_point_position(), src.quantization_info() }; + + const int M = src.shape()[0]; + const int N = src.shape()[1]; + const int num_channels = src.shape()[2]; + const int batches = src.shape()[3]; + const int MxN = M * N; + const int channels_in_group = num_channels / num_groups; + + const T *src_ref = src.data(); + T *dst_ref = dst.data(); + + for(int n = 0; n < batches; ++n) + { + for(int g = 0; g < num_groups; ++g) + { + // Gather the group g block (of size channels_in_group * MxN) from output channels + // g + 0 * G, g + 1 * G, g + 2 * G, g + G * (K - 1) etc. + const T *src_ptr = src_ref + g * MxN + n * num_channels * MxN; + T *dst_ptr = dst_ref + g * channels_in_group * MxN + n * num_channels * MxN; + for(int i = 0; i < channels_in_group; ++i) + { + std::copy(src_ptr + i * num_groups * MxN, + src_ptr + (i * num_groups + 1) * MxN, + dst_ptr + i * MxN); + } + } + } + + return dst; +} + +template SimpleTensor<uint8_t> channel_shuffle(const SimpleTensor<uint8_t> &src, int num_groups); +template SimpleTensor<uint16_t> channel_shuffle(const SimpleTensor<uint16_t> &src, int num_groups); +template SimpleTensor<uint32_t> channel_shuffle(const SimpleTensor<uint32_t> &src, int num_groups); +template SimpleTensor<half> channel_shuffle(const SimpleTensor<half> &src, int num_groups); +template SimpleTensor<float> channel_shuffle(const SimpleTensor<float> &src, int num_groups); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/ChannelShuffle.h b/tests/validation/reference/ChannelShuffle.h new file mode 100644 index 0000000000..52df19e6f3 --- /dev/null +++ b/tests/validation/reference/ChannelShuffle.h @@ -0,0 +1,43 @@ +/* + * 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_CHANNEL_SHUFFLE_H__ +#define __ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +SimpleTensor<T> channel_shuffle(const SimpleTensor<T> &src, int num_groups); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_CHANNEL_SHUFFLE_H__ */ |