Repository navigation
ggml : support a BF16 src1 in MUL_MAT and MUL_MAT_ID (CPU, CUDA) - #32533
Conversation
|
Adding @am17an @gaugarg-nv @ORippler @JohannesGaessler for review. Please take a look when you get a chance. |
39a5faf to
8741067
Compare
am17an
left a comment
There was a problem hiding this comment.
The templated quantizers + quantize_launch_typed wrapper look good, and the float path still compiles to the same direct loads. A few things:
-
MMVF F32 path now branches on
fusion.y_bf16in the inner loop.load_y2checks 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_MATwith F16/BF16 weights, 1-8 tokens)? Otherwise making y's type a template parameter would avoid the question. -
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_idtakes the host-synced path andggml_cuda_mul_mat_id_needs_syncreturns 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. -
Unrelated fixes bundled in, I'd split them out:
int64_t(channel_x)*stride_channel_xin MMVF fixes an existing int32 overflow on the F32 path for large expert tensors; that should land on its own.tiled.cppreturn true->breakmakes 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.
-
Nit: if the overflow fix stays,
offset_ystill doeschannel_y*stride_channel_yas int*int, and dstchannel_dst*stride_channel_dstlikewise. -
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/nb10stride arithmetic (FP8 used to hardcodesizeof(float)). -
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.
…fix moved to its own PR
|
Thanks for the review! Pushed cf64513. 1. MMVF flag.
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.
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. |
| // 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)); | ||
| } | ||
|
|
There was a problem hiding this comment.
If possible, please avoid adding an extra function like this and instead put this logic into the existing should_use functions.
There was a problem hiding this comment.
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.
|
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?
|
| // 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]; | ||
| } | ||
| } |
There was a problem hiding this comment.
I'm not sure this helper is actually needed. I don't see why ggml_cuda_cast could not be used directly.
|
|
||
| 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; |
There was a problem hiding this comment.
| 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.
|
Thanks for the review, addressed both of the comments |
|
The gpu-rocm CI failure seem to be unrelated to this PR and should be fixed by #33045 . |
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.
supports_op.Additional information
test-backend-ops perf, RTX 5090, us/run: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