feat: hybrid CP for Qwen3.5 MoE (DeltaNet + attention) - #2080
Merged
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| substitute_ring_attn(cp_group, heads_k_stride=1, attn_impl=config.model.attn) | ||
| from prime_rl.utils.cp import setup_hybrid_cp | ||
|
|
||
| setup_hybrid_cp(model, cp_group, cp_rank, parallel_dims.cp) |
There was a problem hiding this comment.
setup_hybrid_cp uses model before it's defined in SFT
High Severity
setup_hybrid_cp(model, ...) is called at line 108 inside the if parallel_dims.cp_enabled block, but model is not created until line 124 via setup_model(...). This will raise a NameError at runtime whenever context parallelism is enabled for SFT training. The equivalent code in rl/train.py correctly places setup_hybrid_cp after model creation.
Additional Locations (1)
Integrate fla (flash-linear-attention) library for native context parallelism on GatedDeltaNet layers. Standard attention layers use ring attention for CP, while DeltaNet layers use fla's state-passing CP which communicates only the compact recurrent state (O(d²) per layer) instead of all-gathering full sequences (O(N×d)). - Add flash-linear-attention dependency (Triton kernels + CP support) - Wire up fla's build_cp_context/cp_context in GatedDeltaNet forward - Add setup_hybrid_cp to configure DeltaNet modules for CP - Add CP gather/scatter autograd primitives as fallback in cp.py - Fix VLM num_hidden_layers access for debug.num_layers config - Add test configs for Qwen3.5-35B-A3B CP testing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move the VLM bfloat16 validation from ModelConfig (which only had the model name) to get_model() where we have the actual loaded HF config and can reliably detect VLMs via is_vlm_config(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Apr 3, 2026
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
Adds context parallelism (CP) support for Qwen3.5 hybrid models that mix GatedDeltaNet (linear attention) with standard softmax attention.
The challenge: CP works via ring attention for standard attention layers, but DeltaNet is recurrent — it maintains a compact state matrix
S ∈ R^{K×V}that's updated sequentially. Ring attention doesn't apply.Approach
We use different CP strategies per layer type:
This follows the LASP-2H approach from the literature (paper) and uses fla's merged PR #691 which implements CP for
chunk_gated_delta_rule.Communication cost per DeltaNet layer
O(N × d)— e.g. 256 MB at seq_len=32K, d=4096O(H × K × (K+V))— e.g. 4 MB for 64 heads × 128 × 256~64x reduction in communication volume for DeltaNet layers.
Changes
flash-linear-attentionadded as dependency — provides Triton kernels for GatedDeltaNet with CP supportmodeling_qwen3_5_moe.py:Qwen3_5MoeGatedDeltaNetnow buildsFLACPContextand passes it to fla'schunk_gated_delta_rulewhen CP is enabledcp.py:setup_hybrid_cp()configures DeltaNet modules with CP group/rank info. Also addscp_gather/cp_scatterautograd primitives as general-purpose fallback utilitiesrl/train.pyandsft/train.py: callsetup_hybrid_cp()after ring attention setup when CP is enabledmodel.py: fixdebug.num_layersfor VLM configs (nestedtext_config)rl_cp_test.toml(Qwen3.5-35B-A3B, CP=2, 1 node, fake data) for quick validationTest plan
🤖 Generated with Claude Code
Note
Medium Risk
Touches distributed context-parallel training paths and introduces a new CUDA/Triton dependency (
flash-linear-attention), which could impact correctness/performance across CP setups and hardware environments.Overview
Enables hybrid context parallelism for Qwen3.5 MoE hybrid models by using ring-attention CP for standard attention layers while using
flash-linear-attention’s native CP/state-passing forGatedDeltaNet(linear attention) layers.This wires CP group/rank info into DeltaNet modules via new
setup_hybrid_cp()(called from bothrl/train.pyandsft/train.py) and updates the DeltaNet fast path to build/pass anFLACPContextintochunk_gated_delta_rulewhen available, falling back to the existing PyTorch kernel otherwise.Also tightens VLM training constraints (requires bf16 optimization/reduce dtypes) and fixes
debug.num_layershandling for VLM configs by applying it to nestedtext_config.Written by Cursor Bugbot for commit fe4bdb5. This will update automatically on new commits. Configure here.