You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve Documentation on Loading Fine-Tuned Models with Added Tokens #1863
TL;DR: Unsloth currently lacks clear guidance on how to properly load a fine-tuned model that includes added tokens.
Description
First, I want to thank the maintainers for this great repository!
I would like to suggest an improvement to the documentation on adding new tokens. While the existing guide demonstrates how to add tokens to the base model, it does not explain how to properly load a fine-tuned model that includes these added tokens.
Issue
When trying to load my fine-tuned model following the instructions in the Colab notebook, I encountered an issue:
fromunslothimportFastLanguageModelmodel, tokenizer=FastLanguageModel.from_pretrained(
model_name="lora_model", # YOUR MODEL YOU USED FOR TRAININGmax_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
)
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
This approach does not properly account for the added tokens. After researching, I found that I should add the tokens before loading the model, but Unsloth does not currently seem to support this directly.
The workaround I found is:
model, tokenizer=FastLanguageModel.from_pretrained(...)
fromunslothimportadd_new_tokensadd_new_tokens(model, tokenizer, new_tokens= ["<CHARACTER_1>", "<THINKING>", "<SCRATCH_PAD>"])
# Instead of this:model=FastLanguageModel.get_peft_model(...)
# The correct approach should be:model=PeftModel.from_pretrained(model, "lora_model/")
Proposed Improvement
I believe adding clearer instructions on this process to the documentation would help users avoid confusion.
Additionally, I am considering a small improvement to the FastLanguageModel.from_pretrained API to handle this more seamlessly. However, before implementing any changes, I’d like to confirm whether the maintainers see this as an issue worth addressing or if there is an existing intended way to handle this case.
Would you be open to a change like this? Looking forward to your thoughts! 🚀
RuntimeError: Error(s) in loading state_dict for PeftModelForCausalLM:
While copying the parameter named "base_model.model.lm_head.original_module.weight", whose dimensions in the model are torch.Size([100517, 5120]) and whose dimensions in
the checkpoint are torch.Size([100517, 5120]), an exception occurred : ('Inplace update to inference tensor outside InferenceMode is not allowed.You can make a clone to get a nor
mal tensor before doing inplace update.See pytorch/rfcs#17 for more details.',).
model, tokenizer=FastLanguageModel.from_pretrained(...)
# Not entirely sure whether we need to get rid of the overlapped ones.new_tokens= [, , , ...]
overlapping_tokens=set(new_tokens) &set(tokenizer.vocab.keys())
new_tokens= [xforxinnew_tokensifxnotinoverlapping_tokens]
tokenizer.add_tokens(new_tokens)
model.resize_token_embeddings(len(tokenizer))
model=PeftModel.from_pretrained(model, "your checkpoint path")
My guess is that add_new_tokens forces lm_head and input embeddings to be trainable here. Which messes up with the adapter loading in PeftModel.from_pretrained().
My suggestion is that either impove FastLanguageModel.from_pretrained to be compatible with models trained using add_new_tokens, or at least add one parameter in add_new_tokens to expend the embeddings with and without making them trainable.
Activity
shimmyshimmer commentedon Mar 1, 2025
Hi there this is more custom but it is a good addition to the docs for sure.
I will need to transport our docs to github hopefully soon.
XinnuoXu commentedon Apr 24, 2025
I encountered the same problem.
However the following suggested solution did not work:
I got the following error:
RuntimeError: Error(s) in loading state_dict for PeftModelForCausalLM:
While copying the parameter named "base_model.model.lm_head.original_module.weight", whose dimensions in the model are torch.Size([100517, 5120]) and whose dimensions in
the checkpoint are torch.Size([100517, 5120]), an exception occurred : ('Inplace update to inference tensor outside InferenceMode is not allowed.You can make a clone to get a nor
mal tensor before doing inplace update.See pytorch/rfcs#17 for more details.',).
My environment is:
My solution is:
My guess is that add_new_tokens forces lm_head and input embeddings to be trainable here. Which messes up with the adapter loading in PeftModel.from_pretrained().
My suggestion is that either impove FastLanguageModel.from_pretrained to be compatible with models trained using add_new_tokens, or at least add one parameter in add_new_tokens to expend the embeddings with and without making them trainable.
Also, please please update the document 🙏 🙏 🙏!
Pin the sd.cpp prebuilt that actually renders MiniMax-H3
Studio: add MiniMax H3 video generation (#7989)
Studio: MiniMax-H3 joint video and audio LoRA training (#8244)