Skip to content

fix(allspark): keep the checkpoint layout exportable after repack - #3792

Merged
Qubitium merged 2 commits into
mainfrom
fix/allspark-checkpoint-export
Nov 12, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/allspark-checkpoint-export

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

AllSpark W8A16 post_init() deleted the checkpoint tensors (qweight, and for AWQ qzeros/scales) after repacking them into the kernel layout, which it then registered as non-persistent buffers. The module is persistent-parameter based, so state_dict() and get_state_dict_for_save() exported only bias, 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/main at 77ecd32d6 (same code as the report's audit-20261112 worktree for both modules):

before state_dict: {'qweight': (64, 512), 'scales': (1, 512), 'bias': (512,)}
after  state_dict: {'bias': (512,)}
get_state_dict_for_save: {'bias': (512,)}
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 --> H
Loading

Mechanisms removed, with file:line on 77ecd32d6:

Mechanism Location
Kernel layout only, checkpoint tensors dropped gptqmodel/nn_modules/qlinear/allspark.py:189-194
Same for AWQ qweight/qzeros/scales gptqmodel/nn_modules/qlinear/allspark_awq.py:229-236
_repacked boolean as the only cache-validity state allspark.py:194, allspark_awq.py:149
Fusion gate keyed on not module._repacked gptqmodel/models/fusion_kernels.py:149

Scope 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"]
Loading

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-mode
source 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
Loading
Situation Before After
Export after a forward {bias} checkpoint layout
Strict reload of original checkpoint missing keys succeeds, same output
Self state-dict round trip valid (but lossy export) valid, cache invalidated then rebuilt
Versioned in-place scales write stale packed scales served detected, repacked
Inference-mode source write stale packed scales served caller explicitly clears cache, then repacks
Device/dtype move nothing to rebuild from checkpoint tensors move, cache repacks
Fusion over invalidated sources rejected (_repacked false) prepare() repacks, group proceeds
Fusion over CPU/meta or split-device sources repack would raise in forward declared ineligible

4. 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 when packed_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

Area File ± Purpose
GPTQ module gptqmodel/nn_modules/qlinear/allspark.py +44/−12 Keep checkpoint tensors; derived keyed cache
AWQ module gptqmodel/nn_modules/qlinear/allspark_awq.py +54/−12 Same, plus pack() invalidates
Fusion gptqmodel/models/fusion_kernels.py +32/−7 prepare/shape_error/rebind follow the new ownership
Tests tests/test_allspark_linear.py +98 GPTQ export, strict reload, cache invalidation
Tests tests/test_allspark_awq_cuda.py +60 CUDA AWQ export and reload
Tests tests/test_allspark_awq.py +58 ROCm AWQ export and reload
Docs docs/allspark-checkpoint-layout.md new Layouts, flow, before/after, failure contract
Docs README.md +3/−1 Link the page

6. Tests / guarantees

Test Invariant
test_module_export_keeps_checkpoint_layout state_dict()/get_state_dict_for_save() shapes unchanged by post_init(); strict reload reproduces baseline; fresh module loads the export
test_runtime_state_dict_roundtrip_and_cache_invalidation self round trip valid; in-place scales write repacked; dtype move keeps the checkpoint keys
test_awq_awq_module_export_keeps_checkpoint_layout same for CUDA AWQ
test_awq_module_export_keeps_checkpoint_layout same for ROCm AWQ
existing test_native_fusion_shapes_oracle_ownership_and_invalidation fusion still exact after load/clear/rebase

All 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.py 41 passed; tests/test_allspark_jit.py + test_allspark_cuda_graph.py + test_rocm_kernel_contracts.py + test_kernel_audit_regressions.py 490 passed; tests/module_tree/test_fusion_kernel_backends.py 156 passed, 34 skipped; test_llama_fusion.py + test_fusion_device_inference.py 103 passed, 8 skipped; test_offload_files.py + test_writer_state_overlay.py 104 passed.

ROCm (torch 2.16, RDNA4 gfx1201): test_allspark_hip.py + test_allspark_awq.py + test_allspark_linear.py 189 passed; AllSpark cases of test_fusion_kernel_backends.py 12 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

  • No behaviour change for 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.
  • The doc page records the mechanism, not the PR; a later change to the same flow must update it in the same PR.

`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
ZX-ModelCloud marked this pull request as draft November 12, 2026 03:22

Copy link
Copy Markdown
Collaborator

Review: keeping checkpoint-format tensors authoritative fixes the post-post_init() export and strict reload failure. I found two follow-up issues and pushed 1a6936e1: fusion now rejects checkpoint operands split across devices instead of accepting stale packed views, and the code/docs state that inference-mode source writes require an explicit clear_weight_cache() because those tensors have no version counter. Verified on RTX 4090: 54 AllSpark and fusion tests passed, 12 ROCm cases skipped; Ruff and git diff --check passed. All current PR checks are green. No remaining blockers found.

@Qubitium
Qubitium marked this pull request as ready for review November 12, 2026 05:26
@Qubitium
Qubitium merged commit 8c5c349 into main Nov 12, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/allspark-checkpoint-export branch November 12, 2026 05:26
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