Skip to content

feat(sp): refactor ulysses - #352

Merged
tastelikefeet merged 3 commits into
modelscope:dev-swift-v5from
werwrewe:sp-slim-20261029
Oct 29, 2026
Merged

tastelikefeet merged 3 commits into
modelscope:dev-swift-v5from
werwrewe:sp-slim-20261029

Conversation

@werwrewe

Copy link
Copy Markdown
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

隔离每个模型的 SP 上下文

  • 每个 SequenceParallelStrategy 持有自己的 SequenceParallel 上下文,保存该模型的通信组、rank 和当前 batch 状态。
  • Transformers 的 attention/mask 全局包装器安装一次,在调用时按 attention 模块或 forward 上下文栈找到当前模型。
  • GatherLoss 显式保存前向所属的上下文,反向传播不再依赖可能已被其他模型覆盖的全局单例。
  • router replay 和 Qwen3.5 线性注意力路径使用模型对应的上下文。

从Processor当中分离SP strategy

Processor 接收 gather_fn、reduce_fn 两个普通通信回调,不接收或调用 SP strategy。

bug fix

  • FlashAttention 构建序列边界时排除 SP 补齐的 position_ids=-1 干扰,保留真实 packed 边界;
  • Qwen3.5 GDN 恢复全局 padding mask 后按 rank 切分,避免将末尾 mask 错用于前面的分片。

Experiment results

测试环境

硬件/软件:Ascend 910B4;CANN 9.1.0、驱动 25.5.1、Python 3.11.15、PyTorch 2.10.0+cpu / torch_npu 2.10.0、Transformers 5.13.1、base code commit id:f11bd8e

dpo支持实验:
dpo_comparison
transformers后端回归实验(图1 loss不对齐是base代码中GDN的bug引发,已经修复)
patched_transformers_overview
megatron后端回归实验
megatron_text_overview

while isinstance(getattr(root_model, 'module', None), torch.nn.Module):
root_model = root_model.module
llm_model = get_llm_model(root_model)
if not (getattr(llm_model, '_supports_attention_backend', False) or self._is_qwen35_model(llm_model)):

@tastelikefeet tastelikefeet Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_supports_attention_backend 这个兼容性如何
_is_qwen35_model 是否生效面太窄,或者说,太过定制化

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_supports_attention_backend 是transformers库内函数,主流模型都支持。_is_qwen35_model 是因为目前linear attn支持sp的补丁只有Qwen3_5GatedDeltaNetUlyssesPatch,所以专门给qwen35开个口子。

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_supports_attention_backend 对旧版本 transformers 的兼容性呢

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最低支持这个字段的transformers 版本是4.49

Comment thread docs/source_zh/组件/训练中间件/序列并行.md Outdated
@tastelikefeet
tastelikefeet merged commit 88c1d48 into modelscope:dev-swift-v5 Oct 29, 2026
0 of 5 checks passed
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.

2 participants