Add Texture Pipe Support for Matmul Lhs T/NT Rhs NT kernels

Resolves: COMPMID-5945, COMPMID-5954

Change-Id: I7b27021d21f8e08c4896f6b1f595a75125064f9e
Signed-off-by: Gunes Bayir <gunes.bayir@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9356
Reviewed-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
Reviewed-by: SiCong Li <sicong.li@arm.com>
Reviewed-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Benchmark: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
diff --git a/src/gpu/cl/kernels/ClNativeMatMulKernel.cpp b/src/gpu/cl/kernels/ClNativeMatMulKernel.cpp
index ffbaf49..c1f150d 100644
--- a/src/gpu/cl/kernels/ClNativeMatMulKernel.cpp
+++ b/src/gpu/cl/kernels/ClNativeMatMulKernel.cpp
@@ -22,16 +22,21 @@
  * SOFTWARE.
  */
 #include "src/gpu/cl/kernels/ClNativeMatMulKernel.h"
+
+#include "arm_compute/core/CL/CLHelpers.h"
 #include "arm_compute/core/CL/ICLTensor.h"
+#include "arm_compute/core/ITensorPack.h"
 #include "arm_compute/core/TensorInfo.h"
 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
-#include "src/core/helpers/AutoConfiguration.h"
 
-#include "arm_compute/core/ITensorPack.h"
 #include "src/common/utils/Log.h"
+#include "src/core/CL/CLUtils.h"
+#include "src/core/helpers/AutoConfiguration.h"
 #include "src/core/helpers/WindowHelpers.h"
+#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
+
 #include "support/Cast.h"
-#include "utils/TypePrinter.h"
+#include "support/StringSupport.h"
 
 namespace arm_compute
 {
@@ -54,7 +59,7 @@
 
     if(adj_lhs)
     {
-        ARM_COMPUTE_RETURN_ERROR_ON_MSG(((m0 & (m0 - 1)) && (m0 != 3)) || (m0 > 16), "Only 1,2,3,4,8,16 are supported for N0 for Lhs transposed");
+        ARM_COMPUTE_RETURN_ERROR_ON_MSG(((m0 & (m0 - 1)) && (m0 != 3)) || (m0 > 16), "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed");
     }
 
     // Validate N0
@@ -88,6 +93,27 @@
 
     return Status{};
 }
+
+Status validate_export_to_cl_image(const ITensorInfo *rhs, const MatMulKernelInfo &matmul_kernel_info)
+{
+    ARM_COMPUTE_RETURN_ERROR_ON(matmul_kernel_info.export_rhs_to_cl_image && rhs->lock_paddings());
+    if(matmul_kernel_info.export_rhs_to_cl_image)
+    {
+        if(matmul_kernel_info.adj_rhs)
+        {
+            const int k0 = matmul_kernel_info.k0;
+            ARM_COMPUTE_RETURN_ERROR_ON_MSG(k0 != 4 && k0 != 8 && k0 != 16, "K0 can only be: 4, 8, and 16 for Rhs transposed");
+        }
+        else
+        {
+            const int n0 = matmul_kernel_info.n0;
+            ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 != 4 && n0 != 8 && n0 != 16, "N0 can only be: 4, 8, and 16 for Rhs non-transposed");
+        }
+        ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(rhs), "Export to CLImage is not supported for this device/configuration");
+    }
+
+    return Status {};
+}
 }
 ClNativeMatMulKernel::ClNativeMatMulKernel()
 {
@@ -100,6 +126,7 @@
     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
     ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
     ARM_COMPUTE_RETURN_ON_ERROR(validate_input_shapes(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info));
+    ARM_COMPUTE_RETURN_ON_ERROR(validate_export_to_cl_image(rhs, matmul_kernel_info));
 
     if(output->total_size() != 0)
     {
@@ -114,10 +141,10 @@
 {
     ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, output, &compile_context, &matmul_kernel_info);
     ARM_COMPUTE_LOG_PARAMS(lhs, rhs, output, matmul_kernel_info);
+    ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, output, matmul_kernel_info));
 
     // output tensor auto initialization if not yet initialized
     auto_init_if_empty(*output, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)));
-    ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, output, matmul_kernel_info));
 
     const int  m       = output->dimension(1);
     const int  n       = output->dimension(0);
@@ -127,14 +154,16 @@
     int m0 = adj_lhs ? adjust_vec_size(matmul_kernel_info.m0, m) : std::min(matmul_kernel_info.m0, m);
     int n0 = adjust_vec_size(matmul_kernel_info.n0, n);
 
+    _export_rhs_to_cl_image = matmul_kernel_info.export_rhs_to_cl_image && !rhs->lock_paddings();
+
     // Configure kernel window
     Window win = calculate_max_window(*output, Steps(n0, m0));
     win        = win.collapse(win, Window::DimZ);
     IClKernel::configure_internal(win);
 
     // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks at the end of a row/column if any. This is to avoid padding.
-    const unsigned int partial_store_m0 = m % m0; // M is output->dimension(1)
-    const unsigned int partial_store_n0 = n % n0; // N is output->dimension(0)
+    const unsigned int partial_store_m0 = m % m0;
+    const unsigned int partial_store_n0 = n % n0;
 
     CLBuildOptions build_opts;
     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
@@ -144,6 +173,7 @@
     build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
     build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
     build_opts.add_option("-DK=" + support::cpp11::to_string(k));
+    build_opts.add_option_if_else(_export_rhs_to_cl_image, "-DRHS_TENSOR_TYPE=IMAGE", "-DRHS_TENSOR_TYPE=BUFFER");
 
     std::string kernel_name("mat_mul_native");
     kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
@@ -152,6 +182,11 @@
     // A macro guard to compile ONLY the kernel of interest
     build_opts.add_option("-D" + upper_string(kernel_name));
 
+    if(_export_rhs_to_cl_image)
+    {
+        gemm::update_padding_for_cl_image(rhs);
+    }
+
     // Create kernel
     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
 
@@ -160,12 +195,16 @@
     _config_id += "_";
     _config_id += lower_string(string_from_data_type(lhs->data_type()));
     _config_id += "_";
-    _config_id += support::cpp11::to_string(output->dimension(1));
+    _config_id += support::cpp11::to_string(m);
     _config_id += "_";
-    _config_id += support::cpp11::to_string(output->dimension(0));
+    _config_id += support::cpp11::to_string(n);
+    _config_id += "_";
+    _config_id += support::cpp11::to_string(k);
     _config_id += "_";
     _config_id += support::cpp11::to_string(output->dimension(2));
     _config_id += "_";
+    _config_id += support::cpp11::to_string(_export_rhs_to_cl_image);
+    _config_id += "_";
     _config_id += support::cpp11::to_string(m0);
     _config_id += "_";
     _config_id += support::cpp11::to_string(n0);
@@ -188,6 +227,20 @@
     Window       window_collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
 
     add_3d_tensor_nhw_argument(idx, lhs);
+
+    cl::Image2D rhs_cl_image;
+    if(_export_rhs_to_cl_image)
+    {
+        const size_t      image_w = rhs->info()->dimension(0) / 4;
+        const size_t      image_h = rhs->info()->tensor_shape().total_size() / rhs->info()->dimension(0);
+        const TensorShape shape2d(image_w, image_h);
+        const size_t      image_row_pitch = rhs->info()->strides_in_bytes()[1];
+
+        // Export cl_buffer to cl_image
+        rhs_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), rhs->cl_buffer(), shape2d, rhs->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
+        _kernel.setArg(idx++, rhs_cl_image);
+    }
+
     add_3d_tensor_nhw_argument(idx, rhs);
     add_3d_tensor_nhw_argument(idx, output);