support ring fp4 comm and sla sparse#933
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for FP4 communication in Ring Attention and adds a flag for SLA sparse attention. The changes in ring_attn.py correctly add the FP4 communication path alongside the existing FP8 path. The modifications in sage_attn.py and scheduler.py are minor and appropriate. My main feedback is on refactoring a section of duplicated code in ring_attn.py to improve maintainability. Overall, the changes are well-structured to support the new features.
| assert not (use_fp8_comm and use_fp4_comm), "use_fp8_comm and use_fp4_comm can't be enabled at the same time." | ||
| B, H, N, D2 = next_tensor_quant.shape | ||
| D = D2 * 2 | ||
| D16 = D // 16 | ||
| if use_kv_fusion and is_kv_fusion: | ||
| # KV 融合模式 | ||
| return dequant_fp8_vllm(next_tensor_fp8, next_tensor_scale, original_dtype) | ||
| if use_fp8_comm: | ||
| return dequant_fp8_vllm(next_tensor_quant, next_tensor_scale, original_dtype) | ||
| else: | ||
| return dequant_fp4_sage3(next_tensor_quant.reshape(1, 1, -1, D2), next_tensor_scale.reshape(1, 1, -1, D16)).reshape(B, H, N, D) | ||
| elif not use_kv_fusion: | ||
| # 分离模式 | ||
| k = dequant_fp8_vllm(next_tensor_fp8, next_tensor_scale, original_dtype) | ||
| v = dequant_fp8_vllm(v_fp8, v_scale, original_dtype) | ||
| return k, v | ||
| if use_fp8_comm: | ||
| k = dequant_fp8_vllm(next_tensor_quant, next_tensor_scale, original_dtype) | ||
| v = dequant_fp8_vllm(v_quant, v_scale, original_dtype) | ||
| return k, v | ||
| else: | ||
| k = dequant_fp4_sage3(next_tensor_quant.reshape(1, 1, -1, D2), next_tensor_scale.reshape(1, 1, -1, D16)).reshape(B, H, N, D) | ||
| v = dequant_fp4_sage3(v_quant.reshape(1, 1, -1, D2), v_scale.reshape(1, 1, -1, D16)).reshape(B, H, N, D) | ||
| return k, v | ||
| else: | ||
| # 默认返回单个张量 | ||
| return dequant_fp8_vllm(next_tensor_fp8, next_tensor_scale, original_dtype) | ||
| if use_fp8_comm: | ||
| return dequant_fp8_vllm(next_tensor_quant, next_tensor_scale, original_dtype) | ||
| else: | ||
| return dequant_fp4_sage3(next_tensor_quant.reshape(1, 1, -1, D2), next_tensor_scale.reshape(1, 1, -1, D16)).reshape(B, H, N, D) |
There was a problem hiding this comment.
The function _dequantize_received contains duplicated logic for handling FP8 and FP4 dequantization within its conditional branches (if use_kv_fusion and is_kv_fusion, elif not use_kv_fusion, and else). This makes the code harder to read and maintain.
You can refactor this by extracting the dequantization logic into a nested helper function. This will remove redundancy and make the parent function's logic clearer.
assert not (use_fp8_comm and use_fp4_comm), "use_fp8_comm and use_fp4_comm can't be enabled at the same time."
def _dequant(tensor_quant, scale):
if use_fp8_comm:
return dequant_fp8_vllm(tensor_quant, scale, original_dtype)
B, H, N, D2 = tensor_quant.shape
D = D2 * 2
D16 = D // 16
return dequant_fp4_sage3(tensor_quant.reshape(1, 1, -1, D2), scale.reshape(1, 1, -1, D16)).reshape(B, H, N, D)
if use_kv_fusion and is_kv_fusion:
# KV 融合模式
return _dequant(next_tensor_quant, next_tensor_scale)
elif not use_kv_fusion:
# 分离模式
k = _dequant(next_tensor_quant, next_tensor_scale)
v = _dequant(v_quant, v_scale)
return k, v
else:
# 默认返回单个张量
return _dequant(next_tensor_quant, next_tensor_scale)
No description provided.