Skip to content

Improve Documentation on Loading Fine-Tuned Models with Added Tokens #1863

Closed

Description

@anthony-wss

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:

from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "lora_model", # YOUR MODEL YOU USED FOR TRAINING
    max_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(...)
from unsloth import add_new_tokens
add_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! 🚀

Activity

  1. shimmyshimmer commented on Mar 1, 2025

    @shimmyshimmer
    Member

    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.

  2. XinnuoXu commented on Apr 24, 2025

    @XinnuoXu

    I encountered the same problem.

    However the following suggested solution did not work:

    model, tokenizer = FastLanguageModel.from_pretrained(...)
    
    from unsloth import add_new_tokens
    add_new_tokens(model, tokenizer, new_tokens = [, , , ...])
    
    model = PeftModel.from_pretrained(model, "your checkpoint path") 

    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:

    unsloth==2025.3.19
    unsloth_zoo==2025.3.17
    peft==0.15.0
    

    My solution is:

    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 = [x for x in new_tokens if x not in overlapping_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.

    Also, please please update the document 🙏 🙏 🙏!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions