Skip to content

convert : handle pre-quantized models - #14810

Merged
compilade merged 6 commits into
masterfrom
compilade/convert-prequant
Oct 23, 2025
Merged

convert : handle pre-quantized models#14810
compilade merged 6 commits into
masterfrom
compilade/convert-prequant

Conversation

@compilade

Copy link
Copy Markdown
Collaborator

Should fix #14762, and also address #3353.

This roughly implements the idea in #14762 (comment) to allow converting from pre-quantized models, by splitting ModelBase.get_tensors into an intermediate ModelBase.index_tensors which returns a dict[str, Callable[[], Tensor]], which can be modified before get_tensors is called. get_tensors still keeps the same signature (it still returns an Iterator[tuple[str, Tensor]]).

For now, support for these pre-quantizations has been implemented:

The 3-bit variant of GPTQ is more complicated, and so was omitted for now.

Notes

TODO

  • Test if this causes memory usage regressions
    • Lazy or not, safetensors or not
    • So far it seems good.
  • Test remote conversion (with --remote)

Make sure to read the contributing guidelines before submitting a PR

@compilade compilade added enhancement New feature or request python python script changes labels Jul 22, 2025

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

In case you feel like it, add support for MXFP4 as well. I will be upstreaming a ggml implementation soon and it would be nice to have HF conversion support. You can use some of the smaller models here https://huggingface.co/models?sort=created&search=mxfp4 (anyone without hadamard matrices should work).

@CISC

CISC commented Jul 22, 2025

Copy link
Copy Markdown
Member

In case you were wondering, the workaround was about this line:
https://huggingface.co/WisdomShell/CodeShell-7B-Chat/blob/main/pytorch_model.bin.index.json#L555

Conversion would fail because that tensor doesn't exist.

Edit: I might have used the safetensors version, could be it actually works with pytorch bins.

@CISC

CISC commented Aug 8, 2025

Copy link
Copy Markdown
Member

MLX support could be useful, they have .scales and .biases tensors:
https://ml-explore.github.io/mlx/build/html/python/_autosummary/mlx.core.dequantize.html

Edit: group_size and bits are stored in config.json (not sure why there are two entries):

    "quantization": {
        "group_size": 32,
        "bits": 5
    },
    "quantization_config": {
        "group_size": 32,
        "bits": 5
    },

@MaoJianwei

Copy link
Copy Markdown

Is it ready to be merged? We need this :)

#15173

@compilade

compilade commented Aug 9, 2025

Copy link
Copy Markdown
Collaborator Author

Is it ready to be merged? We need this :)

It kind of is, but I wanted to make it more general, and also repack instead of going through F32 and requantize. I've described this in #15111 (comment), but the approach here isn't compatible with how MXFP4 is handled for gpt-oss. I mean, whenever the mxfp4 quant method is handled, it will break the repacking which the gpt-oss conversion relies on.

I guess I can change this in a follow-up PR, since mxfp4 isn't handled here yet.

I have started working on a deferred repacking system to transparently handle transformations of pre-quantized tensors. (it's a bit more involved than I thought, so I guess it makes sense to leave it for a follow-up pull-request)
It's going to be a bit like LoraTorchTensor from convert_lora_to_gguf.py, but for pre-quantized tensors, and with a repacking API for types gguf-py/gguf/quants.py can currently quantize. It should allow avoiding to go through F32 when possible, which means it will be possible to handle mxfp4 and also ternary models more satisfyingly (pre-quantized tensors will also keep a compatible type when using --outtype auto in what I'm planning).

So this can be considered ready to be merged (assuming this doesn't break remote conversion), and a follow-up pull-request will handle repacking (instead of requantizing).

@compilade
compilade marked this pull request as ready for review August 9, 2025 04:19
@MaoJianwei

Copy link
Copy Markdown

So this can be considered ready to be merged (assuming this doesn't break remote conversion), and a follow-up pull-request will handle repacking (instead of requantizing).

It sounds good :)

@BugBusterMax

Copy link
Copy Markdown

How can I convert llama3.2_1b after GPTQ to gguf?

@CISC

CISC commented Aug 10, 2025

Copy link
Copy Markdown
Member

@compilade Preferably #14737 should be merged first, then you can rebase this.

@CISC

CISC commented Aug 16, 2025

Copy link
Copy Markdown
Member

@compilade Merge now?

@MaoJianwei

Copy link
Copy Markdown

when to merge?

@Green-Sky

Copy link
Copy Markdown
Collaborator

I think this is ready, but it has been stuck in unmerged limbo for a while now. @ggerganov please merge/rereview.

@CISC

CISC commented Sep 23, 2025

Copy link
Copy Markdown
Member

It is approved, but should be merged by @compilade esp. as there are some interdependent PRs.

@ggerganov

Copy link
Copy Markdown
Member

@compilade Checking in to see if you plan to bring this to completion. If you don't have time - no worries. We'll try to take over and finish it. Just give us some indication about your plans.

@compilade

compilade commented Oct 23, 2025

Copy link
Copy Markdown
Collaborator Author

@compilade Checking in to see if you plan to bring this to completion. If you don't have time - no worries. We'll try to take over and finish it. Just give us some indication about your plans.

I do plan to finish this. Sorry for the delay (@ggerganov, @CISC, @MaoJianwei, @Green-Sky).

My plans are still somewhat as before in #14810 (comment), but this doesn't necessarily block merging this. After merging this, I plan to finalize #15667 and #15727 next.

I will merge this today at 20:30UTC (in around 1h30 after this message is sent), so that I can test for regressions in the meantime (in case some were introduced in recent merges), although this PR apparently has been already used a bunch for recent-ish FP8 models.

The most noticeable difference between this PR and master (apart from dequantizing pre-quantized models) is when the source tensor files are enumerated. Before this PR, they were interleaved with tensor transformations, but now they are all indexed up-front. This shouldn't affect conversion speed, I think.

Again, sorry for not merging this earlier; I have a tendency to avoid merging right before going to sleep, and in recent months my availability times didn't really align with that approach (but today it's earlier).

I'll try to be clearer in the future when I don't merge for similar reasons, so that merge limbo doesn't last as long.

@compilade
compilade merged commit f8f071f into master Oct 23, 2025
11 checks passed
@julmb

julmb commented Oct 24, 2025

Copy link
Copy Markdown

Is there an issue or a PR that I can track to see when mxfp4 will be available with the new conversion method?

@MaoJianwei

Copy link
Copy Markdown

Congratulations!

blime4 referenced this pull request in blime4/llama.cpp Feb 5, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
fewtarius pushed a commit to fewtarius/CachyLLama-archived that referenced this pull request May 30, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* convert : begin handling pre-quantized models

* convert : fix conversion from FP8 for Deepseek-V3.1-Base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request python python script changes

Projects

None yet

7 participants