opencl: add multi token f32 GEMV for spec verify (#33098)
Some checks are pending
Behavior Check / ubuntu (push) Waiting to run
Behavior Check / windows (push) Waiting to run
CI (3rd-party) / ubuntu-26-llguidance (push) Waiting to run
CI (android) / ndk (push) Waiting to run
CI (android) / default (push) Waiting to run
CI (android) / arm64 (push) Waiting to run
CI (apple) / macos-latest-arm64 (push) Waiting to run
CI (cpu) / ubuntu (arm64, ubuntu-26.04-arm) (push) Waiting to run
CI (cpu) / ubuntu (x64, ubuntu-26.04) (push) Waiting to run
CI (cpu) / windows / arm64 (push) Waiting to run
CI (cpu) / windows / x64-cpu-static (push) Waiting to run
CI (cpu) / windows / x64-openblas (push) Waiting to run
CI (cpu) / build-cmake-pkg (push) Waiting to run
CI (CUDA, ubuntu) / cuda (push) Waiting to run
CI (CUDA, ubuntu) / hip (push) Waiting to run
CI (CUDA, ubuntu) / musa (push) Waiting to run
CI (ibm) / ubuntu-26-s390x (VXE OFF) (push) Waiting to run
CI (ibm) / ubuntu-26-s390x (VXE ON) (push) Waiting to run
CI (ibm) / ubuntu-26-zdnn-s390x (push) Waiting to run
CI (ibm) / ubuntu-26-ppc64le (push) Waiting to run
CI (sanitize) / ctest (ubuntu-26.04, UNDEFINED) (push) Waiting to run
CI (sanitize) / ctest (ubuntu-26.04, THREAD) (push) Waiting to run
CI (sanitize) / ctest (ubuntu-26.04, ADDRESS) (push) Waiting to run
CI (opencl) / windows-2025-opencl-adreno (push) Waiting to run
CI (openvino) / openvino-windows-2022 (push) Waiting to run
CI (riscv) / ubuntu-cpu-riscv64-native (push) Waiting to run
CI (self-hosted CPU backend) / cpu-arm64-high-perf-graviton4 (push) Waiting to run
CI (self-hosted CUDA backend) / gpu-cuda (push) Waiting to run
CI (self-hosted CUDA backend) / gpu-rocm (gfx1151) (push) Waiting to run
CI (self-hosted CUDA backend) / gpu-rocm (gfx1201) (push) Waiting to run
CI (self-hosted KleidiAI backend) / cpu-arm64-graviton4-kleidiai (push) Waiting to run
CI (self-hosted Metal backend) / gpu-metal (push) Waiting to run
CI (self-hosted OpenVINO backend) / gpu-openvino-low-perf (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-nvidia-cm (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-nvidia-cm2 (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-apple (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-intel-linux (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-intel-windows (push) Waiting to run
CI (self-hosted WebGPU backend) / gpu-webgpu-nvidia (push) Waiting to run
CI (self-hosted WebGPU backend) / gpu-webgpu-apple (push) Waiting to run
Code Style Checker / model-naming (push) Waiting to run
EditorConfig Checker / editorconfig (push) Waiting to run
HIP quality check / ubuntu-22-hip-quality-check (push) Waiting to run
Models Backend Check / cuda (push) Waiting to run
Models Backend Check / metal (push) Waiting to run
Models Backend Check / rocm (push) Waiting to run
Models Backend Check / vulkan-nvidia (push) Waiting to run
Models Backend Check / vulkan-amd (push) Waiting to run
Models Backend Check / webgpu-nvidia (push) Waiting to run
flake8 Lint / Lint (push) Waiting to run
Python Type-Check / python type-check (push) Waiting to run
Release / check-release (push) Waiting to run
CI (sycl) / ubuntu-26-sycl (fp16, ON) (push) Waiting to run
CI (sycl) / ubuntu-26-sycl (fp32, OFF) (push) Waiting to run
CI (sycl) / windows-latest-sycl (push) Waiting to run
CI (virtgpu) / ubuntu-26-virtgpu (push) Waiting to run
CI (vulkan) / ubuntu (push) Waiting to run
CI (vulkan) / ubuntu-llvmpipe (push) Waiting to run
CI (vulkan) / windows / arm64 (push) Waiting to run
CI (vulkan) / windows / x64 (push) Waiting to run
CI (wasi) / ubuntu-webgpu (push) Waiting to run
CI (wasm) / ubuntu-webgpu (push) Waiting to run
CI (self-hosted CPU backend) / cpu-x64-high-perf (push) Waiting to run
UI / Checks (push) Blocked by required conditions
UI / Build static output (push) Waiting to run
UI / Library checks (push) Waiting to run
UI / Voice checks (push) Waiting to run
UI / Harness checks (push) Waiting to run
Release / macos-cpu (arm64, arm64, -DGGML_METAL_EMBED_LIBRARY=ON -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3, macos-26) (push) Blocked by required conditions
Release / macos-cpu (x64, x64, -DGGML_METAL=OFF -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3, macos-15-intel) (push) Blocked by required conditions
Release / linux-arm64-snapdragon (push) Blocked by required conditions
Release / ubuntu-cpu (26.04-s390x, ubuntu-26.04-s390x) (push) Blocked by required conditions
Release / ubuntu-cpu (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Release / ubuntu-cpu (s390x, ubuntu-24.04-s390x) (push) Blocked by required conditions
Release / ubuntu-cpu (x64, ubuntu-22.04) (push) Blocked by required conditions
Release / ubuntu-24-openvino (push) Blocked by required conditions
Release / windows-openvino (push) Blocked by required conditions
Release / ubuntu-vulkan (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Release / ubuntu-vulkan (x64, ubuntu-22.04) (push) Blocked by required conditions
Release / ubuntu-cuda (13.5, arm64) (push) Blocked by required conditions
Release / ubuntu-cuda (12.8, x64) (push) Blocked by required conditions
Release / ubuntu-cuda (13.5, x64) (push) Blocked by required conditions
Release / android-arm64 (push) Blocked by required conditions
Release / android-arm64-snapdragon (push) Blocked by required conditions
Release / windows-cpu / arm64 (push) Blocked by required conditions
Release / windows-cpu / x64 (push) Blocked by required conditions
Release / windows-rocm (10.0.0, x64, gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Blocked by required conditions
Release / windows (arm64, opencl-adreno, -G "Ninja Multi-Config" -D CMAKE_TOOLCHAIN_FILE=cmake/arm64-windows-llvm.cmake -DCMAKE_PREFIX_PATH="$env:RUNNER_TEMP/opencl-arm64-release" -DGGML_OPENCL=ON -DGGML_OPENCL_USE_ADRENO_KERNELS=ON -DGGML_OPENCL_ADRENO_BIN_KERN… (push) Blocked by required conditions
Release / windows (arm64, vulkan, -G "Ninja Multi-Config" -DGGML_VULKAN=ON -D CMAKE_TOOLCHAIN_FILE=cmake/arm64-windows-llvm.cmake, ggml-vulkan) (push) Blocked by required conditions
Release / windows (x64, vulkan, -DGGML_VULKAN=ON, ggml-vulkan) (push) Blocked by required conditions
Release / windows-cuda (13.5, arm64) (push) Blocked by required conditions
Release / windows-cuda (12.4, x64) (push) Blocked by required conditions
Release / windows-cuda (13.5, x64) (push) Blocked by required conditions
Release / windows-sycl (push) Blocked by required conditions
Release / ubuntu-24-sycl (fp16, ON) (push) Blocked by required conditions
Release / ubuntu-24-sycl (fp32, OFF) (push) Blocked by required conditions
Release / ubuntu-24-rocm (10.0.0, x64, gfx908;gfx90a;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1200;gfx1201) (push) Blocked by required conditions
Release / ubuntu-26-zdnn (push) Blocked by required conditions
Release / ios-xcode (push) Blocked by required conditions
Release / ui-build (push) Blocked by required conditions
Server (sanitize) / server (RelWithDebInfo, ADDRESS) (push) Waiting to run
Server (sanitize) / server (RelWithDebInfo, UNDEFINED) (push) Waiting to run
Server (self-hosted) / server-cuda (push) Waiting to run
Server (self-hosted) / server-metal (push) Waiting to run
Server (self-hosted) / server-kleidiai (push) Waiting to run
Server (speculative decoding) / server-speculative (push) Waiting to run
Server / ubuntu (push) Waiting to run
Server / windows (push) Waiting to run
UI (self-hosted) / Build static output (push) Waiting to run
UI (self-hosted) / Checks (push) Blocked by required conditions
UI (self-hosted) / E2E Tests (push) Blocked by required conditions
UI Storybook / Deploy to Hugging Face Spaces (push) Waiting to run
UI / Chat-only build (push) Waiting to run
UI / Demo harness checks (push) Waiting to run
UI / E2E Tests (push) Blocked by required conditions

* opencl: add multi token F32 GEMV

Co-authored-by: Hongqiang Wang <wangh@qti.qualcomm.com>
Assisted-by: Codex

* opencl: let ne01 > 512 still use tiled GEMM

---------

Co-authored-by: Hongqiang Wang <wangh@qti.qualcomm.com>
This commit is contained in:
lhez 2026-11-26 13:26:17 -08:00 • committed by GitHub
parent e087c6c4e7
commit 9fa7a33cbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 144 additions and 2 deletions

View file

@ -1249,6 +1249,7 @@ struct ggml_backend_opencl_context {
cl_kernel kernel_mul_mv_id_mxfp4_f32_flat;
cl_kernel kernel_mul_mm_f32_f32_l4_lm;
cl_kernel kernel_gemv_f32_f32_mc; // multi-column (small-N) f32 GEMV for spec/MTP verify
cl_kernel kernel_gemv_f32_f32_mcr = nullptr;
cl_kernel kernel_mul_mm_f16_f32_l4_lm;
cl_kernel kernel_mul_mm_f16_f32_l4_lm_n8;
cl_kernel kernel_mul_mm_q1_0_f32_l4_lm;
@ -1571,6 +1572,7 @@ struct ggml_backend_opencl_context {
}
#endif
for (cl_kernel kernel : {
kernel_gemv_f32_f32_mcr,
kernel_xielu_f32,
kernel_xielu_f16,
kernel_mul_mm_f16_f32_kq_p8,
@ -4282,6 +4284,16 @@ static void load_cl_kernels(ggml_backend_opencl_context *backend_ctx) {
CL_CHECK((backend_ctx->kernel_mul_mm_f32_f32_l4_lm = clCreateKernel(backend_ctx->program_mul_mm_f32_f32_l4_lm, "kernel_mul_mm_f32_f32_l4_lm", &err), err));
CL_CHECK((backend_ctx->kernel_gemv_f32_f32_mc = clCreateKernel(backend_ctx->program_mul_mm_f32_f32_l4_lm, "kernel_gemv_f32_f32_mc", &err), err));
cl_kernel mcr = clCreateKernel(backend_ctx->program_mul_mm_f32_f32_l4_lm, "kernel_gemv_f32_f32_mcr", &err);
if (err == CL_SUCCESS) {
if (backend_ctx->get_kernel_workgroup_size(mcr) >= 64) {
backend_ctx->kernel_gemv_f32_f32_mcr = mcr;
} else {
CL_CHECK(clReleaseKernel(mcr));
}
}
GGML_LOG_CONT(".");
}
@ -33076,6 +33088,20 @@ static void ggml_cl_fwht(ggml_backend_t backend, const ggml_tensor * src, ggml_t
backend_ctx->enqueue_ndrange_kernel(kernel, 2, global, local, dst);
}
// reduce lanes per row for short K, down to four lanes.
static int ggml_cl_mcr_lpr_log2(int ne00) {
int lpr_log2 = 6;
while (lpr_log2 > 2 && ((ne00 / 4) >> lpr_log2) < 4) {
lpr_log2--;
}
return lpr_log2;
}
static bool ggml_cl_mcr_wide_enough(int ne00, int ne01) {
const int rows_per_wg = 64 >> ggml_cl_mcr_lpr_log2(ne00);
return CEIL_DIV((size_t) ne01, rows_per_wg) >= 256;
}
static void ggml_cl_mul_mat(ggml_backend_t backend, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
GGML_ASSERT(src0);
GGML_ASSERT(src0->extra);
@ -33670,8 +33696,63 @@ static void ggml_cl_mul_mat(ggml_backend_t backend, const ggml_tensor * src0, co
// under-occupied WG at ~2.3% tile utilization. Route to a per-output
// (m,n) GEMV (64-thread WG, K-split + __local reduce) instead.
// 2D contiguous, small N + skinny M only. Default on for X2E, GGML_OPENCL_F32_MC=0/1 forces it.
static const char * f32_mc_env = getenv("GGML_OPENCL_F32_MC");
const bool f32_mc = f32_mc_env ? atoi(f32_mc_env) != 0 : backend_ctx->adreno_gen == ADRENO_GPU_GEN::X2E;
static const int f32_mc_env = [] {
const char * env = getenv("GGML_OPENCL_F32_MC");
return env ? atoi(env) : -1;
}();
const bool f32_mc = f32_mc_env >= 0 ? f32_mc_env != 0 : backend_ctx->adreno_gen == ADRENO_GPU_GEN::X2E;
static const int f32_mcr_env = [] {
const char * env = getenv("GGML_OPENCL_F32_MCR");
return env && *env ? atoi(env) : -1;
}();
const bool f32_mcr = f32_mcr_env >= 0 ? f32_mcr_env != 0 :
(backend_ctx->adreno_gen == ADRENO_GPU_GEN::X2E || backend_ctx->adreno_gen == ADRENO_GPU_GEN::A8X);
const bool f32_mcr_allowed = f32_mc_env >= 0 ? f32_mc_env != 0 : !adreno_e17_compiler_quirks(backend_ctx);
if (f32_mcr && f32_mcr_allowed && backend_ctx->kernel_gemv_f32_f32_mcr != nullptr &&
ne11 >= 2 && ne11 <= 8 && ne00 > 0 && ne00 <= INT_MAX && ne01 > 0 && ne01 <= 512 &&
(size_t) ne01 <= SIZE_MAX / 64 && (ne00 % 4 == 0) &&
ne02 == 1 && ne03 == 1 && ne12 == 1 && ne13 == 1 &&
dst->ne[2] == 1 && dst->ne[3] == 1 &&
ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst) &&
offset0 % 16 == 0 && offset1 % 16 == 0 && offsetd % sizeof(float) == 0 &&
ggml_cl_mcr_wide_enough((int) ne00, (int) ne01)
) {
const int K = (int) ne00;
const int M = (int) ne01;
const int N = (int) ne11;
const int stride_a = K;
const int stride_b = K;
const int stride_d = M;
const int lpr_log2 = ggml_cl_mcr_lpr_log2(K);
const int rows_per_wg = 64 >> lpr_log2;
cl_kernel mcr = backend_ctx->kernel_gemv_f32_f32_mcr;
CL_CHECK(clSetKernelArg(mcr, 0, sizeof(cl_mem), &extra0->data_device));
CL_CHECK(clSetKernelArg(mcr, 1, sizeof(cl_ulong), &offset0));
CL_CHECK(clSetKernelArg(mcr, 2, sizeof(cl_mem), &extra1->data_device));
CL_CHECK(clSetKernelArg(mcr, 3, sizeof(cl_ulong), &offset1));
CL_CHECK(clSetKernelArg(mcr, 4, sizeof(cl_mem), &extrad->data_device));
CL_CHECK(clSetKernelArg(mcr, 5, sizeof(cl_ulong), &offsetd));
CL_CHECK(clSetKernelArg(mcr, 6, sizeof(int), &K));
CL_CHECK(clSetKernelArg(mcr, 7, sizeof(int), &M));
CL_CHECK(clSetKernelArg(mcr, 8, sizeof(int), &N));
CL_CHECK(clSetKernelArg(mcr, 9, sizeof(int), &stride_a));
CL_CHECK(clSetKernelArg(mcr, 10, sizeof(int), &stride_b));
CL_CHECK(clSetKernelArg(mcr, 11, sizeof(int), &stride_d));
CL_CHECK(clSetKernelArg(mcr, 12, sizeof(int), &lpr_log2));
size_t global[] = {CEIL_DIV((size_t) M, rows_per_wg) * 64, 1, 1};
size_t local[] = {64, 1, 1};
backend_ctx->enqueue_ndrange_kernel(mcr, 3, global, local, dst);
return;
}
if (f32_mc && ne11 >= 2 && ne11 <= 8 && ne01 <= 512 && (ne00 % 4 == 0) &&
ne02 == 1 && ne12 == 1 && ne13 == 1 &&
ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) {

View file

@ -194,3 +194,64 @@ kernel void kernel_gemv_f32_f32_mc(
}
if (lane == 0) dst[(ulong)n * (uint)stride_d + m] = red[0];
}
// Reuse each weight row across the narrow batch, with one accumulator per column.
kernel void kernel_gemv_f32_f32_mcr(
global const float * src0, ulong offset0,
global const float * src1, ulong offset1,
global float * dst, ulong offsetd,
int ne00, // K
int ne01, // M
int ne11, // N, 2..8
int stride_a,
int stride_b,
int stride_d,
int lpr_log2 // lanes per row = 1 << lpr_log2
) {
src0 = (global const float*)((global const char*)src0 + offset0);
src1 = (global const float*)((global const char*)src1 + offset1);
dst = (global float*)((global char*)dst + offsetd);
const uint lane = get_local_id(0);
const uint lpr = 1u << lpr_log2;
const uint l = lane & (lpr - 1);
const uint mr = get_group_id(0) * (64u >> lpr_log2) + (lane >> lpr_log2);
const bool valid = mr < (uint)ne01;
// Keep padded rows in the local-memory barriers.
const uint m = valid ? mr : (uint)ne01 - 1;
global const float4 * wrow = (global const float4*)(src0 + (ulong)m * (uint)stride_a);
const uint k4 = (uint)ne00 >> 2;
float acc[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
for (uint k = l; k < k4; k += lpr) {
const float4 w = wrow[k];
#pragma unroll
for (int n = 0; n < 8; n++) {
if (n < ne11) {
acc[n] += dot(w, ((global const float4*)(src1 + (ulong)n * (uint)stride_b))[k]);
}
}
}
local float red[8 * 64];
#pragma unroll
for (int n = 0; n < 8; n++) {
red[n * 64 + lane] = acc[n];
}
barrier(CLK_LOCAL_MEM_FENCE);
for (uint s = lpr >> 1; s > 0; s >>= 1) {
if (l < s) {
#pragma unroll
for (int n = 0; n < 8; n++) {
red[n * 64 + lane] += red[n * 64 + lane + s];
}
}
barrier(CLK_LOCAL_MEM_FENCE);
}
if (l == 0 && valid) {
for (int n = 0; n < ne11; n++) {
dst[(ulong)n * (uint)stride_d + m] = red[n * 64 + lane];
}
}
}