Repository navigation
Conversation
Distillation runs a trainable student and a frozen teacher in one process. - register_replicated_grad_hooks registered a gradient hook on every replicated parameter, which PyTorch refuses for parameters that do not require gradients, so initializing a frozen model with AutoTP failed. Frozen parameters have no gradient to reduce and are now skipped. - deepspeed.initialize switches the process-wide AutoTP mode to training and never resets it, so a teacher passed to init_inference afterwards was sharded with the student's TP group and left trainable. init_inference now builds its engine in inference mode and restores the caller's mode, and each AutoTP layer records the mode it was built in instead of reading the global. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi <youzhi.jin@intel.com>
Replicated params frozen at deepspeed.initialize get no gradient all-reduce hook, so making them trainable later would let TP replicas drift silently. Raise a clear error in the backward prologue instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi <youzhi.jin@intel.com>
Layers and AutoTP read the training mode from AutoTPMeta, which AutoTP fills from its explicit training_mode argument. A frozen inference teacher and a training student therefore keep their own mode without init_inference having to save and restore the process-wide flag. Callers that build AutoTP or layers without a mode keep following set_autotp_mode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi <youzhi.jin@intel.com>
Layers built without tp_meta captured the global AutoTP mode at construction, so a later set_autotp_mode(training=True) no longer reached them and the in-place inference bias add broke autograd. Resolve the mode at call time when tp_meta does not set it. Also let the 4-rank teacher/student test run on CPU. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi <youzhi.jin@intel.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 63bb7f58cb
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if training_mode is None: | ||
| training_mode = is_autotp_training_mode() |
There was a problem hiding this comment.
Preserve the dynamic mode for default AutoTP instances
When a direct AutoTP caller leaves training_mode=None and later switches modes with set_autotp_mode (for example, constructing under inference and enabling training before replacement or forward), this eagerly collapses None to the current global value and stores that boolean in every layer's tp_meta. Unlike the pre-change behavior and TensorParallel_Layer.is_training_mode()'s new fallback, those layers can no longer observe the later mode change; after gradients are re-enabled, an inference-built LinearAllreduce still uses the in-place bias/inference collective path and can fail backward. Keep None in the per-layer metadata while resolving a separate effective mode only for construction-time routing.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 11a3cb3: tp_meta now keeps training=None, and None is resolved to the current mode only for construction-time routing. test_layer_without_explicit_mode_follows_set_autotp_mode now also covers a layer built with a default AutoTP's tp_meta (fails before the fix).
A direct AutoTP caller that leaves training_mode=None and switches modes later with set_autotp_mode expects its layers to follow the switch, as they did before. Resolving None to the current mode before building tp_meta pinned every layer to the mode at construction. Keep None in tp_meta and resolve it only for construction-time routing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi <youzhi.jin@intel.com>
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_hooksregisters a gradient all-reduce hook on every replicated (non-TP) parameter. PyTorch refuses hooks on tensors withrequires_grad=False, so a fully or partially frozen model fails duringinitialize.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.initializewith AutoTP sets a process-global training-mode flag. A teacher passed todeepspeed.init_inferenceafterwards still sees training mode. As a result:tp_size;The fix makes the mode per model instead of process-wide:
AutoTPMeta, which AutoTP already passes to every layer it builds, gains atrainingfield. AutoTP fills it from itstraining_modeargument, whichdeepspeed.initializesets toTrueandinit_inferencetoFalse.TensorParallel_Layertakes its mode fromtp_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_configandreplace_transformer_layer(kernel injection,set_lm_head, MP checkpoint saving) use the explicittraining_modeinstead of the global flag.AutoTP(training_mode=None)(the new default) and layers built withouttp_metastill followset_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_MODEand frozen AutoTP models, and found no existing fix. Reported while integrating #1019.Sibling paths checked:
is_autotp_training_mode()reader indeepspeed/:auto_tp.py(set_tensor_parallel_config) andreplace_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.AutoTP(...)call sites indeepspeed/(engine,TpTrainingManager,InferenceEngine) already passtraining_modeexplicitly. No code indeepspeed/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 withinit_inference(tp_size=4)and checked to be sharded 4 ways and frozen.Results at head
68647c93e:tests/unit/v1/autotp/test_tp_plan_e2e.pytests/unit/module_inject+ the othertests/unit/v1/autotpfilestests/unit/v1/autotp/test_autotp_training.py(skipped on XPU byskip_on_device; run locally with that skip removed)tests/unit/v1/autotp/test_autotp_training.pywithDS_ACCELERATOR=cputest_autoep_autotp_sharded_attention.py,test_autoep_autotp_runtime.py,test_per_head_muon_accelerator.py,test_checkpoint_sharding.py(-k "not True")The 3 failures (
TestTiedOutputHead::test_row_parallel_tied_head_is_refused[False]and twotest_per_head_on_a_shard_is_the_shard_of_per_head[2-*]) fail the same way on mastera4811fbf9. Thezero_init=Truecases oftest_autoep_autotp_sharded_attention.pywere deselected becausetest_grads_and_norm_match_reference[3-True-4-2-2]hangs on master as well.Hardware and software:
Compatibility matrix
The changes are mode bookkeeping and hook registration; no numerics change on the existing training path.
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.