Skip to content

Add Q4_K_M GGUF→ONNX conversion support - #399

Merged
justinchuby merged 1 commit into
mainfrom
feat/gguf-q4km-support
Jul 13, 2026
Merged

justinchuby merged 1 commit into
mainfrom
feat/gguf-q4km-support

Conversation

@justinchuby

Copy link
Copy Markdown
Member

build-gguf previously only handled Q4_0; Q4_K_M (llama.cpp's default deployment quant) failed. Adds Q4_K_M support:

  • Reference-dequantizes Q4_K super-blocks (256-elem, 8 sub-blocks, 6-bit scales/mins per llama.cpp dequantize_row_q4_K), then affine-requantizes into packed uint4 MatMulNBits blocks (block_size=32). Mixed Q5_0/Q6_K/Q8_0 projections use the same target.
  • Correctness test: worst Q4_K max_abs_diff 0.037 < tolerance 0.061.
  • Verified end-to-end: converted Qwen2.5-0.5B-Instruct-GGUF:q4_k_m → 168 MatMulNBits → onnx-genai output 'The capital of France is Paris.'

lintrunner clean; gguf pytest 154 passed.

@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing e8dc837 → 5b0467f

Model Sub-model Changes Status

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing e8dc837 → 5b0467f

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 60 60 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 68 68 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 107 107 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 54 54 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 62 62 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 58 58 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 98 98 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 60 60 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 62 62 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 413 413 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

@codecov

codecov Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.56164% with 24 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/integrations/gguf/_repacker.py 71.73% 8 Missing and 5 partials ⚠️
src/mobius/integrations/gguf/_builder.py 62.50% 6 Missing and 3 partials ⚠️
src/mobius/integrations/gguf/_repacker_test.py 96.22% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the GGUF→ONNX “keep_quantized” pipeline to support llama.cpp’s mixed Q4_K_M-style quantization by adding Q4_K handling and a float dequantize→requantize path that normalizes mixed source quant types into a single MatMulNBits target layout.

Changes:

  • Add Q4_K super-block handling that reference-dequantizes across flattened storage (including super-blocks crossing logical row boundaries) and requantizes into MatMulNBits 4-bit block-32 layout.
  • Introduce repack_dequantized_tensor() to requantize dequantized float weights into MatMulNBits, enabling normalization of mixed GGUF presets.
  • Improve GGUF dequantization API (dequantize_raw_tensor) and add targeted tests for mixed-profile detection and Q4_K correctness/error bounds.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/mobius/integrations/gguf/_repacker.py Implements Q4_K repacking via reference dequantization + requantization; adds repack_dequantized_tensor() helper.
src/mobius/integrations/gguf/_repacker_test.py Adds MatMulNBits dequant helper and new tests for Q4_K requantization bounds and row-crossing behavior.
src/mobius/integrations/gguf/_reader.py Factors out dequantize_raw_tensor() to preserve K-quant flattened layout before reshaping.
src/mobius/integrations/gguf/_builder.py Normalizes mixed quant presets by repacking all QuantizedLinear weights; adds requantization for non-matching types; adjusts quant-target detection.
src/mobius/integrations/gguf/_builder_test.py Adds tests for mixed-profile target selection and dequantize_raw_tensor() parity with get_tensor().

@justinchuby
justinchuby force-pushed the feat/gguf-q4km-support branch from f859573 to 019f7f2 Compare July 13, 2026 05:17
@justinchuby

Copy link
Copy Markdown
Member Author

@copilot rebase, and address comments if relevant

@justinchuby
justinchuby force-pushed the feat/gguf-q4km-support branch from 019f7f2 to 5b0467f Compare July 13, 2026 13:47
@justinchuby
justinchuby merged commit 400808d into main Jul 13, 2026
21 of 23 checks passed
@justinchuby
justinchuby deleted the feat/gguf-q4km-support branch July 13, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants