[model, peft] feat: GLM-5 DSA option: tilelang (2/2) - #17
Merged
Conversation
This was referenced Jul 2, 2026
yushengsu-thu
force-pushed
the
glm5-dsa-2-glm-native
branch
from
July 5, 2026 21:53
4c86b56 to
a73fb7d
Compare
yushengsu-thu
force-pushed
the
glm5-dsa-2-glm-native
branch
from
July 6, 2026 21:26
a73fb7d to
d71fad8
Compare
…ption: megatron (1/2) Restructure models/glm_moe_dsa/ -> models/glm5/ (HF-facing identity unchanged: model_type=glm_moe_dsa, GlmMoeDsaForCausalLM; deprecated import shim kept) and enable GLM-5.x DSA training via the portable unfused megatron-core kernels, selected by the GLM5ModelProvider dataclass field dsa_attention_backend="megatron" (the default; the fused "tilelang" backend lands in part 2/2 under models/glm5/tilelang/). - old-core feature detection: back-fill the "dsa" spec + metainfo on megatron-core versions whose dispatcher does not know the variant - fix DSA-indexer rope-half layout on HF<->Megatron conversion (_IndexerRopeHalfSwapMapping, self-inverse on export, with layout guard) - fix GLM-5.2 MLA rope dims vs transformers>=5.12 mis-parse (re-read qk_rope_head_dim / rotary_base straight from config.json) - GLM-5.2 cross-layer DSA index sharing (CrossLayerDSAttention): anchor layers publish top-k, skip layers drop their indexer and reuse it; build-time PP-stage assert + forward-time bshd+recompute rejection - peft: default share_expert_adapters=False -- per-expert is the default grouped-expert LoRA layout; shared-outer is opted into solely via the experts_shared_outer_loras field Signed-off-by: Yusheng Su <yushengsu.thu@gmail.com>
…e-MLA backend (2/2) Add the second DSA kernel backend, dsa_attention_backend="tilelang": vendored fused TileLang kernels (SparseMLA + lighting_indexer fwd/bwd under models/glm5/tilelang/) driven by TileLangMLASelfAttention, which replicates slime's absorbed-latent q/kv + apex fuse_rope numerics so the train-side attention output matches the sglang rollout, including R3 indexer replay. Requires the optional tilelang dependency (lazy import; the default megatron path stays dependency-free) and the thd packed layout; training/forward-only (asserts inference_context is None). - SP/CP token-dim reconciliation in _tilelang_index_qkw/_tilelang_topk (SP all-gather of index_q/index_k/head_weights; CP ops no-op at CP=1) - fused indexer: clamp top-k to seq_len for short sequences, pad selection back to the fixed topk width with the -1 sentinel - LoRA: fold the kv_b_proj adapter delta into the absorb weight via torch.distributed.nn.functional.all_gather (autograd-aware) so LoRA-A receives gradients at TP>1 - dispatch: CrossLayerDSAttention reads the backend field; the block-spec builders point the MLA module at TileLangMLASelfAttention (delegates to the base class on the default backend -> byte-identical) Stacked on top of "DSA option: megatron (1/2)"; the two commits together reproduce the bridge-dev-glm-merging tree exactly. Signed-off-by: Yusheng Su <yushengsu.thu@gmail.com>
yushengsu-thu
force-pushed
the
glm5-dsa-2-glm-native
branch
from
July 6, 2026 23:20
d71fad8 to
dd9dad3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Part 2/2 of splitting #15 (part 1/2 = #16: the
megatronunfused backend + GLM-5.x enablement). Stacked on #16 — this branch contains part 1's commit, so the diff shows both until part 1 merges; review the[model, peft] feat: ... tilelang ... (2/2)commit only. The two parts together reproduce thebridge-dev-glm-mergingmodel tree byte-for-byte (verified:git diffvs its head is empty forsrc/).This part adds the second DSA sparse-MLA kernel backend:
dsa_attention_backend="tilelang"— vendored fused TileLang kernels (SparseMLA+lighting_indexerfwd/bwd undermodels/glm5/tilelang/) driven byTileLangMLASelfAttention(tilelang/tilelang_mla.py), which replicates slime's absorbed-latent q/kv + apexfuse_ropenumerics so the train-side attention output matches the sglang rollout, including R3 indexer replay. Requires the optionaltilelangdependency (imported lazily — the defaultmegatronpath stays dependency-free) and the thd packed layout (--qkv-format thd). Training/forward-only: assertsinference_context is None(no KV cache); rollout is always served by sglang.Bug fixes (fused-backend specific)
"CuSeqLenKS shape[0] expected 512, got 128":_tilelang_index_qkw/_tilelang_topkomitted the SP/CP token-dim reconciliation native slime performs → added SP all-gather ofindex_q/index_k/head_weights, CP all-gather ofindex_k, and CP-scatter of the per-query starts/ends (CP ops are no-ops at CP=1).torch.topk(logits, index_topk=2048)raises "selected index k out of range" when the packed sequence is shorter thanindex_topk→_original_topk(tilelang/indexer.py) caps k atlogits.shape[-1]and pads the selection back to the fixedtopkwidth with the-1sentinel, preserving the shape SparseMLA and R3 replay expect.TileLangMLASelfAttention._kv_up_proj_weight_and_normfolded the kv_b_proj LoRA delta using a non-differentiabletorch.distributed.all_gatheronlinear_in(LoRA-A), detaching it from the autograd graph, so LoRA-B trained while LoRA-A never received gradients → usetorch.distributed.nn.functional.all_gather(autograd-aware; reduce-scatter backward, correct because the gathered A participates in every TP rank's local delta).New features
TileLangMLASelfAttention.forward, which intercepts at the MLA level (the fused kernel consumes the absorbed-latent q/kv, which only exist upstream of core attention); the block-spec builders (glm5_bridge._build_glm5_dsa_block_spec,cross_layer_dsa_dispatch.get_glm5_crosslayer_dsa_spec) point the MLA module atTileLangMLASelfAttention— on the defaultmegatronbackend its forward delegates to the base class → byte-identical.CrossLayerDSAttentionalso reads the flag so GLM-5.1/5.2 forwards share one dispatch point.lighting_indexerroutes its top-k through miles'indexer_replay_manager.get_topk_fnwhen present + enabled, andTileLangMLASelfAttention.__init__registers the per-layer replay stream (megatron-core'sDSAIndexeronly self-registers in DeepSeek-V4 mode). No-op unless--use-indexer-replayenabled the manager; import-guarded so the package works without miles.lighting_indexerreturns only discrete top-k, no indexer loss → no gradient); the miles launcher excludes the indexer from LoRA targets by default.Validation
dist.all_gatherthe kv_uplora_Astays frozen at TP>1 (5-angle verification + CPU micro-test:linear_in.gradNone → nonzero with the fix).Notes for reviewers
context_parallel_size==1for the"dsa"variant); the CP branches here are no-op-at-CP=1 forward-compat mirroring native slime.CrossLayerDSAttention._sparse_attentioncurrently invokes the unfused kernel on both branches (leftover Step-4 TODO; the fused SparseMLA is never called from there — the real fused dispatch is at the MLA level).bridge-dev-glm-mergingis kept (not deleted) as the integration branch.🤖 Generated with Claude Code
https://claude.ai/code/session_01HFoyYjC55SRsNRXUzWMWKH