Skip to content

fix: bound offload source-reference RSS and stop cached kernel-validation failures pinning models (#4463, #4465) - #4496

Merged
Qubitium merged 2 commits into
mainfrom
fix/offload-ref-rss-and-validate-cache-4463-4465
Nov 16, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/offload-ref-rss-and-validate-cache-4463-4465

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4465
Addresses #4463 item 1 (base-module offload source-reference RSS). Item 2 was fixed in #4492; item 3 is handled separately.

#4463 item 1: the base-module reference check no longer copies the tensor

  • Before: _module_source_reference_index proved each base-module tensor equal to the source checkpoint with safe_open().get_tensor() + torch.equal. That is a full owned copy plus the mapped file pages.
  • After: _source_payload_sha256_if_equal streams the payload's byte range through one 16 MiB readinto buffer. It compares each chunk with a uint8 view of the live tensor and hashes the compared bytes for the index sha256.
  • The reference offload and its disk saving are unchanged.
GPTQ 4-bit, offload_to_disk, 128 samples, RTX 4090 main this PR
Llama-3.2-1B: RSS added in base-module offload (anon/file) +502 / +502 MiB +1 / +0 MiB
Llama-3.2-1B: quantize peak RSS / offload temp disk 3954 / 484 MiB 3327 / 475 MiB
Qwen2.5-0.5B: RSS added in base-module offload +261 / +261 MiB +1 / +0 MiB
Qwen2.5-0.5B: quantize peak RSS / offload temp disk 3098 / 179 MiB 2842 / 179 MiB

Saved checkpoints are byte-identical.

#4465: cached validation failures hold text, not exceptions

  • The leak: cached_validate_once cached the exception object. Its __cause__ traceback, and through f_back the loader frames that hold the model, stayed alive for the life of the process. A later raise err attached a new traceback to the cached object.
  • The fix: _cache_validation_status now caches (ok, type, message). new_validation_error, shared with validate_quant_linear, builds a fresh exception for each caller.
  • A second root in GGUF: gguf_cpp kept the llama-cpp import failure as an exception in a module global, which pinned the same frames. It now keeps that failure, and the ggml-cuda load failure, as text quoted in the raised message.
  • Behaviour change: callers get a fresh exception without __cause__; only the message text is kept.
  • Result: loading a GGUF Qwen2.5-0.5B without llama-cpp-python left 471.2 MiB allocated after del; it now leaves 0.0 MiB.
flowchart TD
    A["make_quant / validate_quant_linear"] --> B["BaseQuantLinear.validate"]
    B --> C["cached_validate_once (_cache_validation_status)"]
    C --> D{"status cached?"}
    D -- no --> E["cls.validate_once()"]
    E -- ok --> G["cache (True, None, None)"]
    E -- "exception" --> H["cache (False, type, str) - exception dropped"]
    D -- yes --> I["new_validation_error -> fresh exception"]
    G --> I
    H --> I
    classDef failure fill:#ffe8e8,stroke:#b30000,color:#111;
    class H failure;
Loading

Before/after details with file:line evidence are in docs/safetensors-reference-offload.md and docs/kernel-validation-and-cache-lifecycle.md.

Tests

  • tests/test_offload_reference_rss_4463.py:
    • chunk boundaries;
    • a one-bit mismatch in the first chunk and in the last chunk;
    • a peak-RSS (VmHWM) bound for a 128 MiB tensor: main adds 84–95 MiB, this PR about 2 MiB.
  • tests/test_cached_validate_once_lifetime_4465.py: a weakref'd model must be collectable after a chained failure, after raising the returned error, and after a GGUF probe without llama_cpp.
  • After rebasing on 218ce13, RTX 4090: 244 passed, 1 skipped across the offload, kernel-validation and checkpoint-dtype suites. ruff is clean.

🤖 Generated with Claude Code

Qubitium and others added 2 commits November 16, 2026 19:33
…heck (#4463)

`_module_source_reference_index` proved each base-module tensor equal to the
source checkpoint by loading it with `safe_open().get_tensor()` and comparing
with `torch.equal`. That is a full owned copy plus the mapped file pages, so
the check added ~2x the embedding size to the quantize peak RSS (+1004 MiB in
the Llama-3.2-1B base-module offload call).

`_source_payload_sha256_if_equal` now streams the payload range through one
reusable 16 MiB `readinto` buffer, compares it with a uint8 view of the live
tensor, and hashes the compared bytes for the index `sha256`. The reference
offload and its disk saving are unchanged.

Llama-3.2-1B GPTQ quantize peak RSS 3954 -> 3327 MiB, Qwen2.5-0.5B 3098 ->
2842 MiB; offload temp disk unchanged; checkpoints byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… loading model (#4465)

`cached_validate_once` lru-cached the exception `validate_once` returned;
`with_traceback(None)` cleared only its own traceback, so the chained
`__cause__` frames (and through `f_back` the loader frames holding the model)
lived for the whole process, and a caller that raised the cached object
re-attached a traceback to it. GGUF had a second root: the llama-cpp import
failure was kept as an exception in a module global.

- Cache `(ok, type, message)` via `_cache_validation_status` and rebuild a
  fresh exception per caller with `new_validation_error` (moved from
  utils/importer.py, which now imports it).
- gguf_cpp keeps the llama-cpp import and ggml-cuda load failures as text and
  reports them inline in the raised message.

GGUF Qwen2.5-0.5B load without llama-cpp-python: 471.2 MiB still allocated
after `del` -> 0.0 MiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium merged commit 860ae2d into main Nov 16, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/offload-ref-rss-and-validate-cache-4463-4465 branch November 16, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant