Repository navigation
Description
I was following the official Google Colab notebook to train my reasoning LoRA model on Paperspace. I was able to run the notebook without any issues.
However, when trying to save the trained model, I encountered some unexpected behavior. I have attempted multiple methods to save the trained model/LoRA, but none of them produce the same chat response after loading (I'm using temperature = 0.0).
Additionally, if I save the LoRA using model.save_lora("grpo_saved_lora"), After restarting and kernel and reload the model, I get the exact same inference response whether I use lora_request = None or load it with model.load_lora("grpo_saved_lora").
Issue Details
I consistently use SYSTEM_PROMPT to construct the prompt, and my test question is:
"How many r's are in strawberry?"
Responses Right After Training
- Without LoRA:
<reasoning> To determine how many times the letter 'r' appears in the word "strawberry", I will go through the word character by character and count each occurrence of 'r'. </reasoning> <answer> The letter 'r' appears 3 times in the word "strawberry". </answer> - With LoRA:
<reasoning>\nTo determine the number of \'r\'s in the word "strawberry," we need to count each occurrence of the letter \'r\' in the word. Let\'s examine the word character by character:\n\n1. s\n2. t\n3. r\n4. a\n5. w\n6. b\n7. y\n8. r\n9. r\n\nWe can see that the letter \'r\' appears twice in the word "strawberry."\n</reasoning>\n<answer>\n2\n</answer>
Responses After Saving and Reloading
Method 1: model.save_lora("grpo_saved_lora"), Restart Kernel, Load with model.load_lora("grpo_saved_lora")
- With or Without LoRA:
'<reasoning>\nTo determine how many times the letter \'r\' appears in the word "strawberry", we can go through the word character by character and count each occurrence of \'r\'. \n\n- The first character is \'s\', not \'r\'.\n- The second character is \'t\', not \'r\'.\n- The third character is \'r\', this is the first occurrence of \'r\'.\n- The fourth character is \'w\', not \'r\'.\n- The fifth character is \'a\', not \'r\'.\n- The sixth character is \'r\', this is the second occurrence of \'r\'.\n- The seventh character is \'r\', this is the third occurrence of \'r\'.\n- The eighth character is \'b\', not \'r\'.\n- The ninth character is \'b\', not \'r\'.\n- The tenth character is \'e\', not \'r\'.\n\nBy going through the word, we find that the letter \'r\' appears 3 times.\n</reasoning>\n<answer>\n3\n</answer>'
Method 2: model.save_pretrained_merged("qwen-2.5-3b-lora-251", tokenizer, save_method="lora"), Load with FastLanguageModel.from_pretrained(...)
- Without LoRA: Same result as Method 1.
- With LoRA: Error - 'Qwen2ForCausalLM' object has no attribute 'load_lora'.
Workaround
The only way I found to get consistent responses is to use the "Finetuning from Last Checkpoint" method:
- Load the model.
- Execute the same training code, but use
trainer.train(resume_from_checkpoint=True). Ensuremax_steps <= save_stepsto stop training immediately.
By doing this, the inference response matches what I got during the initial training.
I'm unsure if this is a bug or if I'm missing something in the saving/loading process. Could you please advise?
Thanks!
Activity
danielhanchen commentedon Feb 12, 2025
Have you tried directly loading the LoRA ie
FastLanguageModel.from_pretrained("qwen-2.5-3b-lora-251")Xiaohan-Tian commentedon Feb 12, 2025
Yes, I tried that, and it produces the same result as in Method 1 when I pass
lora_request = Nonetomodel.fast_generate():However, if I try to load the LoRA with
model.load_lora("grpo_saved_lora"), I get the following error:AttributeError: 'Qwen2ForCausalLM' object has no attribute 'load_lora'.Here is the code I'm using to load the model:
And here is the code I'm using to perform the inference:
I'm not sure if I'm missing something or if there's an issue with the loading process. Any guidance would be appreciated.
BY571 commentedon Feb 13, 2025
@Xiaohan-Tian Hey, did you figure out how to load the lora adapter?
Xiaohan-Tian commentedon Feb 14, 2025
I'm able to load the model, but the inference results I'm getting now aren't consistent with what I saw immediately after training (w/LoRA). I'll investigate further tomorrow.
Erland366 commentedon Feb 15, 2025
I think
load_lorahas already been fixed. I did a fresh install and now it's working.saadi297 commentedon Feb 17, 2025
@Xiaohan-Tian I am facing the same issue. Did you find the solution?
fullstackwebdev commentedon Feb 18, 2025
I am getting this error when I try to load a LoRA
with this script and my LoRA
https://gist.github.com/fullstackwebdev/c5af23b036cc30a71f8c7331a30bbf1c
janpf commentedon Feb 20, 2025
Hey all,
I managed to make it reproducible. The only way I managed to load the adapter is via the trained hack described by @Xiaohan-Tian.
I trained using GRPO and a random reward with an absurd LR, in order to break the model and make the effect of loading the adapter more clearly visible.
Please see attached notebook for more details :) (i had to rename it to .txt, as github doesn't allow ipynb, sorry.)
try_to_load_model.ipynb
lucky-bai commentedon Feb 21, 2025
Hi @Xiaohan-Tian, I'm attempting to follow your suggestion to load the LoRA adapter from a trained checkpoint using your workaround of doing
trainer.train(resume_from_checkpoint=True)with 0 steps:When I do this, the model produces output that's different from the base model but is not the trained model either (using version
unsloth==2025.2.15). Not sure what's happening - perhaps it is randomly initializing instead. Can you share how you're loading from checkpoint with this method if it's working correctly for you?janpf commentedon Feb 21, 2025
not sure if it helps, but i also tried this in above notebook and it reproduces the adapter outputs exactly :)
wooozihui commentedon Feb 22, 2025
Same issue, and when I want to solve the problem
by using:
report a new error:
wooozihui commentedon Feb 22, 2025
Hi, can you share your versions of transformers and vllm?
janpf commentedon Feb 22, 2025
sure, it's all in the notebook in the first cell:
wooozihui commentedon Feb 22, 2025
This code works for me:
SpaceHunterInf commentedon Mar 14, 2025
I have the same issue, I think this should be an urgent bug to fix.
danielhanchen commentedon Mar 18, 2025
Apologies on the issue - I might, but not 100% sure might have fixed the issue - if you guys still have the trained LoRA on your disk, could you try
model.load_lora( "directory", load_tensors = False)ie useload_tensors = Falseas the extra argumentshimmyshimmer commentedon Mar 18, 2025
Hi guys we have likely resolved the issue. Please update unsloth and let us know if it works! Thank you
canrertugrul commentedon Mar 18, 2025
This fix has not addressed the issue.
shimmyshimmer commentedon Mar 19, 2025
do you know if this issue is for grpo specifically or also normal SFT?
canrertugrul commentedon Mar 19, 2025
I am having this issue with normal SFT. Haven't tried GRPO.
canrertugrul commentedon Mar 19, 2025
For reference, I am expanding the model's vocabulary with a few new tokens. Perhaps that might be related?
whiskyboy commentedon Mar 22, 2025
Facing the same issue.
@shimmyshimmer Upgrading
unslothto2025.3.18and passingload_tensors = Falsetomodel.load_lora(...)did not work in my code. It still reported the following error:AttributeError: 'Qwen2ForCausalLM' object has no attribute 'load_loraMy current workaround is to get a PEFT model as in training script, then execute
model.load_lora(...)explicitly, finally call generate:No idea why the above code works.
canrertugrul commentedon Mar 22, 2025
The only workaround I’ve found so far is to upload the adapters to Hugging Face and load them before running inference.
yuimo commentedon Mar 24, 2025
after installing the latest version of unsloth,and it works for me.
rolandtannous commentedon Jun 6, 2025
@BY571 @FloLey @Joinn99 @K1nght @Mtrya @SpaceHunterInf @Xiaohan-Tian @Yangt524 @canrertugrul @fullstackwebdev @janpf @juli2807 @kjm981995 @liuchengyuan123 @lucky-bai @maopian2333 @matthewchung74 @mingqing431 @mominabbass @saadi297 @vasqu @whiskyboy @wofeishenling @wooozihui @yuimo @yuiseki
Please use FastVisionModel.from_pretrained or FastLanguageModel.from_pretrained to load your lora model directly.
If you want to use load_adapter, follow my comment here: #2411 (comment)
As for saving and merging:
A fix has now been merged into pypi for both vision and language models and extensively tested.
reference:
unslothai/unsloth-zoo#135
unslothai/unsloth-zoo#158
#2673
Merging and saving should work now.
Best Practice is save to 16bits (which is the default operation if you do not specify anything), and load in 4 bits if you have to.
For production systems, you might wanna consider post training quantization methods with calibration. These take longer but can also be more reliable.
Saving and merging to 4bits might degrade performance of the model.
To access this recent fix, and if you're running this in a local python environment, please make sure to upgrade your python environment using
If you're in colab, the notebooks install cell will also be updated shortly but should be something like
as we're no longer pinning transformers or trl.
Also note that whenever you need to merge while saving or while pushing to hub, make sure to use
and
If you are not merging at all, use
and
instead.
I'll close this for now. Feel free to comment if you face any problems.
Happy finetuning.