Repository navigation
Keep a modules_to_save copy on the card it was loaded onto - #10034
Conversation
Continued pretraining puts `embed_tokens` and `lm_head` in `target_modules`,
so PEFT wraps each in a `ModulesToSaveWrapper` and
`_offload_frozen_module_for_training` moves the trainable copy onto the
accelerator. It was passed `DEVICE_TYPE_TORCH`, the bare string "cuda", and a
bare "cuda" carries no index:
>>> torch.device("cuda").index is None
True
>>> torch.zeros(2).to("cuda").device
device(type='cuda', index=0)
So on a model the loader split across cards, a copy sitting on cuda:1 was
moved to cuda:0 while the rest of its layer stayed behind, and the forward
then mixed devices. Single-GPU runs never saw it, because there cuda:0 is
where the copy already was.
Keep the index the copy already has when it is on the right kind of device.
A copy still on cpu or meta has none to keep and is moved as before, so the
single-GPU path is unchanged.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c8ab7b4c7b
ℹ️ 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".
| target_device = module.modules_to_save.default.weight.device | ||
| if target_device.type != torch.device(device_type).type: | ||
| target_device = device_type |
There was a problem hiding this comment.
Preserve the device captured before disk offload
With the default use_gradient_checkpointing="unsloth", lines 3631-3643 first replace each trained embedding/head with a CPU module loaded from disk, and PEFT consequently creates modules_to_save.default on CPU. This branch therefore falls back to the same bare "cuda" destination as before, losing the original indexed placement already captured in input_embeddings_device/output_embeddings_device; when (for example) lm_head was mapped to cuda:1, continued pretraining still moves its trainable copy to the current card and can fail with mixed-device tensors. The helper needs the saved pre-offload device (or equivalent device-map placement), and the tests should exercise this default offload path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and the fix as it stood did not reach the default path. Confirmed in the source: with use_gradient_checkpointing = "unsloth", offload_input_embeddings / offload_output_embeddings run at lines 3632-3643, before _get_peft_model, so PEFT builds modules_to_save.default on CPU. A CPU copy has no index to preserve, so the helper fell straight back to the bare "cuda" and a head mapped to cuda:1 still had its trainable copy moved to cuda:0.
Fixed in 2ea2523. input_embeddings_device and output_embeddings_device were already captured at lines 3625-3629, immediately above that offload, and then discarded at the call sites; they are now passed through as original_device. The order is copy device, then the recorded device, then the bare type, so a copy that still carries an index keeps it, a model loaded on CPU is still moved onto the accelerator, and the two call sites that have nothing to record are unchanged.
Tests are 20 now. Seven cover this path specifically, including a copy rebuilt on CPU with cuda:1 recorded, a recorded cpu device that must not pin the copy to cpu, and an AST guard that both continued-pretraining call sites actually pass original_device, since a helper that supports it while the callers drop it is exactly the bug you found. Mutation tested both ways: removing original_device from a call site fails the AST guard, and making the helper ignore it fails three placement tests.
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. |
The previous commit preserved the index the trainable copy already carried, which is not enough on the DEFAULT path. With `use_gradient_checkpointing = "unsloth"`, `offload_input_embeddings` and `offload_output_embeddings` run before `_get_peft_model`, so PEFT builds `modules_to_save.default` on CPU. A CPU copy has no index to preserve, so the helper fell back to the bare "cuda" exactly as before and a head mapped to cuda:1 still had its trainable copy moved to cuda:0. llama.py already records the real placement in `input_embeddings_device` and `output_embeddings_device`, captured immediately above that offload, and then threw it away. Pass it through and use it when the copy itself no longer carries one. Order is copy device, then the recorded device, then the bare type, so a copy that still has an index keeps it, a model loaded on CPU is still moved onto the accelerator, and the two call sites with nothing to record are unchanged.
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
68 comment lines to 38. Drops docstrings that only restated the test name and condenses the module docstring to the mechanism. Comments only, verified by comparing the ASTs with docstrings stripped.
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
* Name the encoding when the offload test reads llama.py main is red on Repo tests (CPU): FAILED tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding offenders: tests/test_offload_frozen_module_device.py:40, :254 #10034 added that file with two bare read_text() calls, and its own CI caught them. It merged at 02:54:14 on head 14c6f0b; the fix I pushed to that branch landed a few minutes later and so is not in the squash. Same change, against main. The gate is right and the reads were wrong. Both read unsloth/models/llama.py, a checked-in source file, with the platform default encoding, which is cp1252 on Windows. Latent rather than live: llama.py is 177,788 bytes and currently all ASCII, so nothing breaks today. Failing at the point the read is added is exactly what the gate is for -- otherwise this surfaces months from now, on Windows only, when someone puts a non-ASCII character in llama.py. test_source_read_encoding and the file's own 20 tests pass: 21 passed. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: danielhanchen <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
What breaks
Continued pretraining puts
embed_tokensandlm_headintarget_modules, so PEFT wraps each one in aModulesToSaveWrapper.llama.pythen calls_offload_frozen_module_for_trainingto move the trainable copy onto the accelerator and offload the frozen original to CPU, passingDEVICE_TYPE_TORCH:DEVICE_TYPE_TORCHis the bare string"cuda", and a bare"cuda"carries no index:The helper then did:
So on a model the loader split across cards, a copy sitting on cuda:1 was moved to cuda:0 while the rest of its layer stayed on cuda:1, and the forward then mixed devices. The destination was decided by the current CUDA device rather than by the placement the loader chose, so it ignored
hf_device_mapentirely.Why it was not noticed
Single-GPU runs cannot see it. There cuda:0 is where the copy already is, so the move is a no-op and every existing test passes. It needs a second card and
embed_tokensorlm_headintarget_modules, which is the continued-pretraining recipe specifically, not the ordinary LoRA one.The fix
Keep the index the copy already has when it is on the right kind of device. A copy still on cpu or meta has no index to keep and is moved exactly as before, so the single-GPU path is byte for byte unchanged.
Tests
tests/test_offload_frozen_module_device.py, 13 tests.Twelve drive the real helper with a stub that records the device it is handed rather than allocating. That is deliberate rather than a shortcut: the case under test is a second card, and recording the argument tests the decision on any box, including CI runners with no GPU. The thirteenth allocates for real, is skipped without CUDA, and covers only the no-regression direction, which one card is enough for.
Mutation tested by restoring
device = device_type. Three tests fail, including the one that names the regression:Also covered so the fix cannot quietly drop the behaviour around it: float16 is still promoted to float32 for Tesla T4 (#1200), bfloat16 is left alone, only the trainable copy gets
requires_grad, the frozen original still goes to CPU,offload_device = Nonestill leaves it in place, and a module with nomodules_to_saveis still untouched.Scope
Found while investigating #9995 and deliberately kept out of it, since that PR is about dispatch hooks and this is about a placement decision. The two are independent and neither depends on the other.
I do not have a two-card box to hand, so the multi-card behaviour here is asserted through the recorded device argument and the source, not measured on two GPUs. The single-card no-regression direction is measured.