From f1adf11c776aebaa8da1b8644a4ba2453afd2b81 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Fri, 2 Nov 2018 12:54:18 +0000 Subject: COMPMID-1579: Add support for ChannelShuffle operator in NEON Change-Id: I6d5f91579850906e1eb973ff6c5612195255e631 --- arm_compute/core/NEON/NEKernels.h | 1 + .../NEON/kernels/NEChannelShuffleLayerKernel.h | 80 +++++++ arm_compute/core/Validate.h | 52 +++++ arm_compute/runtime/NEON/NEFunctions.h | 1 + .../runtime/NEON/functions/NEChannelShuffleLayer.h | 61 +++++ examples/graph_cl_shufflenet.cpp | 256 --------------------- examples/graph_shufflenet.cpp | 256 +++++++++++++++++++++ .../NEON/kernels/NEChannelShuffleLayerKernel.cpp | 193 ++++++++++++++++ src/graph/backends/NEON/NEFunctionFactory.cpp | 2 + src/graph/backends/NEON/NENodeValidator.cpp | 2 +- .../NEON/functions/NEChannelShuffleLayer.cpp | 43 ++++ tests/validation/NEON/ChannelShuffle.cpp | 132 +++++++++++ 12 files changed, 822 insertions(+), 257 deletions(-) create mode 100644 arm_compute/core/NEON/kernels/NEChannelShuffleLayerKernel.h create mode 100644 arm_compute/runtime/NEON/functions/NEChannelShuffleLayer.h delete mode 100644 examples/graph_cl_shufflenet.cpp create mode 100644 examples/graph_shufflenet.cpp create mode 100644 src/core/NEON/kernels/NEChannelShuffleLayerKernel.cpp create mode 100644 src/runtime/NEON/functions/NEChannelShuffleLayer.cpp create mode 100644 tests/validation/NEON/ChannelShuffle.cpp diff --git a/arm_compute/core/NEON/NEKernels.h b/arm_compute/core/NEON/NEKernels.h index cb2e851b66..76eb5cb5f2 100644 --- a/arm_compute/core/NEON/NEKernels.h +++ b/arm_compute/core/NEON/NEKernels.h @@ -39,6 +39,7 @@ #include "arm_compute/core/NEON/kernels/NECannyEdgeKernel.h" #include "arm_compute/core/NEON/kernels/NEChannelCombineKernel.h" #include "arm_compute/core/NEON/kernels/NEChannelExtractKernel.h" +#include "arm_compute/core/NEON/kernels/NEChannelShuffleLayerKernel.h" #include "arm_compute/core/NEON/kernels/NECol2ImKernel.h" #include "arm_compute/core/NEON/kernels/NEColorConvertKernel.h" #include "arm_compute/core/NEON/kernels/NEConvertFullyConnectedWeightsKernel.h" diff --git a/arm_compute/core/NEON/kernels/NEChannelShuffleLayerKernel.h b/arm_compute/core/NEON/kernels/NEChannelShuffleLayerKernel.h new file mode 100644 index 0000000000..e9ef2d4a51 --- /dev/null +++ b/arm_compute/core/NEON/kernels/NEChannelShuffleLayerKernel.h @@ -0,0 +1,80 @@ +/* + * 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_NECHANNELSHUFFLELAYERKERNEL_H__ +#define __ARM_COMPUTE_NECHANNELSHUFFLELAYERKERNEL_H__ + +#include "arm_compute/core/NEON/INEKernel.h" + +namespace arm_compute +{ +// Forward declarations +class ITensor; + +/** Interface for the channel shuffle kernel */ +class NEChannelShuffleLayerKernel : public INEKernel +{ +public: + const char *name() const override + { + return "NEChannelShuffleLayerKernel"; + } + /** Default constructor */ + NEChannelShuffleLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEChannelShuffleLayerKernel(const NEChannelShuffleLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEChannelShuffleLayerKernel &operator=(const NEChannelShuffleLayerKernel &) = delete; + /** Allow instances of this class to be moved */ + NEChannelShuffleLayerKernel(NEChannelShuffleLayerKernel &&) = default; + /** Allow instances of this class to be moved */ + NEChannelShuffleLayerKernel &operator=(NEChannelShuffleLayerKernel &&) = default; + /** Default destructor */ + ~NEChannelShuffleLayerKernel() = default; + /** Configure function's inputs and outputs. + * + * @param[in] input Input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 + * @param[out] output Output tensor. Data type supported: Same as @p input + * @param[in] num_groups Number of groups. Must be greater than 1 and the number of channels of the tensors must be a multiple of the number of groups. + */ + void configure(const ITensor *input, ITensor *output, unsigned int num_groups); + /** Static function to check if given info will lead to a valid configuration of @ref NEChannelShuffleLayerKernel + * + * @param[in] input Input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 + * @param[out] output Output tensor. Data type supported: Same as @p input + * @param[in] num_groups Number of groups. Must be greater than 1 and the number of channels of the tensors must be a multiple of the number of groups. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups); + + // Inherited methods overridden: + void run(const Window &window, const ThreadInfo &info) override; + +private: + const ITensor *_input; + ITensor *_output; + unsigned int _num_groups; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NECHANNELSHUFFLELAYERKERNEL_H__ */ diff --git a/arm_compute/core/Validate.h b/arm_compute/core/Validate.h index 918c8e5fc3..dab4221a3b 100644 --- a/arm_compute/core/Validate.h +++ b/arm_compute/core/Validate.h @@ -693,6 +693,58 @@ inline arm_compute::Status error_on_data_type_not_in(const char *function, const #define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) \ ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)) +/** Return an error if the data layout of the passed tensor info does not match any of the data layouts provided. + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info Tensor info to validate. + * @param[in] dl First data layout allowed. + * @param[in] dls (Optional) Further allowed data layouts. + * + * @return Status + */ +template +inline arm_compute::Status error_on_data_layout_not_in(const char *function, const char *file, const int line, + const ITensorInfo *tensor_info, T &&dl, Ts &&... dls) +{ + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line); + + const DataLayout &tensor_dl = tensor_info->data_layout(); //NOLINT + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_dl == DataLayout::UNKNOWN, function, file, line); + + const std::array dls_array{ { std::forward(dls)... } }; + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(tensor_dl != dl && std::none_of(dls_array.begin(), dls_array.end(), [&](const T & l) + { + return l == tensor_dl; + }), + function, file, line, "ITensor data layout %s not supported by this kernel", string_from_data_layout(tensor_dl).c_str()); + return arm_compute::Status{}; +} +/** Return an error if the data layout of the passed tensor does not match any of the data layout provided. + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor Tensor to validate. + * @param[in] dl First data layout allowed. + * @param[in] dls (Optional) Further allowed data layouts. + * + * @return Status + */ +template +inline arm_compute::Status error_on_data_layout_not_in(const char *function, const char *file, const int line, + const ITensor *tensor, T &&dl, Ts &&... dls) +{ + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_layout_not_in(function, file, line, tensor->info(), std::forward(dl), std::forward(dls)...)); + return arm_compute::Status{}; +} +#define ARM_COMPUTE_ERROR_ON_DATA_LAYOUT_NOT_IN(t, ...) \ + ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_data_layout_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t, ...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_layout_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)) + /** Return an error if the data type or the number of channels of the passed tensor info does not match any of the data types and number of channels provided. * * @param[in] function Function in which the error occurred. diff --git a/arm_compute/runtime/NEON/NEFunctions.h b/arm_compute/runtime/NEON/NEFunctions.h index 57bd5859f9..555d7d7551 100644 --- a/arm_compute/runtime/NEON/NEFunctions.h +++ b/arm_compute/runtime/NEON/NEFunctions.h @@ -39,6 +39,7 @@ #include "arm_compute/runtime/NEON/functions/NECannyEdge.h" #include "arm_compute/runtime/NEON/functions/NEChannelCombine.h" #include "arm_compute/runtime/NEON/functions/NEChannelExtract.h" +#include "arm_compute/runtime/NEON/functions/NEChannelShuffleLayer.h" #include "arm_compute/runtime/NEON/functions/NECol2Im.h" #include "arm_compute/runtime/NEON/functions/NEColorConvert.h" #include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h" diff --git a/arm_compute/runtime/NEON/functions/NEChannelShuffleLayer.h b/arm_compute/runtime/NEON/functions/NEChannelShuffleLayer.h new file mode 100644 index 0000000000..20fe483604 --- /dev/null +++ b/arm_compute/runtime/NEON/functions/NEChannelShuffleLayer.h @@ -0,0 +1,61 @@ +/* + * 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_NECHANNELSHUFFLELAYER_H__ +#define __ARM_COMPUTE_NECHANNELSHUFFLELAYER_H__ + +#include "arm_compute/runtime/NEON/INESimpleFunction.h" + +namespace arm_compute +{ +// Forward declarations +class ITensor; + +/** Basic function to run @ref NEChannelShuffleLayerKernel + * + * @note The function performs a channel shuffle operation on the input tensor. Given NCHW tensor with group G, it will + * first divide the channels into G groups, C = (G * C'), and perform a transpose of the channel, which gives C = (C' * G). + * for more details see: https://arxiv.org/pdf/1707.01083.pdf + */ +class NEChannelShuffleLayer : public INESimpleFunction +{ +public: + /** Initialize the function + * + * @param[in] input Input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 + * @param[out] output Output tensor. Data type supported: Same as @p input + * @param[in] num_groups Number of groups. Must be greater than 1 and the number of channels of the tensors must be a multiple of the number of groups. + */ + void configure(const ITensor *input, ITensor *output, unsigned int num_groups); + /** Static function to check if given info will lead to a valid configuration of @ref NEChannelShuffleLayer + * + * @param[in] input Input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 + * @param[out] output Output tensor. Data type supported: Same as @p input + * @param[in] num_groups Number of groups. Must be greater than 1 and the number of channels of the tensors must be a multiple of the number of groups. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NECHANNELSHUFFLELAYER_H__ */ diff --git a/examples/graph_cl_shufflenet.cpp b/examples/graph_cl_shufflenet.cpp deleted file mode 100644 index 0db34d6123..0000000000 --- a/examples/graph_cl_shufflenet.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/* - * 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/graph.h" -#include "support/ToolchainSupport.h" -#include "utils/CommonGraphOptions.h" -#include "utils/GraphUtils.h" -#include "utils/Utils.h" - -using namespace arm_compute::utils; -using namespace arm_compute::graph::frontend; -using namespace arm_compute::graph_utils; - -/** Example demonstrating how to implement ShuffleNet network using the Compute Library's graph API */ -class ShuffleNetExample : public Example -{ -public: - ShuffleNetExample() - : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ShuffleNet") - { - } - bool do_setup(int argc, char **argv) override - { - // Parse arguments - cmd_parser.parse(argc, argv); - - // Consume common parameters - common_params = consume_common_graph_parameters(common_opts); - - // Return when help menu is requested - if(common_params.help) - { - cmd_parser.print_help(argv[0]); - return false; - } - - // Set default layout if needed (Single kernel grouped convolution not yet supported int NHWC) - if(!common_opts.data_layout->is_set()) - { - common_params.data_layout = DataLayout::NCHW; - } - - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - ARM_COMPUTE_EXIT_ON_MSG(common_params.target != Target::CL, "Only CL backend is supported for this network"); - - // Print parameter values - std::cout << common_params << std::endl; - std::cout << "Model: Shufflenet_1_g4" << std::endl; - - // Create model path - std::string model_path = "/cnn_data/shufflenet_model/"; - - // Get trainable parameters data path - std::string data_path = common_params.data_path; - - // Add model path to data path - if(!data_path.empty()) - { - data_path += model_path; - } - - // Create input descriptor - const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout); - TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); - - // Set weights trained layout - const DataLayout weights_layout = DataLayout::NCHW; - - // Create preprocessor - std::unique_ptr preprocessor = arm_compute::support::cpp14::make_unique(0); - - graph << common_params.target - << common_params.fast_math_hint - << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */)) - << ConvolutionLayer( - 3U, 3U, 24U, - get_weights_accessor(data_path, "conv3_0_w_0.npy", weights_layout), - get_weights_accessor(data_path, "conv3_0_b_0.npy", weights_layout), - PadStrideInfo(2, 2, 1, 1)) - .set_name("Conv1/convolution") - << BatchNormalizationLayer( - get_weights_accessor(data_path, "conv3_0_bn_rm_0.npy"), - get_weights_accessor(data_path, "conv3_0_bn_riv_0.npy"), - get_weights_accessor(data_path, "conv3_0_bn_s_0.npy"), - get_weights_accessor(data_path, "conv3_0_bn_b_0.npy"), - 1e-5f) - .set_name("Conv1/BatchNorm") - << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv1/Relu") - << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 1, 1))).set_name("pool1/MaxPool"); - - // Stage 2 - add_residual_block(data_path, DataLayout::NCHW, 0U /* unit */, 112U /* depth */, 2U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 1U /* unit */, 136U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 2U /* unit */, 136U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 3U /* unit */, 136U /* depth */, 1U /* stride */); - - // Stage 3 - add_residual_block(data_path, DataLayout::NCHW, 4U /* unit */, 136U /* depth */, 2U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 5U /* unit */, 272U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 6U /* unit */, 272U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 7U /* unit */, 272U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 8U /* unit */, 272U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 9U /* unit */, 272U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 10U /* unit */, 272U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 11U /* unit */, 272U /* depth */, 1U /* stride */); - - // Stage 4 - add_residual_block(data_path, DataLayout::NCHW, 12U /* unit */, 272U /* depth */, 2U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 13U /* unit */, 544U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 14U /* unit */, 544U /* depth */, 1U /* stride */); - add_residual_block(data_path, DataLayout::NCHW, 15U /* unit */, 544U /* depth */, 1U /* stride */); - - graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("predictions/AvgPool") - << FlattenLayer().set_name("predictions/Reshape") - << FullyConnectedLayer( - 1000U, - get_weights_accessor(data_path, "pred_w_0.npy", weights_layout), - get_weights_accessor(data_path, "pred_b_0.npy")) - .set_name("predictions/FC") - << SoftmaxLayer().set_name("predictions/Softmax") - << OutputLayer(get_output_accessor(common_params, 5)); - - // Finalize graph - GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_file = common_params.tuner_file; - - graph.finalize(common_params.target, config); - - return true; - } - - void do_run() override - { - // Run graph - graph.run(); - } - -private: - CommandLineParser cmd_parser; - CommonGraphOptions common_opts; - CommonGraphParams common_params; - Stream graph; - - void add_residual_block(const std::string &data_path, DataLayout weights_layout, - unsigned int unit, unsigned int depth, unsigned int stride) - { - PadStrideInfo dwc_info = PadStrideInfo(1, 1, 1, 1); - const unsigned int gconv_id = unit * 2; - const unsigned int num_groups = 4; - const std::string unit_id_name = arm_compute::support::cpp11::to_string(unit); - const std::string gconv_id_name = arm_compute::support::cpp11::to_string(gconv_id); - const std::string gconv_id_1_name = arm_compute::support::cpp11::to_string(gconv_id + 1); - const std::string unit_name = "unit" + unit_id_name; - - SubStream left_ss(graph); - SubStream right_ss(graph); - - if(stride == 2) - { - right_ss << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(2, 2, 1, 1))).set_name(unit_name + "/pool_1/AveragePool"); - dwc_info = PadStrideInfo(2, 2, 1, 1); - } - - left_ss << ConvolutionLayer( - 1U, 1U, depth, - get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_w_0.npy", weights_layout), - std::unique_ptr(nullptr), - PadStrideInfo(1, 1, 0, 0), num_groups) - .set_name(unit_name + "/gconv1_" + gconv_id_name + "/convolution") - << BatchNormalizationLayer( - get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_rm_0.npy"), - get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_riv_0.npy"), - get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_s_0.npy"), - get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_b_0.npy"), - 1e-5f) - .set_name(unit_name + "/gconv1_" + gconv_id_name + "/BatchNorm") - << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/gconv1_" + gconv_id_name + "/Relu") - << ChannelShuffleLayer(num_groups).set_name(unit_name + "/shuffle_0/ChannelShufle") - << DepthwiseConvolutionLayer( - 3U, 3U, - get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_w_0.npy", weights_layout), - std::unique_ptr(nullptr), - dwc_info) - .set_name(unit_name + "/gconv3_" + unit_id_name + "/depthwise") - << BatchNormalizationLayer( - get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_rm_0.npy"), - get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_riv_0.npy"), - get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_s_0.npy"), - get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_b_0.npy"), - 1e-5f) - .set_name(unit_name + "/gconv3_" + unit_id_name + "/BatchNorm") - << ConvolutionLayer( - 1U, 1U, depth, - get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_w_0.npy", weights_layout), - std::unique_ptr(nullptr), - PadStrideInfo(1, 1, 0, 0), num_groups) - .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/convolution") - << BatchNormalizationLayer( - get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_rm_0.npy"), - get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_riv_0.npy"), - get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_s_0.npy"), - get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_b_0.npy"), - 1e-5f) - .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/BatchNorm"); - - if(stride == 2) - { - graph << ConcatLayer(std::move(left_ss), std::move(right_ss)).set_name(unit_name + "/Concat"); - } - else - { - graph << EltwiseLayer(std::move(left_ss), std::move(right_ss), EltwiseOperation::Add).set_name(unit_name + "/Add"); - } - graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/Relu"); - } -}; - -/** Main program for ShuffleNet - * - * Model is based on: - * https://arxiv.org/abs/1707.01083 - * "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices" - * Xiangyu Zhang, Xinyu Zhou, Mengxiao Lin, Jian Sun - * - * @note To list all the possible arguments execute the binary appended with the --help option - * - * @param[in] argc Number of arguments - * @param[in] argv Arguments - */ -int main(int argc, char **argv) -{ - return arm_compute::utils::run_example(argc, argv); -} diff --git a/examples/graph_shufflenet.cpp b/examples/graph_shufflenet.cpp new file mode 100644 index 0000000000..af8d11beb9 --- /dev/null +++ b/examples/graph_shufflenet.cpp @@ -0,0 +1,256 @@ +/* + * 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/graph.h" +#include "support/ToolchainSupport.h" +#include "utils/CommonGraphOptions.h" +#include "utils/GraphUtils.h" +#include "utils/Utils.h" + +using namespace arm_compute::utils; +using namespace arm_compute::graph::frontend; +using namespace arm_compute::graph_utils; + +/** Example demonstrating how to implement ShuffleNet network using the Compute Library's graph API */ +class ShuffleNetExample : public Example +{ +public: + ShuffleNetExample() + : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "ShuffleNet") + { + } + bool do_setup(int argc, char **argv) override + { + // Parse arguments + cmd_parser.parse(argc, argv); + + // Consume common parameters + common_params = consume_common_graph_parameters(common_opts); + + // Return when help menu is requested + if(common_params.help) + { + cmd_parser.print_help(argv[0]); + return false; + } + + // Set default layout if needed (Single kernel grouped convolution not yet supported int NHWC) + if(!common_opts.data_layout->is_set()) + { + common_params.data_layout = DataLayout::NCHW; + } + + // Checks + ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); + ARM_COMPUTE_EXIT_ON_MSG(common_params.data_type == DataType::F16 && common_params.target == Target::NEON, "F16 NEON not supported for this graph"); + + // Print parameter values + std::cout << common_params << std::endl; + std::cout << "Model: Shufflenet_1_g4" << std::endl; + + // Create model path + std::string model_path = "/cnn_data/shufflenet_model/"; + + // Get trainable parameters data path + std::string data_path = common_params.data_path; + + // Add model path to data path + if(!data_path.empty()) + { + data_path += model_path; + } + + // Create input descriptor + const TensorShape tensor_shape = permute_shape(TensorShape(224U, 224U, 3U, 1U), DataLayout::NCHW, common_params.data_layout); + TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); + + // Set weights trained layout + const DataLayout weights_layout = DataLayout::NCHW; + + // Create preprocessor + std::unique_ptr preprocessor = arm_compute::support::cpp14::make_unique(0); + + graph << common_params.target + << common_params.fast_math_hint + << InputLayer(input_descriptor, get_input_accessor(common_params, std::move(preprocessor), false /* Do not convert to BGR */)) + << ConvolutionLayer( + 3U, 3U, 24U, + get_weights_accessor(data_path, "conv3_0_w_0.npy", weights_layout), + get_weights_accessor(data_path, "conv3_0_b_0.npy", weights_layout), + PadStrideInfo(2, 2, 1, 1)) + .set_name("Conv1/convolution") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "conv3_0_bn_rm_0.npy"), + get_weights_accessor(data_path, "conv3_0_bn_riv_0.npy"), + get_weights_accessor(data_path, "conv3_0_bn_s_0.npy"), + get_weights_accessor(data_path, "conv3_0_bn_b_0.npy"), + 1e-5f) + .set_name("Conv1/BatchNorm") + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name("Conv1/Relu") + << PoolingLayer(PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 1, 1))).set_name("pool1/MaxPool"); + + // Stage 2 + add_residual_block(data_path, DataLayout::NCHW, 0U /* unit */, 112U /* depth */, 2U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 1U /* unit */, 136U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 2U /* unit */, 136U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 3U /* unit */, 136U /* depth */, 1U /* stride */); + + // Stage 3 + add_residual_block(data_path, DataLayout::NCHW, 4U /* unit */, 136U /* depth */, 2U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 5U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 6U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 7U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 8U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 9U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 10U /* unit */, 272U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 11U /* unit */, 272U /* depth */, 1U /* stride */); + + // Stage 4 + add_residual_block(data_path, DataLayout::NCHW, 12U /* unit */, 272U /* depth */, 2U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 13U /* unit */, 544U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 14U /* unit */, 544U /* depth */, 1U /* stride */); + add_residual_block(data_path, DataLayout::NCHW, 15U /* unit */, 544U /* depth */, 1U /* stride */); + + graph << PoolingLayer(PoolingLayerInfo(PoolingType::AVG)).set_name("predictions/AvgPool") + << FlattenLayer().set_name("predictions/Reshape") + << FullyConnectedLayer( + 1000U, + get_weights_accessor(data_path, "pred_w_0.npy", weights_layout), + get_weights_accessor(data_path, "pred_b_0.npy")) + .set_name("predictions/FC") + << SoftmaxLayer().set_name("predictions/Softmax") + << OutputLayer(get_output_accessor(common_params, 5)); + + // Finalize graph + GraphConfig config; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + + graph.finalize(common_params.target, config); + + return true; + } + + void do_run() override + { + // Run graph + graph.run(); + } + +private: + CommandLineParser cmd_parser; + CommonGraphOptions common_opts; + CommonGraphParams common_params; + Stream graph; + + void add_residual_block(const std::string &data_path, DataLayout weights_layout, + unsigned int unit, unsigned int depth, unsigned int stride) + { + PadStrideInfo dwc_info = PadStrideInfo(1, 1, 1, 1); + const unsigned int gconv_id = unit * 2; + const unsigned int num_groups = 4; + const std::string unit_id_name = arm_compute::support::cpp11::to_string(unit); + const std::string gconv_id_name = arm_compute::support::cpp11::to_string(gconv_id); + const std::string gconv_id_1_name = arm_compute::support::cpp11::to_string(gconv_id + 1); + const std::string unit_name = "unit" + unit_id_name; + + SubStream left_ss(graph); + SubStream right_ss(graph); + + if(stride == 2) + { + right_ss << PoolingLayer(PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(2, 2, 1, 1))).set_name(unit_name + "/pool_1/AveragePool"); + dwc_info = PadStrideInfo(2, 2, 1, 1); + } + + left_ss << ConvolutionLayer( + 1U, 1U, depth, + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_w_0.npy", weights_layout), + std::unique_ptr(nullptr), + PadStrideInfo(1, 1, 0, 0), num_groups) + .set_name(unit_name + "/gconv1_" + gconv_id_name + "/convolution") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_rm_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_riv_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_s_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_name + "_bn_b_0.npy"), + 1e-5f) + .set_name(unit_name + "/gconv1_" + gconv_id_name + "/BatchNorm") + << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/gconv1_" + gconv_id_name + "/Relu") + << ChannelShuffleLayer(num_groups).set_name(unit_name + "/shuffle_0/ChannelShufle") + << DepthwiseConvolutionLayer( + 3U, 3U, + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_w_0.npy", weights_layout), + std::unique_ptr(nullptr), + dwc_info) + .set_name(unit_name + "/gconv3_" + unit_id_name + "/depthwise") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_rm_0.npy"), + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_riv_0.npy"), + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_s_0.npy"), + get_weights_accessor(data_path, "gconv3_" + unit_id_name + "_bn_b_0.npy"), + 1e-5f) + .set_name(unit_name + "/gconv3_" + unit_id_name + "/BatchNorm") + << ConvolutionLayer( + 1U, 1U, depth, + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_w_0.npy", weights_layout), + std::unique_ptr(nullptr), + PadStrideInfo(1, 1, 0, 0), num_groups) + .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/convolution") + << BatchNormalizationLayer( + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_rm_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_riv_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_s_0.npy"), + get_weights_accessor(data_path, "gconv1_" + gconv_id_1_name + "_bn_b_0.npy"), + 1e-5f) + .set_name(unit_name + "/gconv1_" + gconv_id_1_name + "/BatchNorm"); + + if(stride == 2) + { + graph << ConcatLayer(std::move(left_ss), std::move(right_ss)).set_name(unit_name + "/Concat"); + } + else + { + graph << EltwiseLayer(std::move(left_ss), std::move(right_ss), EltwiseOperation::Add).set_name(unit_name + "/Add"); + } + graph << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU)).set_name(unit_name + "/Relu"); + } +}; + +/** Main program for ShuffleNet + * + * Model is based on: + * https://arxiv.org/abs/1707.01083 + * "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices" + * Xiangyu Zhang, Xinyu Zhou, Mengxiao Lin, Jian Sun + * + * @note To list all the possible arguments execute the binary appended with the --help option + * + * @param[in] argc Number of arguments + * @param[in] argv Arguments + */ +int main(int argc, char **argv) +{ + return arm_compute::utils::run_example(argc, argv); +} diff --git a/src/core/NEON/kernels/NEChannelShuffleLayerKernel.cpp b/src/core/NEON/kernels/NEChannelShuffleLayerKernel.cpp new file mode 100644 index 0000000000..f8217d3505 --- /dev/null +++ b/src/core/NEON/kernels/NEChannelShuffleLayerKernel.cpp @@ -0,0 +1,193 @@ +/* + * 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/NEON/kernels/NEChannelShuffleLayerKernel.h" + +#include "arm_compute/core/CPP/Validate.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/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) +{ + ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, + 1, + DataType::U8, DataType::S8, DataType::QASYMM8, + DataType::U16, DataType::S16, + DataType::U32, DataType::S32, + DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NCHW, DataLayout::NHWC); + + const unsigned int channels = input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)); + + ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups == channels, "Channel shuffling with same number of groups as number of channels would be inefficient"); + ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels); // There cannot be more groups than channels + ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, "The number of channels must be a multiple of the number of groups"); + + // Checks performed when output is configured + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); + } + + return Status{}; +} +void channel_shuffle_nhwc(const ITensor *input, ITensor *output, unsigned int num_groups, const Window &window) +{ + const DataLayout data_layout = input->info()->data_layout(); + const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); + + const size_t element_size = input->info()->element_size(); + const unsigned int K = input->info()->dimension(channel_idx) / num_groups; + const float rK = 1.f / K; + + Iterator in(input, window); + + execute_window_loop(window, [&](const Coordinates & id) + { + // Shuffle channel + const unsigned int curr_channel = id.x(); + const unsigned int group_id = curr_channel * rK; + const unsigned int r = group_id * K; + const unsigned int channel_id = curr_channel - r; + + // Calculate output coordinates + Coordinates out_coords = id; + out_coords.set(Window::DimX, channel_id * num_groups + group_id); + std::copy_n(in.ptr(), element_size, output->ptr_to_element(out_coords)); + }, + in); +} +void channel_shuffle_nchw(const ITensor *input, ITensor *output, unsigned int num_groups, const Window &window) +{ + Window win = window; + win.set(Window::DimX, Window::Dimension(0, 1, 1)); + win.set(Window::DimY, Window::Dimension(0, 1, 1)); + + const DataLayout data_layout = input->info()->data_layout(); + const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); + + const unsigned int height = input->info()->tensor_shape().y(); + const size_t input_stride_y = input->info()->strides_in_bytes().y(); + const size_t output_stride_y = output->info()->strides_in_bytes().y(); + const size_t row_size = input->info()->dimension(width_idx) * input->info()->element_size(); + + const unsigned int K = input->info()->dimension(channel_idx) / num_groups; + const float rK = 1.f / K; + + Iterator in(input, win); + + execute_window_loop(win, [&](const Coordinates & id) + { + // Shuffle channel + const unsigned int curr_channel = id.z(); + const unsigned int group_id = curr_channel * rK; + const unsigned int r = group_id * K; + const unsigned int channel_id = curr_channel - r; + + // Calculate output coordinates + Coordinates out_coords = id; + out_coords.set(Window::DimZ, channel_id * num_groups + group_id); + const uint8_t *input_ptr = in.ptr(); + uint8_t *output_ptr = output->ptr_to_element(out_coords); + + // Copy plane + for(unsigned int y = 0; y < height; ++y) + { + std::copy_n(input_ptr, row_size, output_ptr); + input_ptr += input_stride_y; + output_ptr += output_stride_y; + } + }, + in); +} +} // namespace + +NEChannelShuffleLayerKernel::NEChannelShuffleLayerKernel() + : _input(nullptr), _output(nullptr), _num_groups() +{ +} + +void NEChannelShuffleLayerKernel::configure(const ITensor *input, ITensor *output, unsigned int num_groups) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output->info(), *input->info()->clone()); + + _input = input; + _output = output; + _num_groups = num_groups; + + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), num_groups)); + + // Configure kernel window + Window win = calculate_max_window(*input->info(), Steps()); + + // The NEChannelShuffleLayerKernel doesn't need padding so update_window_and_padding() can be skipped + Coordinates coord; + coord.set_num_dimensions(output->info()->num_dimensions()); + output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); + + INEKernel::configure(win); +} + +Status NEChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups)); + return Status{}; +} + +void NEChannelShuffleLayerKernel::run(const Window &window, const ThreadInfo &info) +{ + ARM_COMPUTE_UNUSED(info); + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + switch(_input->info()->data_layout()) + { + case DataLayout::NHWC: + channel_shuffle_nhwc(_input, _output, _num_groups, window); + break; + case DataLayout::NCHW: + channel_shuffle_nchw(_input, _output, _num_groups, window); + break; + default: + ARM_COMPUTE_ERROR("Unsupported data layout!"); + break; + } +} +} // namespace arm_compute diff --git a/src/graph/backends/NEON/NEFunctionFactory.cpp b/src/graph/backends/NEON/NEFunctionFactory.cpp index e967c1be61..f03cead2b4 100644 --- a/src/graph/backends/NEON/NEFunctionFactory.cpp +++ b/src/graph/backends/NEON/NEFunctionFactory.cpp @@ -189,6 +189,8 @@ std::unique_ptr NEFunctionFactory::create(INode *node, GraphContext & return detail::create_activation_layer(*polymorphic_downcast(node)); case NodeType::BatchNormalizationLayer: return detail::create_batch_normalization_layer(*polymorphic_downcast(node)); + case NodeType::ChannelShuffleLayer: + return detail::create_channel_shuffle_layer(*polymorphic_downcast(node)); case NodeType::ConvolutionLayer: return detail::create_convolution_layer(*polymorphic_downcast(node), ctx); case NodeType::DeconvolutionLayer: diff --git a/src/graph/backends/NEON/NENodeValidator.cpp b/src/graph/backends/NEON/NENodeValidator.cpp index 06023f0714..325b943e81 100644 --- a/src/graph/backends/NEON/NENodeValidator.cpp +++ b/src/graph/backends/NEON/NENodeValidator.cpp @@ -48,7 +48,7 @@ Status NENodeValidator::validate(INode *node) switch(type) { case NodeType::ChannelShuffleLayer: - return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : ChannelShuffleLayer"); + return detail::validate_channel_shuffle_layer(*polymorphic_downcast(node)); case NodeType::ConvolutionLayer: return detail::validate_convolution_layer(); + k->configure(input, output, num_groups); + _kernel = std::move(k); +} + +Status NEChannelShuffleLayer::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) +{ + return NEChannelShuffleLayerKernel::validate(input, output, num_groups); +} +} // namespace arm_compute diff --git a/tests/validation/NEON/ChannelShuffle.cpp b/tests/validation/NEON/ChannelShuffle.cpp new file mode 100644 index 0000000000..474fb49d29 --- /dev/null +++ b/tests/validation/NEON/ChannelShuffle.cpp @@ -0,0 +1,132 @@ +/* + * 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/NEON/functions/NEChannelShuffleLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/NEON/Accessor.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(NEON) +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::F32 })), + shape, num_groups, data_type) +{ + // Create tensors + Tensor ref_src = create_tensor(shape, data_type); + Tensor dst = create_tensor(shape, data_type); + + // Create and Configure function + NEChannelShuffleLayer 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 +using NEChannelShuffleLayerFixture = ChannelShuffleLayerValidationFixture; + +TEST_SUITE(U8) +FIXTURE_DATA_TEST_CASE(RunSmall, NEChannelShuffleLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallRandomChannelShuffleLayerDataset(), + framework::dataset::make("DataType", DataType::U8)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEChannelShuffleLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeRandomChannelShuffleLayerDataset(), + framework::dataset::make("DataType", + DataType::U8)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() // U8 + +TEST_SUITE(Float) +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEChannelShuffleLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallRandomChannelShuffleLayerDataset(), + 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, NEChannelShuffleLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeRandomChannelShuffleLayerDataset(), + framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() // FP16 +#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NEChannelShuffleLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallRandomChannelShuffleLayerDataset(), + 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, NEChannelShuffleLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeRandomChannelShuffleLayerDataset(), + framework::dataset::make("DataType", + DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() // FP32 +TEST_SUITE_END() // Float + +TEST_SUITE_END() // ChannelShuffle +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute -- cgit v1.2.1