blob: 557f71b07da1db342b2e6e8755fa16b0f9e33e6d [file] [log] [blame]
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLGEMMRESHAPERHSMATRIXKERNEL_H
25#define ARM_COMPUTE_CLGEMMRESHAPERHSMATRIXKERNEL_H
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000026
27#include "arm_compute/core/CL/ICLKernel.h"
28
29namespace arm_compute
30{
31class ICLTensor;
32
33/** OpenCL kernel to reshape the RHS matrix when performing the matrix multiplication
34 * In particular, this kernel splits the input matrix in blocks of size K0xN0 and stores each one in
35 * the output matrix unrolling the values */
36class CLGEMMReshapeRHSMatrixKernel : public ICLKernel
37{
38public:
39 /** Default constructor */
40 CLGEMMReshapeRHSMatrixKernel();
41 /** Prevent instances of this class from being copied (As this class contains pointers) */
42 CLGEMMReshapeRHSMatrixKernel(const CLGEMMReshapeRHSMatrixKernel &) = delete;
43 /** Prevent instances of this class from being copied (As this class contains pointers) */
44 CLGEMMReshapeRHSMatrixKernel &operator=(const CLGEMMReshapeRHSMatrixKernel &) = delete;
45 /** Allow instances of this class to be moved */
46 CLGEMMReshapeRHSMatrixKernel(CLGEMMReshapeRHSMatrixKernel &&) = default;
47 /** Allow instances of this class to be moved */
48 CLGEMMReshapeRHSMatrixKernel &operator=(CLGEMMReshapeRHSMatrixKernel &&) = default;
49 /** Initialise the kernel's input and output.
50 *
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010051 * @note If rhs_info.export_to_cl_image = true, this OpenCL kernel will guarantee the OpenCL pitch alignment for the output tensor,
52 * required to create a OpenCL image object from buffer in @ref CLGEMMMatrixMultiplyReshapedKernel and in @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
53 * Since the OpenCL image object is created importing the OpenCL buffer, the following conditions are required:
54 * -# rhs_info.n0 can only be 4, 8 and 16
55 * -# rhs_info.k0 can only be 4, 8 and 16
SiCong Li5bdde852020-08-26 13:55:15 +010056 * -# Data type can only be F32, F16
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010057 * -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension
58 * -# output width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4)
59 * -# output (height * depth) should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT
60 * -# The output tensor should be only consumed by @ref CLGEMMMatrixMultiplyReshapedKernel or @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
61 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000062 * @param[in] input Input tensor. Data types supported: All
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000063 * @param[out] output Output tensor. Data type supported: same as @p input
64 * @param[in] rhs_info RHS matrix information to be used for reshaping. This object contains all the necessary
Manuel Bottini2d2551e2019-03-27 17:31:27 +000065 * information to reshape the input tensor. Only the following values are supported:
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010066 * rhs_info.n0: 2,3,4,8,16 (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
67 * rhs_info.k0: 1,2,3,4,8,16 (k0 = 1 only if rhs_info.transpose = false), (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
Manuel Bottini2d2551e2019-03-27 17:31:27 +000068 * rhs_info.h0: greater than 0
69 * rhs_info.transpose: true, false
70 * rhs_info.interleave: true, false
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000071 */
72 void configure(const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010073 /** Initialise the kernel's input and output.
74 *
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010075 * @note If rhs_info.export_to_cl_image = true, this OpenCL kernel will guarantee the OpenCL pitch alignment for the output tensor,
76 * required to create a OpenCL image object from buffer in @ref CLGEMMMatrixMultiplyReshapedKernel and in @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
77 * Since the OpenCL image object is created importing the OpenCL buffer, the following conditions are required:
78 * -# rhs_info.n0 can only be 4, 8 and 16
79 * -# rhs_info.k0 can only be 4, 8 and 16
SiCong Li5bdde852020-08-26 13:55:15 +010080 * -# Data type can only be F32, F16
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010081 * -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension
82 * -# output width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4)
83 * -# output (height * depth) should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT
84 * -# The output tensor should be only consumed by @ref CLGEMMMatrixMultiplyReshapedKernel or @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
85 *
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086 * @param[in] compile_context The compile context to be used.
87 * @param[in] input Input tensor. Data types supported: All
88 * @param[out] output Output tensor. Data type supported: same as @p input
89 * @param[in] rhs_info RHS matrix information to be used for reshaping. This object contains all the necessary
90 * information to reshape the input tensor. Only the following values are supported:
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010091 * rhs_info.n0: 2,3,4,8,16 (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
92 * rhs_info.k0: 1,2,3,4,8,16 (k0 = 1 only if rhs_info.transpose = false), (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010093 * rhs_info.h0: greater than 0
94 * rhs_info.transpose: true, false
95 * rhs_info.interleave: true, false
96 */
Manuel Bottini679fc962020-04-21 16:08:53 +010097 void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info);
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000098 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMReshapeRHSMatrixKernel
99 *
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100100 * @note If rhs_info.export_to_cl_image = true, this OpenCL kernel will guarantee the OpenCL pitch alignment for the output tensor,
101 * required to create a OpenCL image object from buffer in @ref CLGEMMMatrixMultiplyReshapedKernel and in @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
102 * Since the OpenCL image object is created importing the OpenCL buffer, the following conditions are required:
103 * -# rhs_info.n0 can only be 4, 8 and 16
104 * -# rhs_info.k0 can only be 4, 8 and 16
SiCong Li5bdde852020-08-26 13:55:15 +0100105 * -# Data type can only be F32, F16
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100106 * -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension
107 * -# output width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4)
108 * -# output (height * depth) should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT
109 * -# The output tensor should be only consumed by @ref CLGEMMMatrixMultiplyReshapedKernel or @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
110 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000111 * @param[in] input Input tensor info. Data types supported: All
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000112 * @param[in] output Output tensor info which stores the interleaved matrix. Data type supported: same as @p input.
113 * @param[in] rhs_info RHS matrix information to be used for reshaping. This object contains all the necessary
Manuel Bottini2d2551e2019-03-27 17:31:27 +0000114 * information to reshape the input tensor. Only the following values are supported:
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100115 * rhs_info.n0: 2,3,4,8,16 (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
116 * rhs_info.k0: 1,2,3,4,8,16 (k0 = 1 only if rhs_info.transpose = false),(only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
Manuel Bottini2d2551e2019-03-27 17:31:27 +0000117 * rhs_info.h0: greater than 0
118 * rhs_info.transpose: true, false
119 * rhs_info.interleave: true, false
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000120 *
121 * @return a status
122 */
123 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info);
124
125 // Inherited methods overridden
126 void run(const Window &window, cl::CommandQueue &queue) override;
127
128private:
129 const ICLTensor *_input;
130 ICLTensor *_output;
131};
132} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000133#endif /* ARM_COMPUTE_CLGEMMRESHAPERHSMATRIXKERNEL_H */