Commit 81a22ceb7 for llama.cpp
commit 81a22ceb7bf3026860664cc08d8918849c7ad65d
Author: Xuan-Son Nguyen <son@huggingface.co>
Date: Mon Nov 30 12:53:08 2026 +0100
webgpu: mat-vec workgroup size by row length (#33308)
diff --git a/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp b/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp
index d37e7ec78..fe0865e81 100644
--- a/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp
+++ b/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp
@@ -1020,10 +1020,12 @@ struct ggml_webgpu_mul_mat_vec_pipeline_key {
uint32_t num_cols;
bool use_mmvq;
bool src_overlap;
+ uint32_t wg_size;
bool operator==(const ggml_webgpu_mul_mat_vec_pipeline_key & other) const {
return src0_type == other.src0_type && src1_type == other.src1_type && vectorized == other.vectorized &&
- num_cols == other.num_cols && use_mmvq == other.use_mmvq && src_overlap == other.src_overlap;
+ num_cols == other.num_cols && use_mmvq == other.use_mmvq && src_overlap == other.src_overlap &&
+ wg_size == other.wg_size;
}
};
@@ -1036,6 +1038,7 @@ struct ggml_webgpu_mul_mat_vec_pipeline_key_hash {
ggml_webgpu_hash_combine(seed, key.num_cols);
ggml_webgpu_hash_combine(seed, key.use_mmvq);
ggml_webgpu_hash_combine(seed, key.src_overlap);
+ ggml_webgpu_hash_combine(seed, key.wg_size);
return seed;
}
};
@@ -2272,6 +2275,40 @@ class ggml_webgpu_shader_lib {
context.supports_dot_product, context.vendor);
key.src_overlap = src_overlap;
+ // short rows leave most threads idle (K = 1024 = 4 Q4_K blocks of 16 threads)
+ // use the smallest power-of-two workgroup that covers the row
+ key.wg_size = WEBGPU_MUL_MAT_VEC_WG_SIZE;
+ if (!key.use_mmvq) {
+ uint32_t threads_per_block = 0;
+ switch (key.src0_type) {
+ case GGML_TYPE_Q4_0:
+ case GGML_TYPE_Q4_1:
+ case GGML_TYPE_Q5_0:
+ case GGML_TYPE_Q5_1:
+ case GGML_TYPE_Q8_0:
+ threads_per_block = 4;
+ break;
+ case GGML_TYPE_Q2_K:
+ case GGML_TYPE_Q3_K:
+ case GGML_TYPE_Q4_K:
+ case GGML_TYPE_Q5_K:
+ case GGML_TYPE_Q6_K:
+ threads_per_block = 16;
+ break;
+ default:
+ break;
+ }
+ if (threads_per_block > 0) {
+ const uint64_t threads =
+ (uint64_t) (context.src0->ne[0] / ggml_blck_size(key.src0_type)) * threads_per_block;
+ uint32_t wg = 64;
+ while (wg < threads && wg < WEBGPU_MUL_MAT_VEC_WG_SIZE) {
+ wg <<= 1;
+ }
+ key.wg_size = wg;
+ }
+ }
+
auto it = mul_mat_vec_pipelines.find(key);
if (it != mul_mat_vec_pipelines.end()) {
return it->second;
@@ -2381,8 +2418,11 @@ class ggml_webgpu_shader_lib {
// VEC/SCALAR controls
defines.push_back(key.vectorized ? "VEC" : "SCALAR");
- uint32_t wg_size = WEBGPU_MUL_MAT_VEC_WG_SIZE;
+ uint32_t wg_size = key.wg_size;
uint32_t outputs_per_wg = WEBGPU_MUL_MAT_VEC_FLOAT_OUTPUTS_PER_WG;
+ if (wg_size != WEBGPU_MUL_MAT_VEC_WG_SIZE) {
+ variant += "_wg" + std::to_string(wg_size);
+ }
if (key.src0_type == GGML_TYPE_Q1_0) {
outputs_per_wg = WEBGPU_MUL_MAT_VEC_LEGACY_Q_OUTPUTS_PER_WG;