Repository navigation
Attach save_lora without a vLLM engine - #8127
Conversation
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
`patch_peft_fast_inference` set `save_lora` only inside `if vllm_engine is not None`, so a GRPO run with `fast_inference = False` reached `model.save_lora(...)` and got AttributeError: 'Lfm2ForCausalLM' object has no attribute 'save_lora' which names neither vLLM nor the flag that caused it. Found running LFM2.5_(1.2B)-GRPO, which loads with fast_inference = False and saves at the end. unsloth_zoo's save_lora is save_pretrained over the lora_A/lora_B keys of the state dict and never touches the engine, so the gate was not buying anything. load_lora stays behind it: that one copies tensors into vLLM's own adapter buffers, so without an engine there is nothing to load into. Set only when absent, and the import is guarded, so an older unsloth_zoo without the helper still loads.
Comments and docstrings only. AST gate with --strip-docstrings passes on both files; without it both report CODE CHANGED, which is expected since a docstring is an AST node. Docstrings touched: the module docstring of tests/test_save_lora_without_vllm.py and three of its test docstrings.
b59422c to
0b12caa
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0b12caa095
ℹ️ 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".
| except Exception: | ||
| save_lora = None | ||
| if save_lora is not None: | ||
| model.save_lora = functools.partial(save_lora, model) |
There was a problem hiding this comment.
Preserve non-matrix adapter weights when saving
For non-vLLM fine-tuning with modules_to_save, this newly attached method delegates to a helper that prefilters the state dict to only .lora_A. and .lora_B. keys. This repository automatically adds embed_tokens and lm_head to modules_to_save when new tokens are trained (unsloth/models/llama.py:3325-3350), so PEFT receives none of those module weights and can raise a KeyError during the final save; DoRA checkpoints similarly omit their trained magnitude vectors. Use the normal PEFT adapter-state selection, or extend the helper to preserve every trainable adapter component, before exposing it to all non-vLLM models.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and worse than the KeyError alone. Reproduced against peft 0.20.0 on a tiny Llama: modules_to_save = ["embed_tokens", "lm_head"] raises KeyError: 'modules_to_save.default.weight', and use_dora = True saves silently while losing all four lora_magnitude_vector tensors. The mechanism is in peft's own get_peft_model_state_dict: it builds each AuxiliaryTrainingWrapper's module-relative dict out of the state dict it was given, so a dict pre-filtered to .lora_A./.lora_B. has nothing for it to look up. And you are right that it is reachable with no vLLM in sight, since get_peft_model appends embed_tokens/lm_head to modules_to_save on its own once new tokens are trained.
Fixed in 77ce195 by letting peft do the selection: the whole state dict is handed over and only the adapter tensors are cast to the embedding dtype, which is the one thing the helper adds over save_pretrained. Non-adapter entries pass through by reference, so nothing is copied that peft is going to drop. Saved key sets now equal what peft.save_pretrained writes on all four shapes (plain, modules_to_save, DoRA, DoRA + modules_to_save), the adapter loads back through PeftModel.from_pretrained, and plain LoRA is unchanged. A model that has a vLLM engine keeps the Zoo helper it has always had, since vLLM reads that file back through its own LoRA loader.
The Zoo helper filters the state dict to .lora_A./.lora_B. before PEFT makes its own selection, so PEFT then looks up modules_to_save.<adapter>.weight in what it was handed and raises KeyError, and a DoRA run loses its lora_magnitude_vector without saying so. Both are reachable with no engine in sight: get_peft_model adds embed_tokens and lm_head to modules_to_save by itself once new tokens are trained. Hand PEFT the whole state dict and cast only the adapter tensors, so its selection finds the keys it looks up. A model that has a vLLM engine keeps the Zoo helper unchanged, since vLLM reads the saved adapter back through its own LoRA loader. Verified against peft 0.20.0 on plain LoRA, modules_to_save, DoRA and both together: the saved key set now equals what peft save_pretrained writes in all four, the adapter loads back, and plain LoRA is byte-for-byte what it was.
Where this stands1. Before the PR. The message named neither vLLM nor the flag that caused it, so there was nothing in the traceback to act on. The gate was wrong on its own terms: unsloth_zoo's 2. After the PR. Every PEFT model gets a 3. Real or theoretical. Real, and found by running the notebook rather than by reading the code. The The review round on it was also real, and turned out to be the more serious half. The Zoo helper filters the state dict to
Both are reachable with no vLLM in sight, because 4. What merging could break. No new hardware path and no new branch on any existing one. The engine branch is untouched, asserted by test rather than by inspection: Old installs. The engineless attach no longer imports from unsloth_zoo at all, which is a test in its own right: an unsloth_zoo predating Models that already have the attribute keep it, SimulationsTheorised first, then tested against the theory: (a) Executed on Linux, CUDA_VISIBLE_DEVICES=0, torch 2.9.1+cu128, transformers 5.14.1, peft 0.20.0, real Not executed, reasoned from source: the vLLM engine branch, since it needs a live engine. It is unchanged, and pinned by two AST tests so it cannot be loosened by accident later. macOS and Windows were not executed either; the change is |
Review notes: what this does, and what it cannot breakBefore the PR
After the PR
Real or theoreticalReal. It came out of running the 21 base notebooks that no sweep had ever executed, and it is the last call in one of them, so the failure destroys the output of a completed GRPO run. If merged, what could it breakI drove
One thing worth calling out, because it is the reason this does not simply reuse the Zoo helper. That helper filters the state dict down to |
|
@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". |
The bug
LFM2.5_(1.2B)-GRPOdies at its final save:The notebook loads with
fast_inference = Falseand ends withmodel.save_lora("grpo_saved_lora").patch_peft_fast_inferenceattachessave_loraonly insideif vllm_engine is not None, so with no engine theattribute is never set and the call raises. The message names neither vLLM nor
the flag that caused it, so there is nothing in the traceback to act on.
Found by running the 21 base notebooks that no sweep had ever executed.
Why the gate was wrong
save_lorain unsloth_zoo does not use the engine at all:It is
save_pretrainedover the LoRA keys of the state dict. Nothing thereneeds vLLM, so gating it behind the engine bought nothing and cost every
non-vLLM PEFT model the method.
load_lorastays behind the gate, and that is deliberate: it copies tensorsinto vLLM's own adapter buffers, so with no engine there is nothing to load
into.
Compatibility
Additive. Models that previously had
save_lorastill have it, with the sameimplementation.
save_lorakeeps it.rather than raising during
from_pretrained.vllm_engine,fast_generateandfast_generate_batchesare untouchedbehind the engine gate, and a test pins that so they are not loosened by
accident later.
Tests
tests/test_save_lora_without_vllm.py, 6 cases. Three fail againstmainandall six pass with the change; the other three pin behaviour that must not move.
Wider run:
1239 passed, 24 skippedacrosstests/*.pyplustests/saving/test_export_dispatch.py. One unrelated failure,test_model_registry.py::test_model_registration[qwen], reproduces identicallyon clean
main.Not fixed here
This restores the method. Whether
LFM2.5_(1.2B)-GRPOshould be running withfast_inference = Falseat all is a separate question for the notebooks repo:its own comment on that line reads "Enable vllm fast inference", which no longer
matches the value.