From c8e6e2c48e558da0c0698428fe496491e18c022a Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Thu, 2 Jul 2020 11:27:27 +0100 Subject: COMPMID-3325: Add support in gemm_tuner for cl_image Change-Id: I78f815005516ca0e83366bab017884530c1d2e86 Signed-off-by: Manuel Bottini Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3518 Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins --- arm_compute/core/CL/gemm/CLGEMMHelpers.h | 9 +++- examples/gemm_tuner/cl_gemm_reshaped.cpp | 51 +++++++++++------- examples/gemm_tuner/cl_gemm_reshaped_rhs_only.cpp | 60 +++++++++++++--------- src/core/CL/gemm/CLGEMMHelpers.cpp | 17 +++++- .../CL/kernels/CLGEMMReshapeRHSMatrixKernel.cpp | 11 +--- 5 files changed, 94 insertions(+), 54 deletions(-) diff --git a/arm_compute/core/CL/gemm/CLGEMMHelpers.h b/arm_compute/core/CL/gemm/CLGEMMHelpers.h index b2d9f21c3d..a370f9171a 100644 --- a/arm_compute/core/CL/gemm/CLGEMMHelpers.h +++ b/arm_compute/core/CL/gemm/CLGEMMHelpers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Arm Limited. + * Copyright (c) 2019-2020 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -24,6 +24,7 @@ #ifndef ARM_COMPUTE_CLGEMMHELPERS_H #define ARM_COMPUTE_CLGEMMHELPERS_H +#include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Types.h" namespace arm_compute @@ -48,6 +49,12 @@ namespace cl_gemm */ std::pair configure_lhs_rhs_info(unsigned int m, unsigned int n, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int v0, unsigned int h0, bool lhs_interleave, bool rhs_interleave, bool lhs_transpose, bool rhs_transpose); + +/** Update padding required to export the OpenCL buffer to OpenCL image2d + * + * @param[in,out] tensor ITensorInfo of the tensor required to be exported to OpenCL image2d + */ +void update_padding_for_cl_image(ITensorInfo *tensor); } // namespace cl_gemm } // namespace arm_compute #endif /*ARM_COMPUTE_CLGEMMHELPERS_H */ diff --git a/examples/gemm_tuner/cl_gemm_reshaped.cpp b/examples/gemm_tuner/cl_gemm_reshaped.cpp index 8000e1ab7b..93125dd4a3 100644 --- a/examples/gemm_tuner/cl_gemm_reshaped.cpp +++ b/examples/gemm_tuner/cl_gemm_reshaped.cpp @@ -26,6 +26,7 @@ #endif /* ARM_COMPUTE_CL */ #include "CommonGemmExampleOptions.h" +#include "arm_compute/core/CL/gemm/CLGEMMHelpers.h" #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h" #include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h" #include "arm_compute/core/Helpers.h" @@ -52,15 +53,16 @@ namespace /** Structure holding all tunable gemm configs specific to this example/strategy */ struct GemmConfigs { - size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */ - size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */ - size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */ - size_t v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */ - size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ - bool interleave_lhs{ true }; /**< Interleave lhs matrix */ - bool transpose_lhs{ true }; /**< Transpose lhs matrix. */ - bool interleave_rhs{ true }; /**< Interleave rhs matrix */ - bool transpose_rhs{ true }; /**< Transpose rhs matrix. */ + size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */ + size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */ + size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */ + size_t v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */ + size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ + bool interleave_lhs{ true }; /**< Interleave lhs matrix */ + bool transpose_lhs{ true }; /**< Transpose lhs matrix. */ + bool interleave_rhs{ true }; /**< Interleave rhs matrix */ + bool transpose_rhs{ true }; /**< Transpose rhs matrix. */ + bool export_to_cl_image_rhs{ true }; /**< Export rhs matrix to cl_image. */ }; /** Formatted output of the GemmConfigs type @@ -84,6 +86,7 @@ struct GemmConfigs os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl; os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl; os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl; + os << "export_to_cl_image_rhs : " << (configs.export_to_cl_image_rhs ? true_str : false_str) << std::endl; return os; } @@ -103,7 +106,8 @@ public: h0(parser.add_positional_option>("h0", 1)), interleave_lhs(parser.add_positional_option>("interleave_lhs", 1)), interleave_rhs(parser.add_positional_option>("interleave_rhs", 1)), - transpose_rhs(parser.add_positional_option>("transpose_rhs", 1)) + transpose_rhs(parser.add_positional_option>("transpose_rhs", 1)), + export_to_cl_image_rhs(parser.add_positional_option>("export_to_cl_image_rhs", 1)) { m0->set_help("Number of rows processed by the matrix multiplication"); n0->set_help("Number of columns processed by the matrix multiplication"); @@ -116,6 +120,7 @@ public: // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other // 2 variants (both transposed and none transposed) transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0)"); + export_to_cl_image_rhs->set_help("Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)"); } /** Prevent instances of this class from being copied (As this class contains pointers) */ GemmConfigOptions(const GemmConfigOptions &) = delete; @@ -138,7 +143,8 @@ public: // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other // 2 variants (both transposed and none transposed) - SimpleOption *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */ + SimpleOption *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */ + SimpleOption *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/ }; /** Consumes the gemm configuration options and creates a structure containing all information @@ -159,9 +165,10 @@ GemmConfigs consume_gemm_configs(const GemmConfigOptions &options) // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other // 2 variants (both transposed and none transposed) - configs.transpose_lhs = options.transpose_rhs->value() == 0; - configs.interleave_rhs = options.interleave_rhs->value() != 0; - configs.transpose_rhs = options.transpose_rhs->value() != 0; + configs.transpose_lhs = options.transpose_rhs->value() == 0; + configs.interleave_rhs = options.interleave_rhs->value() != 0; + configs.transpose_rhs = options.transpose_rhs->value() != 0; + configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0; return configs; } @@ -230,11 +237,12 @@ public: lhs_info.transpose = configs.transpose_lhs; GEMMRHSMatrixInfo rhs_info; - rhs_info.n0 = configs.n0; - rhs_info.k0 = configs.k0; - rhs_info.h0 = configs.h0; - rhs_info.interleave = configs.interleave_rhs; - rhs_info.transpose = configs.transpose_rhs; + rhs_info.n0 = configs.n0; + rhs_info.k0 = configs.k0; + rhs_info.h0 = configs.h0; + rhs_info.interleave = configs.interleave_rhs; + rhs_info.transpose = configs.transpose_rhs; + rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs; GEMMKernelInfo kernel_info; kernel_info.m = params.M; @@ -251,6 +259,11 @@ public: // Initialise rhs_reshaped tensor info auto_init_if_empty(*rhs_reshaped.info(), rhs.info()->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*rhs.info(), rhs_info))); + if(rhs_info.export_to_cl_image) + { + arm_compute::cl_gemm::update_padding_for_cl_image(rhs_reshaped.info()); + } + // Configure reshape lhs function reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); diff --git a/examples/gemm_tuner/cl_gemm_reshaped_rhs_only.cpp b/examples/gemm_tuner/cl_gemm_reshaped_rhs_only.cpp index 4e7b93dba8..45a28abca6 100644 --- a/examples/gemm_tuner/cl_gemm_reshaped_rhs_only.cpp +++ b/examples/gemm_tuner/cl_gemm_reshaped_rhs_only.cpp @@ -26,6 +26,7 @@ #endif /* ARM_COMPUTE_CL */ #include "CommonGemmExampleOptions.h" +#include "arm_compute/core/CL/gemm/CLGEMMHelpers.h" #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.h" #include "arm_compute/core/Helpers.h" #include "arm_compute/core/KernelDescriptors.h" @@ -51,12 +52,13 @@ namespace /** Structure holding all tunable gemm configs specific to this example/strategy */ struct GemmConfigs { - size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */ - size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */ - size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */ - size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ - bool interleave_rhs{ true }; /**< Interleave rhs matrix */ - bool transpose_rhs{ true }; /**< Transpose rhs matrix */ + size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */ + size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */ + size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */ + size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ + bool interleave_rhs{ true }; /**< Interleave rhs matrix */ + bool transpose_rhs{ true }; /**< Transpose rhs matrix */ + bool export_to_cl_image_rhs{ true }; /**< Export rhs matrix to cl_image.*/ }; /** Formatted output of the GemmConfigs type @@ -77,6 +79,7 @@ struct GemmConfigs os << "h0 : " << configs.h0 << std::endl; os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl; os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl; + os << "export_to_cl_image_rhs : " << (configs.export_to_cl_image_rhs ? true_str : false_str) << std::endl; return os; } @@ -94,7 +97,8 @@ public: k0(parser.add_positional_option>("k0", 4)), h0(parser.add_positional_option>("h0", 1)), interleave_rhs(parser.add_positional_option>("interleave_rhs", 1)), - transpose_rhs(parser.add_positional_option>("transpose_rhs", 1)) + transpose_rhs(parser.add_positional_option>("transpose_rhs", 1)), + export_to_cl_image_rhs(parser.add_positional_option>("export_to_cl_image_rhs", 1)) { m0->set_help("Number of rows processed by the matrix multiplication"); n0->set_help("Number of columns processed by the matrix multiplication"); @@ -102,6 +106,7 @@ public: h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row"); interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)"); transpose_rhs->set_help("Transpose rhs matrix (1) / Do not transpose rhs matrix (0)"); + export_to_cl_image_rhs->set_help("Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)"); } /** Prevent instances of this class from being copied (As this class contains pointers) */ GemmConfigOptions(const GemmConfigOptions &) = delete; @@ -114,12 +119,13 @@ public: /** Default destructor */ ~GemmConfigOptions() = default; - SimpleOption *m0; /**< Number of rows processed by the matrix multiplication option */ - SimpleOption *n0; /**< Number of columns processed by the matrix multiplication option */ - SimpleOption *k0; /**< Number of partial accumulations performed by the matrix multiplication option */ - SimpleOption *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */ - SimpleOption *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */ - SimpleOption *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable) */ + SimpleOption *m0; /**< Number of rows processed by the matrix multiplication option */ + SimpleOption *n0; /**< Number of columns processed by the matrix multiplication option */ + SimpleOption *k0; /**< Number of partial accumulations performed by the matrix multiplication option */ + SimpleOption *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */ + SimpleOption *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */ + SimpleOption *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable) */ + SimpleOption *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/ }; /** Consumes the gemm configuration options and creates a structure containing all information @@ -131,12 +137,13 @@ public: GemmConfigs consume_gemm_configs(const GemmConfigOptions &options) { GemmConfigs configs; - configs.m0 = options.m0->value(); - configs.n0 = options.n0->value(); - configs.k0 = options.k0->value(); - configs.h0 = options.h0->value(); - configs.interleave_rhs = options.interleave_rhs->value() != 0; - configs.transpose_rhs = options.transpose_rhs->value() != 0; + configs.m0 = options.m0->value(); + configs.n0 = options.n0->value(); + configs.k0 = options.k0->value(); + configs.h0 = options.h0->value(); + configs.interleave_rhs = options.interleave_rhs->value() != 0; + configs.transpose_rhs = options.transpose_rhs->value() != 0; + configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0; return configs; } @@ -200,11 +207,12 @@ public: lhs_info.k0 = configs.k0; GEMMRHSMatrixInfo rhs_info; - rhs_info.n0 = configs.n0; - rhs_info.k0 = configs.k0; - rhs_info.h0 = configs.h0; - rhs_info.interleave = configs.interleave_rhs; - rhs_info.transpose = configs.transpose_rhs; + rhs_info.n0 = configs.n0; + rhs_info.k0 = configs.k0; + rhs_info.h0 = configs.h0; + rhs_info.interleave = configs.interleave_rhs; + rhs_info.transpose = configs.transpose_rhs; + rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs; GEMMKernelInfo kernel_info; kernel_info.m = params.M; @@ -218,6 +226,10 @@ public: // Initialise rhs_reshaped tensor info auto_init_if_empty(*rhs_reshaped.info(), rhs.info()->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*rhs.info(), rhs_info))); + if(rhs_info.export_to_cl_image) + { + arm_compute::cl_gemm::update_padding_for_cl_image(rhs_reshaped.info()); + } // Configure function gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); diff --git a/src/core/CL/gemm/CLGEMMHelpers.cpp b/src/core/CL/gemm/CLGEMMHelpers.cpp index 8f54d3f670..1165e70273 100644 --- a/src/core/CL/gemm/CLGEMMHelpers.cpp +++ b/src/core/CL/gemm/CLGEMMHelpers.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Arm Limited. + * Copyright (c) 2019-2020 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -23,6 +23,8 @@ */ #include "arm_compute/core/CL/gemm/CLGEMMHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" + #include namespace arm_compute @@ -51,5 +53,18 @@ std::pair configure_lhs_rhs_info(unsigned return std::make_pair(lhs_info, rhs_info); } + +void update_padding_for_cl_image(ITensorInfo *tensor) +{ + constexpr unsigned int num_floats_per_pixel = 4; + + const unsigned int stride_y_in_elements = tensor->strides_in_bytes()[1] / tensor->element_size(); + const unsigned int pixel_aligment = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()); + const unsigned int row_pitch_alignment = pixel_aligment * num_floats_per_pixel; + const unsigned int round_up_width = ((stride_y_in_elements + row_pitch_alignment - 1) / row_pitch_alignment) * row_pitch_alignment; + const unsigned int padding = round_up_width - stride_y_in_elements; + + tensor->extend_padding(PaddingSize(0, padding, 0, 0)); +} } // namespace cl_gemm } // namespace arm_compute \ No newline at end of file diff --git a/src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.cpp b/src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.cpp index 5e09899ec8..c57066ae03 100644 --- a/src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.cpp +++ b/src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.cpp @@ -29,6 +29,7 @@ #include "arm_compute/core/CL/CLValidate.h" #include "arm_compute/core/CL/ICLTensor.h" #include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/CL/gemm/CLGEMMHelpers.h" #include "arm_compute/core/Error.h" #include "arm_compute/core/Helpers.h" #include "arm_compute/core/TensorInfo.h" @@ -107,15 +108,7 @@ std::pair validate_and_configure_window(ITensorInfo *input, ITen if(rhs_info.export_to_cl_image) { - constexpr unsigned int num_floats_per_pixel = 4; - - const unsigned int stride_y_in_elements = output->strides_in_bytes()[1] / output->element_size(); - const unsigned int pixel_aligment = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()); - const unsigned int row_pitch_alignment = pixel_aligment * num_floats_per_pixel; - const unsigned int round_up_width = ((stride_y_in_elements + row_pitch_alignment - 1) / row_pitch_alignment) * row_pitch_alignment; - const unsigned int padding = round_up_width - stride_y_in_elements; - - output->extend_padding(PaddingSize(0, padding, 0, 0)); + arm_compute::cl_gemm::update_padding_for_cl_image(output); } // Collapse along the Z direction -- cgit v1.2.1