Skip to content

[v5] 减少 FSDP2 累积与 vLLM 权重发布开销 - #390

Closed
qychen2001 wants to merge 5 commits into
dev-swift-v5from
perf/1117
Closed

qychen2001 wants to merge 5 commits into
dev-swift-v5from
perf/1117

Conversation

@qychen2001

@qychen2001 qychen2001 commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

修改动机与实现

减少 FSDP2 训练与 vLLM 权重发布中的同步、复制和内存清理开销,并改善各 rank 的 micro-batch 执行顺序。

  • 连接配置归属后端:移除公共 worker 的单连接限制,由 Megatron 自行设置连接数,让 FSDP2 保留通信与计算重叠的空间。
  • 延迟梯度同步:为 FSDP2 补齐梯度累积时的 no-sync 行为,退出上下文时恢复各 FSDP unit 的状态。
  • 对齐执行顺序:前向和训练共用按估算成本降序排列的 micro-batch 顺序,保持批次成员与返回顺序不变。
  • 复用权重传输 buffer:共享 TP 的非量化 base 权重直接借用 IPC bucket,加载及同步完成后再 ACK,减少中间复制和重复 GC/empty_cache。

配套:ms-swift #10586。

实验结果

Swift + Twinkle 联合优化后与 veRL 对比,各运行 5 步。Qwen3.5-4B,8 × PPU-ZW810:4 卡 FSDP2 训练 + 4 卡 rollout(TP2 × 2),每步 64 条样本,micro-batch/GPU=1,GA=16,最大生成长度 4096。

指标 Swift veRL
完整 5 步吞吐(response tokens/s/卡) 216.05 227.97
第 2–4 步吞吐(response tokens/s/卡) 394.64 384.01
old-logprob 平均耗时 4.882 s 5.766 s
训练更新平均耗时 15.880 s 15.413 s
权重同步平均耗时 1.064 s 0.579 s

吞吐按实际消费的 response tokens / iteration 墙钟时间 / 8 计算。阶段耗时取第 2–4 步,避开首步预热与末步发布差异;veRL 的 old-logprob 同时计算 entropy。

AI assistance: OpenAI Codex.

qychen2001 and others added 5 commits November 17, 2026 17:23
Let FSDP workers retain the platform connection default. Set Megatron connection limits before backend imports can create a device context.

Co-authored-by: OpenAI Codex <noreply@openai.com>
Use the FSDP2 gradient synchronization flags when no_sync is unavailable, preserving each sharded unit state across nested contexts.

Co-authored-by: OpenAI Codex <noreply@openai.com>
Schedule longer causal-LM micro-batches first on distributed workers while retaining batch membership and output ordering. Check gradients, final-only synchronization and response alignment.

Co-authored-by: OpenAI Codex <noreply@openai.com>
Reuse device bucket views for base unquantized weights until loading and TP barriers finish. Avoid heap collection and cache flushing on the reusable device-buffer path.

Co-authored-by: OpenAI Codex <noreply@openai.com>
Provide the existing rollout feature hook and assert loss sum/count semantics in micro-batch regression fixtures.

Co-authored-by: OpenAI Codex <noreply@openai.com>
@qychen2001
qychen2001 marked this pull request as ready for review November 17, 2026 09:42
tastelikefeet added a commit that referenced this pull request Nov 17, 2026
_no_sync_context now routes FSDPModule models (no no_sync method) to a
flag-based context instead of nullcontext, cutting per-micro-batch
reduces to one per accumulation window. Save/restore walks
state._fsdp_param_groups (the same list torch's setter walks; the
singular property returns None pre-init and raises on multi-group HSDP).

Design adapted from #390.
tastelikefeet added a commit that referenced this pull request Nov 17, 2026
Extend borrow_bucket eligibility beyond independent receivers: every
shared-TP rank loads views, synchronizes, and passes the TP barrier
before the driver acks, so the sender still cannot overwrite early.
Skip the receiver-side gc.collect/empty_cache and the sender-side heap
collection on the reusable device-buffer path.

Design adapted from #390.
tastelikefeet added a commit that referenced this pull request Nov 17, 2026
Drop the worker-wide CUDA_DEVICE_MAX_CONNECTIONS='1' pin from
_torch_setup so FSDP2 workers keep the platform default (a single
connection restricts communication/compute overlap). The Megatron
models now set it in __init__ before any backend import or CUDA-context
creation: '32' for sharded (FSDP) wrappers, setdefault '1' otherwise to
depress megatron warnings.

Design adapted from #390.
@qychen2001 qychen2001 closed this Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant