Open
[RFC] DeepSeek-V4/V4.1长序列切分支持及后续上游回合计划 #175
weijinyiAdd on Oct 24
Oct 24 Added label: RFC
Oct 24 Assign lrwei0709 as responsible person
24 days ago changed custom category from 任务 to 需求
hanboyou
8 days ago Comments:
8 days ago Comments:
@lrwei0709,PR #1342 已合入 dev_0.4.0,覆盖部分逐文档上下文并行工作。建议补齐关联实现和复测计划,特别是 1M 长序列范围及多卡验证。请确认当前覆盖范围,并回复剩余工作、下一步和预计更新时间。


DeepSeek-V4 / V4.1 长序列切分整体设计
1. 设计目标
Attention.forward对 CP 无感;2. 总体架构
CP = 1:plan 为
None,exchange inactive,base 组件原样运行。3. 静态 H/T 布局
对
Ntoken、CP = R、C = N/R,rankr的 query 帧:per_doc_alignment仍在 config 侧;build_attention_metadata从cu_seqlens/cp_size/rank纯本地推导 permutation、H/T 分段和seqused;CPInputLayout在模型入口应用一次(x / positions / labels),不是每层;seqused:(r+1)·Q_d(R-r)·Q_d4. canonical KV 与 seqused
swa_k、cmp_k、idx_k全部保持 canonical;seqused/ query 段不同;swa_kall-gather 保留为 fallback,未来由WindowExchange只发送窗口并集。5. 三个 CP-aware 组件
CompressorCPCompressorSlabExchange(恢复 slab 后算 cmp_k)CompressedSparseInnerAttentionCPInnerAttentionWindowExchange(只送窗口并集)LightningIndexerCPLightningIndexerKExchange(恢复 idx_k 的 slab 依赖)Attention(外层)class Exchange(Module): def parallelize(self, parallel_dims): self.group = ... @property def active(self): return self.group is not None and self.group.size() > 1SlabExchange.restore(x_ht, positions_ht, plan) -> SlabStreamKExchange.restore(...)(可共享 slab 流)WindowExchange.gather(swa_k_ht, window_plan) -> WindowStream_functional_collectives.all_to_all_single(autograd-aware,反向是反向交换);WindowExchange发送的是每个 query chunk 的窗口并集[win_start, q0),不是每个 query 各 128 个 key。6. Config transform
用
ModelConfigTransform做组件替换,不在运行时 patch module:Attention.forward保持 canonical。7. 逐层数据流(CP > 1)
CP = 1:
restoreidentity、seqused全行、无 transform。8. Head/tail
seqused不同;seqused/ 输入布局,不改 attention 代码;9. 1M 通信 / 显存
N = 1M、CP = 128、C = 8192、D = 4096、head_dim = 512、bf16:swa_kcmp_k(ratio 4)相对整行
swa_k的比例:8/R(R = 128 → 0.0625);16/R;80/R。10. 设计约束
11. 与上游两个 PR 的联系
11.1 #5607
Put varlen windows in metadatawindow_size从VarlenInnerAttention.Config移到VarlenAttentionMetadata,新增SlidingWindowVarlenInnerAttention;Decoder拒绝同一 backend 内不同 layer 使用不同 window。seqused、slab/window/k plans 都由build_attention_metadata产出,base 组件只消费 metadata,不在 config 里分叉。compress_ratio是每层几何(1 / 4 / 128),metadata 需要按几何区分;#5607 的方向是禁止同一 backend 内 window 不一致。V4 需要把“几何(ratio)”和“window”分开:ratio 是 metadata 的几何键,window 仍是统一参数,不能把 ratio 当成 per-layer window。11.2 #5071
[CP] Add graph-safe varlen K/V all-gatherCPInnerAttention.prepare_cp_metadata、AllGatherKVVarlen、_chunk_metadata的 bounded K/V view;支持 contiguous 与 head-tail;GPU 测试断言 bitwise。CPCompressor/CPInnerAttention/CPLightningIndexer就是 V4 版的inner_attention_map,由ModelConfigTransform安装,对应上游的ContextParallelTransform。seqused,1M 下不重排 KV。WindowExchange是对 #5071 全量 K/V gather 的滑窗优化:只送窗口并集。11.3 一句话总结
seqused;滑窗用WindowExchange只送窗口并集