Repository navigation
FastSentenceTransformer: non-reentrant gradient checkpointing under DDP for encoder models - #13639
Conversation
|
@codex review |
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. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Fixes #4680 for the encoder models.
Multi-GPU (torchrun / DDP) LoRA training of an MPNet embedding model such as
unsloth/all-mpnet-base-v2with gradient checkpointing crashes on the first backward:FastSentenceTransformerloads BERT-family models (ENCODER_MODEL_TYPES) through its own fast encoder route, which never reaches the FastModel DDP handling from #3751 / #4240. Two spots there pick reentrant checkpointing:use_reentrant=Truegradient_checkpointing_enable()is called with no kwargs, which is reentrant on transformers 4.57.x ({"use_reentrant": True}) and non-reentrant on 5.xReentrant checkpointing runs one backward per checkpointed layer call. Sentence-transformers losses such as MultipleNegativesRankingLoss call the model once per text column, so each LoRA weight's DDP hook fires more than once per step and DDP raises. Setting
ddp_find_unused_parameters=Falseby hand does not help.Change
Under DDP only (
is_distributed(), the same check the FastModel route uses), the fast encoder route now uses non-reentrant checkpointing: the MPNet patch takesuse_reentrant=False, andgradient_checkpointing_enablegets{"use_reentrant": False}. On one GPU nothing changes: MPNet keepsuse_reentrant=Trueandgradient_checkpointing_enable()is still called without kwargs, so the transformers default stays.EmbeddingGemma, Qwen3-Embedding and other decoder-based embedders go through FastModel and were already fixed by #4240 (released in 2026.10.4).
Evidence
2x B200, torchrun, NCCL, transformers 5.17.0, sentence-transformers 6.1.0, MultipleNegativesRankingLoss,
use_gradient_checkpointing=True,ddp_find_unused_parametersunset:"unsloth"GCSingle GPU, same scripts: mpnet and MiniLM losses and grad norms stay within the main vs main run-to-run spread (step 1 loss identical; grad norm spread 2.398 to 2.433 on main, 2.536 on this PR for mpnet step 1, step 6 loss 0.5583 / 0.5592 main vs 0.5561 here); EmbeddingGemma is bit-identical.
tests/python/test_sentence_transformer_ddp_gc.pyruns two gloo ranks on CPU throughget_peft_modelwith a tiny MPNet and a tiny BERT, calling the model twice per step like MultipleNegativesRankingLoss, plus a single-process check that MPNet keeps reentrant checkpointing without DDP and switches withWORLD_SIZE=2:WORLD_SIZE=2, mpnet non-reentrantKaggle 2x T4 (NCCL), all-mpnet-base-v2 with gradient checkpointing: crashes on main (marked ready twice), trains on this PR.
Not covered: 4-bit encoders (
load_in_4bit=True) with gradient checkpointing under DDP still crash, sinceprepare_model_for_kbit_trainingforces reentrant checkpointing in unsloth-zoo; 4-bit is off by default for encoders.