llmcompressor.modifiers.quantization.quantization.mixin
Classes:
-
QuantizationMixin–Mixin which enables a Modifier to act as a quantization config, attaching observers,
QuantizationMixin
Bases: HooksMixin
Mixin which enables a Modifier to act as a quantization config, attaching observers, calibration hooks, and compression wrappers to modifiers
Lifecycle:
- on_initialize: QuantizationMixin.initialize_quantization
- Attach schemes to modules
- Attach observers to modules
- Disable quantization until calibration starts/finishes
- on_calibration_start: QuantizationMixin.start_calibration
- Attach calibration hooks
- Apply calibration status
- Enable quantization during calibration
- on_calibration_end: QuantizationMixin.end_calibration
- Remove calibration hooks
- Apply freeze status
- Keep quantization enabled for future steps
NOTE: QuantizationMixin does not update scales and zero-points on its own,
as this is not desired for all Modifiers inheriting from it. Modifier must
explicitly call observe(modules, base_name="weight") then
update_qparams(modules, base_name="weight").
See QuantizationModifier.on_event method for example
NOTE: When no observer is specified, weight observers default to
imatrix_mse if calibration data is available to the session and
memoryless_minmax otherwise. Activation observers default to
minmax.
Parameters:
-
config_groups–dictionary specifying quantization schemes to apply to target modules. Modules not matching a scheme target will NOT be quantized.
-
targets–list of layer names to quantize if a scheme is provided. If unset, will contain all targets listed in config_groups. If config_groups is also unset, will default to ["Linear"] (i.e. all Linear layers will be targeted). This field is not the source of truth for finding all matching target layers in a model. Additional information can be stored in
config_groups. Use self.resolved_targets instead. -
ignore–optional list of module class names or submodule names to not quantize even if they match a target in config_groups. Defaults to empty list.
-
scheme–a single quantization scheme to apply to the model. This is a dictionary that supports all keys from QuantizationScheme except targets, which will be set to the targets parameter set at the modifier level. Can also be set to a dictionary of the format
preset_scheme_name: targetsfor example:W8A8: ['Linear']for weight and activation 8-bit. -
kv_cache_scheme–optional QuantizationArgs, that specify the quantization of the kv cache. If None, kv cache is not quantized. When applying kv cache quantization to transformer AutoModelForCausalLM, the kv_cache_scheme gets converted into a QuantizationScheme that: - targets the
q_projandk_projmodules of the model. The outputs of those modules are the keys and values that might be cached - quantizes the outputs of the aforementioned layers, so that keys and values are compressed before storing them in the cache There is an explicit assumption that the model contains modules withk_projandv_projin their names. If this is not the case and kv_cache_scheme != None, the quantization of kv cache will fail -
weight_observer–optional observer name for weight quantization. Overrides the default observer specified in the scheme. Valid values include "minmax", "mse", "static_minmax", "memoryless_minmax", "memoryless_mse", and "imatrix_mse". Defaults to "imatrix_mse" when calibration data is available and "memoryless_minmax" otherwise.
-
input_observer–optional observer name for input activation quantization. Overrides the default observer specified in the scheme. Valid values include "minmax", "mse", "static_minmax", "memoryless_minmax", "memoryless_mse".
-
output_observer–optional observer name for output activation quantization. Overrides the default observer specified in the scheme. Valid values include "minmax", "mse", "static_minmax", "memoryless_minmax", "memoryless_mse".
-
observer–optional dictionary to specify observers for multiple quantization types at once. Keys can be "weights", "input", or "output". Values are observer names. Example: {"weights": "MSE", "input": "MSE"}. If both individual observer parameters (weight_observer, input_observer, output_observer) and observer dict are provided, the observer dict takes precedence.
-
bypass_divisibility_checks–if True, skip the check that weight columns are divisible by group_size for GROUP/TENSOR_GROUP. Use when your runtime (e.g. vLLM) supports non-divisible dimensions. Defaults to False.
Methods:
-
end_calibration–Remove calibration hooks and observers, and set the model status to frozen.
-
has_config–Determine if the user has specified a quantization config on this modifier
-
initialize_quantization–Attach quantization schemes to modules in the model according to
-
resolve_quantization_config–Returns the quantization config specified by this modifier
-
start_calibration–Attach observers, register activation calibration hooks (including
-
start_layerwise_calibration–Set up quantization for a subset of modules after layerwise
-
sync_obs_act_stats–Synchronize the activation statistics for observers
-
validate_observer–Validate observer dictionary format. Accepts keys: 'weights', 'input', 'output'
Attributes:
-
resolved_targets(set[str]) –Set of all resolved targets, i.e. all unique targets listed
resolved_targets
property
Set of all resolved targets, i.e. all unique targets listed in resolved quantization config. Use this property instead of the targets field, as targets can also come from config_groups depending on how recipe is configured.
end_calibration
Remove calibration hooks and observers, and set the model status to frozen. Keep quantization enabled for future operations
Parameters:
-
model(Module) –model to end calibration for
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
has_config
Determine if the user has specified a quantization config on this modifier
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
initialize_quantization
Attach quantization schemes to modules in the model according to the quantization config specified on this modifier
Parameters:
-
state(State) –session state holding the model to attach schemes and observers to. Observer defaults are resolved according to
state.has_calibration_data
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
resolve_quantization_config
Returns the quantization config specified by this modifier
Parameters:
-
has_calibration_data(bool, default:False) –whether calibration data will be available to this modifier. If True, the default weight observer is
imatrix_mse, which weights quantization error by per-channel activation importance; otherwise the default ismemoryless_minmax
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
start_calibration
Attach observers, register activation calibration hooks (including kv_cache quantization) and enable quantization as we calibrate
Parameters:
-
model(Module) –model to prepare for calibration
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
start_layerwise_calibration
Set up quantization for a subset of modules after layerwise decompression. Applies quantization config scoped to the given modules, then initializes observers, calibration hooks, and fuses weight observers.
Parameters:
-
state(State) –session state, used to resolve observer defaults according to
state.has_calibration_data -
model(Module) –the full model (needed for config application)
-
modules(list[Module]) –modules in the current subgraph to prepare
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
sync_obs_act_stats
Synchronize the activation statistics for observers across DDP ranks. Iterates all observers (weight, input, output, q, k, v); note: No-op when not distributed and most weight observers don't have activation statistics and thus are no-ops as well.
Parameters:
-
modules(Iterator[Module]) –iterable of modules to sync (e.g., from a sequential chunk)
Source code in src/llmcompressor/modifiers/quantization/quantization/mixin.py
validate_observer
Validate observer dictionary format. Accepts keys: 'weights', 'input', 'output'