From ba5e096b8b2a9f777695844746ec3ff1ef90ade8 Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Mon, 11 Mar 2019 12:17:44 +0000 Subject: COMPMID-1964: Implement CLGEMMMatrixMultiplyReshapedOnlyRHS - Not transposed Change-Id: I6b7f7a406fcb8d64adb221a24d7983e11bcb391e Signed-off-by: Gian Marco Iodice Reviewed-on: https://review.mlplatform.org/c/846 Tested-by: Arm Jenkins Reviewed-by: Michele Di Giorgio --- src/core/CL/CLKernelLibrary.cpp | 1 + src/core/CL/cl_kernels/gemm.cl | 509 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 508 insertions(+), 2 deletions(-) (limited to 'src/core') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 7476e19011..b7973efa9d 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -284,6 +284,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "gemm_mm_floating_point_f32_bifrost", "gemm.cl" }, { "gemm_mm_floating_point_f32_bifrost_1000", "gemm.cl" }, { "gemm_mm_reshaped_lhs_nt_rhs_t", "gemm.cl" }, + { "gemm_mm_reshaped_only_rhs_nt", "gemm.cl" }, { "gemm_mm_reshaped_only_rhs_t", "gemm.cl" }, { "gemm_lc_vm_f32", "gemm.cl" }, { "gemm_transpose1xW", "gemm.cl" }, diff --git a/src/core/CL/cl_kernels/gemm.cl b/src/core/CL/cl_kernels/gemm.cl index a245aeead3..211484440b 100644 --- a/src/core/CL/cl_kernels/gemm.cl +++ b/src/core/CL/cl_kernels/gemm.cl @@ -1314,10 +1314,10 @@ __kernel void gemm_mm_reshaped_only_rhs_t(IMAGE_DECLARATION(lhs), #endif // REINTERPRET_OUTPUT_AS_3D ) { -// Block size + // Block size #define RHS_BLOCK_SIZE ((K0) * (N0)) -// RHS offset and step X + // RHS offset and step X #if defined(RHS_INTERLEAVE) #define RHS_OFFSET_X (K0) #define RHS_STEP_X ((K0) * (H0)) @@ -1750,6 +1750,511 @@ __kernel void gemm_mm_reshaped_only_rhs_t(IMAGE_DECLARATION(lhs), (c7, 0, (__global DATA_TYPE *)(dst_addr + 7 * dst_stride_y + zout7)); #endif // M0 > 7 +#undef RHS_BLOCK_SIZE +#undef RHS_OFFSET_X +#undef RHS_STEP_X +} + +#define VFMA(a, b, c) \ + ({ \ + c = fma(a, b, c); \ + }) + +#if M0 == 1 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + }) +#elif M0 == 2 // M0 == 2 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + }) +#elif M0 == 3 // M0 == 3 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ + }) +#elif M0 == 4 // M0 == 4 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ + }) +#elif M0 == 5 // M0 == 5 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ + }) +#elif M0 == 6 // M0 == 6 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##5).s##i), b, (c##5)); \ + }) +#elif M0 == 7 // M0 == 7 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##5).s##i), b, (c##5)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##6).s##i), b, (c##6)); \ + }) +#elif M0 == 8 // M0 == 8 +#define LD_RHS_VFMA_M0xN0(i, a, c) \ + ({ \ + VEC_DATA_TYPE(DATA_TYPE, N0) \ + b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0x##i * RHS_STEP_X * sizeof(DATA_TYPE))); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##5).s##i), b, (c##5)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##6).s##i), b, (c##6)); \ + VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##7).s##i), b, (c##7)); \ + }) +#else // M0 not supported +#error "M0 not supported" +#endif // M0 not supported + +/** This OpenCL kernel computes the matrix multiplication between 2 matrices. + * The LHS matrix is NOT reshaped + * The RHS is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is NOT transposed + * + * @note The block's dimensions used for reshaping the RHS matrix (N0 and K0) must be passed at compile time using -DN0 and -DK0 (i.e. -DN0=8, -DK0=4). + * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2) + * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2) + * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time. + * @note Only the following configurations of M0, N0 and K0 are currently supported: + * - M0 = 1, 2, 3, 4, 5, 6, 7, 8 + * - N0 = 2, 3, 4, 8, 16 + * - K0 = 2, 3, 4, 8, 16 + * - H0 > 1 + * + * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time: + * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D + * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D + * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor. + * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor + * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix + * + * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: F16/F32 + * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes) + * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes) + * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix + * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr + * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes) + * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes) + * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix + * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr + * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) + * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) + * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix + * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes) + * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes) + * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) + * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D) + * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D) + */ +__kernel void gemm_mm_reshaped_only_rhs_nt(IMAGE_DECLARATION(lhs), + IMAGE_DECLARATION(rhs), + IMAGE_DECLARATION(dst), + uint lhs_stride_z, + uint rhs_stride_z, + uint dst_stride_z +#if defined(REINTERPRET_INPUT_AS_3D) + , + uint lhs_cross_plane_pad +#endif // REINTERPRET_INPUT_AS_3D +#if defined(REINTERPRET_OUTPUT_AS_3D) + , + uint dst_cross_plane_pad +#endif // REINTERPRET_OUTPUT_AS_3D + ) +{ + // Block size +#define RHS_BLOCK_SIZE ((K0) * (N0)) + + // RHS offset and step X +#if defined(RHS_INTERLEAVE) +#define RHS_OFFSET_X (N0) +#define RHS_STEP_X ((N0) * (H0)) +#define RHS_STEP_LOOP (1) +#else // defined(RHS_INTERLEAVE) +#define RHS_OFFSET_X (RHS_BLOCK_SIZE) +#define RHS_STEP_X (N0) +#define RHS_STEP_LOOP (H0) +#endif // defined(RHS_INTERLEAVE) + + uint x = get_global_id(0); + uint y = get_global_id(1); + uint z = get_global_id(2); + + // Compute LHS matrix address + uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y; + + // Compute RHS matrix address + uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X * sizeof(DATA_TYPE) + (x / (uint)H0) * rhs_stride_y; + +#if defined(MATRIX_B_DEPTH) + // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3 + rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z; +#else // defined(MATRIX_B_DEPTH) + rhs_offset += z * rhs_stride_z; +#endif // defined(MATRIX_B_DEPTH) + + REPEAT_VAR_INIT_TO_CONST(8, uint, zin, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; + +#if defined(REINTERPRET_INPUT_AS_3D) + // Since we store a 2D output tile in a 3D tensor, we need to check when the plane changes across the z dimension + // in order to take into account the presence of possible cross plane paddings + // + // | | + // | plane0 | + // | | + // |__________________| + // |******************| + // | cross_plane_pad | + // |******************| + // | | + // | plane1 | + // | | + // |__________________| + + // The plane (zin) is calculated dividing M (y * M0) by HEIGHT_GEMM3D + zin0 = (0 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin0 = min((uint)(DEPTH_GEMM3D - 1), zin0); + zin0 *= (lhs_cross_plane_pad * lhs_stride_y); +#if M0 > 1 + zin1 = (1 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin1 = min((uint)(DEPTH_GEMM3D - 1), zin1); + zin1 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 1 +#if M0 > 2 + zin2 = (2 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin2 = min((uint)(DEPTH_GEMM3D - 1), zin2); + zin2 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 2 +#if M0 > 3 + zin3 = (3 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin3 = min((uint)(DEPTH_GEMM3D - 1), zin3); + zin3 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 3 +#if M0 > 4 + zin4 = (4 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin4 = min((uint)(DEPTH_GEMM3D - 1), zin4); + zin4 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 4 +#if M0 > 5 + zin5 = (5 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin5 = min((uint)(DEPTH_GEMM3D - 1), zin5); + zin5 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 5 +#if M0 > 6 + zin6 = (6 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin6 = min((uint)(DEPTH_GEMM3D - 1), zin6); + zin6 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 6 +#if M0 > 7 + zin7 = (7 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zin7 = min((uint)(DEPTH_GEMM3D - 1), zout7); + zin7 *= (lhs_cross_plane_pad * lhs_stride_y); +#endif // M0 > 7 + + // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we + // multiply lhs_stride_z by DEPTH_GEMM3D + lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D; + +#else // defined(REINTERPRET_INPUT_AS_3D) + + // Add offset for batched GEMM + lhs_offset += z * lhs_stride_z; + +#endif // defined(REINTERPRET_INPUT_AS_3D) + + // Initialize the accumulators + REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(N0-1)=0; + + int i = 0; + for(; i <= (K - K0); i += K0) + { + // Supported cases (M0, K0): + // 1,2 - 1,3 - 1,4 - 1,8 - 1,16 + // 2,2 - 2,3 - 2,4 - 2,8 - 2,16 + // 3,2 - 3,3 - 3,4 - 3,8 - 3,16 + // 4,2 - 4,3 - 4,4 - 4,8 - 4,16 + // 5,2 - 5,3 - 5,4 - 5,8 - 5,16 + // 6,2 - 6,3 - 6,4 - 6,8 - 6,16 + // 7,2 - 7,3 - 7,4 - 7,8 - 7,16 + // 8,2 - 8,3 - 8,4 - 8,8 - 8,16 + // Load values from LHS matrix + VEC_DATA_TYPE(DATA_TYPE, K0) + a0 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 0 * lhs_stride_y + zin0)); +#if M0 > 1 + VEC_DATA_TYPE(DATA_TYPE, K0) + a1 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 1 * lhs_stride_y + zin1)); +#endif // M0 > 1 +#if M0 > 2 + VEC_DATA_TYPE(DATA_TYPE, K0) + a2 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 2 * lhs_stride_y + zin2)); +#endif // M0 > 2 +#if M0 > 3 + VEC_DATA_TYPE(DATA_TYPE, K0) + a3 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 3 * lhs_stride_y + zin3)); +#endif // M0 > 3 +#if M0 > 4 + VEC_DATA_TYPE(DATA_TYPE, K0) + a4 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 4 * lhs_stride_y + zin4)); +#endif // M0 > 4 +#if M0 > 5 + VEC_DATA_TYPE(DATA_TYPE, K0) + a5 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 5 * lhs_stride_y + zin5)); +#endif // M0 > 5 +#if M0 > 6 + VEC_DATA_TYPE(DATA_TYPE, K0) + a6 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 6 * lhs_stride_y + zin6)); +#endif // M0 > 6 +#if M0 > 7 + VEC_DATA_TYPE(DATA_TYPE, K0) + a7 = VLOAD(K0)(0, (__global DATA_TYPE *)(lhs_ptr + lhs_offset + 7 * lhs_stride_y + zin7)); +#endif // M0 > 7 + + LD_RHS_VFMA_M0xN0(0, a, c); + LD_RHS_VFMA_M0xN0(1, a, c); +#if K0 > 2 + LD_RHS_VFMA_M0xN0(2, a, c); +#endif // K0 > 2 +#if K0 > 3 + LD_RHS_VFMA_M0xN0(3, a, c); +#endif // K0 > 3 +#if K0 > 4 + LD_RHS_VFMA_M0xN0(4, a, c); + LD_RHS_VFMA_M0xN0(5, a, c); + LD_RHS_VFMA_M0xN0(6, a, c); + LD_RHS_VFMA_M0xN0(7, a, c); +#endif // K0 > 4 +#if K0 > 8 + LD_RHS_VFMA_M0xN0(8, a, c); + LD_RHS_VFMA_M0xN0(9, a, c); + LD_RHS_VFMA_M0xN0(A, a, c); + LD_RHS_VFMA_M0xN0(B, a, c); + LD_RHS_VFMA_M0xN0(C, a, c); + LD_RHS_VFMA_M0xN0(D, a, c); + LD_RHS_VFMA_M0xN0(E, a, c); + LD_RHS_VFMA_M0xN0(F, a, c); +#endif // K0 > 8 + + lhs_offset += K0 * sizeof(DATA_TYPE); + rhs_offset += K0 * RHS_STEP_X * RHS_STEP_LOOP * sizeof(DATA_TYPE); + } + + // Left-over accumulations + for(; i < K; ++i) + { + // Load values from LHS matrix + VEC_DATA_TYPE(DATA_TYPE, 2) + a0 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 0 * lhs_stride_y + zin0)); +#if M0 > 1 + VEC_DATA_TYPE(DATA_TYPE, 2) + a1 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 1 * lhs_stride_y + zin1)); +#endif // M0 > 1 +#if M0 > 2 + VEC_DATA_TYPE(DATA_TYPE, 2) + a2 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 2 * lhs_stride_y + zin2)); +#endif // M0 > 2 +#if M0 > 3 + VEC_DATA_TYPE(DATA_TYPE, 2) + a3 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 3 * lhs_stride_y + zin3)); +#endif // M0 > 3 +#if M0 > 4 + VEC_DATA_TYPE(DATA_TYPE, 2) + a4 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 4 * lhs_stride_y + zin4)); +#endif // M0 > 4 +#if M0 > 5 + VEC_DATA_TYPE(DATA_TYPE, 2) + a5 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 5 * lhs_stride_y + zin5)); +#endif // M0 > 5 +#if M0 > 6 + VEC_DATA_TYPE(DATA_TYPE, 2) + a6 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 6 * lhs_stride_y + zin6)); +#endif // M0 > 6 +#if M0 > 7 + VEC_DATA_TYPE(DATA_TYPE, 2) + a7 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 7 * lhs_stride_y + zin)); +#endif // M0 > 7 + + LD_RHS_VFMA_M0xN0(0, a, c); + + lhs_offset += sizeof(DATA_TYPE); + rhs_offset += RHS_STEP_X * sizeof(DATA_TYPE); + } + + __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(DATA_TYPE)) + (y * (uint)M0 * dst_stride_y); + + REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0; + +#if defined(REINTERPRET_OUTPUT_AS_3D) + // Since we store a 2D output tile in a 3D tensor, we need to check when the plane changes across the z dimension + // in order to take into account the presence of possible cross plane paddings + // + // | | + // | plane0 | + // | | + // |__________________| + // |******************| + // | cross_plane_pad | + // |******************| + // | | + // | plane1 | + // | | + // |__________________| + + // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D + zout0 = (0 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout0 = min((uint)(DEPTH_GEMM3D - 1), zout0); + zout0 *= (dst_cross_plane_pad * dst_stride_y); +#if M0 > 1 + zout1 = (1 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout1 = min((uint)(DEPTH_GEMM3D - 1), zout1); + zout1 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 1 +#if M0 > 2 + zout2 = (2 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout2 = min((uint)(DEPTH_GEMM3D - 1), zout2); + zout2 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 2 +#if M0 > 3 + zout3 = (3 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout3 = min((uint)(DEPTH_GEMM3D - 1), zout3); + zout3 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 3 +#if M0 > 4 + zout4 = (4 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout4 = min((uint)(DEPTH_GEMM3D - 1), zout4); + zout4 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 4 +#if M0 > 5 + zout5 = (5 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout5 = min((uint)(DEPTH_GEMM3D - 1), zout5); + zout5 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 5 +#if M0 > 6 + zout6 = (6 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout6 = min((uint)(DEPTH_GEMM3D - 1), zout6); + zout6 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 6 +#if M0 > 7 + zout7 = (7 + (uint)(y * (uint)M0)) / (uint)HEIGHT_GEMM3D; + zout7 = min((uint)(DEPTH_GEMM3D - 1), zout7); + zout7 *= (dst_cross_plane_pad * dst_stride_y); +#endif // M0 > 7 + + // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we + // multiply dst_stride_z by DEPTH_GEMM3D + dst_addr += z * dst_stride_z * DEPTH_GEMM3D; + +#else // defined(REINTERPRET_OUTPUT_AS_3D) + + // Add offset for batched GEMM + dst_addr += z * dst_stride_z; + +#endif // defined(REINTERPRET_OUTPUT_AS_3D) + + // Multiply by the weight of matrix-matrix product and store the result +#if defined(ALPHA) + c0 = c0 * (DATA_TYPE)ALPHA; +#if M0 > 1 + c1 = c1 * (DATA_TYPE)ALPHA; +#endif // M0 > 1 +#if M0 > 2 + c2 = c2 * (DATA_TYPE)ALPHA; +#endif // M0 > 2 +#if M0 > 3 + c3 = c3 * (DATA_TYPE)ALPHA; +#endif // M0 > 3 +#if M0 > 4 + c4 = c4 * (DATA_TYPE)ALPHA; +#endif // M0 > 4 +#if M0 > 5 + c5 = c5 * (DATA_TYPE)ALPHA; +#endif // M0 > 5 +#if M0 > 6 + c6 = c6 * (DATA_TYPE)ALPHA; +#endif // M0 > 5 +#if M0 > 7 + c7 = c7 * (DATA_TYPE)ALPHA; +#endif // M0 > 7 +#endif // defined(ALPHA) + + // Store output block + VSTORE(N0) + (c0, 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y + zout0)); +#if M0 > 1 + VSTORE(N0) + (c1, 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y + zout1)); +#endif // M0 > 1 +#if M0 > 2 + VSTORE(N0) + (c2, 0, (__global DATA_TYPE *)(dst_addr + 2 * dst_stride_y + zout2)); +#endif // M0 > 2 +#if M0 > 3 + VSTORE(N0) + (c3, 0, (__global DATA_TYPE *)(dst_addr + 3 * dst_stride_y + zout3)); +#endif // M0 > 3 +#if M0 > 4 + VSTORE(N0) + (c4, 0, (__global DATA_TYPE *)(dst_addr + 4 * dst_stride_y + zout4)); +#endif // M0 > 4 +#if M0 > 5 + VSTORE(N0) + (c5, 0, (__global DATA_TYPE *)(dst_addr + 5 * dst_stride_y + zout5)); +#endif // M0 > 5 +#if M0 > 6 + VSTORE(N0) + (c6, 0, (__global DATA_TYPE *)(dst_addr + 6 * dst_stride_y + zout6)); +#endif // M0 > 6 +#if M0 > 7 + VSTORE(N0) + (c7, 0, (__global DATA_TYPE *)(dst_addr + 7 * dst_stride_y + zout7)); +#endif // M0 > 7 + #undef RHS_BLOCK_SIZE #undef RHS_OFFSET_X #undef RHS_STEP_X -- cgit v1.2.1