Skip to content

fix(looper): share bitwise-identical native sibling inputs (#4391) - #4443

Merged
Qubitium merged 1 commit into
mainfrom
fix/native-sibling-inputs-4391
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/native-sibling-inputs-4391

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

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 at batch_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 by process(), 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.
  • Entry tagging (batch_index, tensor) and _take_native_input pop semantics are unchanged.
  • Consumers audited: GPTAQ/FOEM only use .to/reshape/unsqueeze/.t and out-of-place arithmetic, so shared entries stay read-only (the bs=1 alias path already relies on this).
  • Docs: 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-135M with GPTAQ at bs=4 (8 calibration rows, instrumented at NativeProcessor.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.safetensors sha256 identical (124df2e8...c42042), quant_log.csv identical 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-identical H/dXXT versus independent copies and never mutate entries.

Logic Flow Documentation

docs/native-calibration-batch-split.md documents the sibling-share cache lifecycle (token/invalidation, epoch, read-only contract) and docs/multi_gpu_quantization.md notes the data-parallel thread-local scope.

Review Requirements

  • Reviewed; no env-var behavior switches.
  • Sharing only applies where the masked slices are bitwise identical; a missing/mismatched mask keeps the historical layout.
  • Bitwise checkpoint identity verified against pristine main.

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.
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