Skip to content

Issue with mergedmodel not reflecting Fine-Tuned LoRA weights #1763

Closed

Description

@FloLey

Hello,

I've fine-tuned a model (unsloth/mistral-7b-instruct-v0.3-bnb-4bit) and successfully uploaded the LoRA to Hugging Face. Using the LoRA directly works as expected, and the fine-tuned behavior is clearly visible.

Here's how I use the LoRA:

from unsloth import FastLanguageModel
from transformers import TextStreamer

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "Betha/fen_with_cot_base_lora", 
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
    token = access_token
)
FastLanguageModel.for_inference(model)
text_streamer = TextStreamer(tokenizer)

messages = [
    {"role": "user", "content": "2rq1rk1/pp3pb1/3p1pp1/3P3P/8/5p2/PPPQ4/2KR1B1R b - - 0 17"},
]

inputs = tokenizer.apply_chat_template(
    messages,
    tokenize = True,
    add_generation_prompt = True,
    return_tensors = "pt",
).to("cuda")

_ = model.generate(input_ids=inputs, streamer=text_streamer, max_new_tokens=2048,
                   use_cache=True, temperature=1.5, min_p=0.1)

I then tried to merge the base and LoRA and push it to Hugging Face like this:

model.save_pretrained_merged("fen_cot_base", tokenizer, save_method="merged_16bit")
model.push_to_hub_merged("Betha/fen_cot_base", tokenizer, save_method="merged_16bit", token=access_token)

However, when I load the merged model:

from unsloth import FastLanguageModel
from transformers import TextStreamer

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="Betha/fen_cot_base",
    max_seq_length=max_seq_length,
    dtype=dtype,
    load_in_4bit=load_in_4bit,
    token=access_token
)
FastLanguageModel.for_inference(model)
text_streamer = TextStreamer(tokenizer)

messages = [
    {"role": "user", "content": "2rq1rk1/pp3pb1/3p1pp1/3P3P/8/5p2/PPPQ4/2KR1B1R b - - 0 17"},
]

inputs = tokenizer.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    return_tensors="pt",
).to("cuda")

_ = model.generate(input_ids=inputs, streamer=text_streamer, max_new_tokens=2048,
                   use_cache=True, temperature=1.5, min_p=0.1)

The results seem to reflect the base model rather than the fine-tuned one. The distinct style visible when using the LoRA directly is missing after merging.

Am I missing something during the merge or the loading process? Is there an additional step needed to ensure the merged model includes the LoRA weights correctly?

Thanks in advance for your help! 🙏

Activity

  1. lucky-bai commented on Feb 20, 2025

    @lucky-bai

    Likely the same issue as this one: #1670

  2. FloLey commented on Feb 22, 2025

    @FloLey
    Author

    Hello and thanks for taking the time to respond. It seems related indeed. I just want to point out that I'm not using GRPO to fine-tune the mistral model.
    Thanks!

  3. reopened this on Feb 22, 2025
  4. wofeishenling commented on Feb 24, 2025

    @wofeishenling

    same problem!!!

  5. shimmyshimmer commented on Apr 6, 2025

    @shimmyshimmer
    Member

    @FloLey do you have the full script of what you did? Because we tested it and it works

  6. rolandtannous commented on Jun 6, 2025

    @rolandtannous
    Contributor

    @FloLey @Mtrya @lucky-bai @wofeishenling @yuiseki

    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

    pip install --upgrade unsloth_zoo
    pip install --upgrade unsloth
    

    If you're in colab, the notebooks install cell will also be updated shortly but should be something like

    %%capture
    import os
    if "COLAB_" not in "".join(os.environ.keys()):
        !pip install unsloth
    else:
        # Do this only in Colab notebooks! Otherwise use pip install unsloth
        !pip install --no-deps bitsandbytes accelerate xformers==0.0.29.post3 peft trl triton transformers cut_cross_entropy unsloth_zoo
        !pip install sentencepiece protobuf "datasets>=3.4.1" huggingface_hub hf_transfer
        !pip install --no-deps unsloth
    

    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

    save_pretrained_merged()
    

    and

    push_to_hub_merged()
    

    If you are not merging at all, use

    save_pretrained
    

    and

    push_to_hub
    

    instead.

    I'll close this for now. Feel free to comment if you face any problems.
    Happy finetuning.

  7. jasonkhadka commented on Jul 26, 2025

    @jasonkhadka

    #3045
    Still facing the same issue.

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

    currently fixing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions