Skip to content

GGUF llama.cpp direct conversion - BETA testers wanted! #71

Closed

Description

@danielhanchen

Previously when one trains with either HF's TRL library or Unsloth, converting the model to GGUF / llama.cpp was a nightmare.

Either the following strategies were used:

  1. Save PEFT adapters and upload them to HF. Use llama.cpp to combine a GGUF version of Mistral / Llama and combine LoRA via convert-lora-to-ggml.py then quantize. Accuracy is medium, since bitsandbytes's quantization method is different to GGUF. VRAM usage is great.
  2. Train with load_in_4bit = False to use 16bit, then use model.save_pretrained and using llama.cpp then quantize. Accuracy is superb, since 16bit retains full accuracy, but VRAM usage for llama-7b goes to 14.5GB, and Mistral OOMs.
  3. Merge PEFT adapters back using model.merge_and_unload() then converting each layer to 16bit then step 2. Accuracy is low, since conversion to 4bit then upcasting to 16bit loses lots of bits.
  4. [NEW] Unsloth: Combines best of both worlds - train with QLoRA (low memory), then we upcast to 16bit, but on the fly, using mmap. Accuracy is high as well.

A table comparing all strategies:

Strategy VRAM Usage Disk Usage Accuracy
1. PEFT Low Low Medium
2. 16bit Horrible High High
3. Merge Horrible High Low
4. Unsloth Low High High

To use Unsloth:

from unsloth import unsloth_save_model
# unsloth_save_model has the same args as model.save_pretrained
unsloth_save_model(model, tokenizer, "output_model", push_to_hub = False, token = None)
colab_quantize_to_gguf("output_model", quantization_method = "q4_k_m")

I'm looking for beta testers to test out this Colab notebook! It finetunes Mistral and saves to GGUF: https://colab.research.google.com/drive/14DW0VwuqL2O3tqGlX7aUF6TOBA8S59M4?usp=sharing

It would be great it anyone tested it to check the perplexity / whether my conversions are correct! Thanks!

Activity

  1. pinned this issue on Jan 8, 2024
  2. x4080 commented on Jan 9, 2024

    @x4080

    It seems to be a better workflow from a glance at it, great job

  3. danielhanchen commented on Jan 10, 2024

    @danielhanchen
    MemberAuthor

    @x4080 Thanks! Hope it works!

  4. x4080 commented on Jan 12, 2024

    @x4080

    Hi, just tried to upload to hf and got this error

    Unsloth: `unsloth_save_model` is still in development mode.
    If anything errors or breaks, please file a ticket on Github.
    Also, if you used this successfully, please tell us on Discord!
    Unsloth: Merging 4bit and LoRA weights to 16bit...
    100%|██████████| 32/32 [01:12<00:00,  2.26s/it]
    Unsloth: Saving tokenizer...
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    [<ipython-input-11-164e96edda3b>](https://localhost:8080/#) in <cell line: 4>()
          2 
          3 # unsloth_save_model has the same args as model.save_pretrained
    ----> 4 unsloth_save_model(model, tokenizer, "model_combinedunsloth", push_to_hub = True, token = None)
          5 # colab_quantize_to_gguf("output_model", quantization_method = "q4_k_m")
    
    2 frames
    [/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py](https://localhost:8080/#) in save_pretrained(self, save_directory, legacy_format, filename_prefix, push_to_hub, **kwargs)
       2364             commit_message = kwargs.pop("commit_message", None)
       2365             repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1])
    -> 2366             repo_id = self._create_repo(repo_id, **kwargs)
       2367             files_timestamps = self._get_files_timestamps(save_directory)
       2368 
    
    TypeError: PushToHubMixin._create_repo() got an unexpected keyword argument 'is_main_process'
    

    is there a new colab version ?

    Thanks

  5. danielhanchen commented on Jan 12, 2024

    @danielhanchen
    MemberAuthor

    @x4080 OH YYEPPP!! I remember someone else reported this - I'm working on a fix!!!

  6. dsbyprateekg commented on Jan 12, 2024

    @dsbyprateekg

    @danielhanchen I faced following error-
    image

    While I ran the following code-
    image

    It is unable to save locally.

  7. danielhanchen commented on Jan 18, 2024

    @danielhanchen
    MemberAuthor

    @dsbyprateekg @x4080 Sorry for the delay! Just pushed a new version - you can now save to GGUF directly with model.save_pretrained_merged(..., tokenizer = tokenizer) or model.push_to_hub_merged. The Mistral / Llama notebooks got updated!

  8. x4080 commented on Jan 18, 2024

    @x4080
  9. unpinned this issue on Jan 20, 2024
  10. added a commit that references this issue on Jun 8, 2025
  11. added a commit that references this issue on Jun 25, 2025
  12. added a commit that references this issue on Jul 7, 2025
  13. added a commit that references this issue on Mar 12, 2026
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