Skip to content

refactor(writer): one ordered table for the save meta fields - #6666

Merged
Qubitium merged 1 commit into
mainfrom
refactor/writer-meta-table
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
refactor/writer-meta-table

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

The save tail in ModelWriter.save_quantized wrote nine meta entries with nine hand-written meta_set calls, including three inline payload builders (GPTAQ, FOEM, JointQ). This PR replaces the block with one ordered table of (key, value-getter) pairs and one loop.

meta is serialized in insertion order into quantize_config.json, so the table order is part of the checkpoint format. The getters reproduce the old values exactly and the saved bytes are unchanged; no config source, value, key order or public API changes.

What Changed

gptqmodel/models/writer.py (1 file, +35/−66):

  • Added _solver_meta(config, *fields) and _jointq_meta(config) for the three inline payload builders.
  • Added _SAVE_META_FIELDS, the ordered table: damp_percent, damp_auto_increment, static_groups, true_sequential, mse.norm, gptaq, foem, jointq, act_group_aware.
  • Added _stamp_save_meta(quantize_config); save_quantized now calls it instead of the 66-line block (old lines 2726-2786).
  • No API or behavior change; the unreachable split_by="layer" writer is untouched.

Tests

  • Unit tests have been executed and passed.
  • A new unit test is not applicable: the save A/B below compares the saved bytes themselves; a helper test would only restate the table.
export HF_HOME=/root/.cache/huggingface HF_HUB_OFFLINE=1 CUDA_VISIBLE_DEVICES=""

# writer/save surface (11 files)
python -P -m pytest -p no:cacheprovider -q -rs --timeout 1800 \
  tests/test_writer_state_overlay.py tests/test_writer_save_tail_helpers.py \
  tests/test_writer_quant_log.py tests/test_writer_attention.py \
  tests/test_writer_dynamic_order.py tests/test_writer_instance_attrs.py \
  tests/test_writer_remote_code_policy.py tests/test_save_kwargs_validation_4686.py \
  tests/test_split_by_layer_save.py tests/test_docs_contract_refs.py \
  tests/test_save_dispatch.py
#   origin/main 0127a0313: 340 passed, 3 skipped in 184.98s
#   branch b6b598488:      340 passed, 3 skipped in 184.61s
#   343 common node ids: identical outcomes (0 differences)
#   3 skips: tests/test_split_by_layer_save.py:147/174/198 (SUPPORTED_SPLIT_BY)

bash format/check.sh   # All checks passed! (ruff 0.14.2)

Save A/B

Harness kept out of the repo (writer-meta-table.scratch/meta_ab.py): a tiny Llama LlamaQModel with a real packed quantized module, driven through ModelWriter.save_quantized on both trees. Each case writes config.json, quantize_config.json and model.safetensors; the dump records the sha256 of every saved file plus the meta key order.

Cases: gptq_default, gptq_gptaq, gptq_foem, gptq_jointq (TorchLinear), awq (AwqGEMVTorchLinear), gguf, paro, exl3. The last three exercise non-GPTQ methods that add their own meta keys (AWQ scale_search_chunked_activations, Paro opt_*, EXL3 tensor metadata).

cases: 8; files per case: 3; diff: none
# records.json (file sha256 + meta key order) byte-identical main vs branch
# e.g. quantize_config.json meta order: quantizer, uri, damp_percent,
#      damp_auto_increment, static_groups, true_sequential, mse, gptaq, foem,
#      jointq, act_group_aware, ...

Real end-to-end CPU GPTQ (tiny 2-layer Llama, GPTQModel.load -> quantize with 4 calibration rows on BACKEND.TORCH, device="cpu" -> save): quantize_config.json, model.safetensors, config.json, generation_config.json and both tokenizer files are byte-identical main vs branch. Only quant_log.csv differs, and it also differs between two runs on the same tree (per-module timing/loss rows), so it is not an effect of this refactor.

Measured Impact

Not applicable: behavior-preserving refactor with no runtime, memory or accuracy change (save A/B is byte-identical).

Model Support / Module Plan

Not applicable: no MODEL_MAP, module-tree, plan or definition change; only the private save-tail meta writer in writer.py.

AWQ onboarding safety

Not applicable: no AWQ grouping, search, replay or fold change. AWQ appears only as a save A/B case, where the saved bytes are unchanged.

Logic Flow Documentation

The change replaces a straight-line write sequence with a declared ordered table; there is no decision, selection or tensor-lifecycle change.

flowchart LR
    A[save_quantized] --> B[_stamp_quantizer_meta]
    B --> C[_stamp_save_meta]
    C --> D[_SAVE_META_FIELDS in order]
    D --> E["damp_percent .. act_group_aware"]
Loading
Old New
writer.py:2726-2786: nine meta_set calls, three inline payload builders writer.py:889-897: _SAVE_META_FIELDS table; writer.py:900-903: _stamp_save_meta loop
GPTAQ payload: inline alpha + device (str-normalized) _solver_meta(..., "alpha")
FOEM payload: inline alpha/beta + device _solver_meta(..., "alpha", "beta")
JointQ: jointq_cfg.meta_payload() _jointq_meta(...)

No docs page: the table is the complete specification of a fixed write order with no branch to diagram.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

Not applicable: no kernel, dispatch or fusion change.

Notes

  • The table lives at module level next to _stamp_quantizer_meta; save_quantized calls it after the quantizer fingerprint, preserving the exact insertion order.
  • The unreachable split_by="layer" writer was left untouched.

🤖 Generated with Claude Code

The save tail wrote nine `meta_set` calls by hand, three of them through
inline GPTAQ/FOEM/JOINTQ payload builders. Replace them with one ordered
`_SAVE_META_FIELDS` table of (key, getter) pairs and one `_stamp_save_meta`
loop.

`meta` is serialized in insertion order, so the table order is the
checkpoint contract; the getters reproduce the old values exactly,
including the `alpha`/`beta`/`device` payloads and the JointQ
`meta_payload()`. No config source, value or write order changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 17:43
@Qubitium
Qubitium merged commit 7cab639 into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the refactor/writer-meta-table branch December 1, 2026 17:44
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.

1 participant