Commit 9bb00f98c for llama.cpp
commit 9bb00f98c4846f1a2841a61b2096c215889129fa
Author: Clemens Wasser <clemens.wasser@gmail.com>
Date: Mon Nov 30 15:20:18 2026 +0100
sycl : fix batched f16 mul_mat with broadcast batches and strided src1 (#33214)
* sycl : fix oneDNN batched matmul with broadcast batches
* sycl : keep src1 strides of batched f16 mul_mat in oneDNN builds
diff --git a/ggml/src/ggml-sycl/gemm.hpp b/ggml/src/ggml-sycl/gemm.hpp
index 81bc5c2e6..f6e153c07 100644
--- a/ggml/src/ggml-sycl/gemm.hpp
+++ b/ggml/src/ggml-sycl/gemm.hpp
@@ -43,16 +43,22 @@ public:
auto stream = ctx.stream_dnnl(q);
auto eng = ctx.engine_dnnl(q);
- dnnl::memory::dims a_dims = {batches_a, m, k };
- dnnl::memory::dims a_strides = {stra2, stra1, stra0};
+ // broadcast as a size-1 dimension: oneDNN f16 matmul is wrong for shifted, only 2-byte aligned base pointers
+ const dnnl_dim_t outer = std::min(batches_a, batches_b);
+ const dnnl_dim_t ra = batches_a / outer;
+ const dnnl_dim_t rb = batches_b / outer;
+ GGML_ASSERT(ra * outer == batches_a && rb * outer == batches_b);
+
+ dnnl::memory::dims a_dims = { outer, ra, m, k };
+ dnnl::memory::dims a_strides = { stra2 * ra, stra2, stra1, stra0 };
const auto a_in_md = dnnl::memory::desc(a_dims, at, a_strides);
- dnnl::memory::dims b_dims = {batches_b, k, n };
- dnnl::memory::dims b_strides = {strb2, strb0, strb1};
+ dnnl::memory::dims b_dims = { outer, rb, k, n };
+ dnnl::memory::dims b_strides = { strb2 * rb, strb2, strb0, strb1 };
const auto b_in_md = dnnl::memory::desc(b_dims, bt, b_strides);
- dnnl::memory::dims c_dims = { std::max(batches_a, batches_b), m, n};
- dnnl::memory::dims c_strides = {m*n, 1, m };
+ dnnl::memory::dims c_dims = { outer, std::max(ra, rb), m, n };
+ dnnl::memory::dims c_strides = { m * n * std::max(ra, rb), m * n, 1, m };
const auto c_md = dnnl::memory::desc(c_dims, ct, c_strides);
dnnl::primitive_attr primitive_attr;
primitive_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
diff --git a/ggml/src/ggml-sycl/ggml-sycl.cpp b/ggml/src/ggml-sycl/ggml-sycl.cpp
index 8dfcebf41..923127979 100644
--- a/ggml/src/ggml-sycl/ggml-sycl.cpp
+++ b/ggml/src/ggml-sycl/ggml-sycl.cpp
@@ -4154,13 +4154,14 @@ static void ggml_sycl_mul_mat_batched_sycl(ggml_backend_sycl_context & ctx, cons
const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type, dst);
GGML_ASSERT(to_fp16_sycl != nullptr);
to_fp16_sycl(src1_f16, src1_f16_alloc.get(), ne_src1, queue);
+ // same element layout as src1, so s11, s12, s13 still apply (also for oneMKL when oneDNN is disabled at runtime)
+ src1_f16 = src1_f16_alloc.get();
# else
const int64_t ne_src1 = ggml_nelements(src1);
src1_f16_alloc.alloc(ne_src1);
const to_fp16_nc_sycl_t to_fp16_nc_sycl = ggml_get_to_fp16_nc_sycl(src1->type);
GGML_ASSERT(to_fp16_nc_sycl != nullptr);
to_fp16_nc_sycl(src1_f16, src1_f16_alloc.get(), ne10, ne11, ne12, ne13, s11, s12, s13, queue);
-#endif
src1_f16 = src1_f16_alloc.get();
s11 = ne10;
@@ -4168,6 +4169,7 @@ static void ggml_sycl_mul_mat_batched_sycl(ggml_backend_sycl_context & ctx, cons
s13 = ne12 * s12;
is_src1_cont_2 = true;
+#endif
}
ggml_sycl_pool_alloc<sycl::half> dst_f16(ctx.pool());
@@ -4204,57 +4206,14 @@ static void ggml_sycl_mul_mat_batched_sycl(ggml_backend_sycl_context & ctx, cons
int64_t str_b1 = nb11 / type_size_src1;
int64_t str_b2 = nb12 / type_size_src1;
- auto launch_gemm_for_batches = [&ctx, queue](const sycl::half *src0,
- const sycl::half *src1, float *dst,
- int64_t a0, int64_t a1, int64_t batcha,
- int64_t /*b0*/, int64_t b1, int64_t batchb,
- int64_t sa0, int64_t sa1, int64_t sa2,
- int64_t sb0, int64_t sb1, int64_t sb2,
- int64_t sd2) {
- bool supported_broadcast = batchb == batcha ? true
- : batchb == 1 || batcha == 1 ? true
- : false;
- if (supported_broadcast) {
- DnnlGemmWrapper::gemm(ctx, a1, b1, a0, src0,
- DnnlGemmWrapper::to_dt<sycl::half>(), sa0, sa1, sa2, src1,
- DnnlGemmWrapper::to_dt<sycl::half>(), sb0, sb1, sb2, dst,
- DnnlGemmWrapper::to_dt<float>(), queue, batcha, batchb);
- } else {
- // iterate over batches from smaller set of matrices (matrix 0)
- int64_t batches0 = batcha;
- int64_t batches1 = batchb;
-
- if (batches0 > batches1) {
- int64_t num_mul_mats = batches1;
- int64_t sub_batch = batches0 / num_mul_mats;
- // src0 is batched and bigger, shift and multiply with src1
- for (int64_t i0 = 0; i0 < num_mul_mats; i0++) {
- const sycl::half *src0_shifted = src0 + (sa2 * i0 * sub_batch);
- const sycl::half *src1_shifted = src1 + (sb2 * i0);
- float *dst_shifted = dst + (sd2 * i0 * sub_batch);
- DnnlGemmWrapper::gemm(ctx, a1, b1, a0, src0_shifted,
- DnnlGemmWrapper::to_dt<sycl::half>(), sa0, sa1, sa2,
- src1_shifted, DnnlGemmWrapper::to_dt<sycl::half>(), sb0,
- sb1, sb2, dst_shifted, DnnlGemmWrapper::to_dt<float>(),
- queue, sub_batch, 1);
- }
- } else {
- int64_t num_mul_mats = batches0;
- int64_t sub_batch = batches1 / num_mul_mats;
- // src1 is batched and bigger, shift and multiply with src0
- for (int64_t i1 = 0; i1 < num_mul_mats; i1++) {
- const sycl::half *src0_shifted = src0 + (sa2 * i1);
- const sycl::half *src1_shifted = src1 + (sb2 * i1 * sub_batch);
- float *dst_shifted = dst + (sd2 * i1 * sub_batch);
- DnnlGemmWrapper::gemm(ctx, a1, b1, a0, src0_shifted,
- DnnlGemmWrapper::to_dt<sycl::half>(), sa0, sa1, sa2,
- src1_shifted, DnnlGemmWrapper::to_dt<sycl::half>(), sb0,
- sb1, sb2, dst_shifted, DnnlGemmWrapper::to_dt<float>(),
- queue, 1, sub_batch);
- }
- }
- }
- };
+ auto launch_gemm_for_batches =
+ [&ctx, queue](const sycl::half * src0, const sycl::half * src1, float * dst, int64_t a0, int64_t a1,
+ int64_t batcha, int64_t /*b0*/, int64_t b1, int64_t batchb, int64_t sa0, int64_t sa1,
+ int64_t sa2, int64_t sb0, int64_t sb1, int64_t sb2, int64_t /*sd2*/) {
+ DnnlGemmWrapper::gemm(ctx, a1, b1, a0, src0, DnnlGemmWrapper::to_dt<sycl::half>(), sa0, sa1, sa2,
+ src1, DnnlGemmWrapper::to_dt<sycl::half>(), sb0, sb1, sb2, dst,
+ DnnlGemmWrapper::to_dt<float>(), queue, batcha, batchb);
+ };
const bool cont_batches_dim2_a = nb02 * ne02 == nb03;
const bool cont_batches_dim2_b = nb12 * ne12 == nb13;