Skip to content

perf(looper): reuse the native first-layer capture for GPTAQ/FOEM (#5831) - #5890

Merged
Qubitium merged 2 commits into
mainfrom
fix/5831-first-capture-reuse
Nov 20, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5831-first-capture-reuse

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

What Changed

  • gptqmodel/looper/module_looper.py:
    • _loop_impl() asks _reusable_initial_capture() before running cache_inputs for a processor. When it returns the native capture, the GPTQ processor receives copy.deepcopy(capture) and the second model forward is skipped. The clone gives each stream private replay state because per-layer kwargs and hidden frontiers diverge as the native and quantized passes advance.
    • _reusable_initial_capture() requires: previous processor is NativeProcessor, current is GPTQProcessor with gptaq/foem active, identical calibration_dataset object (a distinct adapter/rotation dataset still captures its own), a generic model capture path, and a plain non-empty InputCache.
    • The reused-capture local is deleted after the handoff so it cannot outlive its owner if a checkpoint restore replaces the native cache.
    • _model_initial_capture_is_generic() compares every capture hook (run_input_capture, the pre-quantize hooks, input/example capture hooks, first-layer positional/kwargs capture, and build_quantization_input_cache) against BaseQModel. Models with vision staging, looped-decoder capture, or custom positional inputs keep the old second capture until qualified.
  • tests/test_gptaq_foem_first_capture_5831.py: 6 tests — one capture per batch for GPTAQ and FOEM, bitwise-equal packed tensors against a forced double capture, the reused cache being a distinct tensor copy, and a capture-hook override disabling reuse.

No config, packing, kernel, or model-definition changes.

Tests

# fix branch (head a09de1f5b)
PYTHONPATH=$PWD python -m pytest -q tests/test_gptaq_foem_first_capture_5831.py
# 6 passed

PYTHONPATH=$PWD python -m pytest -q tests/test_gptaq_foem_first_capture_5831.py \
  tests/test_gptaq.py tests/test_gptaq_foem_batch.py \
  tests/test_gptaq_foem_tiled_pre_update_5647.py \
  tests/test_gptaq_foem_conv_geometry_5763.py \
  tests/test_gptaq_foem_processor_percent_fallback_5755.py \
  tests/test_rehome_gptaq_foem_dxxt_4312.py \
  tests/test_native_sibling_input_sharing.py tests/test_looped_decoder_capture.py \
  tests/test_stage_inputs_capture.py
# 84 passed, 3 skipped

PYTHONPATH=$PWD python -m pytest -q tests/test_looper_concurrency.py \
  tests/test_module_looper_quant_log_4321.py tests/test_module_looper_disk_probe.py \
  tests/test_pre_looper_failure_cleanup_4862.py
# 65 passed

uvx [email protected] check gptqmodel docs/eora tests setup.py
# All checks passed!

Red evidence on base 0e0c85066 with the same test file and unfixed library: 4 failed, 2 passed — both per-batch capture counts (32 instead of 16) and both bitwise comparisons (the reuse hook does not exist). The two guard tests pass on both revisions by design.

Environment note: tests/test_gptaq_shuffle_pairing_4869.py fails identically on base and head in this CPU/MPS host (Device not in pool: mps:0), so it could not be used as a local regression signal. No CUDA host was available; CUDA suites were skipped.

Measured Impact

Fixture: 2-layer FP16 Llama (hidden 64), 16 variable-length calibration rows (88-118 tokens), batch_size=1, sort="shuffle", GPTAQ and FOEM (alpha=0.25) in separate runs. Apple Silicon macOS, CPU, torch 2.15.0, transformers 5.23.0, torch.set_num_threads(1), offload_to_disk=False for the timing fixture. Median of 5 interleaved before/after runs per config. "Capture phase" is the summed ModuleLooper.cache_inputs wall time plus the replacement clone; the after value is capture + clone.

Metric (unit) Before After Change (after − before)
GPTAQ model-level capture forwards (count) 32 16 -16 (-50%)
FOEM model-level capture forwards (count) 32 16 -16 (-50%)
GPTAQ cache_inputs calls per run (count) 2 1 -1
FOEM cache_inputs calls per run (count) 2 1 -1
GPTAQ capture phase (ms) 5.71 3.59 (3.02 capture + 0.57 clone) -2.12 (-37.1%)
FOEM capture phase (ms) 5.69 3.53 (3.00 capture + 0.53 clone) -2.16 (-38.0%)
Packed state-dict tensors differing from double capture (count) 0 of 77 0 of 77 0 (bitwise equal)

Accuracy: the reused path and the forced double-capture path produce byte-identical packed tensors for GPTAQ and FOEM (77 tensors each, compared as uint8 views). The quantized weights are therefore unchanged; only the redundant forward disappears. VRAM was not measured: the clone's transient host copy matches the allocation the skipped second capture would have made, so peak host memory is expected to be unchanged, but that was not instrumented.

Benchmark method: a local script that wraps ModuleLooper.cache_inputs and copy.deepcopy with timers and runs the same fixture with reuse enabled and with _reusable_initial_capture forced to return None; base 0e0c85066, head a09de1f5b.

Model Support / Module Plan

Not applicable — no MODEL_MAP entries, model definitions, aliases, or fusion configs change.

Logic Flow Documentation

ModuleLooper._loop_impl previously called cache_inputs for every processor whose dataset verification passed, so GPTAQ/FOEM forwarded every batch once for NativeProcessor and again for GPTQProcessor. The new _reusable_initial_capture branch runs before that call and reuses the native capture only when the ownership, dataset-identity, model-hook, and cache-shape checks above all pass. The branch is skipped for every other processor chain (plain GPTQ, EoRA, AWQ/Paro, looped decoders, vision models with capture overrides), which keeps the existing capture path unchanged.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.

@Qubitium
Qubitium merged commit 051efa8 into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5831-first-capture-reuse branch November 20, 2026 12:42
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.

[Audit][GPTAQ/FOEM] Native and GPTQ processors repeat the same first-layer capture

1 participant