Skip to content

feat(writer): re-enable split_by="layer" with the SGLang caveat - #6683

Merged
Qubitium merged 1 commit into
mainfrom
feat/reenable-split-by-layer
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
feat/reenable-split-by-layer

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Re-enable split_by="layer" (disabled in 952c6fc / #2625 while SGLang could not load the layout) and make the SGLang limitation explicit at save and load time. The writer path itself had not bit-rotted: the three tests skipped since 952c6fc pass unchanged once the gate is removed, and CPU round-trips for tiny Llama GPTQ, Qwen3-MoE GPTQ, Qwen2 AWQ, GPT-2, a quantized lm_head, and a re-save of a loaded checkpoint are bit-identical to split_by=None.

Rebased onto current main (8eeab5e35, after #6666/#6667 reworked the writer meta table and removed the save-state overlay and #6676 changed the loader); the writer split path, the A/B, the round-trips and the red/green run below were all re-executed on that base.

No production code path changed for the default split_by=None; the writer A/B below is byte-identical.

What Changed

  • gptqmodel/models/writer.py: SUPPORTED_SPLIT_BY = {None, "layer"} and an accurate comment replacing the 2026-03 disable note. _parse_split_by keeps its existing error text.
  • gptqmodel/models/writer.py: one log.warning per split_by="layer" save: the layout, the SGLang limitation, and the two bypasses (load with GPT-QModel, or re-save with split_by=None).
  • gptqmodel/models/loader.py: one log.info when a load opens a shard index whose weight_map points into sub-directories (split_by="layer" or a per-layer reshard), with the same bypasses.
  • tests/test_split_by_layer_save.py: un-skipped the three tests from 952c6fc; added a save-warning test and a layout-detection/load-log test.
  • docs/quantization-guide.md: a short save-options note plus the layout/flow diagram.
  • tests/models/model_test.py keeps SPLIT_BY = None. That matrix needs GPUs and model downloads to validate, so this PR does not flip its default; the layout is covered by the dedicated CPU tests and the round-trips below.

No writer bit-rot was found: _stream_state_dict_to_layer_dirs delegates to the same streaming_state_dict_to_shards, so the 8 months of writer refactors (TensorSource, shard reuse, atomic writes, offload guards) apply to the per-layer path too. Verified explicitly: per-layer sharding under max_shard_size, stale-shard cleanup on re-save into the same directory, flat→split and split→flat conversion in one directory, index self-consistency, and load/generate from the nested layout.

SGLang caveat

The message states only what is known from #2625: SGLang currently cannot load this per-layer directory layout. I could not reach GitHub from this host to re-read the #2625 discussion, so the message does not claim a specific SGLang loader mechanism or invent an SGLang PR number. It names the two verified bypasses.

Tests

CPU, /root/venv/bin/python -P, clone first on PYTHONPATH, CUDA_VISIBLE_DEVICES= (this host has no usable NVIDIA driver):

# split_by tests + save-kwargs validation
/root/venv/bin/python -P -m pytest -o addopts= -q -p no:cacheprovider \
  tests/test_split_by_layer_save.py tests/test_save_kwargs_validation_4686.py
# 10 passed

# writer/save set
/root/venv/bin/python -P -m pytest -o addopts= -q -p no:cacheprovider \
  tests/test_save_dispatch.py tests/test_save_kwargs_validation_4686.py \
  tests/test_save_state_dict_buffer_collection.py tests/test_writer_attention.py \
  tests/test_writer_dynamic_order.py tests/test_writer_instance_attrs.py \
  tests/test_writer_quant_log.py tests/test_writer_remote_code_policy.py \
  tests/test_writer_save_tail_helpers.py tests/test_writer_state_overlay.py \
  tests/test_config_only_export.py tests/test_checkpoint_metadata_contracts.py \
  tests/test_out_of_model_tensors.py tests/test_split_by_layer_save.py
# 115 passed (the overlay-contract tests removed by #6667 are no longer part of this set)

# loader path touched by the new info log
/root/venv/bin/python -P -m pytest -o addopts= -q -p no:cacheprovider \
  tests/test_checkpoint_legacy_shadow_5723.py tests/test_loader_double_contract_4941.py \
  tests/test_save_loaded_quantized_model_dtype.py
# 6 passed

# docs contract
/root/venv/bin/python -P -m pytest -o addopts= -q -p no:cacheprovider tests/test_docs_contract_refs.py
# 267 passed (273 together with the loader set)

# formatting (venv ruff 0.14.2; no-op pip shim on PATH)
bash format/check.sh
# All checks passed

# module-tree inventory (untouched by this PR)
/root/venv/bin/python -P tests/module_tree/awq_fold_inventory.py --check
# changed: none

Red/green (the un-skipped test file run against current origin/main 8eeab5e35 vs this branch):

# main 8eeab5e35 (test file copied into a base worktree, base gptqmodel on PYTHONPATH)
pytest tests/test_split_by_layer_save.py
# 5 failed: 3 x ValueError: Unsupported split_by value: layer. Supported values: None, 'layer'.
#           2 x AttributeError: module 'gptqmodel.models.loader' has no attribute
#               '_checkpoint_uses_per_layer_dirs' / '_log_per_layer_checkpoint_layout'
# branch
pytest tests/test_split_by_layer_save.py
# 5 passed

Skips: none in the runs above. tests/models/model_test.py and the CUDA/integration model tests were not run (no GPU, model downloads unavailable), so the model-matrix default stays None; the split layout is unverified for multimodal/vision layer stacks.

Measured Impact

Not applicable: re-enabling a save option does not change the default output. The default-path writer A/B below is byte-identical.

Default-path A/B (split_by=None), base 8eeab5e35 vs branch

Reused the 8-config writer A/B from the writer-meta-table work (meta_ab.py: GPTQ default/GPTAQ/FOEM/JointQ, AWQ, GGUF, Paro, EXL3). It records every saved file's sha256 plus quantize_config.json/config.json meta keys and hashes.

python meta_ab.py --out meta_base     # base worktree at 8eeab5e35
python meta_ab.py --out meta_branch   # branch
cmp meta_base/records.json meta_branch/records.json
# identical: 8/8 configs, same files, same sha256, same meta order

split_by="layer" round-trips (CPU)

All comparisons are bytewise (dtype/shape checked, then view(uint8/int) equality), not allclose.
The table was re-run on the rebased branch (post-#6666/#6667); every case passed again with the
same tensor counts:

Case Result
tiny Llama, GPTQ 4-bit g32, offload_to_disk=True flat vs split tensors bit-identical (63); GPTQModel.load state_dict bit-identical (77); logits bit-identical; generate() works
tiny Qwen3-MoE, GPTQ 4-bit g32 bit-identical (173 reload tensors); logits bit-identical; generate() works
tiny Qwen2 (tied), GPTQ 4-bit g32 bit-identical (83 reload tensors); logits bit-identical; generate() works
tiny Qwen2 (tied), AWQ 4-bit g32 on CPU bit-identical (69 reload tensors); logits bit-identical; generate() works
tiny GPT-2 (transformer.h layer prefix) layer dirs transformer.h.0/1; tensors bit-identical; loads and forwards
quantized lm_head=True lm_head.safetensors at root; flat vs split bit-identical (66)
re-save of a loaded quantized checkpoint through split_by="layer" bit-identical (66); loads and forwards
max_shard_size="8KB" layer 0 written as layer-00001-of-00005..05; index points at all 5; tensors bit-identical to flat
re-save same dir with max_shard_size=None stale layer-0000N-of-0000M files removed, layer.safetensors restored; loads, bit-identical
flat→split→flat in the same directory no stale root model-*.safetensors; loads at each step; tensors bit-identical

Scripts and outputs: pr/reenable-split-by-layer.scratch/{roundtrip,resave,lmhead_resave,roundtrip_gpt2,convert}.py and rt-*/rs/conv output trees.

Model Support / Module Plan

Not applicable: no new model, alias, definition, module plan or fusion change. tests/module_tree is untouched.

AWQ onboarding safety

Not applicable: no model definition, AWQ grouping, search, replay or fold code changed. The AWQ path was exercised only as a saver (tests/round-trip above).

Logic Flow Documentation

The save-layout decision is restored, so the flow is shown here and repeated in the quantization guide.

flowchart TD
    A["model.save(split_by=...)"] --> B["_parse_split_by"]
    B -- "None" --> C["streaming_state_dict_to_shards"]
    B -- "layer" --> W["log.warning: SGLang cannot load this layout"]
    B -- "other" --> X["ValueError: unsupported split_by"]
    W --> D["_stream_state_dict_to_layer_dirs"]
    D --> E["model.layers.N/layer.safetensors"]
    D --> F["model.safetensors.index.json"]
    C --> G["model.safetensors"]
    F --> H["GPTQModel.load: nested shard index"]
    H --> I["log.info: per-layer layout, SGLang bypass"]
    classDef failure fill:#ffe8e8,stroke:#b30000,color:#111;
    class X failure;
Loading
Mechanism Before this PR After
SUPPORTED_SPLIT_BY (models/writer.py:214) {None}; "layer" raised ValueError {None, "layer"}; comment names the SGLang limitation
save_quantized(split_by="layer") (models/writer.py:3077) unreachable writes per-layer dirs + index, logs one warning
GPTQModel.load of that layout (models/loader.py:3966) no message detects nested weight_map, logs one info
Layout itself unchanged since 952c6fc unchanged

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, pack or fusion code changed.

Notes

  • The writer path is unchanged; only the gate, two log lines, tests and docs change.
  • The load info fires for any nested-shard index, including per-layer reshard() output, because it is the same layout and has the same SGLang limitation.
  • tests/models/model_test.py deliberately keeps SPLIT_BY = None (see Summary); flipping it is a follow-up that needs the GPU model matrix.
  • deepseek_v41_flash.save() still rejects split_by explicitly; its single indexed component checkpoint has no per-layer layout.

🤖 Generated with Claude Code

952c6fc disabled split_by="layer" while SGLang could not load the
per-layer directory layout. The writer path still works (the dedicated
tests pass once the gate is removed), so re-enable the option and warn
about the limitation instead of rejecting it.

- SUPPORTED_SPLIT_BY = {None, "layer"} with an accurate comment.
- One log.warning per split_by="layer" save naming the SGLang limitation
  and the two bypasses.
- One log.info when a load opens a shard index that points into
  per-layer sub-directories (split_by="layer" or per-layer reshard).
- Un-skip the three split_by tests; add save-warning and load-detection
  tests; document the layout in the quantization guide.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium force-pushed the feat/reenable-split-by-layer branch from fc8816e to 15a1402 Compare December 1, 2026 18:42
Comment thread tests/test_split_by_layer_save.py Resolved
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 18:59
@Qubitium
Qubitium merged commit 8b4e058 into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the feat/reenable-split-by-layer branch December 1, 2026 18:59
Qubitium added a commit that referenced this pull request Dec 1, 2026
…6689)

#6656 added CalibrationConfig.spill_path and #6683 re-enabled
split_by="layer" after #6640 refreshed the notes.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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