Skip to content

[Bug] save_pretrained_merge does not work as intended for finetuned Gemma-3-4b model. #3045

Closed

Description

@jasonkhadka

I am following unsloth colab notebook for pretriaining. Followed by save_pretrained_merge and reloading the merged with FastVisionModel.from_pretrained(...). The reloaded model has very different output from the non-merged finetuned model.

https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Gemma3_(4B)-Vision.ipynb#scrollTo=QmUBVEnvCDJv

model, processor = FastVisionModel.from_pretrained(
    # "unsloth/gemma-3-4b-it",
    "unsloth/gemma-3-4b-pt",
    load_in_4bit = False, # Use 4bit to reduce memory use. False for 16bit LoRA.
    use_gradient_checkpointing = "unsloth", # True or "unsloth" for long context
)
# train the model on latex dataset from unsloth notebook
# wrap model to Lora: I am not doing vision FT
model = FastVisionModel.get_peft_model(
    model,
    finetune_vision_layers     = False, # False if not finetuning vision layers
    finetune_language_layers   = True, # False if not finetuning language layers
    finetune_attention_modules = True, # False if not finetuning attention layers
    finetune_mlp_modules       = True, # False if not finetuning MLP layers
    r = 16,                           # The larger, the higher the accuracy, but might overfit
    lora_alpha = 16,                  # Recommended alpha == r at least
    lora_dropout = 0,
    bias = "none",
    random_state = 3407,
    use_rslora = False,               # We support rank stabilized LoRA
    loftq_config = None,               # And LoftQ
    target_modules = "all-linear",    # Optional now! Can specify a list if needed
    modules_to_save=[
        "lm_head",
        "embed_tokens",
    ],
)

FastVisionModel.for_training(model) # Enable for training!
trainer = SFTTrainer(...) 
trainer.train()

# infer and save the model
FastVisionModel.for_inference(model)  # Enable for inference!
out1 = model(**inputs)

# save the merged model, model should be merged and saved in fp16
model.save_pretrained_merged("/tmp/unsloth_finetune", processor,)

# reload the merged model
merged_model, merged_processor = FastVisionModel.from_pretrained(
    "/tmp/unsloth_finetune",
    load_in_4bit = False, # Use 4bit to reduce memory use. False for 16bit LoRA.
    use_gradient_checkpointing = "unsloth", # True or "unsloth" for long context
)

FastVisionModel.for_inference(merged_model)  # Enable for inference!
merged_out = merged_model(**inputs)

print((out1.logits - merged_out.logits).mean())
>>> tensor(-0.1275, device='cuda:0', grad_fn=<MeanBackward0>)

print((out1.logits - merged_out.logits).max())
>> tensor(25.7500, device='cuda:0', grad_fn=<MaxBackward1>)

Unsloth versions:

unsloth                  2025.7.8
unsloth_zoo              2025.7.10

Activity

  1. jasonkhadka commented on Jul 26, 2025

    @jasonkhadka
    Author

    This issue should have been fixed and was discussed before, but apparently not.
    @rolandtannous
    #1670 (comment)

  2. rolandtannous commented on Aug 30, 2025

    @rolandtannous
    Contributor

    we've discussed this at length in a separate discord thread. will turn this into a discussion for now.

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

    unsure bug?

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions