Skip to content

fix(exl3): invalidate native cache after inference metadata edits - #3732

Merged
Qubitium merged 2 commits into
mainfrom
codex/fix-exl3-cache-signature
Nov 9, 2026
Merged

Qubitium merged 2 commits into
mainfrom
codex/fix-exl3-cache-signature

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fix the remaining EXL3 native-cache hole for packed sign metadata created under torch.inference_mode(). Such tensors have no version counter, so the first version-counter patch could still reuse a native handle containing stale unpacked signs.

Before a signature is recorded, ExllamaV3Linear._normalize_inference_metadata() copies inference-created sign and codebook metadata once into versioned checkpoint buffers. A later in-place edit changes the signature and rebuilds LinearEXL3. The packed trellis remains shared by reference. clear_weight_cache() provides an explicit refresh after writes that bypass PyTorch version counters.

No dense, dequantized, or expanded weight cache is added. The change copies only suh, svh, su, sv, mcg, and mul1 metadata when needed; the large trellis is not copied. No internal backward-compatibility guarantee is added; hf_ APIs are unchanged.

Before and after

Case Before After
Inference-created su/sv edited in place _current_signature() stored version=None and matched the old handle (gptqmodel/nn_modules/exllamav3.py:137-145 at 0f291b21). LinearEXL3 had already unpacked the old signs. _normalize_inference_metadata() rebinds versioned packed metadata before signature and handle construction (gptqmodel/nn_modules/exllamav3.py:125-166 at 88cfc49d). The edit changes the signature.
Unchanged forward The handle was reused. The handle is still reused; metadata is copied only once.
External write without a version bump There was no explicit clear method on ExllamaV3Linear. clear_weight_cache() drops the native and Torch handles (gptqmodel/nn_modules/exllamav3.py:180-182).
Trellis content edit The handle held the trellis tensor directly. It still does; in-place content edits are read through the same storage, while pointer/shape/dtype changes invalidate the signature.

Runtime flow

flowchart TD
    A["ExllamaV3Linear._ensure_inner"] --> B{"Trellis materialized?"}
    B -- No --> X["RuntimeError: missing or meta trellis"]
    B -- Yes --> C{"validate_device accepts CUDA?"}
    C -- No --> Y["NotImplementedError: unsupported device"]
    C -- Yes --> D["_coerce_buffer_dtypes"]
    D --> E["_current_signature"]
    E --> F["_normalize_inference_metadata"]
    F --> G{"Inference-created metadata?"}
    G -- Yes --> H["Copy metadata once outside inference mode"]
    G -- No --> I["_bias_version"]
    H --> I
    I --> J{"Signature matches _inner_signature?"}
    J -- Yes --> K["Reuse LinearEXL3"]
    J -- No --> L["_drop_inner and _stored_features"]
    L --> M{"Storage geometry valid?"}
    M -- No --> Z["ValueError: invalid storage geometry"]
    M -- Yes --> N["Build LinearEXL3 and save signature"]
    P["clear_weight_cache"] --> Q["_drop_inner"]
    Q --> R["Next _ensure_inner"]
    R --> B
    classDef failure fill:#ffe8e8,stroke:#b30000,color:#111;
    class X,Y,Z failure;
Loading

Reference page: EXL3 native buffer lifecycle, linked from the README.

Validation

  • Final-commit EXL3 suite: python -m pytest tests/test_exl3_cache_signature.py tests/test_exllamav3_torch_padding.py tests/test_exllamav3.py -q -o log_cli=false — 94 passed, 55 skipped.
  • Focused inference-sign and bias lifecycle tests after the final buffer-copy change — 3 passed, 2 skipped.
  • Changed-file Ruff: ruff check gptqmodel/nn_modules/exllamav3.py tests/test_exl3_cache_signature.py — passed.
  • git diff --check — passed. GitHub Ruff Check on 88cfc49d — passed.
  • The CPU regression checks handle reuse, su/sv mutation, replacement, checkpoint loading, unload/rebuild, explicit clear, trellis aliasing, and absence of a dense 128×128 tensor in module/handle state. The fixture's su/sv are 32 packed bytes total; the trellis is 4,096 packed bytes and is not copied.
  • A CUDA-gated test compares native FP16 outputs before and after a sign edit with an independent Torch reconstruction oracle at rtol=atol=2e-3. It skipped here because this host has no available CUDA or ROCm device. Native accuracy and latency are therefore not measured on this host.

The steady-state path checks six metadata slots for inference tensors and creates no copy after the first normalization. GPU latency was not benchmarked on this host.

Comment thread tests/test_exl3_cache_signature.py Fixed
@Qubitium
Qubitium force-pushed the codex/fix-exl3-cache-signature branch from 02f7f3c to 1a35148 Compare November 9, 2026 17:50
@Qubitium
Qubitium force-pushed the codex/fix-exl3-cache-signature branch from 1a35148 to 0f291b2 Compare November 9, 2026 18:30
@Qubitium Qubitium changed the title fix(exl3): invalidate cache on buffer mutation fix(exl3): invalidate native cache after inference metadata edits Nov 9, 2026
@Qubitium
Qubitium merged commit bc88b9a into main Nov 9, 2026
6 checks passed
@Qubitium
Qubitium deleted the codex/fix-exl3-cache-signature branch November 9, 2026 18:57
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