From 5ba5e0938e68d4f90f5545a81066d56f022b376a Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Thu, 6 Dec 2018 17:13:09 +0000 Subject: COMPMID-1774: Implement CLGEMMReshapeLHSMatrixKernel to reshape the LHS matrix of GEMM/GEMMLowp Change-Id: I8c5fd4c8bcdffda1522c83158981ed92baa045f4 Reviewed-on: https://review.mlplatform.org/364 Reviewed-by: Michele Di Giorgio Tested-by: Arm Jenkins --- arm_compute/core/CL/CLKernels.h | 1 + .../core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h | 90 ++++++++++++++++++++++ arm_compute/core/Types.h | 10 +++ arm_compute/core/utils/misc/ShapeCalculator.h | 37 +++++++++ 4 files changed, 138 insertions(+) create mode 100644 arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h (limited to 'arm_compute/core') diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h index 9d7ec71a32..7bfd44721f 100644 --- a/arm_compute/core/CL/CLKernels.h +++ b/arm_compute/core/CL/CLKernels.h @@ -79,6 +79,7 @@ #include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h" #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h" #include "arm_compute/core/CL/kernels/CLGEMMMatrixVectorMultiplyKernel.h" +#include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h" #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h" #include "arm_compute/core/CL/kernels/CLGaussian3x3Kernel.h" #include "arm_compute/core/CL/kernels/CLGaussian5x5Kernel.h" diff --git a/arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h b/arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h new file mode 100644 index 0000000000..77d7494a7f --- /dev/null +++ b/arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h @@ -0,0 +1,90 @@ +/* + * 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_CLGEMMRESHAPELHSMATRIXKERNEL_H__ +#define __ARM_COMPUTE_CLGEMMRESHAPELHSMATRIXKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" + +namespace arm_compute +{ +class ICLTensor; + +/** OpenCL kernel to reshape the LHS matrix when performing the matrix multiplication. + * In particular, this function splits the input matrix in blocks of size M0xK0 (defined through GEMMLHSInfo) and + * stores each one in the output matrix unrolling the values + */ +class CLGEMMReshapeLHSMatrixKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLGEMMReshapeLHSMatrixKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLGEMMReshapeLHSMatrixKernel(const CLGEMMReshapeLHSMatrixKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLGEMMReshapeLHSMatrixKernel &operator=(const CLGEMMReshapeLHSMatrixKernel &) = delete; + /** Allow instances of this class to be moved */ + CLGEMMReshapeLHSMatrixKernel(CLGEMMReshapeLHSMatrixKernel &&) = default; + /** Allow instances of this class to be moved */ + CLGEMMReshapeLHSMatrixKernel &operator=(CLGEMMReshapeLHSMatrixKernel &&) = default; + /** Initialise the kernel's input and output. + * + * @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] lhs_info LHS matrix information to be used for reshaping. This object contains all the necessary + * information to reshape the input tensor. Only the following values are supported: + * lhs_info.m0: 2,3,4,5,6,7,8 + * lhs_info.k0: 2,4,8,16 + * lhs_info.v0: greater than 0 + * lhs_info.transpose: false + * lhs_info.interleave: true, false + * @param[in] reinterpret_input_as_3d (Optional) True if the input has to be reinterpreted as 3D tensor + */ + void configure(const ICLTensor *input, ICLTensor *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false); + /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMReshapeLHSMatrixKernel + * + * @param[in] input Input tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32 + * @param[in] output Output tensor info which stores the interleaved matrix. Data type supported: same as @p input. + * @param[in] lhs_info LHS matrix information to be used for reshaping. This object contains all the necessary + * information to reshape the input tensor. Only the following values are supported: + * lhs_info.m0: 2,3,4,5,6,7,8 + * lhs_info.k0: 2,4,8,16 + * lhs_info.v0: greater than 0 + * lhs_info.transpose: false + * lhs_info.interleave: true, false + * @param[in] reinterpret_input_as_3d True if the input has to be reinterpreted as 3D tensor + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d); + + // Inherited methods overridden + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_input; + ICLTensor *_output; + bool _reinterpret_input_as_3d; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLGEMMRESHAPELHSMATRIXKERNEL_H__ */ \ No newline at end of file diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 75b38c5cb8..55b0ccb30d 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -1771,6 +1771,16 @@ struct GEMMLowpOutputStageInfo int gemmlowp_max_bound{ 0 }; /**< GEMMLowp max value used to saturate down the output result before converting back to QASYMM8 */ }; +/** GEMM LHS (Left Hand Side) matrix information */ +struct GEMMLHSMatrixInfo +{ + unsigned int m0{ 1 }; /**< Number of rows processed by the matrix multiplication */ + unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */ + unsigned int v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */ + bool transpose{ true }; /**< True if the (m0xk0) block has to be transposed before been stored */ + bool interleave{ true }; /**< True if the v0 (m0xk0) blocks have to be interleaved in the output row */ +}; + /** GEMM information class. This class stores the necessary information to compute GEMM functions * * This object also contains the information about how matrix A and matrix B have been reshaped diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index c625a07a7f..88ce8d9e7b 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -101,6 +101,43 @@ inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bo return weights_reshaped; } +inline TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false) +{ + ARM_COMPUTE_ERROR_ON(lhs_info.m0 == 0); + ARM_COMPUTE_ERROR_ON(lhs_info.k0 == 0); + ARM_COMPUTE_ERROR_ON(lhs_info.v0 == 0); + + // Input width/height + const unsigned int input_width = a.dimension(0); + const unsigned int input_height = reinterpret_input_as_3d ? a.dimension(1) * a.dimension(2) : a.dimension(1); + + // Number of horizontal/vertical blocks in the input tensor + const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast(lhs_info.k0)); + const unsigned int num_vert_blocks = std::ceil(input_height / static_cast(lhs_info.m0)); + + // Block size + const unsigned int block_size = lhs_info.m0 * lhs_info.k0; + + // Output width/height + const unsigned int output_width = block_size * num_horiz_blocks * lhs_info.v0; + const unsigned int output_height = std::ceil(num_vert_blocks / static_cast(lhs_info.v0)); + + TensorShape lhs_shape{ a.tensor_shape() }; + lhs_shape.set(0, output_width); + lhs_shape.set(1, output_height); + + if((reinterpret_input_as_3d) && (lhs_shape.num_dimensions() > 2)) + { + // When the data format is NHWC and the shapes are Nx1x1 + // the tensor shape num_dimensions is automatically set to 1 instead of 3. + // To avoid failures by removing a dimension that doesn't exist + // check if the number of dimensions is greater than 2. + lhs_shape.remove_dimension(2); + } + + return lhs_shape; +} + inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1, bool reinterpret_input_as_3d = false) { // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height -- cgit v1.2.1