modelscope / modelscope/ms-swift

Support documented FSDP2 + sequence/context parallel (SP/CP) combo for long-context QLoRA; combining flags on 2 GPUs hits CUDA illegal memory access

Open
#9,895 1 comment 0 reactions 1 assignee View on GitHub

@tastelikefeet is already working on this.

Since Aug 12, 2026.

bug
Dominant language
Python
Stars
15.7k
Forks
1.7k
Avg merge
1d 16h
Merged PRs (30d)
136

Description

Checklist / 检查清单
  • I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。
Bug Description / Bug 描述

背景 / Motivation
长上下文 QLoRA(如 32k)时,仅 FSDP 只能切分权重,每卡仍跑满序列,激活/cross_entropy 容易 OOM;仅 SP/CP 切序列可以顶住,但缺少权重分片。Axolotl
已有一等配方 FSDP2 + context_parallel_size(例如 4 卡上 CP=2 × dp_shard=2)。ms-swift 分别提供:

• FSDP / FSDP2(权重分片)
• --sequence_parallel_size(Ulysses + Ring,文档中的 SP)

但没有官方文档/示例将两者作为推荐组合;仓库脚本与示例多为「二选一」。我们希望 ms-swift 明确支持(或明确拒绝并给出安全替代)FSDP(+FSDP2) + SP/CP 的 2D
并行,并保证 CP/SP × FSDP shard 与 world_size 一致(例如 4 GPU → 2×2)。

实际行为 / What happens
在 2× GPU 上同时启用 accelerate FSDP(QLoRA / bitsandbytes 4bit)与 --sequence_parallel_size=2 时,模型可完成加载并进入
training_step,但在首步前向/loss 路径出现:

torch.AcceleratorError: CUDA error: an illegal memory access was encountered

堆栈大致落在 Trainer training_step → compute_loss / template SFT loss(与 ring/SP + FSDP 同进程组叠用相关)。这与「纯 FSDP @ 32k → CUDA OOM」或「纯 SP
可跑」都不同,更像未文档化组合下的错误/未定义行为,而非单纯显存不足。

对比:同机同模型用 Axolotl FSDP2 + CP,在 mesh 维度与 world_size 对齐(如 4 卡 2×2)时可正常建并行布局;若误设 CP×dp_shard > world_size
会在启动期明确报错,而不是 silent 进 step 后 illegal access。

期望 / Expected

  1. 文档中给出官方支持的 FSDP2 + --sequence_parallel_size(或等价 CP) 配方(含 2/4/8 卡下 SP/CP 与 FSDP shard 如何取值,使乘积 = world_size)。
  2. 若不支持:在参数解析阶段 显式报错(类似 “FSDP and sequence_parallel are mutually exclusive”),避免进训练后 CUDA illegal access。
  3. 若支持:提供最小可复现示例(QLoRA + FA2 + 长 max_length),并说明与 DeepSpeed / device_map 的互斥关系。

环境摘要 / Environment (summary)
• ms-swift ~4.4.x,PyTorch 2.11 + CUDA 12.8,flash-attn 2.8.x
• 模型:Qwen2.5-72B 类 dense(Kimi-Dev-72B),QLoRA 4bit + LoRA on attn projs
• 2× GPU 时实验叠开 FSDP + sequence_parallel_size=2 → illegal memory access
• 纯 FSDP @ 32k → OOM;多卡纯 SP 长序列可顶住(与「需要切序列」一致

How to Reproduce / 如何复现
  1. 安装带 FA 的 GPU 环境:ms-swift、bitsandbytes、flash-attn、accelerate FSDP。
  2. 准备任一 Qwen2 dense 大模型 QLoRA 配置,max_length 设为较长(如 8192–32768),attn_impl=flash_attn。
  3. 双卡启动,同时打开:
    • accelerate distributed_type=FSDP(或 ms-swift FSDP2 路径)
    • --sequence_parallel_size 2(及项目所需的 padding_free / SP 相关项)
    • QLoRA:--quant_method bnb --quant_bits 4,--bnb_4bit_quant_storage bfloat16 等
  4. 观察:权重可加载,进入首个 training_step 后出现 CUDA error: an illegal memory access was encountered(非清晰的 OOM / 互斥参数错误)。
  5. 对照:同一设置去掉 FSDP、只保留 sequence_parallel_size=2(或去掉 SP、只保留 FSDP)时,失败模式变为可解释的 OOM 或可训练,而不是同一 illegal
    access。
Additional Information / 补充信息

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.