Repository navigation
fix(cpu): stop host-memory duplication in GGUF CPU, offload, and pinned staging (#4395) - #4439
Merged
Merged
Conversation
…ed staging (#4395) - GGUF_CPP_CPU: bind the packed qweight in place with ggml_backend_cpu_buffer_from_ptr/ggml_backend_tensor_alloc instead of allocating and copying the whole weight on every forward. post_init() 32-byte aligns mmap-loaded qweight; unaligned or rejected layouts fall back to the copy path. M=1 synthetic Q4_K layer 1.60 -> 0.64 ms (2.49x), bitwise-equal output. - Release PyTorch's pinned host cache at the end of quantize() and after the loader's checkpoint load. Llama-3.2-1B GPTQ pinned bytes 115.70 -> 0.00 MiB after quantize; GPTQ checkpoint load 554.87 -> ~0.00 MiB. - Quantize base-module offload: publish an Accelerate index that references byte-identical source safetensors tensors instead of re-serializing them; fall back to the bundle when any tensor differs. Llama-3.2-1B embed_tokens offload dir 525,336,576 B -> 367 B, bitwise restore. - Reword the LazyTurtle non-layer-wise warning: the header is re-parsed per layer while the data is read once. - Add targeted regressions for each fixed item and document the flows. Item 1 (CPU int4 checkpoint-layout snapshot) is not included: borrowing the snapshot storage is memory-neutral, and Torch exposes no inverse for the ATen int4pack layout, so an on-demand rebuild would be unverified on this host.
This was referenced Nov 16, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
#4577) GGUF_CPP_CUDA (#4387 item 2, left by #4436): the first forward uploaded qweight into a ggml CUDA buffer but kept the Torch qweight allocated, so every layer held its packed weight twice (211 + 211 MiB on Qwen2.5-0.5B q4_k_m). ggml cannot read the Torch allocation (ggml-cuda reserves zero-filled row padding past ggml_nbytes), so ownership is inverted: after the upload, qweight becomes a zero-copy __cuda_array_interface__ view of the ggml buffer. The buffer is reference counted (kernel, plans, any Torch view) and freed by the last holder under the ggml-cuda lock. clear_weight_cache keeps a weight that backs qweight; load_state_dict writes through; .cpu() frees it; __getstate__ keeps raw ggml handles out of copies. GGUF_CPP_CPU (#4439 trade-off): post_init copies an unaligned safetensors mmap qweight into 32-byte aligned memory, but the mmap pages it read stayed mapped next to the copy (+522 MiB RssFile on Llama-3.2-1B q4_k_m). The whole pages inside the source tensor are now madvise(MADV_DONTNEED)-ed when they are file backed and hold no private copy-on-write page; the zero-copy forward is unchanged. Measured (per-process counters): Qwen2.5-0.5B CUDA memory_allocated 471.2 -> 259.9 MiB, process VRAM 1328 -> 1104 MiB; Llama-3.2-1B CUDA 1023.1 -> 501.1 MiB, 2218 -> 1702 MiB; Llama-3.2-1B CPU VmRSS after load 2181 -> 1660 MiB (RssFile 943 -> 423, RssAnon unchanged), token time unchanged. Logits bitwise equal to main for GGUF_CPP_CUDA, GGUF_CPP_CPU, GGUF_TORCH and GGUF_TRITON; save() writes a byte-identical checkpoint. Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Summary
Four host-memory findings from #4395:
GGUF_CPP_CPUallocated a ggml buffer and copied the whole packed weight on every forward (522 MiB/token on Llama-3.2-1B).torch._C._host_emptyCache().embed_tokens(and final norm) into the offload dir, then read it back — 55% of a 1B run's offload dir, ~1.05 GB at 8B.Addresses items 2-5 of #4395. Item 1 (CPU int4 checkpoint-layout qweight snapshot) is intentionally not addressed: a real fix needs an exact inverse of ATen's CPU int4pack layout, which Torch does not expose, and the AVX512 path cannot be executed on this host — implementing it here would be unverified and risks checkpoint corruption.
What Changed
gguf_cpp.py: wrap caller-owned packed-weight storage withggml_backend_cpu_buffer_from_ptr+ggml_backend_tensor_allocwhen it is 32-byte aligned (ggml'sTENSOR_ALIGNMENT), and free the wrapper buffer in the existingfinally; unaligned/older-ABI builds keep the copy path.post_initaligns real safetensors mmap views.utils/torch.py,utils/model.py,models/base.py: release the pinned host cache (torch._C._host_emptyCache()) at the end ofquantize()and after the loader's checkpoint load.utils/offload.py,looper/module_looper.py,looper/weight_only_looper.py: write a safetensors reference for base modules whose tensors are untouched source tensors instead of re-serializing them; a missing or content-mismatched source falls back to the old bundle.utils/structure.py: warning now says the shard is re-opened and its header re-parsed per layer while the data is read once.docs/gguf-kernel-selection.md,docs/safetensors-reference-offload.md(+ README links). New regressions:tests/kernels/test_gguf_cpp_zero_copy_4395.py,tests/test_pinned_host_cache_4395.py,tests/test_offload_reference_4395.py.Tests
Measured: GGUF CPU 2048x8192 Q4_K_S layer M=1 copy 1.07-1.56 ms -> zero-copy 0.35-0.76 ms (~2.5-3.2x) with bitwise-equal output; pinned host bytes after
quantize()115.70 MiB -> 0.00 MiB (1B GPTQ), 554.87 -> ~0.00 MiB after a CUDA reload;embed_tokensoffload dir 525,336,576 B -> 367 B with a bitwise-identical restore and byte-identical saved tensor.Pre-existing failures left untouched (proven identical on pristine main):
test_gguf_cpp_matmul_accuracy_4316.py::test_default_path_is_unchanged(stale env-var test), 9 Q8_K/Q8_1 cases intest_gguf_all_formats.py, 14 AVX512 cases intest_kernel_audit_regressions.py(aarch64 host).Not executed: full GGUF model benchmark (no GGUF checkpoint cached), Marlin load (
CUDA_HOMEunset; usedGPTQ_TORCH, same accelerate pinned staging), 8B models.Logic Flow Documentation
New
docs/gguf-kernel-selection.mdanddocs/safetensors-reference-offload.mddocument the zero-copy selection/fallback and the reference-offload lifecycle (with the missing/mismatched-source fallback path) using real symbol names.Review Requirements