Commit e6443bd7e for llama.cpp
commit e6443bd7eb970d940b0669cd9ea0d82d91d620a1
Author: Shawn Gu <shawngu@qti.qualcomm.com>
Date: Tue Nov 24 16:42:30 2026 -0800
opencl: add clc coopmat moe kernel for q4_0/q4_1/q4_k/q5_0/q5_1/q5_k/q6_k/mxfp4 (#32971)
diff --git a/ggml/src/ggml-opencl/CMakeLists.txt b/ggml/src/ggml-opencl/CMakeLists.txt
index 6d55f5d3d..66d32a88a 100644
--- a/ggml/src/ggml-opencl/CMakeLists.txt
+++ b/ggml/src/ggml-opencl/CMakeLists.txt
@@ -421,7 +421,7 @@ set(GGML_OPENCL_KERNELS
)
if (GGML_OPENCL_USE_ADRENO_KERNELS)
- list(APPEND GGML_OPENCL_KERNELS gemm_xmem_f16_f32_os8 sdpa_xmem_f32_f16_os8)
+ list(APPEND GGML_OPENCL_KERNELS gemm_xmem_f16_f32_os8 sdpa_xmem_f32_f16_os8 gemm_moe_mxfp4_f32_ns_coopmat gemm_moe_q4_0_f32_ns_coopmat gemm_moe_q4_1_f32_ns_coopmat gemm_moe_q5_0_f32_ns_coopmat gemm_moe_q5_1_f32_ns_coopmat gemm_moe_q4_k_f32_ns_coopmat gemm_moe_q5_k_f32_ns_coopmat gemm_moe_q6_k_f32_ns_coopmat)
endif ()
foreach (K ${GGML_OPENCL_KERNELS})
diff --git a/ggml/src/ggml-opencl/ggml-opencl.cpp b/ggml/src/ggml-opencl/ggml-opencl.cpp
index 700d214c3..cdcc05f8a 100644
--- a/ggml/src/ggml-opencl/ggml-opencl.cpp
+++ b/ggml/src/ggml-opencl/ggml-opencl.cpp
@@ -1219,6 +1219,15 @@ struct ggml_backend_opencl_context {
cl_kernel kernel_gemm_moe_q4_k_q8_1_dp4a_bin = nullptr; // binary dp4a (int8) q4_k MoE prefill GEMM
cl_kernel kernel_gemm_moe_q6_k_q8_1_dp4a_bin = nullptr; // binary dp4a (int8) q6_k MoE prefill GEMM
cl_kernel kernel_moe_reorder_b;
+ cl_kernel kernel_moe_reorder_b_f16 = nullptr; // f16 B for the coopmat mxfp4 MoE GEMM
+ cl_kernel kernel_gemm_moe_mxfp4_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix mxfp4 MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q4_0_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q4_0 MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q4_1_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q4_1 MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q5_0_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q5_0 MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q5_1_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q5_1 MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q4_k_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q4_k MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q5_k_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q5_k MoE prefill GEMM, A850 only
+ cl_kernel kernel_gemm_moe_q6_k_f32_ns_coopmat = nullptr; // cl_qcom_kernel_cooperative_matrix q6_k MoE prefill GEMM, A850 only
cl_kernel kernel_moe_histogram, kernel_moe_scan, kernel_moe_fill, kernel_moe_scatter;
cl_kernel kernel_moe_scatter_stable = nullptr; // deterministic slot assignment
cl_kernel kernel_moe_zero_dst;
@@ -2252,6 +2261,191 @@ static void load_cl_kernels_coopmat(ggml_backend_opencl_context * backend_ctx) {
GGML_LOG_CONT(".");
}
}
+
+ // gemm_moe_mxfp4_f32_ns_coopmat
+ // built only on A850 (see the check above), a failed build is not fatal.
+ backend_ctx->kernel_gemm_moe_mxfp4_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_mxfp4_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_mxfp4_f32_ns_coopmat.cl");
+#endif
+ // the router buffers are sub-buffers; read through texture promotion they give wrong values
+ std::string coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_mxfp4_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_mxfp4_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+ // gemm_moe_q4_0_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q4_0_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q4_0_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q4_0_f32_ns_coopmat.cl");
+#endif
+ std::string q4_0_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q4_0_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q4_0_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q4_0_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+
+ // gemm_moe_q4_1_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q4_1_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q4_1_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q4_1_f32_ns_coopmat.cl");
+#endif
+ std::string q4_1_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q4_1_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q4_1_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q4_1_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+
+ // gemm_moe_q5_0_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q5_0_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q5_0_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q5_0_f32_ns_coopmat.cl");
+#endif
+ std::string q5_0_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q5_0_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q5_0_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q5_0_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+
+ // gemm_moe_q5_1_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q5_1_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q5_1_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q5_1_f32_ns_coopmat.cl");
+#endif
+ std::string q5_1_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q5_1_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q5_1_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q5_1_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+
+ // gemm_moe_q4_k_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q4_k_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q4_k_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q4_k_f32_ns_coopmat.cl");
+#endif
+ std::string q4_k_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q4_k_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q4_k_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q4_k_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+
+ // gemm_moe_q5_k_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q5_k_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q5_k_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q5_k_f32_ns_coopmat.cl");
+#endif
+ std::string q5_k_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q5_k_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q5_k_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q5_k_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
+
+ // gemm_moe_q6_k_f32_ns_coopmat, same options as the mxfp4 one
+ backend_ctx->kernel_gemm_moe_q6_k_f32_ns_coopmat = nullptr;
+ {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "gemm_moe_q6_k_f32_ns_coopmat.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("gemm_moe_q6_k_f32_ns_coopmat.cl");
+#endif
+ std::string q6_k_coopmat_opts =
+ " -cl-std=CL3.0 "
+ " -qcom-ray-tracing-kernel "
+ " -qcom-disable-promote-pointer-to-texture ";
+
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), q6_k_coopmat_opts, false);
+ if (prog) {
+ CL_CHECK((backend_ctx->kernel_gemm_moe_q6_k_f32_ns_coopmat = clCreateKernel(prog, "kernel_gemm_moe_q6_k_f32_ns_coopmat", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ GGML_LOG_CONT(".");
+ }
+ }
}
#endif
@@ -7899,6 +8093,7 @@ static void load_cl_kernels(ggml_backend_opencl_context *backend_ctx) {
build_program_from_source(backend_ctx, kernel_src.c_str(), CL_moe_compile_opts);
CL_CHECK((backend_ctx->kernel_moe_reorder_b = clCreateKernel(prog, "kernel_moe_reorder_b", &err), err));
+ CL_CHECK((backend_ctx->kernel_moe_reorder_b_f16 = clCreateKernel(prog, "kernel_moe_reorder_b_f16", &err), err));
CL_CHECK(clReleaseProgram(prog));
GGML_LOG_CONT(".");
}
@@ -10885,6 +11080,72 @@ static bool ggml_opencl_can_fuse_moe_bias_glu(const struct ggml_cgraph * cgraph,
return true;
}
+// Run a cl_qcom_kernel_cooperative_matrix MoE GEMM: f16 reorder of the activations, then the GEMM.
+// The weight buffers come first in the kernel args, in the order of the ns kernel of the same type.
+static void ggml_cl_moe_gemm_coopmat(
+ ggml_backend_opencl_context * backend_ctx, cl_kernel kernel, std::initializer_list<cl_mem> weights,
+ cl_mem sub_buf_src1_pre, cl_mem buf_src2, cl_mem buf_src2_emap,
+ cl_mem dst_data, cl_ulong offsetd, size_t n_dst, const ggml_tensor * dst,
+ int ne00, int ne01, unsigned short map_ratio, int max_post_router_tile, int n_tile_size) {
+ cl_int status;
+
+ // f16 reordered activations, read by the coopmat kernel as a buffer
+ cl_buffer_region region;
+ region.origin = 0;
+ region.size = (size_t)ne00 * max_post_router_tile * n_tile_size * sizeof(cl_half);
+ backend_ctx->prealloc_act_trans.allocate(backend_ctx->context, region.size);
+ cl_mem buf_src1_reordered = clCreateSubBuffer(backend_ctx->prealloc_act_trans.buffer, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &status);
+ CL_CHECK(status);
+
+ cl_kernel rk = backend_ctx->kernel_moe_reorder_b_f16;
+ unsigned int ne00_u = ne00;
+ unsigned int tile_u = n_tile_size;
+ CL_CHECK(clSetKernelArg(rk, 0, sizeof(cl_mem), &sub_buf_src1_pre));
+ CL_CHECK(clSetKernelArg(rk, 1, sizeof(cl_mem), &buf_src2));
+ CL_CHECK(clSetKernelArg(rk, 2, sizeof(cl_mem), &buf_src1_reordered));
+ CL_CHECK(clSetKernelArg(rk, 3, sizeof(cl_mem), &(backend_ctx->prealloc_total_tiles.buffer)));
+ CL_CHECK(clSetKernelArg(rk, 4, sizeof(unsigned int), &ne00_u));
+ CL_CHECK(clSetKernelArg(rk, 5, sizeof(unsigned short), &map_ratio));
+ CL_CHECK(clSetKernelArg(rk, 6, sizeof(unsigned int), &tile_u));
+
+ size_t rk_local_size[3] = {256, 1, 1};
+ size_t rk_global_size[3] = {(size_t)(((ne00 / 8) + 255) / 256 * 256), (size_t)max_post_router_tile * n_tile_size, 1};
+ backend_ctx->enqueue_ndrange_kernel(rk, 3, rk_global_size, rk_local_size, dst);
+
+ // dst as an image
+ region.origin = offsetd;
+ region.size = n_dst * sizeof(float);
+ cl_mem sub_buf_dst = clCreateSubBuffer(dst_data, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &status);
+ CL_CHECK(status);
+ cl_image_format image_format_buf_dst = {CL_R, CL_FLOAT};
+ cl_image_desc image_desc_buf_dst = {CL_MEM_OBJECT_IMAGE1D_BUFFER, n_dst, 0,0,0,0,0,0,0, {sub_buf_dst}};
+ cl_mem buf_dst_image = clCreateImage(backend_ctx->context, CL_MEM_WRITE_ONLY, &image_format_buf_dst, &image_desc_buf_dst, NULL, &status);
+ CL_CHECK(status);
+
+ int arg_idx = 0;
+ for (cl_mem w : weights) {
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &w));
+ }
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_src1_reordered));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_src2));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_src2_emap));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_dst_image));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &(backend_ctx->prealloc_total_tiles.buffer)));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(int), &ne00));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(int), &ne01));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_uint), &backend_ctx->adreno_use_moe_ragged));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_uint), &backend_ctx->adreno_moe_ragged_skip_gran));
+
+ // coopmat kernels need a uniform NDRange
+ size_t global_size[3] = {64, (size_t)((ne01 + 63) / 64), (size_t)max_post_router_tile};
+ size_t local_size[3] = {64, 1, 1};
+ backend_ctx->enqueue_ndrange_kernel(kernel, 3, global_size, local_size, dst);
+
+ clReleaseMemObject(buf_src1_reordered);
+ clReleaseMemObject(sub_buf_dst);
+ clReleaseMemObject(buf_dst_image);
+}
+
static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
// Runs the gate and up matmuls unchanged, then one kernel in place of
@@ -36651,6 +36912,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
unsigned short map_ratio = ne20 / ne11;
GGML_ASSERT(((map_ratio == 1) || (map_ratio == ne20)) && "Map ratio not supported\n");
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q4_0_moe_coopmat_env = getenv("GGML_OPENCL_Q4_0_MOE_COOPMAT");
+ if (!use_moe_dp4a && (q4_0_moe_coopmat_env ? (atoi(q4_0_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q4_0_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q4_0_f32_ns_coopmat,
+ { extra0_q4_0->q_img, extra0_q4_0->d },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
if (!use_moe_dp4a) {
// Create image for reordered src1
// Use pre-allocated placeholder
@@ -36937,6 +37216,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
sub_buf_src1_pre = clCreateSubBuffer(extra1->data_device, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &status);
CL_CHECK(status);
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q4_1_moe_coopmat_env = getenv("GGML_OPENCL_Q4_1_MOE_COOPMAT");
+ if ((q4_1_moe_coopmat_env ? (atoi(q4_1_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q4_1_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q4_1_f32_ns_coopmat,
+ { extra0_q4_1->q_img, extra0_q4_1->d, extra0_q4_1->m },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
// Create image for reordered src1
// Use pre-allocated placeholder
region.origin = 0;
@@ -37198,6 +37495,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
}
}
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q5_0_moe_coopmat_env = getenv("GGML_OPENCL_Q5_0_MOE_COOPMAT");
+ if ((q5_0_moe_coopmat_env ? (atoi(q5_0_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q5_0_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q5_0_f32_ns_coopmat,
+ { extra0_q5_0->qs_img, extra0_q5_0->qh, extra0_q5_0->d },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
// Create image for reordered src1
// Use pre-allocated placeholder
region.origin = 0;
@@ -37379,6 +37694,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
sub_buf_src1_pre = clCreateSubBuffer(extra1->data_device, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &status);
CL_CHECK(status);
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q5_1_moe_coopmat_env = getenv("GGML_OPENCL_Q5_1_MOE_COOPMAT");
+ if ((q5_1_moe_coopmat_env ? (atoi(q5_1_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q5_1_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q5_1_f32_ns_coopmat,
+ { extra0_q5_1->qs_img, extra0_q5_1->qh, extra0_q5_1->d, extra0_q5_1->m },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
// Create image for reordered src1
// Use pre-allocated placeholder
region.origin = 0;
@@ -37845,6 +38178,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
unsigned short map_ratio = ne20 / ne11;
GGML_ASSERT(((map_ratio == 1) || (map_ratio == ne20)) && "Map ratio not supported\n");
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q4_k_moe_coopmat_env = getenv("GGML_OPENCL_Q4K_MOE_COOPMAT");
+ if (!use_moe_dp4a && (q4_k_moe_coopmat_env ? (atoi(q4_k_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q4_k_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q4_k_f32_ns_coopmat,
+ { extra0_q4_K->q_img, extra0_q4_K->d, extra0_q4_K->dm, extra0_q4_K->s },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
if (!use_moe_dp4a) {
// Create image for reordered src1
region.origin = 0;
@@ -38166,6 +38517,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
}
}
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q5_k_moe_coopmat_env = getenv("GGML_OPENCL_Q5K_MOE_COOPMAT");
+ if ((q5_k_moe_coopmat_env ? (atoi(q5_k_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q5_k_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q5_k_f32_ns_coopmat,
+ { extra0_q5_K->q_img, extra0_q5_K->qh, extra0_q5_K->s, extra0_q5_K->d, extra0_q5_K->dm },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
// Create image for reordered src1
// Use pre-allocated placeholder
region.origin = 0;
@@ -38368,6 +38737,24 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
unsigned short map_ratio = ne20 / ne11;
GGML_ASSERT(((map_ratio == 1) || (map_ratio == ne20)) && "Map ratio not supported\n");
+ // cl_qcom_kernel_cooperative_matrix GEMM (A850 only) takes precedence over the bin and Adreno clc kernels
+ {
+ static const char * q6_k_moe_coopmat_env = getenv("GGML_OPENCL_Q6K_MOE_COOPMAT");
+ if (!use_moe_dp4a && (q6_k_moe_coopmat_env ? (atoi(q6_k_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_q6_k_f32_ns_coopmat != nullptr) {
+ unsigned short cp_map_ratio = ne20 / ne11;
+ GGML_ASSERT(((cp_map_ratio == 1) || (cp_map_ratio == ne20)) && "Map ratio not supported\n");
+ ggml_cl_moe_gemm_coopmat(backend_ctx, backend_ctx->kernel_gemm_moe_q6_k_f32_ns_coopmat,
+ { extra0_q6_K->ql_img, extra0_q6_K->qh, extra0_q6_K->s, extra0_q6_K->d },
+ sub_buf_src1_pre, buf_src2, buf_src2_emap, extrad->data_device, offsetd, (size_t)ne0 * ne1 * ne2, dst,
+ ne00, ne01, cp_map_ratio, max_post_router_tile, n_tile_size);
+ clReleaseMemObject(sub_buf_src1_pre);
+ clReleaseMemObject(buf_src2);
+ clReleaseMemObject(buf_src2_emap);
+ return;
+ }
+ }
+
if (!use_moe_dp4a) {
// Create image for reordered src1
region.origin = 0;
@@ -38619,6 +39006,15 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
use_moe_dp4a = use_moe_dp4a && backend_ctx->kernel_gemm_moe_mxfp4_f32_ns_bin == nullptr;
}
+ // Priority on A850: coopmat (fp16) > bin kernel > Adreno clc. The dp4a path is chosen by GGML_OPENCL_MXFP4_MOE_DP4A and has its own priority.
+ static const char * mxfp4_moe_coopmat_env = getenv("GGML_OPENCL_MXFP4_MOE_COOPMAT");
+ const bool use_moe_coopmat = !use_moe_dp4a &&
+ (mxfp4_moe_coopmat_env ? (atoi(mxfp4_moe_coopmat_env) != 0) : true) &&
+ backend_ctx->kernel_gemm_moe_mxfp4_f32_ns_coopmat != nullptr;
+ if (use_moe_coopmat) {
+ kernel = backend_ctx->kernel_gemm_moe_mxfp4_f32_ns_coopmat;
+ }
+
cl_buffer_region region;
region.origin = 0;
region.size = sizeof(int) * max_post_router_tile * n_tile_size;
@@ -38641,7 +39037,32 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
unsigned short map_ratio = ne20 / ne11;
GGML_ASSERT(((map_ratio == 1) || (map_ratio == ne20)) && "Map ratio not supported\n");
- if (!use_moe_dp4a) {
+ if (use_moe_coopmat) {
+ // f16 reordered activations, read by the coopmat kernel as a buffer
+ region.origin = 0;
+ region.size = ne00 * max_post_router_tile * n_tile_size * sizeof(cl_half);
+ backend_ctx->prealloc_act_trans.allocate(backend_ctx->context, region.size);
+ buf_src1_reordered = clCreateSubBuffer(
+ backend_ctx->prealloc_act_trans.buffer,
+ 0,
+ CL_BUFFER_CREATE_TYPE_REGION,
+ ®ion,
+ &status);
+ CL_CHECK(status);
+
+ cl_kernel rk = backend_ctx->kernel_moe_reorder_b_f16;
+ CL_CHECK(clSetKernelArg(rk, 0, sizeof(cl_mem), &sub_buf_src1_pre));
+ CL_CHECK(clSetKernelArg(rk, 1, sizeof(cl_mem), &buf_src2));
+ CL_CHECK(clSetKernelArg(rk, 2, sizeof(cl_mem), &buf_src1_reordered));
+ CL_CHECK(clSetKernelArg(rk, 3, sizeof(cl_mem), &(backend_ctx->prealloc_total_tiles.buffer)));
+ CL_CHECK(clSetKernelArg(rk, 4, sizeof(unsigned int), &ne00));
+ CL_CHECK(clSetKernelArg(rk, 5, sizeof(unsigned short), &map_ratio));
+ CL_CHECK(clSetKernelArg(rk, 6, sizeof(unsigned int), &n_tile_size));
+
+ size_t rk_local_size[3] = {256, 1, 1};
+ size_t rk_global_size[3] = {static_cast<size_t>(((ne00 / 8) + 255) / 256 * 256), static_cast<size_t>(max_post_router_tile * n_tile_size), 1};
+ backend_ctx->enqueue_ndrange_kernel(rk, 3, rk_global_size, rk_local_size, dst);
+ } else if (!use_moe_dp4a) {
// Create image for reordered src1
// Use pre-allocated placeholder
region.origin = 0;
@@ -38785,7 +39206,7 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
int arg_idx = 0;
CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &extra0_mxfp4->q_img));
CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &extra0_mxfp4->e));
- CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &image_src1_reordered));
+ CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), use_moe_coopmat ? &buf_src1_reordered : &image_src1_reordered));
CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_src2));
CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_src2_emap));
CL_CHECK(clSetKernelArg(kernel, arg_idx++, sizeof(cl_mem), &buf_dst_image));
@@ -38806,7 +39227,9 @@ static void ggml_cl_mul_mat_id(ggml_backend_t backend, const ggml_tensor * src0,
clReleaseMemObject(sub_buf_src1_pre);
clReleaseMemObject(buf_src1_reordered);
- clReleaseMemObject(image_src1_reordered);
+ if (image_src1_reordered) {
+ clReleaseMemObject(image_src1_reordered);
+ }
clReleaseMemObject(buf_src2);
clReleaseMemObject(buf_src2_emap);
clReleaseMemObject(sub_buf_dst);
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32_ns_coopmat.cl
new file mode 100644
index 000000000..6c0730571
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32_ns_coopmat.cl
@@ -0,0 +1,113 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE mxfp4 x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_mxfp4_f32_ns.
+// A is the transposed mxfp4 weight of kernel_convert_block_mxfp4_trans4_ns, dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+// 8 E2M1 nibbles -> f16 in k order 0 4 1 5 2 6 3 7 (kernel_moe_reorder_b_f16 writes B in the same order).
+// The bits s e1 e0 m go to f16 bits 15, 11, 10, 9: this is the E2M1 value times 2^-14 for all codes
+// (e1 e0 = 0 gives the f16 subnormal), d holds the 2^14.
+inline half8 mxfp4_to_f16x8(uint v, half d) {
+ uint4 t;
+ t.s0 = ((v << 9) & 0x0E000E00) | ((v << 12) & 0x80008000);
+ t.s1 = ((v << 5) & 0x0E000E00) | ((v << 8) & 0x80008000);
+ t.s2 = ((v << 1) & 0x0E000E00) | ((v << 4) & 0x80008000);
+ t.s3 = ((v >> 3) & 0x0E000E00) | ( v & 0x80008000);
+ return as_half8(t) * d;
+}
+
+#define STORE_C(t, v) { \
+ const uint idx = src2[col + (t)]; \
+ if (idx != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)(idx * ne01 + row + lane), (v)); \
+ } }
+
+// Same arguments as kernel_gemm_moe_mxfp4_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_mxfp4_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_q, // A: [E][K/8][M] u32, 8 E2M1 in natural k order
+ __global uchar * src0_d, // A: [E][K/32][M] e8m0
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ uint qoff = expert * (ne00 / 8) * ne01 + rl;
+ uint eoff = expert * (ne00 / 32) * ne01 + rl;
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ uint4 q;
+ q.s0 = read_imageui(src0_q, (int)(qoff )).x;
+ q.s1 = read_imageui(src0_q, (int)(qoff + ne01)).x;
+ q.s2 = read_imageui(src0_q, (int)(qoff + 2 * ne01)).x;
+ q.s3 = read_imageui(src0_q, (int)(qoff + 3 * ne01)).x;
+ const uint e = src0_d[eoff];
+ qoff += 4 * ne01;
+ eoff += ne01;
+
+ // 2^(e - 127 + 14) overflows f16 for e > 128 (gpt-oss ffn_down_exps has e up to 136): second factor
+ const half d = (half)(as_float(min(e, 128u) << 23) * 16384.0f);
+ half16 h0 = (half16)(mxfp4_to_f16x8(q.s0, d), mxfp4_to_f16x8(q.s1, d));
+ half16 h1 = (half16)(mxfp4_to_f16x8(q.s2, d), mxfp4_to_f16x8(q.s3, d));
+ if (e > 128) {
+ const half d2 = (half)as_float((e - 1) << 23);
+ h0 *= d2;
+ h1 *= d2;
+ }
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ const bool wr = row + lane < ne01;
+ const local float * cr = lc + lane * 32;
+#pragma unroll
+ for (uint t = 0; t < 32; t++) {
+ STORE_C(t, cr[t])
+ }
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q4_0_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q4_0_f32_ns_coopmat.cl
new file mode 100644
index 000000000..c4686a2bb
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q4_0_f32_ns_coopmat.cl
@@ -0,0 +1,131 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q4_0 x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q4_0_f32_ns.
+// A is the transposed q4_0 weight of kernel_convert_block_q4_0_trans4_ns, dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+// 8 nibbles -> (q - 8) * d in k order 0 4 1 5 2 6 3 7 (kernel_moe_reorder_b_f16 writes B in the same order).
+// 0x6400 | q is f16 1024 + q, so the subtract is exact.
+inline half8 q4_0_to_f16x8(uint v, half d) {
+ uint4 t;
+ t.s0 = v & 0x000F000F;
+ t.s1 = (v >> 4) & 0x000F000F;
+ t.s2 = (v >> 8) & 0x000F000F;
+ t.s3 = (v >> 12) & 0x000F000F;
+ t |= (uint4)(0x64006400);
+ return (as_half8(t) - (half8)(1032.0h)) * d;
+}
+
+// loads of one K step of 32 (one q4_0 block) for row rl: 4 u32 of quants and 1 scale
+#define LOAD_A(qv, dv) { \
+ qv.s0 = read_imageui(src0_q, (int)(qoff )).x; \
+ qv.s1 = read_imageui(src0_q, (int)(qoff + ne01)).x; \
+ qv.s2 = read_imageui(src0_q, (int)(qoff + 2 * ne01)).x; \
+ qv.s3 = read_imageui(src0_q, (int)(qoff + 3 * ne01)).x; \
+ dv = src0_d[doff]; \
+ qoff += 4 * ne01; \
+ doff += ne01; }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q4_0_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q4_0_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_q, // A: [E][K/8][M] u32, 8 nibbles in natural k order
+ __global const half * src0_d, // A: [E][K/32][M] f16
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ uint qoff = expert * (ne00 / 8) * ne01 + rl;
+ uint doff = expert * (ne00 / 32) * ne01 + rl;
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn;
+ half dn;
+ LOAD_A(qn, dn)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn;
+ const half d = dn;
+ if (k0 + 32 < ne00) {
+ LOAD_A(qn, dn)
+ }
+
+ vstore16((half16)(q4_0_to_f16x8(q.s0, d), q4_0_to_f16x8(q.s1, d)), 0, la[0] + lane * 16);
+ vstore16((half16)(q4_0_to_f16x8(q.s2, d), q4_0_to_f16x8(q.s3, d)), 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q4_1_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q4_1_f32_ns_coopmat.cl
new file mode 100644
index 000000000..c1234c6fb
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q4_1_f32_ns_coopmat.cl
@@ -0,0 +1,131 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q4_1 x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q4_1_f32_ns.
+// A is the transposed q4_1 weight (same layout as the ns kernel), dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+// 8 nibbles -> q * d + m in k order 0 4 1 5 2 6 3 7 (kernel_moe_reorder_b_f16 writes B in the same order).
+// 0x6400 | q is f16 1024 + q, so the subtract is exact.
+inline half8 q4_1_to_f16x8(uint v, half d, half m) {
+ uint4 t;
+ t.s0 = v & 0x000F000F;
+ t.s1 = (v >> 4) & 0x000F000F;
+ t.s2 = (v >> 8) & 0x000F000F;
+ t.s3 = (v >> 12) & 0x000F000F;
+ t |= (uint4)(0x64006400);
+ return (as_half8(t) - (half8)(1024.0h)) * d + m;
+}
+
+// loads of one K step of 32 for row rl into the *n variables
+#define LOAD_A(kk) { \
+ const uint bo = expert * (ne00 / 32) * ne01 + rl + ((kk) / 32) * ne01; \
+ const uint qo = expert * (ne00 / 8) * ne01 + rl + ((kk) / 8) * ne01; \
+ qn.s0 = read_imageui(src0_q, (int)(qo )).x; \
+ qn.s1 = read_imageui(src0_q, (int)(qo + ne01)).x; \
+ qn.s2 = read_imageui(src0_q, (int)(qo + 2 * ne01)).x; \
+ qn.s3 = read_imageui(src0_q, (int)(qo + 3 * ne01)).x; \
+ dn = src0_d[bo]; \
+ mn = src0_m[bo]; }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q4_1_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q4_1_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_q, // A: [E][K/8][M] u32, 8 nibbles in natural k order
+ __global const half * src0_d, // A: [E][K/32][M] f16
+ __global const half * src0_m, // A: [E][K/32][M] f16
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn; half dn; half mn;
+ LOAD_A(0)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn; const half d = dn; const half m = mn;
+ if (k0 + 32 < ne00) {
+ LOAD_A(k0 + 32)
+ }
+
+ half16 h0 = (half16)(q4_1_to_f16x8(q.s0, d, m), q4_1_to_f16x8(q.s1, d, m));
+ half16 h1 = (half16)(q4_1_to_f16x8(q.s2, d, m), q4_1_to_f16x8(q.s3, d, m));
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q4_k_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q4_k_f32_ns_coopmat.cl
new file mode 100644
index 000000000..fe38d0777
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q4_k_f32_ns_coopmat.cl
@@ -0,0 +1,147 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q4_k x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q4_k_f32_ns.
+// A is the transposed q4_k weight (same layout as the ns kernel), dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+inline void get_scale_min_k4(int j, global const uchar * q, uchar * d, uchar * m) {
+ if (j < 4) {
+ *d = q[j] & 63;
+ *m = q[j+4] & 63;
+ } else {
+ *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2);
+ *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2);
+ }
+}
+
+// 8 nibbles -> q * scale + bias in k order 0 4 1 5 2 6 3 7. Computed in f32 and rounded once to f16.
+inline half8 q4_k_to_f16x8(uint v, float scale, float bias) {
+ uint4 t;
+ t.s0 = v & 0x000F000F;
+ t.s1 = (v >> 4) & 0x000F000F;
+ t.s2 = (v >> 8) & 0x000F000F;
+ t.s3 = (v >> 12) & 0x000F000F;
+ t |= (uint4)(0x64006400);
+ return convert_half8((convert_float8(as_half8(t)) - 1024.0f) * scale + bias);
+}
+
+// loads of one K step of 32 for row rl into the *n variables
+#define LOAD_A(kk) { \
+ const uint sub = (kk) / 32; \
+ const uint sb = sub / 8; \
+ const uint jj = sub % 8; \
+ const uint nsb = ne00 / 256; \
+ const uint dof = rl + sb * ne01 + expert * nsb * ne01; \
+ const uint qo = expert * (ne00 / 8) * ne01 + rl + ((kk) / 8) * ne01; \
+ qn.s0 = read_imageui(src0_q, (int)(qo )).x; \
+ qn.s1 = read_imageui(src0_q, (int)(qo + ne01)).x; \
+ qn.s2 = read_imageui(src0_q, (int)(qo + 2 * ne01)).x; \
+ qn.s3 = read_imageui(src0_q, (int)(qo + 3 * ne01)).x; \
+ uchar sv, mn; \
+ get_scale_min_k4((int)jj, src0_s + (expert * ne01 + rl) * (nsb * 12) + sb * 12, &sv, &mn); \
+ sn = (float)src0_d[dof] * (float)sv; \
+ bn = -((float)src0_dm[dof] * (float)mn); }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q4_k_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q4_k_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_q, // A: [E][K/8][M] u32, 8 nibbles in natural k order
+ __global const half * src0_d, // A: [E][K/256][M] f16
+ __global const half * src0_dm, // A: [E][K/256][M] f16
+ __global const uchar * src0_s, // A: [E][M][K/256][12] packed 6-bit scales and mins
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn; float sn; float bn;
+ LOAD_A(0)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn; const float sc = sn; const float bi = bn;
+ if (k0 + 32 < ne00) {
+ LOAD_A(k0 + 32)
+ }
+
+ half16 h0 = (half16)(q4_k_to_f16x8(q.s0, sc, bi), q4_k_to_f16x8(q.s1, sc, bi));
+ half16 h1 = (half16)(q4_k_to_f16x8(q.s2, sc, bi), q4_k_to_f16x8(q.s3, sc, bi));
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q5_0_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q5_0_f32_ns_coopmat.cl
new file mode 100644
index 000000000..22dba38fc
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q5_0_f32_ns_coopmat.cl
@@ -0,0 +1,131 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q5_0 x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q5_0_f32_ns.
+// A is the transposed q5_0 weight (same layout as the ns kernel), dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+// 8 q5 values (low nibbles in v, 5th bits in the low 8 bits of h) -> (q - 16) * d in k order 0 4 1 5 2 6 3 7.
+// 0x6400 | q is f16 1024 + q, so the subtract is exact.
+inline half8 q5_0_to_f16x8(uint v, uint h, half d) {
+ uint4 t;
+ t.s0 = ( v & 0x000F000F) | ((h << 4) & 0x10) | ((h << 16) & 0x00100000);
+ t.s1 = ((v >> 4) & 0x000F000F) | ((h << 3) & 0x10) | ((h << 15) & 0x00100000);
+ t.s2 = ((v >> 8) & 0x000F000F) | ((h << 2) & 0x10) | ((h << 14) & 0x00100000);
+ t.s3 = ((v >> 12) & 0x000F000F) | ((h << 1) & 0x10) | ((h << 13) & 0x00100000);
+ t |= (uint4)(0x64006400);
+ return (as_half8(t) - (half8)(1040.0h)) * d;
+}
+
+// loads of one K step of 32 for row rl into the *n variables
+#define LOAD_A(kk) { \
+ const uint bo = expert * (ne00 / 32) * ne01 + rl + ((kk) / 32) * ne01; \
+ const uint qo = expert * (ne00 / 8) * ne01 + rl + ((kk) / 8) * ne01; \
+ qn.s0 = read_imageui(src0_qs, (int)(qo )).x; \
+ qn.s1 = read_imageui(src0_qs, (int)(qo + ne01)).x; \
+ qn.s2 = read_imageui(src0_qs, (int)(qo + 2 * ne01)).x; \
+ qn.s3 = read_imageui(src0_qs, (int)(qo + 3 * ne01)).x; \
+ dn = src0_d[bo]; \
+ hn = src0_qh[bo]; }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q5_0_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q5_0_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_qs, // A: [E][K/8][M] u32, 8 low nibbles in natural k order
+ __global const uint * src0_qh, // A: [E][K/32][M] u32, 5th bits of the 32 k
+ __global const half * src0_d, // A: [E][K/32][M] f16
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn; half dn; uint hn;
+ LOAD_A(0)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn; const half d = dn; const uint h = hn;
+ if (k0 + 32 < ne00) {
+ LOAD_A(k0 + 32)
+ }
+
+ half16 h0 = (half16)(q5_0_to_f16x8(q.s0, h, d), q5_0_to_f16x8(q.s1, h >> 8, d));
+ half16 h1 = (half16)(q5_0_to_f16x8(q.s2, h >> 16, d), q5_0_to_f16x8(q.s3, h >> 24, d));
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q5_1_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q5_1_f32_ns_coopmat.cl
new file mode 100644
index 000000000..9b285eba1
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q5_1_f32_ns_coopmat.cl
@@ -0,0 +1,133 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q5_1 x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q5_1_f32_ns.
+// A is the transposed q5_1 weight (same layout as the ns kernel), dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+// 8 q5 values (low nibbles in v, 5th bits in the low 8 bits of h) -> q * d + m in k order 0 4 1 5 2 6 3 7.
+// 0x6400 | q is f16 1024 + q, so the subtract is exact.
+inline half8 q5_1_to_f16x8(uint v, uint h, half d, half m) {
+ uint4 t;
+ t.s0 = ( v & 0x000F000F) | ((h << 4) & 0x10) | ((h << 16) & 0x00100000);
+ t.s1 = ((v >> 4) & 0x000F000F) | ((h << 3) & 0x10) | ((h << 15) & 0x00100000);
+ t.s2 = ((v >> 8) & 0x000F000F) | ((h << 2) & 0x10) | ((h << 14) & 0x00100000);
+ t.s3 = ((v >> 12) & 0x000F000F) | ((h << 1) & 0x10) | ((h << 13) & 0x00100000);
+ t |= (uint4)(0x64006400);
+ return (as_half8(t) - (half8)(1024.0h)) * d + m;
+}
+
+// loads of one K step of 32 for row rl into the *n variables
+#define LOAD_A(kk) { \
+ const uint bo = expert * (ne00 / 32) * ne01 + rl + ((kk) / 32) * ne01; \
+ const uint qo = expert * (ne00 / 8) * ne01 + rl + ((kk) / 8) * ne01; \
+ qn.s0 = read_imageui(src0_qs, (int)(qo )).x; \
+ qn.s1 = read_imageui(src0_qs, (int)(qo + ne01)).x; \
+ qn.s2 = read_imageui(src0_qs, (int)(qo + 2 * ne01)).x; \
+ qn.s3 = read_imageui(src0_qs, (int)(qo + 3 * ne01)).x; \
+ dn = src0_d[bo]; \
+ mn = src0_m[bo]; \
+ hn = src0_qh[bo]; }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q5_1_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q5_1_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_qs, // A: [E][K/8][M] u32, 8 low nibbles in natural k order
+ __global const uint * src0_qh, // A: [E][K/32][M] u32, 5th bits of the 32 k
+ __global const half * src0_d, // A: [E][K/32][M] f16
+ __global const half * src0_m, // A: [E][K/32][M] f16
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn; half dn; half mn; uint hn;
+ LOAD_A(0)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn; const half d = dn; const half m = mn; const uint h = hn;
+ if (k0 + 32 < ne00) {
+ LOAD_A(k0 + 32)
+ }
+
+ half16 h0 = (half16)(q5_1_to_f16x8(q.s0, h, d, m), q5_1_to_f16x8(q.s1, h >> 8, d, m));
+ half16 h1 = (half16)(q5_1_to_f16x8(q.s2, h >> 16, d, m), q5_1_to_f16x8(q.s3, h >> 24, d, m));
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q5_k_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q5_k_f32_ns_coopmat.cl
new file mode 100644
index 000000000..a23e14667
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q5_k_f32_ns_coopmat.cl
@@ -0,0 +1,150 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q5_k x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q5_k_f32_ns.
+// A is the transposed q5_k weight (same layout as the ns kernel), dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+inline void get_scale_min_k4(int j, global const uchar * q, uchar * d, uchar * m) {
+ if (j < 4) {
+ *d = q[j] & 63;
+ *m = q[j+4] & 63;
+ } else {
+ *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2);
+ *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2);
+ }
+}
+
+// 8 q5 values (low nibbles in v, 5th bits in the low 8 bits of h) -> q * scale + bias in k order 0 4 1 5 2 6 3 7.
+// Computed in f32 and rounded once to f16.
+inline half8 q5_k_to_f16x8(uint v, uint h, float scale, float bias) {
+ uint4 t;
+ t.s0 = ( v & 0x000F000F) | ((h << 4) & 0x10) | ((h << 16) & 0x00100000);
+ t.s1 = ((v >> 4) & 0x000F000F) | ((h << 3) & 0x10) | ((h << 15) & 0x00100000);
+ t.s2 = ((v >> 8) & 0x000F000F) | ((h << 2) & 0x10) | ((h << 14) & 0x00100000);
+ t.s3 = ((v >> 12) & 0x000F000F) | ((h << 1) & 0x10) | ((h << 13) & 0x00100000);
+ t |= (uint4)(0x64006400);
+ return convert_half8((convert_float8(as_half8(t)) - 1024.0f) * scale + bias);
+}
+
+// loads of one K step of 32 for row rl into the *n variables
+#define LOAD_A(kk) { \
+ const uint sub = (kk) / 32; \
+ const uint sb = sub / 8; \
+ const uint jj = sub % 8; \
+ const uint nsb = ne00 / 256; \
+ const uint dof = rl + sb * ne01 + expert * nsb * ne01; \
+ const uint qo = expert * (ne00 / 8) * ne01 + rl + ((kk) / 8) * ne01; \
+ qn.s0 = read_imageui(src0_q, (int)(qo )).x; \
+ qn.s1 = read_imageui(src0_q, (int)(qo + ne01)).x; \
+ qn.s2 = read_imageui(src0_q, (int)(qo + 2 * ne01)).x; \
+ qn.s3 = read_imageui(src0_q, (int)(qo + 3 * ne01)).x; \
+ uchar sv, mn; \
+ get_scale_min_k4((int)jj, src0_s + (expert * ne01 + rl) * (nsb * 12) + sb * 12, &sv, &mn); \
+ sn = (float)src0_d[dof] * (float)sv; \
+ bn = -((float)src0_dm[dof] * (float)mn); \
+ hn = src0_qh[rl + sub * ne01 + expert * nsb * 8 * ne01]; }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q5_k_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q5_k_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_q, // A: [E][K/8][M] u32, 8 low nibbles in natural k order
+ __global const uint * src0_qh, // A: [E][K/32][M] u32, 5th bits of the 32 k
+ __global const uchar * src0_s, // A: [E][M][K/256][12] packed 6-bit scales and mins
+ __global const half * src0_d, // A: [E][K/256][M] f16
+ __global const half * src0_dm, // A: [E][K/256][M] f16
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn; float sn; float bn; uint hn;
+ LOAD_A(0)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn; const float sc = sn; const float bi = bn; const uint h = hn;
+ if (k0 + 32 < ne00) {
+ LOAD_A(k0 + 32)
+ }
+
+ half16 h0 = (half16)(q5_k_to_f16x8(q.s0, h, sc, bi), q5_k_to_f16x8(q.s1, h >> 8, sc, bi));
+ half16 h1 = (half16)(q5_k_to_f16x8(q.s2, h >> 16, sc, bi), q5_k_to_f16x8(q.s3, h >> 24, sc, bi));
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/gemm_moe_q6_k_f32_ns_coopmat.cl b/ggml/src/ggml-opencl/kernels/gemm_moe_q6_k_f32_ns_coopmat.cl
new file mode 100644
index 000000000..b4477ee98
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/gemm_moe_q6_k_f32_ns_coopmat.cl
@@ -0,0 +1,141 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#pragma OPENCL EXTENSION cl_qcom_kernel_cooperative_matrix : enable
+
+// MoE q6_k x f32 GEMM with cl_qcom_kernel_cooperative_matrix: m64n32k16, f16 in, f32 accumulate.
+// One wave computes 64 weight rows x one tile of 32 token slots, like kernel_gemm_moe_q6_k_f32_ns.
+// A is the transposed q6_k weight (same layout as the ns kernel), dequantized in registers.
+// B is the f16 copy of the reordered activations written by kernel_moe_reorder_b_f16 (moe_reorder_b.cl).
+// A is dequantized in registers, staged through local memory (one row per lane) and loaded with qcom_coop_mat_load;
+// C is stored with qcom_coop_mat_store to local memory and read back one row per lane. B is loaded from global memory.
+// Keep this kernel alone in the program: with another kernel before it, the compiler gave wrong results.
+
+typedef half mat_a_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 16, CLK_COOPERATIVE_MATRIX_A_QCOM)));
+typedef half mat_b_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 16, 32, CLK_COOPERATIVE_MATRIX_B_QCOM)));
+typedef float mat_c_t __attribute__((qcom_coop_mat(CLK_COOPERATIVE_MATRIX_SCOPE_SUBGROUP_QCOM, 64, 32, CLK_COOPERATIVE_MATRIX_ACCUMULATOR_QCOM)));
+
+// 8 q6 values (low nibbles in v, 2 high bits each in the low 16 bits of h) -> (q - 32) * scale in k order 0 4 1 5 2 6 3 7.
+// Computed in f32 and rounded once to f16.
+inline half8 q6_k_to_f16x8(uint v, uint h, float scale) {
+ uint4 t;
+ t.s0 = ( v & 0x000F000F) | ((h << 4) & 0x30) | ((h << 12) & 0x00300000);
+ t.s1 = ((v >> 4) & 0x000F000F) | ((h << 2) & 0x30) | ((h << 10) & 0x00300000);
+ t.s2 = ((v >> 8) & 0x000F000F) | ( h & 0x30) | ((h << 8) & 0x00300000);
+ t.s3 = ((v >> 12) & 0x000F000F) | ((h >> 2) & 0x30) | ((h << 6) & 0x00300000);
+ t |= (uint4)(0x64006400);
+ return convert_half8((convert_float8(as_half8(t)) - 1056.0f) * scale);
+}
+
+// loads of one K step of 32 for row rl into the *n variables
+#define LOAD_A(kk) { \
+ const uint sub = (kk) / 32; \
+ const uint sb = sub / 8; \
+ const uint jj = sub % 8; \
+ const uint nsb = ne00 / 256; \
+ const uint dof = rl + sb * ne01 + expert * nsb * ne01; \
+ const uint qo = expert * (ne00 / 8) * ne01 + rl + ((kk) / 8) * ne01; \
+ qn.s0 = read_imageui(src0_ql, (int)(qo )).x; \
+ qn.s1 = read_imageui(src0_ql, (int)(qo + ne01)).x; \
+ qn.s2 = read_imageui(src0_ql, (int)(qo + 2 * ne01)).x; \
+ qn.s3 = read_imageui(src0_ql, (int)(qo + 3 * ne01)).x; \
+ const float dv = (float)src0_d[dof]; \
+ global const char * scp = src0_s + (expert * ne01 + rl) * (nsb * 16) + sb * 16; \
+ s0n = dv * (float)scp[jj * 2]; \
+ s1n = dv * (float)scp[jj * 2 + 1]; \
+ const uint qhb = rl + (sub * 2) * ne01 + expert * (nsb * 16) * ne01; \
+ h0n = src0_qh[qhb]; \
+ h1n = src0_qh[qhb + ne01]; }
+
+#define STORE_C(v, idx) { \
+ if ((idx) != 0xFFFFFFFFu && wr) { \
+ write_imagef(dst, (int)((idx) * ne01 + row + lane), (v)); \
+ } }
+
+#define STORE_C16(u, ix) \
+ STORE_C(u.s0, ix.s0) STORE_C(u.s1, ix.s1) STORE_C(u.s2, ix.s2) STORE_C(u.s3, ix.s3) \
+ STORE_C(u.s4, ix.s4) STORE_C(u.s5, ix.s5) STORE_C(u.s6, ix.s6) STORE_C(u.s7, ix.s7) \
+ STORE_C(u.s8, ix.s8) STORE_C(u.s9, ix.s9) STORE_C(u.sa, ix.sa) STORE_C(u.sb, ix.sb) \
+ STORE_C(u.sc, ix.sc) STORE_C(u.sd, ix.sd) STORE_C(u.se, ix.se) STORE_C(u.sf, ix.sf)
+
+// Same arguments as kernel_gemm_moe_q6_k_f32_ns, except src1 is the f16 buffer of kernel_moe_reorder_b_f16.
+// global size (64, ne01/64 rounded up, max tiles), local size (64, 1, 1)
+__attribute__((reqd_work_group_size(64, 1, 1)))
+kernel void kernel_gemm_moe_q6_k_f32_ns_coopmat(
+ __read_only image1d_buffer_t src0_ql, // A: [E][K/8][M] u32, 8 low nibbles in natural k order
+ __global const uint * src0_qh, // A: [E][K/16][M] u32, 2 high bits of each of the 16 k
+ __global const char * src0_s, // A: [E][M][K/256][16] int8 scales, one per 16 k
+ __global const half * src0_d, // A: [E][K/256][M] f16
+ __global const half * src1, // B: [slot][K] f16
+ __global uint * src2, // token slot -> dst row, 0xFFFFFFFF for padding
+ __global ushort * src2_emap, // tile -> expert
+ __write_only image1d_buffer_t dst,
+ __global int * total_tiles,
+ uint ne00,
+ uint ne01,
+ uint is_ragged,
+ uint skip_gran
+) {
+ const uint lane = get_local_id(0);
+ const uint row = get_global_id(1) * 64;
+ const uint tile = get_global_id(2);
+
+ if (tile >= (uint)total_tiles[0]) {
+ return;
+ }
+
+ const uint expert = src2_emap[tile];
+ const uint col = tile * 32;
+ // rows past ne01 (ne01 % 64 == 32) load the last row and are not stored
+ const uint rl = min(row + lane, ne01 - 1);
+
+ // dst rows of the tile, read back as vectors at the store (uniform global loads there are slow)
+ local uint out_idx[32];
+ if (lane < 32) {
+ out_idx[lane] = src2[col + lane];
+ }
+
+ global const half * bp = src1 + (ulong)col * ne00;
+
+ local half la[2][64 * 16]; // A tiles, row-major, ld 16
+ local float lc[64 * 32]; // C tile, row-major, ld 32
+
+ mat_c_t acc = qcom_coop_mat_init(0.0f);
+
+ // the loads of the next K step are issued before the dequant and MMA of the current one
+ uint4 qn; float s0n; float s1n; uint h0n; uint h1n;
+ LOAD_A(0)
+
+#pragma unroll 1
+ for (uint k0 = 0; k0 < ne00; k0 += 32) {
+ const uint4 q = qn; const float sc0 = s0n; const float sc1 = s1n; const uint hq0 = h0n; const uint hq1 = h1n;
+ if (k0 + 32 < ne00) {
+ LOAD_A(k0 + 32)
+ }
+
+ half16 h0 = (half16)(q6_k_to_f16x8(q.s0, hq0, sc0), q6_k_to_f16x8(q.s1, hq0 >> 16, sc0));
+ half16 h1 = (half16)(q6_k_to_f16x8(q.s2, hq1, sc1), q6_k_to_f16x8(q.s3, hq1 >> 16, sc1));
+ vstore16(h0, 0, la[0] + lane * 16);
+ vstore16(h1, 0, la[1] + lane * 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_a_t a0 = qcom_coop_mat_load(la[0], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ mat_a_t a1 = qcom_coop_mat_load(la[1], CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 16);
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ mat_b_t b0 = qcom_coop_mat_load(bp + k0, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ mat_b_t b1 = qcom_coop_mat_load(bp + k0 + 16, CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR_QCOM, ne00);
+ acc = qcom_coop_mat_mulAdd(a0, b0, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ acc = qcom_coop_mat_mulAdd(a1, b1, acc, CLK_COOPERATIVE_MATRIX_OPERAND_NONE_QCOM);
+ }
+
+ qcom_coop_mat_store(lc, acc, CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR_QCOM, 32);
+
+ // the work-group is one subgroup; a full barrier is ~5% slower
+ sub_group_barrier(CLK_LOCAL_MEM_FENCE);
+ const uint16 ix0 = vload16(0, out_idx);
+ const uint16 ix1 = vload16(1, out_idx);
+
+ const bool wr = row + lane < ne01;
+ const float16 c0 = vload16(0, lc + lane * 32);
+ const float16 c1 = vload16(1, lc + lane * 32);
+ STORE_C16(c0, ix0)
+ STORE_C16(c1, ix1)
+}
diff --git a/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl b/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl
index 2f5c110bf..4bebbc4aa 100644
--- a/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl
+++ b/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl
@@ -30,3 +30,33 @@ kernel void kernel_moe_reorder_b(
ushort activation_idx = router_idx / map_ratio;
dst[post_router_idx * K / 4 + k_4] = src[activation_idx * K / 4 + k_4];
}
+
+// f16 variant for kernel_gemm_moe_mxfp4_f32_ns_coopmat, same arguments, one work-item per 8 k.
+// The k order in each group of 8 is 0 4 1 5 2 6 3 7, the order of the coopmat mxfp4 dequant.
+kernel void kernel_moe_reorder_b_f16(
+ global float4 * src,
+ global uint * router,
+ global half * dst,
+ global int * total_tiles,
+ uint K,
+ ushort map_ratio,
+ uint tile_size
+) {
+ uint k_8 = get_global_id(0);
+ uint post_router_idx = get_global_id(1);
+
+ if ((k_8 >= (K / 8)) || (post_router_idx >= total_tiles[0] * tile_size)) {
+ return;
+ }
+
+ // padded slots only affect their own (not stored) columns of C
+ uint router_idx = router[post_router_idx];
+ if (router_idx == 0xFFFFFFFF) {
+ return;
+ }
+
+ uint activation_idx = router_idx / map_ratio;
+ float4 a = src[activation_idx * K / 4 + k_8 * 2];
+ float4 b = src[activation_idx * K / 4 + k_8 * 2 + 1];
+ vstore8(convert_half8((float8)(a.x, b.x, a.y, b.y, a.z, b.z, a.w, b.w)), 0, dst + (ulong)post_router_idx * K + k_8 * 8);
+}