Repository navigation
Conversation
added 6 commits
November 27, 2026 10:16
Search MSE-optimal affine grids and merge them across batches. Co-authored-by: Cline TICO-DCO-1.0-Signed-off-by: d.savchenkov <d.savchenkov@partner.samsung.com>
Plumb collection context and stage-2 hooks through the quantizer lifecycle. Co-authored-by: Cline TICO-DCO-1.0-Signed-off-by: d.savchenkov <d.savchenkov@partner.samsung.com>
This branch has not been deployed
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.
Based on: #782
Issue: #1152 (T1)
What
Adds the infrastructure for two-stage calibration and context-aware observation to the WrapQ quantization backend:
prepare_stage2()lifecycle boundary that propagates between the first and second calibration passes, notifying every quantization wrapper and every observer in the model tree._fq()→ObserverBase.collect()→_update_stats().Both mechanisms default to no-ops: models that never use them are completely unaffected.
Why
Some quantization strategies (e.g. observers that refine their grids from an intermediate candidate, MSE/percentile-style search seeded by a first-pass estimate) need to observe calibration data in two passes: gather preliminary statistics, transform/reconfigure internal state at the boundary, then re-calibrate with the improved configuration. Additionally, such observers need access to values available at the observation site (such as the layer weight) that a plain activation tensor does not carry.
Before this change there was no sanctioned place in the WrapQ lifecycle for that boundary, and
collect()accepted only the activation tensor. This PR adds the mechanical protocol so that feature work in later steps (stage-2-capable observers, context-aware wrappers) can build on it without touching base-class contracts again. It intentionally introduces no behavior change —freeze_qparams()remains the only place that derives, installs, and freezes qparams.Key Design Decisions
prepare_stage2()uses the exact same module/observer inventory asfreeze_qparams()but derives/installs/freezes nothing, changes no mode, and mutates no statistics. This keeps the freeze pipeline as the single owner of final-grid derivation and makes the call safe to repeat and safe before any calibration. Immutability is asserted by tests (twin models freeze to bit-identical grids with and without the boundary).collect()forwards**kwargsto_update_stats()only when the concrete implementation declares**kwargs(detected once per class via a cachedinspect.signatureprobe). This let all existing observers (MinMax, EMA, MSE, Percentile, MX, Identity) stay byte-for-byte unchanged, keeps calls crash-free when a wrapper passes context to a non-aware observer (context is silently dropped), and keeps mypy happy because overrides only widen the base one-argument signature. The alternative (changing the abstract signature to**kwargs) was rejected: mypy flagged every existing subclass override as incompatible._fq()passes context toobs.collect()only in the calibration branch; QUANT (fake_quant) and NO_QUANT paths ignore it, preserving the existing mode semantics. Context is also dropped when collection is disabled or the observer is frozen, matching the "disabled collection collects nothing" contract.prepare_stage2()on the model orPTQQuantizer.prepare_stage2(model)for recipe drivers); nothing inprepare(), calibration, orconvert()calls it._modules()guard that rejects wrapper overrides ofenable_calibration/freeze_qparamsnow also rejects overrides ofprepare_stage2, pointing subclasses to the local_prepare_stage2()hook instead — same pattern as_prepare_calibration/_validate_freeze.Changes
tico/quantization/wrapq/observers/base.py—collect(x, **kwargs)with gated forwarding to_update_stats(); cached_update_stats_accepts_context()signature probe; new no-opObserverBase.prepare_stage2()observer hook; docstring contracts for context keys and drop semantics.tico/quantization/wrapq/wrappers/quant_module_base.py—_fq(x, obs, **kwargs)forwards context tocollect()in CALIB mode only; publicprepare_stage2()delegating to the lifecycle walker; overridable no-op_prepare_stage2()module hook. (One hunk is a formatter-only reflow of a pre-existing ternary.)tico/quantization/wrapq/lifecycle.py— newprepare_stage2(root)walker (same_modules()/_inventory()walk asfreeze_qparams; observers notified first, then modules); override guard extended to reject subclass overrides ofprepare_stage2.tico/quantization/wrapq/quantizer.py—PTQQuantizer.prepare_stage2(model)user-facing entry point (explicit,@torch.no_grad(), returns the model) with the documented usage sequence.tico/quantization/wrapq/README.md— paragraph documenting the two-stage calibration boundary and its no-op-by-default contract.tico/quantization/wrapq/observers/mse.py— formatter-only reflow (no semantic change).test/quantization/wrapq/observers/test_collection_context.py— new, 6 tests for the context plumbing.test/quantization/wrapq/test_prepare_stage2.py— new, 9 tests for the boundary propagation, no-op guarantees, and quantizer hook.Tests
New deterministic CPU tests (no model downloads):
test_collection_context.py— kwargs reach a**kwargs-aware observer viacollect()(identity preserved); one-argument overrides tolerate and ignore context;MinMaxObserveryields identical statistics with and without context; disabled/frozen observers short-circuit before stats even with context.test_prepare_stage2.py—_fqforwards context only in CALIB mode; the walker reaches every wrapper and observer of a nested tree; safe before any calibration and on repeat; statistics and frozen results are provably unchanged (twin-model bit-identical grids after two boundary calls); quantizer hook propagates on hand-wrapped andprepare()-wrappednn.Linearmodels; subclass overrides ofprepare_stage2are rejected.Commands and output:
Regression suites (all pass):
Lint/format (
lintrunner, matching./ccex format): UFMT, PYLINT, MYPY all clean on every changed/added file.Example Scripts
None added. The intended consumer usage (for future stage-2 observers) is: