You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
dev branch megatron provides support for deepseek sparse attention (DSA) (ref: NVIDIA/Megatron-LM#2154) with torch native attention (sbhd format, without CP). Our implementation starts from this version and extends with CP and thd conversion.
better DSA support (support CP and thd format):
Implement CP in DSA
- Index compute: because k only has one head, use one single all-gather on k and compute the entire index of size [q_local, k_global] in one single GEMM. (location: megatron.patch:289-316)
- core attention: For all TE backends, indexer style attention (namely, supporting arbitrary attention bias) + thd format is not supported. For quick iteration, we use the eagle attention with CP in Megatron dev branch (ref: [Community][Dev] feat(moe): Adding context parallel support to eager attention implementation NVIDIA/Megatron-LM#1859), with some minor modifications for DSA. (location: megatron.patch:322-408)
- index loss compute: In this part, we need to re-compute the attention score (softmax((q@k)*scale), and sum up on n_head dimension. We implement the CP logic with an all-gather backend, parallel on the n_heads dimension. We use double buffers for k and slice along n_heads dimension, and sum each head up. (Code index: location:120-283)
Support THD format with ad hoc converter
Currently, Megatron's DSA implementation does not support thd format, which is required in Miles. As a quick solution, we implemented a converter, which gathers activation from zigzag-ed thd format and reshards it to zigzag-ed bsnd format on each rank, and reversely for attention output. (Code index: megatron.patch:415-526)
Miles side
Support Deepseek-v32 in miles
(Node: I use mbridge rather than megatron-bridge because DeepSeek-v3.2 is not supported by transformers..)
Added transformers patch, since DeepSeek-v3.2 is not officially supported in transformers
install fast-hadamard-transform
update transformer_engine version
location: docker/deepseekv32
Fix for Megatron version update
return num_tokens as torch.tensor(1) rather than int(1)
Newer Megatron require receiving num_tokens as a torch tensor for later processing
location: miles/backends/megatron_utils/loss.py
add explicit argument name for get_gpt_layer_with_transformer_engine_spec because of API change
location: miles/backends/megatron_utils/model_provider.py
Fix quantization scale tensor syncing for MoE with fused backend
Need to send float32 quant scale tensor when fused backend is applied (when deepep is not enabled, will fall back to fused backend)
location: miles/backends/megatron_utils/megatron_to_hf/processors/quantizer.py
IIUC, the current implementation is using Megatron's unfused_dsa which is essentially a full attention kernel with a large "sparse" [bs, sq, skv] mask. This is inefficient from a computational and memory perspective and essentially defeats the purpose of the sparse formulation.
Currently, FlashMLA currently only has fwd prefill for sparse attention for sm_{90,100}. Are you looking into implementing a fused bwd pass?
IIUC, the current implementation is using Megatron's unfused_dsa which is essentially a full attention kernel with a large "sparse" [bs, sq, skv] mask. This is inefficient from a computational and memory perspective and essentially defeats the purpose of the sparse formulation.
Currently, FlashMLA currently only has fwd prefill for sparse attention for sm_{90,100}. Are you looking into implementing a fused bwd pass?
Hi @jeromeku , in the current PR, we're using tilelang's fused implementation instead. It should be much faster than the torch native one. Because in most time the training part is not the bottleneck in RL, the tilelang kernel should already be good.
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
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.
Major changes
Megatron side
Update Megatron version to
devbranchdevbranch megatron provides support for deepseek sparse attention (DSA) (ref: NVIDIA/Megatron-LM#2154) with torch native attention (sbhdformat, without CP). Our implementation starts from this version and extends with CP andthdconversion.better DSA support (support CP and
thdformat):Implement CP in DSA
- Index compute: because
konly has one head, use one singleall-gatheronkand compute the entire index of size[q_local, k_global]in one single GEMM. (location:megatron.patch:289-316)- core attention: For all TE backends, indexer style attention (namely, supporting arbitrary attention bias) + thd format is not supported. For quick iteration, we use the eagle attention with CP in Megatron
devbranch (ref: [Community][Dev] feat(moe): Adding context parallel support to eager attention implementation NVIDIA/Megatron-LM#1859), with some minor modifications for DSA. (location:megatron.patch:322-408)- index loss compute: In this part, we need to re-compute the attention score (softmax((q@k)*scale), and sum up on
n_headdimension. We implement the CP logic with an all-gather backend, parallel on then_headsdimension. We use double buffers forkand slice alongn_headsdimension, and sum each head up. (Code index:location:120-283)Support THD format with ad hoc converter
Currently, Megatron's DSA implementation does not support
thdformat, which is required in Miles. As a quick solution, we implemented a converter, which gathers activation from zigzag-edthdformat and reshards it to zigzag-edbsndformat on each rank, and reversely for attention output. (Code index:megatron.patch:415-526)Miles side
Support Deepseek-v32 in miles
(Node: I use
mbridgerather thanmegatron-bridgebecause DeepSeek-v3.2 is not supported bytransformers..)location:
miles_plugins/mbridge/deepseekv32.py,miles_plugins/mbridge/__init__.pylocation:
miles/backends/megatron_utils/megatron_to_hf/deepseekv32.py,miles/backends/megatron_utils/megatron_to_hf/__init__.pyNew Docker for deepseek-v32
location:
docker/deepseekv32Fix for Megatron version update
num_tokensastorch.tensor(1)rather thanint(1)Newer Megatron require receiving num_tokens as a torch tensor for later processing
location:
miles/backends/megatron_utils/loss.pyget_gpt_layer_with_transformer_engine_specbecause of API changelocation:
miles/backends/megatron_utils/model_provider.pyFix quantization scale tensor syncing for MoE with fused backend
Need to send
float32quant scale tensor when fused backend is applied (when deepep is not enabled, will fall back to fused backend)location:
miles/backends/megatron_utils/megatron_to_hf/processors/quantizer.pychat_template hacking for DeepSeek-v32
DeepSeek-v32 cannot load chat_template automatically. So we need to do some hacking here. (Reference: https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/entrypoints/openai/serving_chat.py#L288)
location:
miles/utils/data.pytraining script for debugging
location:
scripts/run_deepseek_v3.2_5layer.pyTest result
sbhdformat, CP=1 (2)thdformat, CP=1, (3)thdformat, CP=2successful run with debugging mode (reduce model layers to 5), with TP = 8, CP = 8, seq_len = 8192, batch = 256.