Repository navigation
fix(allspark): keep the checkpoint layout exportable after repack - #3792
Merged
Merged
Conversation
`AllSparkLinear.post_init()` and `AllSparkAwqLinear.post_init()` repacked the checkpoint tensors into the kernel layout, registered that layout as non-persistent buffers, and then deleted `qweight`/`qzeros`/`scales` (setting them to `None`). The modules are persistent-parameter based, so `state_dict()` and `get_state_dict_for_save()` then exported only `bias` and a strict reload of the original checkpoint failed. Reproduced on `origin/main` at 77ecd32: before state_dict: {'qweight': (64, 512), 'scales': (1, 512), 'bias': (512,)} after state_dict: {'bias': (512,)} get_state_dict_for_save: {'bias': (512,)} The complete `save_quantized()` path has a source-rebuild branch, so not every save was corrupted; the module-level export/reload contract was. Fix: the checkpoint tensors stay registered as the canonical state and the kernel layout becomes a derived, non-persistent cache keyed on them (`tensor_cache_key`). `post_init()` builds it, `forward()` refreshes it when the key changed, and `clear_weight_cache()` invalidates it on `load_state_dict` and on device/dtype moves through the existing `BaseQuantLinear` hooks. No dense weight is retained: only the packed N32K16/WMMA layout and its scale and zero-point rows persist. Fusion follows the same ownership. `FusionKernelSpec.prepare()` rebuilds a stale source cache before concatenating the native operands, `shape_error()` checks `packed_ready()` and declares non-CUDA/ROCm sources ineligible rather than repacking inside a forward, and `rebind()` marks the installed packed operands consistent with the source key so a sibling's forward does not repack the checkpoint tensors underneath the group. Tests: new module-level regression coverage for the GPTQ and AWQ modules asserts the exported key set and shapes are unchanged by `post_init()` and by a forward, that a strict reload (including into a fresh module) reproduces the baseline output, and that a stale cache is never served after an in-place `scales` write, a self state-dict round trip, or a dtype move. All six new cases fail on the unpatched baseline. Verified with torch 2.15.0 on NVIDIA and torch 2.16 ROCm on RDNA4: tests/test_allspark_linear.py + test_allspark_awq.py + test_allspark_hip.py 189 passed (ROCm), 35 passed (CUDA) plus the 6 new cases; tests/module_tree/test_fusion_kernel_backends.py 156 passed, 34 skipped on CUDA and 12 passed for the AllSpark cases on ROCm; test_llama_fusion.py + test_fusion_device_inference.py 103 passed, 8 skipped; test_allspark_jit/cuda_graph/rocm_kernel_contracts/kernel_audit_regressions 490 passed; test_offload_files + test_writer_state_overlay 104 passed. Docs: docs/allspark-checkpoint-layout.md records both layouts, the new flow, the before/after mechanism table with file:line evidence, and the failure contract; README links it from the kernel contract section.
ZX-ModelCloud
marked this pull request as draft
November 12, 2026 03:22
Collaborator
|
Review: keeping checkpoint-format tensors authoritative fixes the post- |
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.
TL;DR
AllSpark W8A16
post_init()deleted the checkpoint tensors (qweight, and for AWQqzeros/scales) after repacking them into the kernel layout, which it then registered as non-persistent buffers. The module is persistent-parameter based, sostate_dict()andget_state_dict_for_save()exported onlybias, and a strict reload of the original checkpoint failed. The checkpoint tensors now stay authoritative and the kernel layout becomes a derived, keyed cache.Net diff: 4 code files (+~115/−25), 3 test files (+~215), 1 doc page, README link.
1. The problem
Reproduced on
origin/mainat77ecd32d6(same code as the report'saudit-20261112worktree for both modules):flowchart TD A["load_state_dict: qweight / scales / bias"] --> B["post_init"] B --> C["repack to kernel layout"] C --> D["register qweight_packed / scales_packed<br/>persistent=False"] D --> E["del self.qweight, self.scales<br/>self.qweight = None"] E --> F["state_dict -> {bias}"] E --> G["qweight_packed excluded (persistent=False)"] F --> H["strict reload fails: missing qweight"] G --> HMechanisms removed, with
file:lineon77ecd32d6:gptqmodel/nn_modules/qlinear/allspark.py:189-194qweight/qzeros/scalesgptqmodel/nn_modules/qlinear/allspark_awq.py:229-236_repackedboolean as the only cache-validity stateallspark.py:194,allspark_awq.py:149not module._repackedgptqmodel/models/fusion_kernels.py:149Scope note from the report holds: the full
save_quantized()path has a turtle/source-rebuild branch (gptqmodel/models/writer.py:1828), so not every save was corrupt. The confirmed defect is the module-level export and reload contract.2. The new flow
flowchart TD A["load_state_dict(checkpoint)"] --> B["qweight / qzeros / scales registered"] B --> C["post_init -> _ensure_repacked"] C --> D["packed_ready: _packed_key equals tensor_cache_key sources"] D -->|no| E["_repack: unpack checkpoint codes"] E --> F["allspark_repack / allspark_hip_repack"] F --> G["qweight_packed / scales_packed / zeros_packed<br/>persistent=False"] G --> H["_packed_key = _source_key"] D -->|yes| I["forward"] H --> I I --> J["allspark_gemm / allspark_hip_gemm"] I --> K["state_dict / get_state_dict_for_save<br/>export qweight / qzeros / scales"]tensor_cache_key(gptqmodel/nn_modules/qlinear/_runtime_cache.py) covers id, version, storage pointer, device, dtype, and shape, so it catches replacement and ordinary versioned in-place writes. Inference-modesource writes require an explicit
clear_weight_cache()call.3. Failure and ambiguity contract
stateDiagram-v2 state "Checkpoint loaded" as Loaded state "Kernel cache fresh" as Fresh state "Kernel cache stale" as Stale state "Source not repackable" as Blocked Loaded --> Fresh: post_init or first forward Fresh --> Stale: load_state_dict Fresh --> Stale: device or dtype move Fresh --> Stale: versioned qweight/scales write Fresh --> Stale: explicit clear after inference-mode write Stale --> Fresh: _ensure_repacked Stale --> Blocked: operand device is not CUDA or ROCm Blocked --> Fresh: move operands to a supported device{bias}scaleswrite_repackedfalse)prepare()repacks, group proceeds4. Per-subsystem detail
FusionKernelSpec.prepare()rebuilds a stale AllSpark source before the group concatenates the operands.shape_error()now accepts a stale-but-repackable source (materialized CUDA/ROCm operands) and only validates native tile shapes whenpacked_ready()is true.rebind()marks the installed packed operands as consistent with the source key so a sibling projection's forward does not repack the checkpoint tensors underneath the group.5. Change inventory
gptqmodel/nn_modules/qlinear/allspark.pygptqmodel/nn_modules/qlinear/allspark_awq.pypack()invalidatesgptqmodel/models/fusion_kernels.pyprepare/shape_error/rebindfollow the new ownershiptests/test_allspark_linear.pytests/test_allspark_awq_cuda.pytests/test_allspark_awq.pydocs/allspark-checkpoint-layout.mdREADME.md6. Tests / guarantees
test_module_export_keeps_checkpoint_layoutstate_dict()/get_state_dict_for_save()shapes unchanged bypost_init(); strict reload reproduces baseline; fresh module loads the exporttest_runtime_state_dict_roundtrip_and_cache_invalidationscaleswrite repacked; dtype move keeps the checkpoint keystest_awq_awq_module_export_keeps_checkpoint_layouttest_awq_module_export_keeps_checkpoint_layouttest_native_fusion_shapes_oracle_ownership_and_invalidationAll six new cases fail on the unpatched baseline (
6 failed) and pass with the fix.7. Validation
CUDA (torch 2.15.0, 2× RTX 4090):
tests/test_allspark_linear.py+tests/test_allspark_awq_cuda.py41 passed;tests/test_allspark_jit.py+test_allspark_cuda_graph.py+test_rocm_kernel_contracts.py+test_kernel_audit_regressions.py490 passed;tests/module_tree/test_fusion_kernel_backends.py156 passed, 34 skipped;test_llama_fusion.py+test_fusion_device_inference.py103 passed, 8 skipped;test_offload_files.py+test_writer_state_overlay.py104 passed.ROCm (torch 2.16, RDNA4 gfx1201):
test_allspark_hip.py+test_allspark_awq.py+test_allspark_linear.py189 passed; AllSpark cases oftest_fusion_kernel_backends.py12 passed.Numerics: no arithmetic changed; the oracles still decode from the original codes, zeros, and scales, and assert the same 2e-3 visible-output gate. Retention is unchanged — the derived cache holds only the packed layout plus its scale/zero rows, no dense or expanded weight.
8. Reviewer notes
save_quantized()'s source-rebuild branch; this fix only restores the module-level contract that the branch previously masked.clear_weight_cache()deliberately keeps the packed buffers (they are non-persistent and may hold fused sibling views); it only invalidates the key, so the next forward repacks.