metal: implement flash attention kernel for quantized KV cache #9735
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is mainly for discussion. The strategy and code quality is far from being merged.
To support quantized KV cache. I write a new FA kernel similar to
kernel_flash_attn_ext_vec_f16
and add dequantization support. Since 'kernel_flash_attn_ext_vec_f16' use vce4 extensively thus forces D is at least 128. I write a new version using only scalars, then D is only required to be multiple of 32.I only implement
ctk = ctv = q8_0
as a proof of concept .The code is generic and support of other formats could be added easily.measurement (before this PR)
measurement (after this PR)
For measurement purpose, this PR forces all FA code path uses the new 'kernel_flash_attn_ext_scalar_f16'.
I observe that prefill slows down severely in long input case (131tok/s vs 265tok/s when pp2048).