Repository navigation
feat(packaging): add single-file llama.cpp GGUF export - #6045
Draft
ZX-ModelCloud wants to merge 3 commits into
Draft
ZX-ModelCloud wants to merge 3 commits into
ZX-ModelCloud wants to merge 3 commits into
Conversation
Add gptqmodel.package(GGUFPackageConfig(...)), which writes one llama.cpp-readable .gguf file from a GPT-QModel GGUF checkpoint. The packed payload is already ggml block bytes, so packaging only renames tensors, reorders whole output rows where an architecture requires it (Llama q/k rotary interleave, including biases), stores dense tensors, and writes the architecture and tokenizer metadata. No weight is dequantized or re-quantized. Refusals are part of the contract: an unregistered architecture, a mixture-of-experts checkpoint, a padded or otherwise mismatched packed row, an unmapped tensor, or a missing dependency fails before a file is published. The output is written to a temporary file and renamed only after a read-back check of names, ggml types and logical shapes. Verified layouts: qwen2, qwen3, llama. docs/gguf-package.md documents the flow and failure contract; tests/test_gguf_package.py covers byte preservation, row permutation, tokenizer arrays and every refusal.
ZX-ModelCloud
commented
Nov 24, 2026
ZX-ModelCloud
left a comment
Collaborator
Author
There was a problem hiding this comment.
Fixed the two writer cleanup findings, atomic publication, and GGUF compatibility checks for RoPE, packed weights and tokenizer metadata. The GGUF regression suites, llama.cpp checks for Qwen2, Qwen3 and Llama, and format/check.sh passed.
ZX-ModelCloud
commented
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
gptqmodel.package(GGUFPackageConfig(...)), which turns a GPT-QModel GGUF checkpoint directory into one llama.cpp-readable.gguffile.+2638 / -1across 12 files: a newgptqmodel/packaging/package (config, errors, architecture registry, tokenizer metadata, writer),docs/gguf-package.md, andtests/test_gguf_package.py.Fixes #6046
Fixes #6047
Fixes #6048
What Changed
New public API:
PackageConfig(model_path, out_path, overwrite=False)holds the in/out paths;GGUFPackageConfigadds the tokenizer, architecture override, dense dtype, alignment, name, file type and extra metadata;gptqmodel.package(config)dispatches on the config type and rejects a type it has no writer for.The writer is a container change, not a conversion: a GPT-QModel GGUF linear already stores ggml block bytes in
qweight([out_features, bytes_per_row] uint8), so packaging only renames tensors, reorders whole output rows where an architecture requires it, stores dense tensors, and writes the architecture and tokenizer metadata. No weight is dequantized or re-quantized.Verified layouts:
qwen2,qwen3,llama(Llama includes the q/k rotary-interleave row permutation for weights and biases, and the Llama-3rope_freqstable). An architecture without a registered layout is refused by default;allow_unverified_arch=Trueuses the generic name map and metadata with no weight transform.Fail-closed contract: an unregistered architecture, a mixture-of-experts checkpoint, a padded or otherwise mismatched packed row, an unmapped tensor, a duplicate output name, or a missing
ggufdependency fails before a file is published. A read-back check verifies names, ggml types and logical shapes before publication. Publication withoverwrite=Falseclaims the destination atomically. Writer, verification and publication failures run temporary-file cleanup.The tokenizer is read from the checkpoint directory (or
tokenizer_path=/tokenizer=) and never executes checkpoint code. Fast BPE tokenizer rows past the vocabulary are marked unused so the token list matches the embedding. SentencePiece and WordPiece vocabularies are rejected.docs/gguf-package.mddocuments the flow, the byte-safe operation table and the failure contract; it is linked fromREADME.mdanddocs/README.md.The writer reads current
rope_parametersand supports default and linear RoPE plus Llama-3 scaling on Llama. Qwen2 and Qwen3 require full attention and their architecture-specific tensors. Packed payloads must be uint8 and use types llama.cpp can serve as weights. Generated metadata keys are reserved.Mixture-of-experts packing, multimodal
mmprojfiles and re-quantization are outside the scope of this export.Tests
$ bash format/check.sh All checks passed! $ python -m pytest tests/test_gguf_package.py tests/test_internal_gguf.py tests/test_gguf_load_policy_env_removal.py tests/test_gguf_all_formats.py -o addopts= -o log_cli=false -q -rs --tb=short 1032 passed, 14 skippedThe package, reader and load-policy suites passed all 89 cases, including llama.cpp load, token ID parity and evaluation on Qwen2, Qwen3 and Llama fixtures. The format suite passed 943 cases. Its 14 skips cover two extreme-value IQ3 cases and 12 combinations without a native ggml matmul implementation.
test_current_rope_parameters_set_frequency_base,test_llama3_rope_freqs_matches_the_piecewise_spec,test_linear_rope_scaling_is_preserved,test_refuses_unsupported_attention_layouttest_refuses_non_byte_packed_storage,test_refuses_activation_only_ggml_types_as_weights,test_refuses_missing_required_attention_tensors,test_absent_tie_flag_uses_the_model_defaulttest_refuses_metadata_that_replaces_generated_fields,test_signed_extra_metadata_round_trips,test_refuses_sentencepiece_tokenizers_without_a_gguf_vocabularytest_output_created_during_verification_is_preserved,test_writer_failure_leaves_existing_output_and_cleans_temp_filetest_implicit_model_defaults_are_written,test_llamacpp_loads_the_packaged_filetest_refuses_unrepresentable_mlp_activationtest_only_known_derived_buffers_are_ignoredMeasured Impact
Not applicable: this change adds an export path and makes no VRAM, performance or accuracy claim. The packed payload is copied, and the tests assert byte equality rather than error statistics.
Model Support / Module Plan
Not applicable: no
MODEL_MAPentry, model type, alias, definition or fusion eligibility changes. The architecture registry ingptqmodel/packaging/arch.pyis local to the export path and does not affect model loading or quantization.AWQ onboarding safety
Not applicable: no AWQ grouping, search, replay or folding changes.
Logic Flow Documentation
file:lineevidence.docs/repeats the diagrams, titled for the mechanism, and is linked fromREADME.md.Before this change there was no single-file export at all: a llama.cpp artifact could only be produced by re-converting the dense model with llama.cpp's
convert_hf_to_gguf.py, which re-quantizes every weight. The new entry point consumes the existing packed checkpoint instead.package()writes it from the existing packed checkpointarch.py:109andgguf_package.py:724read legacy fieldsArchSpec.extra_metadataandLlamaSpec.extra_tensorsshare current configuration precedencegguf_package.py:631checked before an unconditional replace at line 691_write_ggufclaims an unused destination or explicitly replaces it after verificationflowchart TD A["gptqmodel.package(GGUFPackageConfig)"] --> B["_package_gguf reads config.json and quantization_config"] B --> C{"GPT-QModel GGUF checkpoint?"} C -- "no" --> R1["CheckpointFormatError"] C -- "yes" --> D["_resolve_arch and resolve_spec"] D --> E{"Registered layout or explicit opt-in?"} E -- "no" --> R2["UnsupportedArchitectureError"] E -- "yes" --> W["ArchSpec.validate_layout and _build_tokenizer_metadata"] W -- "incompatible" --> R3["Reject the layout"] W -- "valid" --> F["_plan_tensors checks names, storage, required tensors and shapes"] F -- "incompatible" --> R3 F -- "valid" --> H["_write_gguf writes metadata, tensor info and payloads to a temporary file"] H --> I["_verify_written reads names, ggml types and shapes"] H -- "write fails" --> R4["Remove temporary file"] I -- "verification fails" --> R4 I -- "verified" --> J{"overwrite?"} J -- "yes" --> K["os.replace publishes the file"] J -- "no" --> L["os.link publishes at a free destination"] K -- "publication fails" --> R4 L -- "destination already exists" --> R5["FileExistsError and temporary file cleanup"] L -- "other publication failure" --> R4 L -- "published" --> M["Remove temporary link"] classDef bad fill:#fdd,stroke:#c00,color:#111; class R1,R2,R3,R4,R5 bad;The same diagram and the full situation-to-result table are in
docs/gguf-package.md.Review Requirements
Kernel Accuracy Requirements
Not applicable: no kernel, dispatch, packer or fusion change. The writer copies packed bytes and does not execute a kernel.
Notes
GPTQMODEL_*environment switch is introduced or read.gguf>=0.19.0(already the[gguf]extra floor) provides the writer and the tensor-name map. The writer is imported lazily, soimport gptqmodeldoes not require the extra.gptqmodel/packaging/arch.pyis the extension point for a new architecture (metadata, row permutation, generated tensors, expected shapes).gptqmodel/packaging/gguf_package.py::_check_shapeis the single place that decides whether a layout is byte-safe.