* metal : accumulate the bf16 flash attention O in bfloat
The bf16 prefill kernel kept the O accumulator in half while Q/K/V are
bfloat, which mixed the accumulator type with the operands. Accumulate
in bfloat and rescale the O matrix in float, since Metal does not allow
bfloat4 *= float.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : fix flash attention kv_rows with partial K/V chunks
The kv_rows path assumed the row lists are padded to the kernel chunk
(256/64/32) and asserted it, so a partial last chunk was mishandled:
- the pad kernel copied cache columns instead of the gathered rows, and
the pad buffer was sized/indexed per cache stream (ne13) instead of
per q slice (ne03), so slices >= 1 read the wrong region
- the vec path did not gather the partial chunk through the row list and
read V at the wrong pad offset, dropping the last chunk
- direct row reads applied the cache stream stride to a shared cache
Gather the partial chunk through the row list, size the pad per slice,
and zero the stream stride for direct row reads. Also allow BF16 K/V with
kv_rows: the non-vec gather converts to F16 and the tensor path uses the
f16 kernel over the gathered rows.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use bfloat shared memory for the bf16 vec FA kernels
The bf16 vec instantiations used the half shared-memory type set, so
K/V were converted bfloat->half on load. Add FA_TYPES_BF with bfloat
Q/K/V in shared memory so the vec kernels read bfloat directly through
the direct K/V path, and convert Q from f32 to bfloat as the prefill
bf16 kernel does.
Add the tuned _q%d_ne%d bf16 variants to match the f16/quantized sets,
since the fa_vec tuning selects them, and use a float4 broadcast for the
zero-fill casts that bfloat4 does not accept. Add bf16 to the FA vec
slice test types to cover the new path.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : gather the kv_rows K/V in their own type
The non-vec kv_rows path always gathered the referenced rows into an F16
buffer, so a BF16 cache was converted to F16 and the attention ran the
f16 kernels. Replace the use_kv_f16 flag with the type of the buffer that
the attention reads: quantized K/V is still dequantized to F16, but the
cache type is otherwise kept, so BF16 stays BF16 through the gather and
the partial-chunk pad.
The gather kernel is now same-type (template on T, no cast), the gather
and pad sizes/strides use ggml_type_size(kv_type), and the tensor,
half8x8 and vec pipeline selectors take kv_type instead of the boolean.
Add BF16 gather tests for the half8x8 path with and without the pad.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use clearer names for the FA kernel paths
Replace the historical half8x8/half4x4 labels with the 8x8 simdgroup
kernel and the vec kernel, matching the actual kernel families. Comment
only, no functional change.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : trim redundant flash attention comments
Update the kv_rows support check that still claimed a contiguous F16 copy
with BF16 converted to F16, drop the duplicated kv_type, pad-per-slice,
dense-op and column-order explanations, and keep only the comments that
explain non-obvious behavior. Comment only, no functional change.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : simplify the flash attention K/V type helpers
Make kv_type the single source of truth for the type the flash attention
kernels read, and derive dequant_kv from it (quantized cache and kv_type
F16). Remove use_kv_f16 - its heuristic moves into kv_type - and use
kv_type in the tensor path check and the is_q flag. This also enables the
tensor path for a quantized rows gather whose shape fills the GPU.
Remove the stale commented-out use_kv_f16/use_vec lines as well.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)