Skip to content

[Bug] GGUF not working [in my case works, but needs fixing] #3215

Closed

Description

@kittawere
  1. Did you update? pip install --upgrade unsloth unsloth_zoo
    Yes

  2. Colab or Kaggle or local / cloud
    Local (debian LXC)

  3. Number GPUs used, use nvidia-smi
    4070 - not relevant

  4. Which notebook? Please link!
    None, my own code

  5. Which Unsloth version, TRL version, transformers version, PyTorch version?
    unsloth==2025.8.9
    unsloth-zoo==2025.8.8

  6. Which trainer? SFTTrainer, GRPOTrainer etc
    The problem is not on training

if not os.path.isdir('llama.cpp'):
    subprocess.run(f"apt install libcurl4-openssl-dev -y && git clone --recursive https://github.com/ggerganov/llama.cpp && cd llama.cpp && cmake -B build -DGGML_CUDA=ON && cmake --build build --config Release -j {os.cpu_count()} && cd .. ", shell=True, executable="/bin/bash")


model, tokenizer = FastLanguageModel.from_pretrained(
        model_name=MODEL_SAVE_PATH,
        load_in_4bit=load_in_4bit
    )

    # Quantization types
    q_types = ["f16","q8_0","q4_k_m","q4_0"]

    # Save model with different quantizations
    for q_type in q_types:
        save_path = f"repos/{MODEL_SAVE_PATH}-GGUF"
        print(f"Saving GGUF with quantization {q_type} to {save_path}...")
        model.save_pretrained_gguf(save_path, tokenizer, quantization_method=q_type)
        #subprocess.run(f"cd llama.cpp && git checkout .  ", shell=True, executable="/bin/bash") # hack since it does an error

Now that I have complete that, here is my issue

Ok, so when I run that code, I got this error:

This might take 3 minutes...
  File "/root/unsloth_master/llama.cpp/convert_hf_to_gguf.py", line 34
    from mistral_common.tokens.tokenizers.base import TokenizerVersion
    ^
IndentationError: expected an indented block after 'try' statement on line 33
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/root/unsloth_master/helpers/unsloth_quant1.py", line 93, in <module>
    main()
  File "/root/unsloth_master/helpers/unsloth_quant1.py", line 39, in main
    model.save_pretrained_gguf(save_path, tokenizer, quantization_method=q_type)
  File "/root/unsloth_master/venv/lib/python3.11/site-packages/unsloth/save.py", line 1898, in unsloth_save_pretrained_gguf
    all_file_locations, want_full_precision = save_to_gguf(
                                              ^^^^^^^^^^^^^
  File "/root/unsloth_master/venv/lib/python3.11/site-packages/unsloth/save.py", line 1255, in save_to_gguf
    raise RuntimeError(
RuntimeError: Unsloth: Quantization failed for /root/unsloth_master/repos/kittawere/test-GGUF/unsloth.Q8_0.gguf
You might have to compile llama.cpp yourself, then run this again.
You do not need to close this Python program. Run the following commands in a new terminal:
You must run this in the same folder as you're saving your model.
git clone --recursive https://github.com/ggerganov/llama.cpp
cd llama.cpp && make clean && make all -j
Once that's done, redo the quantization.

What I have observed:

1º It does not import mistral_common.tokens.tokenizers.base

I have not proof but I think it fails since it is modifying llama.cpp/convert_hf_to_gguf.py from:

from gguf.vocab import MistralTokenizerType, MistralVocab
from mistral_common.tokens.tokenizers.base import TokenizerVersion
from mistral_common.tokens.tokenizers.multimodal import DATASET_MEAN, DATASET_STD
from mistral_common.tokens.tokenizers.tekken import Tekkenizer
from mistral_common.tokens.tokenizers.sentencepiece import (
    SentencePieceTokenizer,
)

to

from gguf.vocab import MistralTokenizerType, MistralVocab
try:
    from mistral_common.tokens.tokenizers.base import TokenizerVersion
except:
    pass
try:
    from mistral_common.tokens.tokenizers.multimodal import DATASET_MEAN, DATASET_STD
except:
    pass
try:
    from mistral_common.tokens.tokenizers.tekken import Tekkenizer
except:
    pass
try:
    from mistral_common.tokens.tokenizers.sentencepiece import (
    SentencePieceTokenizer,
)
except:
    pass

2º It performs the GGUF transformation of F16

It works well :)

3º It trys to do the GGUF to Q_8

It fails IDK were and fallback to adding the "try excepts"

So it goes from:

from gguf.vocab import MistralTokenizerType, MistralVocab
try:
    from mistral_common.tokens.tokenizers.base import TokenizerVersion
except:
    pass
try:
    from mistral_common.tokens.tokenizers.multimodal import DATASET_MEAN, DATASET_STD
except:
    pass
try:
    from mistral_common.tokens.tokenizers.tekken import Tekkenizer
except:
    pass
try:
    from mistral_common.tokens.tokenizers.sentencepiece import (
    SentencePieceTokenizer,
)
except:
    pass

to:

from gguf.vocab import MistralTokenizerType, MistralVocab
try:
    try:
    from mistral_common.tokens.tokenizers.base import TokenizerVersion
except:
    pass
except:
    pass
try:
    try:
    from mistral_common.tokens.tokenizers.multimodal import DATASET_MEAN, DATASET_STD
except:
    pass
except:
    pass
try:
    try:
    from mistral_common.tokens.tokenizers.tekken import Tekkenizer
except:
    pass
except:
    pass
try:
    try:
    from mistral_common.tokens.tokenizers.sentencepiece import (
    SentencePieceTokenizer,
)
except:
    pass

except:
    pass

4º It fails from IndentationError

As seen above :(

IDK what is happening, hope someone see this useful

BTW I don't care (just writing this in case someone find it useful (or the devs fix this "try except" madness 😹 )), in my case if I add subprocess.run(f"cd llama.cpp && git checkout . ", shell=True, executable="/bin/bash") in the loop it does not give any errors, and everything goes well 😸

Activity

  1. Datta0 commented on Aug 26, 2025

    @Datta0
    Contributor

    Hey @kittawere
    I just tried running the conversion part and the indentation looks fine to me. Did you modify anything by chance?

  2. kittawere commented on Aug 26, 2025

    @kittawere
    Author

    Nope, the only out of thing out the ordinary was that I used uv instead of pip

    Maybe is this :V

    I will try and report

  3. kittawere commented on Aug 26, 2025

    @kittawere
    Author

    No, same problem

  4. shinypipibear commented on Sep 3, 2025

    @shinypipibear

    The same error happened to me, didn't modify anything :(

  5. dasmikko commented on Sep 4, 2025

    @dasmikko

    I'm experiencing the same issue. Super annoying.

  6. rememberSoftwares commented on Sep 8, 2025

    @rememberSoftwares

    Same.

    model.save_pretrained_gguf("gguf_model_scratch_fixed", tokenizer, quantization_method="q5_k_m", maximum_memory_usage = 0.3)
    
    Unsloth: Merging 4bit and LoRA weights to 16bit...
    Unsloth: Will use up to 0.75 out of 12.67 RAM for saving.
    Unsloth: Saving model... This might take 5 minutes ...
    100%|██████████| 32/32 [02:25<00:00,  4.55s/it]
    Unsloth: Saving tokenizer... Done.
    Unsloth: Saving gguf_model_scratch_fixed/pytorch_model.bin...
    Done.
    Unsloth: Extending gguf_model_scratch_fixed/tokenizer.model with added_tokens.json.
    Originally tokenizer.model is of size (32000).
    But we need to extend to sentencepiece vocab size (32011).
    ==((====))==  Unsloth: Conversion from QLoRA to GGUF information
       \\   /|    [0] Installing llama.cpp might take 3 minutes.
    O^O/ \_/ \    [1] Converting HF to GGUF 16bits might take 3 minutes.
    \        /    [2] Converting GGUF 16bits to ['q5_k_m'] might take 10 minutes each.
     "-____-"     In total, you will have to wait at least 16 minutes.
    
    Unsloth: Installing llama.cpp. This might take 3 minutes...
    Unsloth: [1] Converting model at gguf_model_scratch_fixed into f16 GGUF format.
    The output location will be /content/gguf_model_scratch_fixed/unsloth.F16.gguf
    This might take 3 minutes...
      File "/content/llama.cpp/convert_hf_to_gguf.py", line 34
        try:
        ^^^
    IndentationError: expected an indented block after 'try' statement on line 33
    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    [/tmp/ipython-input-22881174.py](https://localhost:8080/#) in <cell line: 0>()
    ----> 1 model.save_pretrained_gguf("gguf_model_scratch_fixed", tokenizer, quantization_method="q5_k_m", maximum_memory_usage = 0.3)
    
    1 frames
    [/usr/local/lib/python3.12/dist-packages/unsloth/save.py](https://localhost:8080/#) in save_to_gguf(model_type, model_dtype, is_sentencepiece, model_directory, quantization_method, first_conversion, _run_installer)
       1254                 )
       1255         else:
    -> 1256             raise RuntimeError(
       1257                 f"Unsloth: Quantization failed for {final_location}\n"\
       1258                 "You might have to compile llama.cpp yourself, then run this again.\n"\
    
    RuntimeError: Unsloth: Quantization failed for /content/gguf_model_scratch_fixed/unsloth.F16.gguf
    You might have to compile llama.cpp yourself, then run this again.
    You do not need to close this Python program. Run the following commands in a new terminal:
    You must run this in the same folder as you're saving your model.
    git clone --recursive https://github.com/ggerganov/llama.cpp
    cd llama.cpp && make clean && make all -j
    Once that's done, redo the quantization.
    
  7. MathieuBosse commented on Sep 10, 2025

    @MathieuBosse

    Still experiencing the same problem with the indentation block after try

  8. rolandtannous commented on Oct 14, 2025

    @rolandtannous
    Contributor

    @MathieuBosse @dasmikko @kittawere @rememberSoftwares @shinypipibear

    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)

    Have fun

  9. kittawere commented on Oct 21, 2025

    @kittawere
    Author

    Ok managed to Install unsloth on conda,

    🥳

    Can confirm this issue is fix

    This also breaks my pipeline but technically is fixed 🥸

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

    bugfixed - pending confirmation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions