algo_cfg#
Compile a quantize config into an ordered list of scoped calibration stages.
This is the compile half of the calibration plan (the calibration_plan mode in
mode is the execute half). It is deliberately
side-effect free: it reads the already-quantized model’s structure — quantizer and
linear names — to resolve globs and validate, but it mutates nothing, runs no forward and
touches no data. Consequences the design leans on:
bad configs fail fast, before any expensive calibration runs;
it is testable without running a model;
the resulting plan is a pure function of
(config, model structure), so it is identical on every rank — which is what keeps predicate scoping from desynchronizing collectives in distributed calibration.
Both surfaces lower here. algorithm="max" becomes the single all-"*" stage, so
the legacy whole-model path is a special case of the scoped one rather than a second
engine.
Classes
What one calibration algorithm consumes, produces and needs to run. |
|
One algorithm applied to one scope — the unit of work in a calibration plan. |
Functions
Lower a quantize config into an ordered, validated list of scoped stages. |
|
Human-readable plan dump, used by the demos and for debugging. |
|
A stable hash of the plan. |
|
Build the |
- class AlgoCapabilities#
Bases:
objectWhat one calibration algorithm consumes, produces and needs to run.
Only the fields the compiler actually uses today are declared.
produces/requiresare a small open vocabulary of state tokens:weight,weight_amax,input_amax,pre_quant_scale,acts.- __init__(granularity, role, requires=frozenset({}), produces=frozenset({}), needs_forward=True, self_forwards=False, requires_absent=frozenset({}))#
- Parameters:
granularity (str)
role (str)
requires (frozenset[str])
produces (frozenset[str])
needs_forward (bool)
self_forwards (bool)
requires_absent (frozenset[str])
- Return type:
None
- granularity: str#
- needs_forward: bool = True#
- produces: frozenset[str] = frozenset({})#
- requires: frozenset[str] = frozenset({})#
- requires_absent: frozenset[str] = frozenset({})#
- role: str#
- self_forwards: bool = False#
Whether this stage could ride a forward pass shared with other stages.
- exception AlgoCfgValidationError#
Bases:
ValueErrorRaised when an
algo_cfgcannot be lowered into a valid plan.
- class AlgoStage#
Bases:
objectOne algorithm applied to one scope — the unit of work in a calibration plan.
- __init__(algo, cfg, scope, selector, order, entry, exclude=())#
- Parameters:
algo (str | None)
cfg (dict)
scope (str)
selector (str)
order (int)
entry (int)
exclude (tuple[tuple[str, str], ...])
- Return type:
None
- algo: str | None#
- property capabilities: AlgoCapabilities | None#
Declared capabilities of this stage’s algorithm, or
Noneif undeclared.Looked up rather than copied onto the stage: capabilities describe the algorithm, so a stage that carried its own copy could drift from the registry.
- cfg: dict#
- entry: int#
- exclude: tuple[tuple[str, str], ...] = ()#
- key()#
Execution-relevant identity, used for the plan hash.
entryis deliberately excluded: it records where in the config a stage came from, which is provenance, not behaviour. Leaving it out is what makes the legacyalgorithm="max"plan and the explicit[{"quantizer_name": "*", "cfg": ["max"]}]plan hash identical – the same execution, written two ways.- Return type:
tuple
- order: int#
- scope: str#
- selector: str#
- compile_algo_cfg(config, model=None, strict=True)#
Lower a quantize config into an ordered, validated list of scoped stages.
Pure: reads model structure only, mutates nothing, runs no forward.
- Parameters:
config (QuantizeConfig | dict) – a
QuantizeConfigor a mapping withalgo_cfg/algorithm.model (Module | None) – the already-quantized model. Required for the model-aware validation (scope resolution, roles, fused siblings, dependencies); when
Noneonly the config-only rules run.strict (bool) – raise
AlgoCfgValidationErroron violations.Falsedowngrades them to warnings, which is what lets a knowingly-broken pipeline be run for demonstration purposes.
- Returns:
The ordered plan. Stages run in list order.
- Return type:
list[AlgoStage]
- describe_plan(plan, model=None)#
Human-readable plan dump, used by the demos and for debugging.
- Parameters:
plan (list[AlgoStage])
model (Module | None)
- Return type:
str
- plan_hash(plan)#
A stable hash of the plan.
Distributed calibration is safe only if every rank runs the same stages over the same scopes — otherwise a predicate skips a quantizer on one rank and its amax all-reduce never matches, which hangs. Comparing this hash across ranks turns that silent deadlock into a clear error.
- Parameters:
plan (list[AlgoStage])
- Return type:
str
- stage_predicate(model, stage)#
Build the
should_processwrite-mask for a stage.The predicate is AND-ed into each algorithm’s existing
is_enabledfilter, so a stage writes only its targets and never toggles enable-state — reads (and hence the activations seen by search-based algorithms like AWQ and GPTQ) are unchanged.- Parameters:
model (Module)
stage (AlgoStage)
- Return type:
Callable[[str], bool]