Repository navigation
refactor(gptq): use SharedInputCaptureMixin instead of GPTQProcessor's private copy - #6428
Merged
Merged
Conversation
Qubitium
marked this pull request as ready for review
November 29, 2026 20:13
…s private copy GPTQProcessor carried its own copy of the shared-input leader election (_resolve_shared_input_plan, begin/end_shared_input_capture, shared_input_leader and the inline election loop). #4514 introduced SharedInputCaptureMixin with the same algorithm for QQQ, VPTQ, MACaQ and EXL3 but left the GPTQ copy in place. GPTQProcessor now derives from SharedInputCaptureProcessor and keeps only its GPTQ-specific parts as mixin hooks: - _shared_input_task_enabled: the per-task hessian.dedup_shared_inputs check. - _shared_input_task_signature: the former _hessian_capture_signature (GPTQ/ADMM-Q, GPTAQ, FOEM and JointQ cohorts), renamed. - _shared_input_begin_cohort: clears the plain-GPTQ factor caches of the subset. The mixin hook now also receives subset_names, because GPTQ only clears tasks in the subset. - _shared_input_task_adopt: adopts the Hessian, then attaches one SharedHessianFactorCache per plain-GPTQ leader. The leader's cache was cleared when the cohort began, so its first follower creates the cache and the rest join it. That matches the old per-call dict without the processor holding a reference to cached factors after the pass ends. Shared-input dedup decides which modules share a Hessian, so the leaders, telemetry, per-task H/dXXT/quantized weights and factor-cache identity graph were compared against origin/main on CPU fixtures. GPTQ and GPTAQ quantization of Qwen2.5-0.5B-Instruct and of a tiny random Mixtral (a shared-input verified MoE, so expert gate/up share a Hessian) were also compared on GPU, together with the sorted quant-log rows and dedup telemetry. All of them are bit-identical. Two observable differences remain. The generation counter now also advances when a model has no shared-input plan; no factor cache exists then, and every cache that is created keeps the same generation as before. The mismatch warning says "accumulation settings" instead of "capture settings". Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium
force-pushed
the
refactor/gptq-shared-input-mixin
branch
from
November 29, 2026 20:43
0ed0386 to
044903f
Compare
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
Behavior-preserving debloat.
GPTQProcessorcarried a private copy of the shared-input Hessian leader election:_resolve_shared_input_plan,begin_shared_input_capture,end_shared_input_capture,shared_input_leaderand the election loop insidebegin. When #4514 addedSharedInputCaptureMixinwith the same algorithm for QQQ, VPTQ, MACaQ and EXL3, it left that copy in place.GPTQProcessornow derives fromSharedInputCaptureProcessor, like the other calibrated processors, and keeps only its GPTQ-specific behavior as four mixin hooks.Net diff:
6 files changed, 73 insertions(+), 173 deletions(-)(-100 lines). Thegptqmodel/part is3 files changed, 33 insertions(+), 163 deletions(-)(-130).No issue is claimed. This is one of the maintainer-requested debloat refactors.
What Changed
gptqmodel/looper/gptq_processor.pyLoopProcessor→SharedInputCaptureProcessor. Deleted the private state init,_resolve_shared_input_plan,begin_shared_input_capture,end_shared_input_captureandshared_input_leader(about 150 lines)._hessian_capture_signatureis renamed to the mixin hook_shared_input_task_signature, with the same body. Added_shared_input_task_enabled,_shared_input_begin_cohortand_shared_input_task_adopt.gptqmodel/looper/shared_input.py_shared_input_begin_cohort(tasks)now also receivessubset_names, because GPTQ clears factor caches only for tasks in the subset. No other processor overrides this hook.begin_shared_input_capturematerializessubset_namesonce, so the hook and the election see the same names. Module docstring updated.gptqmodel/looper/g2ptq_processor.pydocs/gptq-hessian-accumulation.mdtests/test_shared_input_hessian_dedup_methods.pytest_shared_input_capture_methods_resolve_to_single_shared_basenow also runs forGPTQProcessor.tests/test_shared_input_hessian_dedup.pytest_factor_cache_is_one_per_leader_and_fresh_per_cohort.The GPTQ hooks:
endkept a call-localcachesdict keyed by the leader's name. The debloat map suggested a processor-level_factor_cachesdict that would be reset at the next cohort. That dict would keep every leader's cached Cholesky factors alive between passes, so I did not use it. The leader task's own cache slot serves as that dict instead.beginclears it, the first follower fills it, and later followers join it. Only the tasks reference a cache, as before.leader.nameequals the old dict key becauseGPTQProcessor.tasksis keyed bymodule.name, which isGPTQ.name. Peaktorch.cuda.max_memory_allocatedis unchanged (see the GPU A/B)._hessian_capture_signatureis renamed, and nothing outsidegptq_processor.pycalled it._shared_input_capture_generationnow also advances when the model has no shared-input groups. No factor cache exists in that case. Every factor cache that is created gets the same generation as onorigin/main, as the structural A/B shows. The generation is used only inside one cache's key.dedup_shared_inputs_enabled(self.qcfg)where the old code testedself.qcfg.hessian.dedup_shared_inputs. These are the same here:_normalize_hessianalways gives GPTQ configs aHessianConfig.Tests
New and extended tests:
tests/test_shared_input_hessian_dedup.py::TestProcessorLeaderElection::test_factor_cache_is_one_per_leader_and_fresh_per_cohortruns two leaders (q→k,v and g→u) in one subset, twice. It checks one cache per leader shared by its followers, the cohort and leader strings, and a fresh cache with a higher generation in the second cohort. It passes onorigin/mainand on this branch. Red evidence on this branch, with each mutant applied separately:clear_shared_hessian_cache()from_shared_input_begin_cohortmakes it fail atcaches[1][0] is not caches[0][0](a stale cache is reused)._shared_input_task_adoptmakes it fail attasks["self_attn.k"]._shared_hessian_cache is attn(followers stop sharing).tests/test_shared_input_hessian_dedup_methods.py::test_shared_input_capture_methods_resolve_to_single_shared_base[GPTQProcessor-qcfg0]is new. Onorigin/mainit fails atissubclass(GPTQProcessor, SharedInputCaptureProcessor).Commands and results:
Structural A/B: election, adoption and factor caches (CPU)
struct_ab.pyis attached in the collapsed block below. It drivesGPTQProcessor.begin/preprocess/hook/end_shared_input_captureand thenquantize()on CPU. The fixture is a two-group tree (self_attnq,k,vin=x;mlpg,uin=y) with correlated, non-diagonal activations. The scenarios are the product of:konly throughdynamic, akstaging-dtype mismatch, GPTAQ, FOEM, JointQ and ADMM-Q;vfor GPTQ), and a group split across subsets;is_lm_head_module, no plan, a leader pruned beforeend, and two capture passes over the same tasks.For each scenario it records the elected leaders, the
endtelemetry, every capture counter and nsamples, sha256 ofH,dXXTand the quantized weight, the factor-cache identity graph (which tasks share one cache object, with its cohort, leader and generation), and the warnings.GPU A/B: bit-identical checkpoints
One GPU. Each model was quantized on
origin/main(6b25d2c) and on this branch with the same config, seed and calibration (64 fixed synthetic texts,batch_size=1, 4-bit g128). The script compares every saved tensor byte-for-byte, every*.json, the sortedquant_logrows withtime,fwd_timeand(v)ramdropped, and everyend_shared_input_capturetelemetry record.(v)ramis Device-SMI's device-wide usage, which includes other tenants on the shared GPU, so allocator peak is compared separately. Each log shows thatgptqmodel.__file__came from the intended checkout.samples)7553)verified7553)verifiedmixtralis a shared-input verified MoE: q/k/v plus per-expert gate/up; 2 layers, 4 experts, top-2, hidden 256)verifiedverifiedThe tiny Qwen3.5-MoE checkpoint has
model_typeqwen3_5_moe. That type is not inQwen3_5_MoeQModel.shared_input_verified_model_types; onlyqwen3_5_moe_textis listed there. Its plan therefore has no explicit groups and no module shares a Hessian, which is intended. That is why I added the tiny Mixtral, built bymake_tiny_mixtral.pywithtransformers.MixtralForCausalLM,torch.manual_seed(0)and the Qwen2.5 tokenizer, as the MoE case that does dedup. OLMoE-1B-7B is cached, but itsmodule_treehas no:in=tags, so it would not exercise dedup either.quant_ab.py / struct_ab.py / make_tiny_mixtral.py
quant_ab.pystruct_ab.pymake_tiny_mixtral.pyMeasured Impact
Not applicable: no claimed issue and no runtime change. Peak
torch.cuda.max_memory_allocatedmatches on the tiny Mixtral (table above).Model Support / Module Plan
Not applicable: no model, alias, definition or module-plan change.
Model test node IDs and results (or "not applicable"): not applicable.
AWQ onboarding safety
Not applicable: AWQ is not touched.
AWQ evidence and per-variant policy (or "not applicable" with a reason): not applicable.
Logic Flow Documentation
file:lineevidence.docs/repeats the diagrams, titled for the mechanism, and is linked fromREADME.md.Diagram summary: the election itself does not change, but GPTQ's copy is replaced by the shared mechanism. The existing page
docs/gptq-hessian-accumulation.md("Shared-input dedup for calibrated quantizers") already has this flow diagram. Its text and table are updated here to say that GPTQ uses the mixin.flowchart TD A["stage_subset: subset_names"] --> B["SharedInputCaptureMixin.begin_shared_input_capture"] B --> C["GPTQProcessor._shared_input_begin_cohort<br/>clear plain-GPTQ factor caches in subset"] C --> D{"is_lm_head_module or dedup off?"} D -- Yes --> Z["return empty: every module captures"] D -- No --> E["_resolve_shared_input_plan(model)"] E --> F["elect_shared_input_leaders<br/>enabled_for = _shared_input_task_enabled<br/>signature_for = _shared_input_task_signature"] F --> G{"signatures equal?"} G -- No --> W["log.warn; follower captures independently"] G -- Yes --> H["_shared_input_leaders = follower to leader"] H --> I["pre_process_fwd_hook: follower hook skips"] I --> J["SharedInputCaptureMixin.end_shared_input_capture"] J --> K{"leader and follower tasks present?"} K -- No --> M["skip; telemetry status = mismatch"] K -- Yes --> L["GPTQProcessor._shared_input_task_adopt<br/>adopt_hessian_from, then one SharedHessianFactorCache per plain-GPTQ leader"] classDef fail fill:#fdd,stroke:#c33 classDef ok fill:#dfd,stroke:#060 class W,M fail class L okorigin/main6b25d2c)gptq_processor.py:251-259, inline copySharedInputCaptureMixin._init_shared_input_capture(shared_input.py:105), called atgptq_processor.py:249GPTQProcessor._resolve_shared_input_plan(gptq_processor.py:511)SharedInputCaptureMixin._resolve_shared_input_plan(shared_input.py:136), same bodybegin(gptq_processor.py:596-599)GPTQProcessor._shared_input_begin_cohort(gptq_processor.py:545)self.qcfg.hessian.dedup_shared_inputs(gptq_processor.py:601)dedup_shared_inputs_enabled(self.qcfg)(shared_input.py:169), the same for GPTQ configsgptq_processor.py:612-636)elect_shared_input_leaders(shared_input.py:42)task.qcfg.hessian.dedup_shared_inputs(gptq_processor.py:619)GPTQProcessor._shared_input_task_enabled(gptq_processor.py:542)_hessian_capture_signature(gptq_processor.py:552)_shared_input_task_signature(gptq_processor.py:517), same bodyend, call-localcachesdict (gptq_processor.py:642-667)SharedInputCaptureMixin.end_shared_input_capture(shared_input.py:187) andGPTQProcessor._shared_input_task_adopt(gptq_processor.py:553)gptq_processor.py:672-685)shared_input.py:204-217), same keys and valuesshared_input_leadergptq_processor.py:687shared_input.py:219, same bodyReview Requirements
Kernel Accuracy Requirements
Notes
_shared_input_task_adoptreads the privateGPTQ._shared_hessian_cacheslot from the processor. That avoids keeping a processor-level cache dict alive. A public getter onGPTQwould be a separate, purely cosmetic change.G2PTQProcessorstill overridesbegin_shared_input_captureand callssuper(), which now resolves to the mixin. Its tasks still get aNonesignature.🤖 Generated with Claude Code