Skip to content

ggml : support a BF16 src1 in MUL_MAT and MUL_MAT_ID (CPU, CUDA) - #32533

Merged
anujj merged 6 commits into
ggml-org:masterfrom
anujj:anujj/bf16-mul-mat-src1
Nov 26, 2026
Merged

anujj merged 6 commits into
ggml-org:masterfrom
anujj:anujj/bf16-mul-mat-src1

Conversation

@anujj

@anujj anujj commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Let the CUDA matmuls read a BF16 src1 directly, the next step for BF16 activations after #32135 and #32387. On master a BF16 src1 goes to cuBLAS with dequantized weights, and MUL_MAT_ID aborts.

  • The src1 quantizers (Q8_1, NVFP4, MXFP4, FP8) take a src1 type template parameter, and MMVF uses a runtime flag. BF16 is widened to F32 on load, and the matmul fusions accept it.
  • MMF stays F32-only for now (follow-up): cuBLAS for MUL_MAT, MMVF or the sorted fallback for MUL_MAT_ID.
  • CPU converts a BF16 src1 to vec_dot_type. Backends without BF16-src1 kernels reject it in supports_op.
  • A MUL_MAT with the Hadamard hint is an FWHT of src1 (F32/F16 only), so it now rejects a BF16 src1 for every src0 type.

Additional information

test-backend-ops perf, RTX 5090, us/run:

op weights tokens F32 src1 BF16 src1 change
MUL_MAT (4096 x 14336) F8_E4M3 1 17.40 15.18 -12.8%
MUL_MAT F8_E4M3 512 168.92 155.95 -7.7%
MUL_MAT_ID (Qwen3-30B-A3B) Q4_K 512 170.60 156.24 -8.4%
MUL_MAT_ID IQ4_XS 512 157.19 149.68 -4.8%

Limitations

MMF still reads only an F32 src1, so with a BF16 src1, F16/BF16 weights past MMVF go to cuBLAS (MUL_MAT) or to the slower sorted fallback without CUDA graphs (MUL_MAT_ID). Nothing emits BF16 activations yet; I'll address this in a follow-up PR.

Requirements

@anujj
anujj marked this pull request as ready for review November 18, 2026 11:28
@anujj
anujj requested review from a team, bartowski1182, ggerganov and wine99 as code owners November 18, 2026 11:28
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language Apple Metal https://en.wikipedia.org/wiki/Metal_(API) Ascend NPU issues specific to Ascend NPUs CUDA Related to the CUDA backend AMD ZenDNN Issues related to the AMD ZenDNN backend OpenVINO labels Nov 18, 2026
@anujj

anujj commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Adding @am17an @gaugarg-nv @ORippler @JohannesGaessler for review. Please take a look when you get a chance.

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The templated quantizers + quantize_launch_typed wrapper look good, and the float path still compiles to the same direct loads. A few things:

  1. MMVF F32 path now branches on fusion.y_bf16 in the inner loop. load_y2 checks the flag on every y load in every weight variant (F32/F16/BF16/FP8), even when src1 is F32. It's uniform so probably cheap, but MMVF is the decode path for F16/BF16 models and the numbers in the description only compare F32 vs BF16 src1 on this branch. Can you post F32-src1 MMVF numbers master vs this PR (e.g. test-backend-ops perf -o MUL_MAT with F16/BF16 weights, 1-8 tokens)? Otherwise making y's type a template parameter would avoid the question.

  2. BF16 src1 + float experts (F16/BF16) on NVIDIA past 8 tokens goes to the sorted fallback. Since MMF is F32-only, ggml_cuda_mul_mat_id takes the host-synced path and ggml_cuda_mul_mat_id_needs_sync returns true, so CUDA graphs are off for that graph. Plain MUL_MAT with F16 weights past MMVF goes to cuBLAS instead of MMF. Not a regression today since nothing emits BF16 activations by default, but whatever does will need the MMF follow-up first (or keep F32 there). Worth stating in the description.

  3. Unrelated fixes bundled in, I'd split them out:

    • int64_t(channel_x)*stride_channel_x in MMVF fixes an existing int32 overflow on the F32 path for large expert tensors; that should land on its own.
    • tiled.cpp return true -> break makes the src1 type check after the switch reachable (it was dead code), which also changes behaviour for an F16 src1.
    • MUSA QY2 Q2_K MUL_MAT_ID rejection no longer requires an F32 b.
  4. Nit: if the overflow fix stays, offset_y still does channel_y*stride_channel_y as int*int, and dst channel_dst*stride_channel_dst likewise.

  5. All new BF16 tests use a contiguous src1. A permuted / row-strided BF16 src1 case for MMQ, MMVQ and FP8 would cover the new nb1x/nb10 stride arithmetic (FP8 used to hardcode sizeof(float)).

  6. CPU supports_op: with the Hadamard hint a BF16 src1 is now rejected for every src0 type (previously the hint check only applied to F32 src0). Fine, just worth a line in the description.

@anujj

anujj commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Pushed cf64513.

1. MMVF flag. y_bf16 is a kernel argument, so nvcc checks it once before the loop and runs separate F32 and BF16 loops; there's no branch per load. In a few kernels nvcc picks a different unroll for the F32 loop (FP8, F16 with F32 accumulation), so I timed all MMVF cases. F32 src1, master vs this PR, RTX 5090, us/run, one ABBA round:

weights (4096 x 14336) tokens master this PR change
F32 1 143.5 142.3 -0.9%
F16 1 73.3 72.8 -0.7%
F16, F32 acc. 1 72.4 72.4 +0.1%
BF16 1 72.9 72.9 0.0%
F8_E4M3 1 18.4 18.2 -1.1%
F8_E4M3 4 51.5 47.8 -7.2%

All MMVF cases are within ±1.1%, and F8 at 4 tokens is 7% faster. So I'd keep the runtime flag: a template parameter would double the 440 MMVF kernels for no gain.

2. BF16 src1 past MMVF. Agreed, added to the description under Limitations: MMF has to read BF16 before anything turns BF16 activations on.

3. Bundled fixes.

  • MMVF 64-bit offsets: removed from this PR, I'll open it separately (it also widens offset_y, dst, the bias and row offsets
  • tiled.cpp: needed here. The tiled driver asserts an F32 src1, so with the dead check a BF16 src1 crashed the CPU reference. For an F16 src1 nothing reachable changes, because CPU supports_op already rejects F16 with K-quant/IQ weights.
  • MUSA: restored the explicit b->type == GGML_TYPE_F32 || b->type == GGML_TYPE_BF16. A BF16 b reaches the same Q2_K kernels.

4. Strided tests. Added 6 BF16 src1 cases with a permuted src1 (s11 != ne10): MMVQ, few-row MMQ, MMQ (Q8_1, FP4), FP8 and MMVF. nsys shows each runs its kernel. A deliberate stride bug fails all 6, while the older contiguous cases pass.

5. Hadamard hint. Added a line to the description.

Comment thread ggml/src/ggml-cuda/ggml-cuda.cu Outdated
Comment on lines +2303 to +2311
// MUL_MAT_ID with F32/F16/BF16 experts takes MMVF (up to MMVF_MAX_BATCH_SIZE tokens) on AMD, and on NVIDIA for a BF16 src1,
// which MMF can not read. On NVIDIA an F32 src1 takes MMF where MMF supports the experts, otherwise the sorted fallback.
// MMVF reads src0 in pairs of values, so it needs an even number of columns and rows aligned to a pair.
static bool ggml_cuda_mul_mat_id_use_mmvf_float(const ggml_tensor * src0, const ggml_tensor * src1, const int cc) {
return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16) &&
(GGML_CUDA_CC_IS_AMD(cc) || src1->type == GGML_TYPE_BF16) &&
src0->ne[0] % 2 == 0 && ggml_cuda_is_aligned(src0, 2*ggml_type_size(src0->type));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, please avoid adding an extra function like this and instead put this logic into the existing should_use functions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't benchmark it but the runtime checks in MMVF are I think liable to cause performance regressions. I would say to just add a template specialization to the BF16 variant for BF16 src1 input.

@anujj

anujj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Both are addressed:

Runtime check in MMVF** (373eab4): removed. The src1 type is now a template parameter, instantiated as BF16 for the BF16 variant as you suggested, and also for FP8, because FP8 weights are where a BF16 src1 gains most. Is FP8 OK with you?

  • F32 and F16 are back to master's code.

test-backend-ops perf, RTX 5090, MUL_MAT 4096 x 14336, 1 token, us/run

weights F32 src1 BF16 src1 change
BF16 73.68 73.61 -0.1%
F8_E4M3 18.80 15.47 -17.7%

Comment thread ggml/src/ggml-cuda/mmvf.cu Outdated
Comment on lines +16 to +24
// pair i of src1 values, from an F32 or a BF16 src1
template <typename T_y>
static __device__ __forceinline__ float2 mmvf_load_y2(const T_y * y, const int i) {
if constexpr (std::is_same_v<T_y, nv_bfloat16>) {
return ggml_cuda_cast<float2>(reinterpret_cast<const nv_bfloat162 *>(y)[i]);
} else {
return reinterpret_cast<const float2 *>(y)[i];
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this helper is actually needed. I don't see why ggml_cuda_cast could not be used directly.

Comment thread ggml/src/ggml-cuda/mmvf.cu Outdated

bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size, int64_t ne11) {
bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, ggml_type type_src1, int cc, int warp_size, int64_t ne11, bool mul_mat_id) {
const ggml_type type = src0->type;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const ggml_type type = src0->type;
const ggml_type type_src0 = src0->type;

I would say to rename this variable to avoid confusion with the new type_src1.

@anujj

anujj commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, addressed both of the comments

@JohannesGaessler

Copy link
Copy Markdown
Contributor

The gpu-rocm CI failure seem to be unrelated to this PR and should be fixed by #33045 .

@anujj
anujj merged commit 09c1410 into ggml-org:master Nov 26, 2026
45 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AMD ZenDNN Issues related to the AMD ZenDNN backend Apple Metal https://en.wikipedia.org/wiki/Metal_(API) Ascend NPU issues specific to Ascend NPUs CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning OpenVINO SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants