Repository navigation
Description
Summary
The replay after quantization (_replay_layer_outputs -> ForwardExecutor.run_single/run_parallel) builds the next layer's inputs as a new list, while the full list of the current layer's inputs stays referenced. That list is only dropped afterwards, by processor.clear_cache_data(). The replay is the last reader of those inputs, but each consumed batch is still kept alive until every batch has been replayed. From layer 1 onward, both sets live on the quantization device when calibration_data_device=None (the default). Peak activation memory at every layer boundary is therefore two full calibration activation sets instead of one plus one batch.
Impact
- Goal: quantization peak VRAM (or host RAM when
calibration_data_device="cpu"). - Severity: medium-high for large calibration sets or wide models. The doubled term is
n_samples x seq_len x hidden x dtype_bytes. Example: Llama-3-70B (hidden 8192), 512 x 2048 tokens, bf16: one set is 16 GiB, so the replay peak is 32 GiB of activations on the GPU on top of the layer and its Hessian workspace. With 128 x 2048 tokens on an 8B model it is about 2 GiB extra. - Speed is unaffected. This is purely a lifetime extension, with no numeric change.
Location (@ 85caf6c)
gptqmodel/looper/stage_layer.py:1015: stage-local alias of the cache list:layer_inputs = processor.inputs_cache.layer_inputsgptqmodel/looper/stage_layer.py:1311-1328: replay call,layer_outputs = _replay_layer_outputs(..., layer_inputs=layer_inputs, ...)gptqmodel/looper/forward_executor.py:389-548(run_single): readscached_layer_inputs = layer_inputs[batch_idx]and appends the output with_store_output(outputs, batch_idx, primary). The input slot is never released.gptqmodel/looper/forward_executor.py:729-820(run_parallel): same pattern, usingresults[batch_idx] = primary.gptqmodel/looper/stage_layer.py:1397-1400: the inputs are dropped only after the whole replay:
if forward.replay_after_process:
processor.clear_cache_data()
processor.receive_layer_inputs(layer_outputs)
layer_inputs = processor.inputs_cache.layer_inputs- Device placement:
forward_executor.py:529-532puts outputs oncur_layer_devicewhencalibration_data_device is None. From layer 1 onward the inputs (the previous layer's outputs) are on the same device as well.
Mechanism
During replay of layer N, processor.inputs_cache.layer_inputs and the stage-local layer_inputs both reference the list holding layer N's inputs for every batch. run_single produces layer N's outputs batch by batch into a separate list. Nothing between the replay and clear_cache_data() reads the inputs again: the next statements consume only layer_outputs. Each consumed input batch is still held until the loop ends. At the last batch, both complete sets are resident.
Reproduction / Validation
The repo package cannot be imported on this host because of the torch-version gate. The script re-implements the replay loop with the same reference structure: a cache list, a stage alias, outputs appended to a new list, and the cache swapped at the end. It measures allocator growth on MPS during the replay. Script repro_replay_peak.py (32 batches x 2048 tokens x hidden 2048, fp16):
for batch_idx in range(N_BATCH): # forward_executor.run_single loop
out = layer(layer_inputs[batch_idx][0])
outputs.append([out]) # _store_output
if release_consumed:
layer_inputs[batch_idx] = [] # proposed: drop the consumed batch
...
cache.layer_inputs = outputs # clear_cache_data + receive_layer_inputsActual output (MPS, torch 2.14.0.dev):
release_consumed=False one activation set=256 MiB peak extra during replay=256 MiB (1.00x)
release_consumed=True one activation set=256 MiB peak extra during replay=0 MiB (0.00x)
The current structure needs a full extra activation set at the boundary, on top of the inputs already resident. Releasing each consumed batch keeps the peak at one set plus one batch. This is not executed on CUDA or against the real ForwardExecutor.
Suggested fix
Let the layer stage tell the executor that this replay is the final consumer of the inputs, and release each input slot as soon as its output exists:
# forward_executor.run_single(..., consume_inputs: bool = False)
if need_outputs:
if module_output is not None:
...
self._store_output(outputs, batch_idx, primary)
+ if consume_inputs:
+ layer_inputs[batch_idx] = [] # last use: free this batch nowDo the same in run_parallel once a position's futures resolve. Pass consume_inputs=True from _replay_layer_outputs only when the replay feeds processor.receive_layer_inputs() (forward.replay_after_process). Leave it off when model_output_required/boundary receivers or multi-branch caches still need the inputs (branch_first_layer_inputs holds its own reference). In that case the list is replaced right after the replay anyway (clear_cache_data). Alternatively, write outputs in place into layer_inputs[batch_idx] when the shapes and devices match.
Notes
src_inputsis[]in the common path (stage_inputs_capture.py:171,659), so it does not add a third reference.- For layer 0 the captured inputs live on CPU (
stage_inputs_capture.py:264-290,data_device=CPU), so the GPU doubling starts at layer 1. - The magnitude for real models is computed from shapes. It is not measured on CUDA.
Found by automated code audit (Claude Code).
Activity
Qubitium commentedon Nov 20, 2026
Agent claim released:
agent-fb5145b84190finished this issue.Pull request: #5928
Implementing per-batch input release during post-quant replay on CPU
fix(looper): release replayed calibration batches at their last use (#…