• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 Arm Limited.
3  *
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  */
24 #ifndef ARM_COMPUTE_CLGEMMRESHAPERHSMATRIXKERNEL_H
25 #define ARM_COMPUTE_CLGEMMRESHAPERHSMATRIXKERNEL_H
26 
27 #include "src/core/CL/ICLKernel.h"
28 
29 namespace arm_compute
30 {
31 class 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 */
36 class CLGEMMReshapeRHSMatrixKernel : public ICLKernel
37 {
38 public:
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     /** Default destructor */
50     ~CLGEMMReshapeRHSMatrixKernel() = default;
51     /** Initialise the kernel's input and output.
52      *
53      * @note If rhs_info.export_to_cl_image = true, this OpenCL kernel will guarantee the OpenCL pitch alignment for the output tensor,
54      *       required to create a OpenCL image object from buffer in @ref CLGEMMMatrixMultiplyReshapedKernel and in @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
55      *       Since the OpenCL image object is created importing the OpenCL buffer, the following conditions are required:
56      *       -# rhs_info.n0 can only be 4, 8 and 16
57      *       -# rhs_info.k0 can only be 4, 8 and 16
58      *       -# Data type can only be F32, F16
59      *       -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension
60      *       -# output width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4)
61      *       -# output (height * depth) should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT
62      *       -# The output tensor should be only consumed by @ref CLGEMMMatrixMultiplyReshapedKernel or @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
63      *
64      * @param[in]  input    Input tensor. Data types supported: All
65      * @param[out] output   Output tensor. Data type supported: same as @p input
66      * @param[in]  rhs_info RHS matrix information to be used for reshaping. This object contains all the necessary
67      *                      information to reshape the input tensor. Only the following values are supported:
68      *                      rhs_info.n0: 2,3,4,8,16 (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
69      *                      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)
70      *                      rhs_info.h0: greater than 0
71      *                      rhs_info.transpose: true, false
72      *                      rhs_info.interleave: true, false
73      */
74     void configure(const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info);
75     /** Initialise the kernel's input and output.
76      *
77      * @note If rhs_info.export_to_cl_image = true, this OpenCL kernel will guarantee the OpenCL pitch alignment for the output tensor,
78      *       required to create a OpenCL image object from buffer in @ref CLGEMMMatrixMultiplyReshapedKernel and in @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
79      *       Since the OpenCL image object is created importing the OpenCL buffer, the following conditions are required:
80      *       -# rhs_info.n0 can only be 4, 8 and 16
81      *       -# rhs_info.k0 can only be 4, 8 and 16
82      *       -# Data type can only be F32, F16
83      *       -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension
84      *       -# output width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4)
85      *       -# output (height * depth) should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT
86      *       -# The output tensor should be only consumed by @ref CLGEMMMatrixMultiplyReshapedKernel or @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
87      *
88      * @param[in]  compile_context The compile context to be used.
89      * @param[in]  input           Input tensor. Data types supported: All
90      * @param[out] output          Output tensor. Data type supported: same as @p input
91      * @param[in]  rhs_info        RHS matrix information to be used for reshaping. This object contains all the necessary
92      *                             information to reshape the input tensor. Only the following values are supported:
93      *                             rhs_info.n0: 2,3,4,8,16 (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
94      *                             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)
95      *                             rhs_info.h0: greater than 0
96      *                             rhs_info.transpose: true, false
97      *                             rhs_info.interleave: true, false
98      */
99     void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info);
100     /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMReshapeRHSMatrixKernel
101      *
102      * @note If rhs_info.export_to_cl_image = true, this OpenCL kernel will guarantee the OpenCL pitch alignment for the output tensor,
103      *       required to create a OpenCL image object from buffer in @ref CLGEMMMatrixMultiplyReshapedKernel and in @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
104      *       Since the OpenCL image object is created importing the OpenCL buffer, the following conditions are required:
105      *       -# rhs_info.n0 can only be 4, 8 and 16
106      *       -# rhs_info.k0 can only be 4, 8 and 16
107      *       -# Data type can only be F32, F16
108      *       -# The platform should support the OpenCL cl_khr_image2d_from_buffer extension
109      *       -# output width should be less or equal to (CL_DEVICE_IMAGE2D_MAX_WIDTH * 4)
110      *       -# output (height * depth) should be less or equal to CL_DEVICE_IMAGE2D_MAX_HEIGHT
111      *       -# The output tensor should be only consumed by @ref CLGEMMMatrixMultiplyReshapedKernel or @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
112      *
113      * @param[in] input    Input tensor info. Data types supported: All
114      * @param[in] output   Output tensor info which stores the interleaved matrix. Data type supported: same as @p input.
115      * @param[in] rhs_info RHS matrix information to be used for reshaping. This object contains all the necessary
116      *                     information to reshape the input tensor. Only the following values are supported:
117      *                     rhs_info.n0: 2,3,4,8,16 (only 4, 8 and 16 if rhs_info.export_to_cl_image == true)
118      *                     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)
119      *                     rhs_info.h0: greater than 0
120      *                     rhs_info.transpose: true, false
121      *                     rhs_info.interleave: true, false
122      *
123      * @return a status
124      */
125     static Status validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info);
126 
127     // Inherited methods overridden
128     void run(const Window &window, cl::CommandQueue &queue) override;
129 
130 private:
131     const ICLTensor *_input;
132     ICLTensor       *_output;
133 };
134 } // namespace arm_compute
135 #endif /* ARM_COMPUTE_CLGEMMRESHAPERHSMATRIXKERNEL_H */