Repository navigation
Keep offloaded embeddings in host RAM when Trainer and accelerate place the model - #13320
Merged
Merged
Conversation
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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()callsmodel.to(args.device)wheneverbf16_full_eval/fp16_full_evalis set, which Unsloth turns on by default, andAccelerator.prepare_modeldoes the same when the model carries nohf_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 withNo or negligible GPU memory available for fused cross entropy.Fix
When
offload_input_embeddinginstalls its hooks, it also wrapsTrainer._move_model_to_deviceandAccelerator.prepare_modelonce. 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 thatrestore_input_embeddingput 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 withset_per_process_memory_fractionand reported 7.3 GiB free.does not fit in GPU memory ... Pass offload_layers = "auto"No or negligible GPU memory available for fused cross entropy(7.30 GiB allocated, table back on cuda:0)The losses match the no-offload reference exactly.
tests/test_offload_embedding_trainer_move.pyruns the realTrainer._move_model_to_deviceon CPU, withmetastanding 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.pyloadsoffload_input_embeddingthrough 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.