Skip to content

FastSentenceTransformer: non-reentrant gradient checkpointing under DDP for encoder models - #13639

Merged
danielhanchen merged 5 commits into
mainfrom
fix/st-ddp-nonreentrant-gc
Oct 22, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix/st-ddp-nonreentrant-gc

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #4680 for the encoder models.

Multi-GPU (torchrun / DDP) LoRA training of an MPNet embedding model such as unsloth/all-mpnet-base-v2 with gradient checkpointing crashes on the first backward:

RuntimeError: Expected to mark a variable ready only once. ... 2) Reused parameters in multiple reentrant backward passes.

FastSentenceTransformer loads 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:

  • the patched MPNet encoder hard-codes use_reentrant=True
  • gradient_checkpointing_enable() is called with no kwargs, which is reentrant on transformers 4.57.x ({"use_reentrant": True}) and non-reentrant on 5.x

Reentrant 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=False by 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 takes use_reentrant=False, and gradient_checkpointing_enable gets {"use_reentrant": False}. On one GPU nothing changes: MPNet keeps use_reentrant=True and gradient_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_parameters unset:

model main this PR
all-mpnet-base-v2 crash at step 0 6 steps OK (twice)
all-MiniLM-L6-v2 6 steps OK 6 steps OK
embeddinggemma-300m, "unsloth" GC 6 steps OK 6 steps OK, losses bit-identical to main

Single 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.py runs two gloo ranks on CPU through get_peft_model with 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 with WORLD_SIZE=2:

test main this PR
DDP mpnet, transformers 5.17 fails (marked ready twice) passes
DDP bert, transformers 5.17 passes passes
DDP mpnet, transformers 4.57.6 fails (rank hangs after the other raised) passes
DDP bert, transformers 4.57.6 fails (rank hangs after the other raised) passes
one process, mpnet reentrant passes passes
WORLD_SIZE=2, mpnet non-reentrant fails passes

Kaggle 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, since prepare_model_for_kbit_training forces reentrant checkpointing in unsloth-zoo; 4-bit is off by default for encoders.

@danielhanchen danielhanchen self-assigned this Oct 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T10:41:00.652258Z 547eaee Manual request
🔒 Security Review ⚠️ Failed 2026-10-13T17:05:58.324394Z
We couldn’t complete this review. Comment @codex security review to try again.
2b5334e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 2b5334e745

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 547eaeea1c

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 8334168 into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the fix/st-ddp-nonreentrant-gc branch October 22, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multi-GPU DDP support for FastSentenceTransformer?

1 participant