Repository navigation
fix(gguf): keep one resident copy of the packed weight on CUDA and CPU (#4387, #4395) - #4577
Merged
Merged
Conversation
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.
Removes the two remaining duplicate copies of GGUF packed weights left after #4436 (#4387 item 2) and #4439 (#4395). Output is bitwise identical, and
save()is byte-identical to main.1. GGUF_CPP_CUDA kept the packed weight twice (#4387 item 2)
On the first forward,
GGUFCudaKernel._cuda_weightuploadsqweightinto a shared ggml CUDA buffer, but the Torchqweightstayed allocated next to it. ggml can't read Torch's memory in place: ggml-cuda pads rows that aren't a multiple of 512 pastggml_nbytes.Change (
gguf_cpp.py): the ggml buffer now owns the bytes, andqweightbecomes a zero-copytorch.as_tensorview of it (via__cuda_array_interface__, created underinference_mode(False)). The Torch allocation is released.state_dict()tensors) and freed under the ggml-cuda lock after a device sync.load_state_dictwrites straight into the buffer;.cpu()frees it, and the next CUDA forward re-uploads;clear_weight_cachekeeps a buffer that backsqweight.__getstate__drops the raw ggml handles, so a deepcopy or pickle gets its own weight. Before, a deepcopy after a forward copied the handles, so the buffer could be freed twice.qweightlives outside the caching allocator, so it's invisible totorch.cuda.memory_allocated.memory_allocatedDecode time is unchanged within noise.
2. GGUF_CPP_CPU aligned copy left the checkpoint pages resident (#4439 trade-off)
post_initcopies each unaligned mmap'dqweightinto 32-byte-aligned memory for ggml zero-copy, but the file pages it read stayed mapped. Every Llama-3.2-1B qweight starts at 24 mod 32 (safetensors header), and remapping can't fix that.Change: after the aligned copy,
madvise(MADV_DONTNEED)drops the whole file-backed pages inside the source tensor. It runs only when/proc/self/mapssays the range is file-backed and/proc/self/pagemapshows no copy-on-write or swapped page; otherwise it skips (heap, non-Linux, edge pages shared with other tensors).MADV_PAGEOUTwas rejected because it keeps dirty pages of a freshly written file.Peak VmHWM drops by ~520 MiB, and token time is unchanged within noise. RssAnon is unchanged: the single remaining copy is the aligned anonymous one. Binding ggml to the unaligned mmap pointer was rejected as undefined behaviour against ggml's 32-byte contract.
Verification
state_dict()qweights equal the checkpoint;save()is byte-identical to main; reloading gives equal logits. GGUF_TORCH/TRITON are untouched and bitwise equal.tests/kernels/test_gguf_cuda_weight_storage_4387.py(7 tests; 6 fail on main): allocated memory drops by the qweight size, state-dict round trip and view lifetime,load_state_dictwrite-through,.cpu()/.cuda(), deepcopy isolation.tests/kernels/test_gguf_cpp_cpu_mmap_residency.py(3 tests): source-page residency is 0 afterpost_init(main leaves all 143 pages resident), copy-on-write pages are kept with their data, heap is never advised.backs/as_tensor/devicecontract.*gguf*tests plus selection/registry/audit, 1583 collected): 1089 passed, 488 skipped, 6 failed. The same 6test_gguf_all_formatsq8_1/q8_k fallback cases fail on main.torch.as_tensor-over-__cuda_array_interface__probe is added to the free-threaded compat ledger; it passes underPYTHON_GIL=0.docs/kernel-scratch-lifecycle.mdanddocs/gguf-kernel-selection.mdhave flow diagrams and before/after tables.🤖 Generated with Claude Code