← Back to Pull requests

[AutoTP] Support frozen AutoTP models and manage AutoTP mode per model #8851

Closed
unknown closed this pull request without merging on Nov 04, 2026 07:41 UTC
View on GitHub
unknown unknown commented on Oct 17, 2026 16:19 UTC
Author

Summary

Two bugs show up when a frozen teacher runs next to an AutoTP student in one process, as in on-policy distillation (deepspeedai/DeepSpeedExamples#1019, #8850).

1. Frozen replicated parameters break deepspeed.initialize.
register_replicated_grad_hooks registers a gradient all-reduce hook on every replicated (non-TP) parameter. PyTorch refuses hooks on tensors with requires_grad=False, so a fully or partially frozen model fails during initialize.

  • Frozen params are now skipped. They get no gradient, so their TP replicas cannot diverge.

  • To keep that guarantee, the engine remembers which replicated params were frozen at initialize. If any of them becomes trainable later, the next backward raises a clear error instead of letting TP replicas drift apart silently.
  • 2. The AutoTP training mode leaks into init_inference.
    deepspeed.initialize with AutoTP sets a process-global training-mode flag. A teacher passed to deepspeed.init_inference afterwards still sees training mode. As a result:

  • it is sharded with the student's TP group instead of its own tp_size;

  • its LM head is not handled as for inference;

  • its parameters are left trainable.
  • The fix makes the mode per model instead of process-wide:

  • AutoTPMeta, which AutoTP already passes to every layer it builds, gains a training field. AutoTP fills it from its training_mode argument, which deepspeed.initialize sets to True and init_inference to False.

  • Each TensorParallel_Layer takes its mode from tp_meta, so the forward path (RowParallel, add_bias) follows how that model was sharded. The student keeps training-mode behavior after the teacher is built, and vice versa.

  • AutoTP.set_tensor_parallel_config and replace_transformer_layer (kernel injection, set_lm_head, MP checkpoint saving) use the explicit training_mode instead of the global flag.

  • Backward compatibility: AutoTP(training_mode=None) (the new default) and layers built without tp_meta still follow set_autotp_mode, so existing direct callers behave as before.
  • Related work

    I searched open and closed PRs and issues for register_replicated_grad_hooks, AUTOTP_MODE and frozen AutoTP models, and found no existing fix. Reported while integrating #1019.

    Sibling paths checked:

  • Every is_autotp_training_mode() reader in deepspeed/: auto_tp.py (set_tensor_parallel_config) and replace_module.py (kernel injection, set_lm_head, MP checkpoint saving) now use the explicit mode; the remaining reads are the fallbacks for callers that pass no mode.

  • All AutoTP(...) call sites in deepspeed/ (engine, TpTrainingManager, InferenceEngine) already pass training_mode explicitly. No code in deepspeed/ builds TP layers outside AutoTP.
  • Tests

    New tests in tests/unit/v1/autotp/test_tp_plan_e2e.py. Each fails without the corresponding fix and passes with it.

  • test_frozen_qwen3_initializes_and_runs: 2 ranks; the frozen model's outputs match the unsharded reference.

  • test_unfreezing_replicated_param_after_initialize_raises: 2 ranks.

  • TestInferenceTeacherAfterTrainingStudent: 4 ranks. A student with TP=2 is trained, then a teacher is built with init_inference(tp_size=4) and checked to be sharded 4 ways and frozen.
  • Results at head 68647c93e:

    | Tests | Result |
    |---|---|
    | tests/unit/v1/autotp/test_tp_plan_e2e.py | 8 passed |
    | tests/unit/module_inject + the other tests/unit/v1/autotp files | 171 passed, 37 skipped |
    | tests/unit/v1/autotp/test_autotp_training.py (skipped on XPU by skip_on_device; run locally with that skip removed) | 42 passed, 2 skipped (bf16 overflow skip) |
    | tests/unit/v1/autotp/test_autotp_training.py with DS_ACCELERATOR=cpu | 11 passed, 33 skipped |
    | test_autoep_autotp_sharded_attention.py, test_autoep_autotp_runtime.py, test_per_head_muon_accelerator.py, test_checkpoint_sharding.py (-k "not True") | 44 passed, 4 skipped, 3 failed |

    The 3 failures (TestTiedOutputHead::test_row_parallel_tied_head_is_refused[False] and two test_per_head_on_a_shard_is_the_shard_of_per_head[2-*]) fail the same way on master a4811fbf9. The zero_init=True cases of test_autoep_autotp_sharded_attention.py were deselected because test_grads_and_norm_match_reference[3-True-4-2-2] hangs on master as well.

    Hardware and software:

  • 8× Intel Arc Pro B70 (2/4/8 ranks depending on the test)

  • Level Zero driver 1.15.39122 (compute runtime 26.27.39122.11)

  • torch 2.14.0.dev20260804+xpu, transformers 5.17.0
  • Compatibility matrix

    The changes are mode bookkeeping and hook registration; no numerics change on the existing training path.

    | | Validated | Not validated |
    |---|---|---|
    | ZeRO | New tests: stage 0. Existing AutoTP suite: stages 0/1/2. | Frozen model with ZeRO-3 + AutoTP |
    | Precision | New tests: fp32. Existing suite: fp16/bf16. | |
    | Offload | | Not exercised |
    | Parallelism | TP=2 and TP=4; student TP=2 with teacher TP=4 | DP>1 together with a frozen teacher |
    | Accelerator | XPU | CUDA, HPU |

    AI assistance

    This PR was developed with AI assistance (GitHub Copilot CLI). I reviewed every changed line and ran the tests above.

    cc @0z5a I try to fix the bug you described in Vocab-parallel pipeline with OPSD.
    @delock And address AutoTP_MODE issue, I think OPSD will reach the issue soon.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jinyouzhi Review duration: 25402m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8851"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%