Repository navigation
fix(looper): share bitwise-identical native sibling inputs (#4391) - #4443
Merged
Merged
Conversation
NativeProcessor._split_batch_samples built a fresh per-sample masked copy for every sibling module fed by the same source activation, so q/k/v held three copies and gate/up held two copies of the same bytes. Reuse one entry per (batch index, source storage, mask) in a thread-local cache. The cache is invalidated when the batch index, keep-mask, or layer forward pass changes, so it never pins a finished batch or matches a recycled source address. Entries keep the (batch_index, tensor) tagging and pop semantics; GPTAQ/FOEM only read them, so the shared storage stays read-only. Measured on cached SmolLM2-135M with GPTAQ at batch_size=4: 1680 native entries drop from 1680 to 960 distinct storages (44,029,440 -> 28,788,480 bytes, -34.6%), and the saved model.safetensors is byte-identical to origin/main.
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
NativeProcessor._split_batch_samplesbuilt a fresh per-sample masked copy for every sibling module fed by the same source activation, so q/k/v held three copies and gate/up held two copies of the same bytes atbatch_size > 1. At bs=1 the entries already alias the shared input. On Llama-3.2-1B GPTAQ4 bs=4 that was 359.7 MiB of bitwise-duplicate sibling copies (30% of the native cache, +34% at layer boundaries), scaling with samples x tokens x hidden.Addresses the first half of item 4 of #4391 (the GPTAQ/FOEM H/dXXT adoption half is separate). Items 1-3 shipped in #4432/#4438.
What Changed
looper/native_processor.py: keep one entry per(batch index, source storage, mask)in a thread-local cache and return the same storage to sibling modules. Key:(untyped_storage().data_ptr(), storage_offset, shape, stride, dtype, device); the cache token also carries the batch index, the keep-mask identity and a forward-pass epoch bumped byprocess(), so a finished batch or a recycled source address can never match a stale entry. The cache holds the source and mask alive while entries are cached.(batch_index, tensor)and_take_native_inputpop semantics are unchanged..to/reshape/unsqueeze/.tand out-of-place arithmetic, so shared entries stay read-only (the bs=1 alias path already relies on this).docs/native-calibration-batch-split.md,docs/multi_gpu_quantization.md. New tests:tests/test_native_sibling_input_sharing.py.Tests
PYTHONPATH=/root/work-4391c /root/venv/bin/python -m pytest \ tests/test_native_sibling_input_sharing.py tests/test_gptaq_foem_batch.py \ tests/test_gptaq_foem_multigpu.py tests/test_moe_gptaq_foem.py \ tests/test_gptq_keep_mask_device.py tests/test_processor_mask_flags.py \ tests/test_moe_padding_rows.py -q 36 passed, 6 skipped /root/venv/bin/python -m ruff check gptqmodel/looper/native_processor.py \ tests/test_native_sibling_input_sharing.py All checks passed!Measured on cached
HuggingFaceTB/SmolLM2-135Mwith GPTAQ at bs=4 (8 calibration rows, instrumented atNativeProcessor.process): 1680 entries stay 1680 but distinct storages drop 1680 -> 960 and distinct bytes 44,029,440 -> 28,788,480 (-34.6%). Checkpoint A/B against pristine main with the same seed/data:model.safetensorssha256 identical (124df2e8...c42042),quant_log.csvidentical apart from row order and per-row time.New tests pin: q/k/v and gate/up share
untyped_storage().data_ptr()at bs>1; the bs=1 mask-less path still aliases the source; distinct sources/batches/forward passes do not share; per-batch tagging is preserved; consumers see bitwise-identicalH/dXXTversus independent copies and never mutate entries.Logic Flow Documentation
docs/native-calibration-batch-split.mddocuments the sibling-share cache lifecycle (token/invalidation, epoch, read-only contract) anddocs/multi_gpu_quantization.mdnotes the data-parallel thread-local scope.Review Requirements