-
Notifications
You must be signed in to change notification settings - Fork 744
[OP] support cfp8 in blackwell mla #7876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+92
−32
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -809,7 +809,7 @@ def forward_mixed( | |
| metadata.block_tables, | ||
| forward_meta.slot_mapping, | ||
| metadata.kv_signal_data_list[layer.layer_id], | ||
| "none", | ||
| getattr(layer, "cache_quant_type_str", "none"), | ||
| ) | ||
|
|
||
| # Prefill branch: k is not None | ||
|
|
@@ -947,11 +947,13 @@ def forward_mixed( | |
| @staticmethod | ||
| def mla_blackwell(decoder_q, latent_cache, block_table, cache_seqlens, attn_softmax_scale): | ||
|
|
||
| # decoder_q = decoder_q.cast(paddle.float8_e4m3fn) | ||
| # latent_cache = latent_cache.cast(paddle.float8_e4m3fn) | ||
| assert latent_cache.dtype in [paddle.bfloat16, paddle.uint8], latent_cache.dtype | ||
| use_fp8_cache_kv = latent_cache.dtype == paddle.uint8 | ||
| if use_fp8_cache_kv: | ||
| decoder_q = decoder_q.cast(paddle.float8_e4m3fn) | ||
| latent_cache = latent_cache.view(paddle.float8_e4m3fn) | ||
|
|
||
| assert decoder_q.dtype == latent_cache.dtype | ||
| q_dtype = decoder_q.dtype | ||
|
|
||
| page_size = latent_cache.shape[2] | ||
| q_num_heads = decoder_q.shape[2] | ||
|
|
@@ -998,11 +1000,16 @@ def mla_blackwell(decoder_q, latent_cache, block_table, cache_seqlens, attn_soft | |
| softmax_scale = attn_softmax_scale | ||
| output_scale = 1.0 | ||
|
|
||
| from mla_decode_fp16 import BlackwellMultiHeadLatentAttentionForwardFP16 | ||
|
|
||
| # from mla_decode_fp8 import BlackwellMultiHeadLatentAttentionForwardFP8 | ||
| if use_fp8_cache_kv: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 建议 当前逻辑: 建议修复:使用独立变量或 dict 分别缓存两个 kernel 的编译结果: global compiled_mla_fp8, compiled_mla_fp16
if use_fp8_cache_kv:
if compiled_mla_fp8 is None:
compiled_mla_fp8 = cute.compile(mla, ...)
compiled_mla_fp8(...)
else:
if compiled_mla_fp16 is None:
compiled_mla_fp16 = cute.compile(mla, ...)
compiled_mla_fp16(...) |
||
| from mla_decode_fp8 import ( | ||
| BlackwellMultiHeadLatentAttentionForwardFP8 as kernel, | ||
| ) | ||
| else: | ||
| from mla_decode_fp16 import ( | ||
| BlackwellMultiHeadLatentAttentionForwardFP16 as kernel, | ||
| ) | ||
|
|
||
| mla = BlackwellMultiHeadLatentAttentionForwardFP16( | ||
| mla = kernel( | ||
| cutlass.Float32, | ||
| cutlass.Float32, | ||
| mma_qk_tiler_mn=(128, 128), | ||
|
|
@@ -1057,7 +1064,7 @@ def mla_blackwell(decoder_q, latent_cache, block_table, cache_seqlens, attn_soft | |
| stream, | ||
| ) | ||
|
|
||
| if q_dtype == paddle.float8_e4m3fn: | ||
| if use_fp8_cache_kv: | ||
| paddle_output = paddle_output.cast("bfloat16") | ||
| return paddle_output | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ 疑问 FP8 量化未使用 scale 因子
当前实现等价于
scale=1.0的静态量化(仅 clamp 至 FP8 e4m3 的 ±448 范围)。若 MLA KV cache 的激活值实际分布在较小量级(如 ±10),FP8 e4m3 在该范围内只有约 4 个指数级别,精度损失可能不可忽略。请确认: