Skip to content

[Bug] Impossible to convert Gemma3 4b into gguf #2399

Closed

Description

@edoproch

I tried to save my fine-tuned gemma3 on HuggingFace in gguf format, I followed the unsloth's notebook but I have the following error when I run model.save_pretrained_merged("gemma-3-finetune", tokenizer)

NotImplementedError: Unsloth: llama.cpp GGUF conversion does not yet support converting model types of Gemma3ForConditionalGeneration.

Suggestions?

PS: same error using model.push_to_hub_gguf

Activity

  1. tw-evan commented on Apr 23, 2025

    @tw-evan

    I have the same issue!

  2. marinewhlae commented on Apr 24, 2025

    @marinewhlae

    I also have the same issues.
    "NotImplementedError: Unsloth: llama.cpp GGUF conversion does not yet support converting model types of Gemma3ForCausalLM."

    It was working until last week, but a few days ago it didn't work.
    I used the unsloth/gemma-3-12-it model.
    I hope it will be solved.

  3. askie commented on Apr 24, 2025

    @askie

    +1

  4. hdiaztuesta commented on Apr 24, 2025

    @hdiaztuesta

    Is there any workaround for this issue?, It was working well at the beginning of April.

    The model:
    unsloth/gemma-3-4b-it

    The error message:
    /usr/local/lib/python3.11/dist-packages/unsloth_zoo/llama_cpp.py in convert_to_gguf(input_folder, output_filename, quantization_type, max_shard_size, print_output, print_outputs) 657 arch = config_file["architectures"][0] 658 if arch not in supported_types:--> 659 raise NotImplementedError( 660 f"Unsloth: llama.cpp GGUF conversion does not yet support "\ 661 f"converting model types of {arch}."NotImplementedError: Unsloth: llama.cpp GGUF conversion does not yet support converting model types of Gemma3ForCausalLM.

    The error occurs when I try to run the following code:
    model.save_pretrained_gguf(
    "gemma-3-finetune",
    quantization_type = "Q8_0",
    )

  5. FlorinAndrei commented on Apr 24, 2025

    @FlorinAndrei

    @hdiaztuesta It could be fallout from this bug:

    unslothai/unsloth-zoo#119

    If that's the case, here's how I made it work again:

    I assume you have the following versions installed:

    unsloth                           2025.3.19
    unsloth_zoo                       2025.3.17
    

    Delete llama.cpp from your local folder where your code is, download llama.cpp version b5137 and install it in your local folder:

    wget https://github.com/ggml-org/llama.cpp/archive/refs/tags/b5137.zip
    unzip b5137.zip
    rm -rf llama.cpp b5137.zip
    mv llama.cpp-b5137 llama.cpp
    cd llama.cpp
    # change this to ON if you have CUDA installed, but it's not mandatory just for fine-tuning
    cmake -B build -DGGML_CUDA=OFF
    cmake --build build --config Release
    ln -s ./build/bin/llama-quantize llama-quantize
    ln -s ./build/bin/llama-gguf-split llama-gguf-split
    cd ..
    

    Create unsloth_zoo.patch with this content:

    diff -u unsloth_zoo.orig/llama_cpp.py unsloth_zoo/llama_cpp.py
    --- unsloth_zoo.orig/llama_cpp.py	2025-03-14 08:02:07.000000000 -0700
    +++ unsloth_zoo/llama_cpp.py	2025-04-24 15:31:20.129628636 -0700
    @@ -32,8 +32,7 @@
     import contextlib
     import os
     
    -LLAMA_CPP_CONVERT_FILE = \
    -    "https://github.com/ggerganov/llama.cpp/raw/refs/heads/master/convert_hf_to_gguf.py"
    +LLAMA_CPP_CONVERT_FILE = "llama.cpp/convert_hf_to_gguf.py"
     
     COMMANDS_NOT_FOUND = (
         "command not found",
    @@ -348,11 +347,12 @@
         # All Unsloth Zoo code licensed under LGPLv3
         # Downloads from llama.cpp's Github repo
         try:
    -        converter_latest = requests.get(LLAMA_CPP_CONVERT_FILE).content
    +        with open(LLAMA_CPP_CONVERT_FILE, 'rb') as f:
    +            converter_latest = f.read()
         except:
             raise RuntimeError(
                 f"Unsloth: Could not obtain `{LLAMA_CPP_CONVERT_FILE}`.\n"\
    -            f"Maybe you don't have internet ocnnection?"
    +            f"Is llama.cpp installed?"
             )
     
         # Get all supported models
    

    Apply the patch:

    patch -p0 -u < unsloth_zoo.patch
    

    Now you have:

    • a patched unsloth_zoo-2025.3.17 that does not do sneaky downloads of external code anymore
    • a version of llama.cpp known to work with unsloth_zoo-2025.3.17

    Restart your Jupyter kernels and try again.

    Do not change or delete the folders llama.cpp and unsloth_zoo. Or, if you do, repeat the process above.

    TLDR: Unsloth relies on external libraries, but it does not check properly when those dependencies change. Freezing the version of the dependency to a value known to work, and preventing the automatic downloads, might fix it.

  6. hdiaztuesta commented on Apr 25, 2025

    @hdiaztuesta

    Dear @FlorinAndrei ,

    Thank you very much for your help. I will follow your instructions and try again.

    Greetings.

  7. tw-evan commented on Apr 25, 2025

    @tw-evan

    Hi, thank you for your help!
    However, after following your suggestions, I encountered a runtime error when executing model.save_pretrained_gguf().

    /bin/sh: 1: python: not found
    Unsloth GGUF:hf-to-gguf:Loading model: gemma-3
    Unsloth GGUF:gguf.gguf_writer:gguf: This GGUF file is for Little Endian only
    Unsloth GGUF:hf-to-gguf:Exporting model...
    Unsloth GGUF:hf-to-gguf:gguf: loading model weight map from 'model.safetensors.index.json'
    Unsloth GGUF:hf-to-gguf:gguf: loading model part 'model-00001-of-00002.safetensors'
    Unsloth GGUF:hf-to-gguf:token_embd.weight,                 torch.bfloat16 --> F16, shape = {2560, 262208}
    Unsloth GGUF:hf-to-gguf:gguf: loading model part 'model-00002-of-00002.safetensors'
    Unsloth GGUF:hf-to-gguf:output_norm.weight,                torch.bfloat16 --> F32, shape = {2560}
    Unsloth GGUF:hf-to-gguf:Set meta model
    Unsloth GGUF:hf-to-gguf:Set model parameters
    Traceback (most recent call last):
      File "/app/llama.cpp/unsloth_convert_hf_to_gguf.py", line 5932, in <module>
        main()
      File "/app/llama.cpp/unsloth_convert_hf_to_gguf.py", line 5926, in main
        model_instance.write()
      File "/app/llama.cpp/unsloth_convert_hf_to_gguf.py", line 3757, in write
        super().write()
      File "/app/llama.cpp/unsloth_convert_hf_to_gguf.py", line 725, in write
        self.prepare_metadata(vocab_only=False)
      File "/app/llama.cpp/unsloth_convert_hf_to_gguf.py", line 715, in prepare_metadata
        self.set_gguf_parameters()
      File "/app/llama.cpp/unsloth_convert_hf_to_gguf.py", line 3783, in set_gguf_parameters
        assert hparams.get("attn_logit_softcapping") is None
    AssertionError
    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    Cell In[26], line 2
          1 if True: # Change to True to save to GGUF
    ----> 2     model.save_pretrained_gguf(
          3         "gemma-3",
          4         quantization_type = "f16", # For now only Q8_0, BF16, F16 supported
          5     )
    
    File /usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py:116, in context_decorator.<locals>.decorate_context(*args, **kwargs)
        113 @functools.wraps(func)
        114 def decorate_context(*args, **kwargs):
        115     with ctx_factory():
    --> 116         return func(*args, **kwargs)
    
    File /usr/local/lib/python3.10/dist-packages/unsloth/save.py:2246, in save_to_gguf_generic(model, save_directory, quantization_type, repo_id, token)
       2243     install_llama_cpp(just_clone_repo = True)
       2244 pass
    -> 2246 metadata = _convert_to_gguf(
       2247     save_directory,
       2248     print_output = True,
       2249     quantization_type = quantization_type,
       2250 )
       2251 if repo_id is not None:
       2252     prepare_saving(
       2253         model,
       2254         repo_id,
       (...)
       2258         token = token,
       2259     )
    
    File /app/unsloth_zoo/llama_cpp.py:692, in convert_to_gguf(input_folder, output_filename, quantization_type, max_shard_size, print_output, print_outputs)
        689 pass
        691 if metadata is None:
    --> 692     raise RuntimeError(f"Unsloth: Failed to convert {conversion_filename} to GGUF.")
        694 printed_metadata = "\n".join(metadata)
        695 if print_output: print(f"Unsloth: Successfully saved GGUF to:\n{printed_metadata}")
    
    RuntimeError: Unsloth: Failed to convert llama.cpp/unsloth_convert_hf_to_gguf.py to GGUF.
    
    
    

    @hdiaztuesta It could be fallout from this bug:

    unslothai/unsloth-zoo#119

    If that's the case, here's how I made it work again:

    I assume you have the following versions installed:我假設您安裝了以下版本:

    unsloth                           2025.3.19
    unsloth_zoo                       2025.3.17
    

    Delete llama.cpp from your local folder where your code is, download llama.cpp version b5137 and install it in your local folder:

    wget https://github.com/ggml-org/llama.cpp/archive/refs/tags/b5137.zip
    unzip b5137.zip
    rm -rf llama.cpp b5137.zip
    mv llama.cpp-b5137 llama.cpp
    cd llama.cpp
    # change this to ON if you have CUDA installed, but it's not mandatory just for fine-tuning
    cmake -B build -DGGML_CUDA=OFF
    cmake --build build --config Release
    ln -s ./build/bin/llama-quantize llama-quantize
    ln -s ./build/bin/llama-gguf-split llama-gguf-split
    cd ..
    

    Download unsloth_zoo version 2025.3.17 and install it in your local folder. Your Python code will automatically import that instead of the installed unsloth_zoo Python module:

    wget https://files.pythonhosted.org/packages/40/18/bee4d2e93a567fcbd30719a8b2a627eb451f8125db195b5de0f822263888/unsloth_zoo-2025.3.17.tar.gz
    tar -zxf unsloth_zoo-2025.3.17.tar.gz
    rm -rf unsloth_zoo unsloth_zoo-2025.3.17.tar.gz
    mv unsloth_zoo-2025.3.17/unsloth_zoo .
    rm -rf unsloth_zoo-2025.3.17
    

    Create unsloth_zoo.patch with this content:

    diff -u unsloth_zoo.orig/llama_cpp.py unsloth_zoo/llama_cpp.py
    --- unsloth_zoo.orig/llama_cpp.py	2025-03-14 08:02:07.000000000 -0700
    +++ unsloth_zoo/llama_cpp.py	2025-04-24 15:31:20.129628636 -0700
    @@ -32,8 +32,7 @@
     import contextlib
     import os
     
    -LLAMA_CPP_CONVERT_FILE = \
    -    "https://github.com/ggerganov/llama.cpp/raw/refs/heads/master/convert_hf_to_gguf.py"
    +LLAMA_CPP_CONVERT_FILE = "llama.cpp/convert_hf_to_gguf.py"
     
     COMMANDS_NOT_FOUND = (
         "command not found",
    @@ -348,11 +347,12 @@
         # All Unsloth Zoo code licensed under LGPLv3
         # Downloads from llama.cpp's Github repo
         try:
    -        converter_latest = requests.get(LLAMA_CPP_CONVERT_FILE).content
    +        with open(LLAMA_CPP_CONVERT_FILE, 'rb') as f:
    +            converter_latest = f.read()
         except:
             raise RuntimeError(
                 f"Unsloth: Could not obtain `{LLAMA_CPP_CONVERT_FILE}`.\n"\
    -            f"Maybe you don't have internet ocnnection?"
    +            f"Is llama.cpp installed?"
             )
     
         # Get all supported models
    

    Apply the patch:  

    patch -p0 -u < unsloth_zoo.patch
    

    Now you have:  

    • a patched unsloth_zoo-2025.3.17 that does not do sneaky downloads of external code anymore
    • a version of llama.cpp known to work with unsloth_zoo-2025.3.17

    Restart your Jupyter kernels and try again.

    Do not change or delete the folders llama.cpp and unsloth_zoo. Or, if you do, repeat the process above.

    TLDR: Unsloth relies on external libraries, but it does not check properly when those dependencies change. Freezing the version of the dependency to a value known to work, and preventing the automatic downloads, might fix it.TLDR:

  8. FlorinAndrei commented on Apr 25, 2025

    @FlorinAndrei

    @tw-evan I think model.save_pretrained_gguf() is a bit picky with the values. You put f16 in there, but it only recognizes all-caps values, like F16.

    BTW, you probably want BF16 in there instead of plain F16, unless you have a specific requirement for the format that I'm not aware of.

  9. rolandtannous commented on Apr 26, 2025

    @rolandtannous
    Contributor

    @tw-evan [@hdiaztuesta Do not apply patches or follow instructions not coming from maintainers or contributors especially ones that encourage you to download an unverified version of Unsloth and Unsloth-zoo from sources not approved by the maintainers. You do this at your own peril. You risk and you will break your installation.

    Root of the problem

    This is unrelated to #issue 119. Issue #119 is about unsloth not working with the latest version of the llama.cpp (a fix will be released soon), while in here, and in contradiction to issue #119, you're being told to use a much older version of llama.cpp . Please don't do that.

    The issue is not with unsloth being broken. Unsloth used llama.cpp for conversion to the GGUF format. Ollama being a wrapper around llama.cpp, was compatible with models converted using llama.cpp up and until Ollama version 0.6.2. After that version, the folks at Ollama seem to have made breaking changes. For VLMs such as gemma3, ollama now expects the visual projector file (mmproj) and the text model file (model) to be fused in a single file while llama.cpp still separates the two files. Look here for reference: https://github.com/ggml-org/llama.cpp/blob/master/docs/multimodal/gemma3.md

    Small experiment (if you're curious)

    You can manually test this by:

    • downloading the latest version of convert_hf_to_gguf.py
    • run the following sequence of instructions in the folder containing the model you wish to convert:
    python ../llama.cpp/convert_hf_to_gguf.py --outfile model.gguf --outtype f16 .
    

    This will create a model.gguf file
    Also run:

    python ../llama.cpp/convert_hf_to_gguf.py --outfile model.gguf --outtype f16 --mmproj .
    

    This will create a mmproj-model.gguf file.

    To run gemma3 with vision capabilities in llama.cpp:
    1- Make sure you build the latest version of llama.cpp
    2- run:

    llama-mtmd-cli -m model.gguf --mmproj mmproj.gguf --image your_image.jpg
    

    llama-mtmd-cli should be in the build/bin subfolder of your llama.cpp installation folder
    model.gguf and mm-proj.gguf are the result of the conversion steps above.

    You will notice that this will make gemma3 vision capabilities work with llama.cpp

    Now try to run the same model with ollama. It won't work for the reasons I explained in the previous section.

    Moving Forward

    1- We are releasing a fix to make unsloth play nice with the breaking changes introduced in the latest llama.cpp conversion script. This should make any model you convert using _save_to_gguf() work with llama.cpp
    2- Ollama issue is being worked on separately and a fix will be released afterwards.

    Also a general reminder to adhere to the github code of conduct.

  10. shimmyshimmer commented on Apr 27, 2025

    @shimmyshimmer
    Member

    Hi @FlorinAndrei, we really appreciate you helping out! That said, it's generally not good practice to recommend users install patches from sources outside of the official Unsloth releases.

    It would have been better to suggest the previous Unsloth version or suggest code changes instead. Recommending an external package or link can be risky, even if the source seems trustworthy.

    Of course, I'm not saying the link you shared was dangerous - it's just a matter of maintaining standard safety practices. Thanks again for your support!

  11. anti-oksidan commented on Apr 30, 2025

    @anti-oksidan

    İnce ayarlı gemma3'ümü HuggingFace'te gguf formatında kaydetmeye çalıştım, unsloth'un not defterini takip ettim ancak model.save_pretrained_merged("gemma-3-finetune", tokenizer) çalıştırdığımda aşağıdaki hatayı alıyorum.

    NotImplementedError: Unsloth: llama.cpp GGUF dönüştürmesi henüz 'nin model türlerinin dönüştürülmesini desteklemiyor.Gemma3ForConditionalGeneration

    Öneri?

    Not: model.push_to_hub_gguf kullanırken aynı hata

    Hello, I also encountered this kind of problem and tried a different method for GGUF conversion as a solution, I hope it works for you too.

    !git clone https://github.com/ggml-org/llama.cpp.git
    !python ./llama.cpp/convert_hf_to_gguf.py ./gemma_merged_folder --outfile ./gemma3-4b-it.gguf --outtype q8_0

    You can try it from the Colab address below.

    https://colab.research.google.com/drive/1GWC0Tym3b-Fcr3IMtlcwNdKkp79mzMW1?usp=sharing

  12. tw-evan commented on May 1, 2025

    @tw-evan

    Thank you all for your help, but I'm still running into an error.

    model.save_pretrained_merged("gemma-3_merged", tokenizer)
    !git clone  https://github.com/ggml-org/llama.cpp.git
    !python ./llama.cpp/convert_hf_to_gguf.py ./gemma-3_merged  --outfile ./gemma3-4b-it_q8_0.gguf  --outtype q8_0
    
    INFO:hf-to-gguf:Loading model: gemma-3_merged
    INFO:hf-to-gguf:Model architecture: Gemma3ForCausalLM
    INFO:gguf.gguf_writer:gguf: This GGUF file is for Little Endian only
    INFO:hf-to-gguf:Exporting model...
    INFO:hf-to-gguf:gguf: loading model weight map from 'model.safetensors.index.json'
    INFO:hf-to-gguf:gguf: loading model part 'model-00001-of-00002.safetensors'
    INFO:hf-to-gguf:token_embd.weight,                 torch.bfloat16 --> Q8_0, shape = {2560, 262208}
    INFO:hf-to-gguf:blk.0.attn_norm.weight,            torch.bfloat16 --> F32, shape = {2560}
    ...
    INFO:hf-to-gguf:output_norm.weight,                torch.bfloat16 --> F32, shape = {2560}
    INFO:hf-to-gguf:Set meta model
    INFO:hf-to-gguf:Set model parameters
    Traceback (most recent call last):
      File "/content/./llama.cpp/convert_hf_to_gguf.py", line 5957, in <module>
        main()
      File "/content/./llama.cpp/convert_hf_to_gguf.py", line 5951, in main
        model_instance.write()
      File "/content/./llama.cpp/convert_hf_to_gguf.py", line 402, in write
        self.prepare_metadata(vocab_only=False)
      File "/content/./llama.cpp/convert_hf_to_gguf.py", line 477, in prepare_metadata
        super().prepare_metadata(vocab_only=vocab_only)
      File "/content/./llama.cpp/convert_hf_to_gguf.py", line 392, in prepare_metadata
        self.set_gguf_parameters()
      File "/content/./llama.cpp/convert_hf_to_gguf.py", line 3733, in set_gguf_parameters
        assert hparams.get("attn_logit_softcapping") is None
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError
    
  13. Elreniel commented on May 1, 2025

    @Elreniel

    I have finetuned gemma-4b and 12b. I can convert 4b to gguf with no issue. However, for 12b, I got the following error even though the environment, code is same.

    NotImplementedError: Unsloth: llama.cpp GGUF conversion does not yet support converting model types of Gemma3ForCausalLM.

    Note: I already tried all the possible quantization_methods.

  14. rolandtannous commented on May 16, 2025

    @rolandtannous
    Contributor

    @Elreniel llama.cpp GGUF should now support Gemma3ForCausalLM. try again please

  15. greenirvavril commented on Jun 4, 2025

    @greenirvavril

    hello, I still get Unsloth: llama.cpp GGUF conversion does not yet support converting model types of Gemma3ForCausalLM

    I tried with Gemma3 4b and 12b

  16. rolandtannous commented on Jun 4, 2025

    @rolandtannous
    Contributor

    @greenirvavril i just started working on all gguf conversion issues. this should be resolved soon. I will notify you guys when the PR is merged.

  17. Elreniel commented on Jun 4, 2025

    @Elreniel

    Hey everyone,

    I forgot to write it here, I have found my own way to handle this issue.

    First problem was, when the storage is not enough, the save_pretrained_merged function throws an error where the log says completely different thing. I found out that issue by debugging the source code of unsloth.

    Secondly, it is possible to convert that save finetuned model to gguf file by calling convert_to_gguf.py file in lama.cpp directly from cli.

  18. rolandtannous commented on Jun 4, 2025

    @rolandtannous
    Contributor

    @Elreniel you should be able to convert manually by cli . they recently updated the conversion script so the syntax might have changed and the docs are sort of hidden but conversion actually works.

  19. Elreniel commented on Jun 4, 2025

    @Elreniel

    Yes, yes all of my pipeline for all gemma models is working for now. I can finetune, convert to gguf and deploy it with ollama succesfully. Since its working with older version, i prefer not to touch it :D

  20. github-actions commented on Jul 1, 2025

    @github-actions
    Contributor

    Is this issue still important to you? Apologies in advance we might have missed this issue as well. For faster response times, please post on our Reddit server - https://www.reddit.com/r/unsloth or our Discord - https://discord.com/invite/unsloth

  21. rolandtannous commented on Oct 23, 2025

    @rolandtannous
    Contributor

    @greenirvavril @Elreniel @tw-evan @aydndglr @hdiaztuesta @askie @marinewhlae

    We just pushed a major fix for llamacpp integration.

    1- Please make sure to update your local install of unsloth

    pip install --force-reinstall --no-deps unsloth-zoo unsloth
    

    If you're using a notebook on kaggle or colab, it should pull the latest version automatically.

    2- You can now use save_pretrained_gguf to quantize and save to GGUF format:

    model.save_pretrained_gguf(
                save_directory,
                tokenizer=tokenizer,
                quantization_method=quantization_method,
            )
    

    For vision/multimodal models, use tokenizer=processor

    3- You can pass it one or several quant methods (as a list):

    quantization_method = "q8_0"
    

    or

    quantization_method = ["q8_0", "q4_k_m"]
    

    We support all the quants mentioned in this page except the iq variants.

    4- If the model is supported by Ollama, it will now also automatically create a Modelfile for you that you can use to create your model in ollama using:

    ollama create model_name -f ./Modelfile
    

    5- Use push_to_hub_gguf if you want to convert to gguf and push to Huggingface Hub instead.

    6- Should work for all models supported by unsloth and ollama.

    7- It now fully supports linux debian and RPM variants. We are still testing windows (if someone has a windows machine and is on our discord channel ping me to help debug)

    Just tested phi-4 and it worked nicely with llama.cpp and ollama.

    Let me know how it goes for you.

    Will be closing this for now as the issue has been resolved. Please make sure to follow the instructions given in my last response.

    Meanwhile , please feel free to respond if you're still having any issues.

    Thank you

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

Metadata

Metadata

Assignees

Labels

bugfixed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions