Skip to content

Keep offloaded embeddings in host RAM when Trainer and accelerate place the model - #13320

Merged
danielhanchen merged 1 commit into
mainfrom
fix-7055-keep-offloaded-embeddings-on-host
Oct 11, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix-7055-keep-offloaded-embeddings-on-host

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Related to #7055

Problem

offload_layers = "auto" (Studio: Offload layers = Auto) moves large token tables to host RAM so a model bigger than the card can still train. Gemma 4 E2B is the main case: its 4-bit checkpoint is 8.1 GB, and 4.4 GiB of that is the bf16 per-layer embedding table (embed_tokens_per_layer, 262144 x 8960), so on an 8 GB card the default load spills to the CPU and bitsandbytes refuses it.

With Auto the load now fits (2.8 GiB on the card), but the first training step put the table back on the GPU. Trainer.train() calls model.to(args.device) whenever bf16_full_eval / fp16_full_eval is set, which Unsloth turns on by default, and Accelerator.prepare_model does the same when the model carries no hf_device_map (a block swap load uses {"": device}). .to() moves every CPU parameter, including the offloaded table, so allocated memory jumped from 2.9 GiB to 7.3 GiB and the step died with No or negligible GPU memory available for fused cross entropy.

Fix

When offload_input_embedding installs its hooks, it also wraps Trainer._move_model_to_device and Accelerator.prepare_model once. For the length of that call, every hooked embedding whose weight is in host RAM skips _apply. The rest of the model still moves as before. Nothing changes for runs that never offload an embedding, since the wrappers are only installed on that path. An embedding that restore_input_embedding put back on the card is not held on the host.

Evidence

Gemma 4 E2B in 4-bit, the way Studio loads it: FastVisionModel, LoRA r=16, batch 2, 5 steps, seed 3407. To emulate an 8 GB card I capped the process at 8 GiB with set_per_process_memory_fraction and reported 7.3 GiB free.

Run Result Peak alloc Losses
main, default does not fit in GPU memory ... Pass offload_layers = "auto" - -
main, offload_layers = "auto" loads at 2.83 GiB, then No or negligible GPU memory available for fused cross entropy (7.30 GiB allocated, table back on cuda:0) 7.43 GiB -
this PR, offload_layers = "auto" trains, table stays on cpu 5.74 GiB 7.1479, 7.198, 6.5079, 5.3274, 4.7686
no cap, no offload (reference) trains 7.93 GiB 7.1479, 7.198, 6.5079, 5.3274, 4.7686

The losses match the no-offload reference exactly.

tests/test_offload_embedding_trainer_move.py runs the real Trainer._move_model_to_device on CPU, with meta standing in for the accelerator. It fails on main and passes here on transformers 5.17.0 and 4.57.6. All 216 offload and block swap tests pass on GPU. test_offload_tied_autodisable.py loads offload_input_embedding through the AST with its collaborators stubbed, so it now stubs the new one too.

Not covered

On Windows and WSL, embedding offload is still turned off (_offload_embedding_unsupported_platform). There Auto can only move decoder layers, which is not enough room for E2B on an 8 GB card. This PR does not lift that restriction because I have no Windows CUDA machine to prove it on.

@danielhanchen danielhanchen self-assigned this Oct 11, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 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-11T11:26:31.129279Z 9b5dc42 Manual request
ℹ️ 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 9b5dc420be

ℹ️ 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".

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.

1 participant