[RFC] FSDPTurbo as an opt-in Swift SFT backend for DeepSeek V4.1

Open
#10,614 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
8/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python, pytorch

Research direction

This is a design RFC for a new opt-in FSDPTurbo backend, so there is no single change to make yet. Read swift/dev/recipe/sft/assembly.py (build_supervised_objects) and swift/dev/config/distributed_config.py to see where a backend is selected, then swift/dev/recipe/sft/run_sft.py. Done means a maintainer confirms the extension point, and the issue does not yet define a patch to submit.

Written by the indexing model from the issue text.

Description

Checklist
  • I have searched existing issues, and this is a new feature request.
Feature Request Description

I propose an optional FSDPTurbo backend for long-context SFT on MoE models, starting with DeepSeek V4.1.

Prototype branches

Repository Minimal branch Scope
ms-swift fsdpturbo-minimal SFT entry point, model plans, and Swift integration
FSDPTurbo swift-compat-minimal Parallel runtime, model adapters, kernels, and sharded loading
1. Design and fit with Swift main

The prototype exposes swift fsdpturbo sft as a separate entry point alongside the existing HF and Megatron paths. It reuses SwiftSft for arguments, model metadata, processors, templates, and dataset preparation. A small adapter maps those inputs to FSDPTurbo, which owns the distributed meshes, parameter wrapping, optimizer steps, and sharded weight I/O. Swift-facing PEFT options and checkpoint metadata stay in the adapter. Swift SFT pipeline

flowchart TB
    subgraph SW[ms-swift]
        direction LR
        Config["SFT arguments and model registry"] --> Data["Processor, template, and dataset"]
        Config --> Adapter["FSDPTurbo SFT adapter"]
        Data -->|Encoded inputs and labels| Adapter
    end
    subgraph FT[FSDPTurbo]
        direction LR
        Shard["Model factory and distributed meshes"] --> Train["Forward, backward, optimizer"]
        Load["Sharded checkpoint I/O"] -->|Owner-local weights| Shard
        Train --> Ops["Eager operators and device kernels"]
    end
    Adapter -->|Config and model spec| Shard
    Adapter -->|Encoded batches| Train
    Train -.->|Metrics and checkpoint state| Adapter

The Swift boundary carries the model spec, training settings, and encoded batches. The model spec identifies the loader, the modules used for sharding and recomputation, and the supported parallel layouts. This lets the same entry point use a native V4.1 builder or a registered Hugging Face model.

2. Integration with dev-swift-v5

Start with local causal-LM SFT. Add fsdpturbo as an explicit backend in DistributedConfig and select it in build_supervised_objects. Reuse the v5 processor, template, and dataset builders. Translate parallel_spec, tuner, and checkpoint settings into a backend plan, then implement the step, evaluation, and checkpoint operations expected by run_supervised. Use a single launcher and verify encoded batches and one optimizer update across the legacy and v5 entry points. v5 configuration · v5 SFT recipe

flowchart TB
    CLI["v5 SFT CLI and typed Configs"] --> Assembly["build_supervised_objects"]
    Assembly --> Data["Shared processor, template, and dataset builders"]
    Assembly --> Choice{"DistributedConfig.backend"}
    Choice -->|HF or Megatron| Existing["Existing model builder"]
    Choice -->|fsdpturbo, proposed| Turbo["Local FSDPTurbo model and runtime adapter"]
    Data --> Cluster["SupervisedCluster"]
    Existing --> Cluster
    Turbo --> Cluster
    Cluster --> Loop["run_supervised"]

Select the backend before model construction so it owns process groups and parameter wrapping. Validate the requested mesh layout against the model's TP, EP, and CP capabilities before allocating weights. v5 SFT assembly

3. GPU and NPU compatibility

Use FSDPTurbo's torch.accelerator abstraction with NCCL on CUDA and HCCL on Ascend. Start with eager PyTorch operators and enable fused kernels per device after parity tests. With matching checkpoints, tokenized batches, and parallel layouts, compare loss, gradients, and optimizer updates for LoRA and full SFT on both platforms.

Run these checks on one node before scaling out. Match the effective target-token count and accumulation window. After eager parity passes, report fused-path accuracy, throughput, and peak device memory with the hardware and topology.

4. DeepSeek V4.1

Reuse Swift's deepseek_v41 metadata and prompt template, and port its prompt contract to the v5 template registry with token and label parity tests. Keep the native model factory in FSDPTurbo. Build on meta, apply EP/FSDP, then materialize and load each rank's shards from a dequantized BF16 checkpoint in Hugging Face format. The backend handles Host Engram and KV-AllGather context parallelism. Start with text-only LoRA on explicit attention projections. Swift V4.1 template

sequenceDiagram
    participant S as Swift adapter
    participant R as FSDPTurbo runtime
    participant W as BF16 checkpoint
    S->>R: Native meta model, LoRA config, and parallel plan
    R->>R: Warm process groups and apply EP/FSDP
    R->>R: Materialize local shards and Host Engram storage
    R->>W: Read locally owned weight shards
    W-->>R: Load BF16 weights
    R->>R: Initialize or restore LoRA and sync replicas
    loop Training updates
        S->>R: Aligned inputs, masks, and complete labels
        R->>R: KV-AllGather CP, backward, and optimizer update
        R-->>S: Loss and training metrics
    end
    S->>R: Save adapter and training state

For context parallelism, right-pad text to the CP and compression alignment and split token IDs, masks, and positions once. Keep labels complete and unshifted for the model-owned loss. The LoRA path synchronizes replicated adapter gradients and preserves the expert requires_grad flags after sharding.

An earlier prototype completed 1,000 LoRA updates with all 40 language layers at 32K context on four nodes with 32 Ascend NPUs. The proposed merge checks also cover token-weighted accumulation with uneven labels, CP=1 recomputation, and adapter save/resume.

5. Additional model support
Stage Model family Acceptance scope
1 DeepSeek V4.1 Validate the native long-context LoRA path, then qualify full-parameter SFT separately.
2 Qwen3.5-MoE Qualify the existing qwen3_5_moe plan with the HF loader, FSDP and EP, and explicit LoRA and full-SFT checks on CUDA and Ascend.
3 Dense Hugging Face causal LM, starting with a candidate such as Qwen3-8B Exercise generic HF loading, FSDP wrapping, and checkpoint round-trips for both full SFT and LoRA.

Additional families should enter through a model spec with loader, layer-wrapping rules, LoRA targets, and supported parallelism. Native checkpoints or custom attention get a model adapter in FSDPTurbo. All families use the same Swift entry point and share the data, optimizer-update, and save/resume acceptance checks. Prototype model specs · Qwen3-8B backend example

Pull Request

I plan to start with the opt-in adapter and contract tests. Is the local SFT assembly in v5 the right extension point for this backend?

Dominant language
Python
Stars
16.2k
Forks
1.8k
Avg merge
1d 14h
Merged PRs (30d)
68

Getting set up

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.

More from modelscope/ms-swift

All issues in modelscope/ms-swift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.