Skip to content

Gemma2b merging LoRA doesn't work properly on new tokens. #1971

Closed

Description

@Serzhanov

Hello,
First of all great work on Unsloth.

I fine-tuned Gemma 2B (unsloth/gemma-2b-bnb-4bit) on new tokens.

My checkpoint works fine, but when I try to merge it to 16-bit or 4-bit, it doesn't recognize the newly added tokens. However, the general output still aligns with the LoRA model.

I know that Gemma has tied embeddings, so the PEFT model creates two separate weight matrices:

  • One for embeddings
  • One for lm_head
    These weights are different during fine-tuning. However, after merging, they become tied (i.e., they share the same data pointer). I suspect that after merging, my lm_head weight matrix no longer exists, which is causing the issue.

my code of peft model

model = FastLanguageModel.get_peft_model(
model,
    r = 16,
    target_modules = ["embed_tokens","q_proj", "k_proj", "v_proj", "o_proj",
                       "up_proj",'lm_head', "down_proj"],
    modules_to_save=["embed_tokens",'lm_head'],
    lora_alpha = 64,
    lora_dropout = 0, 
    bias = "none",    
    use_gradient_checkpointing = "unsloth",
    random_state = 3407
)

I need the lm_head because it gvies me better performance.

Thanks for help.

Activity

  1. FloLey commented on Mar 14, 2025

    @FloLey

    I think this is the same issue as all those:
    #1670
    #1763
    #1877
    #1929

  2. shimmyshimmer commented on Mar 18, 2025

    @shimmyshimmer
    Member

    Is this for GRPO or purely Lora?

  3. Serzhanov commented on Mar 18, 2025

    @Serzhanov
    Author

    @shimmyshimmer purely Lora.I also tried to patch Lora model lm_head into lm_head's merged model 16bit and 4 bit and it works.
    But it's like using the Lora model without any merge.

  4. Serzhanov commented on Mar 18, 2025

    @Serzhanov
    Author

    Also this solution is not satisfied because Gemma's embeddings kind huge.

  5. daegonYu commented on Apr 14, 2025

    @daegonYu

    I am also training LoRA using the unsloth/gemma2-9b-it model, and when I resume training, I found that the log does not continue the checkpoint reward.

    Image

  6. rolandtannous commented on Jun 6, 2025

    @rolandtannous
    Contributor

    @FloLey @Serzhanov @daegonYu

    A fix has now been merged into pypi for both vision and language models and extensively tested.
    Should in principle also solve your problem here unless this is caused by something else.
    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.

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