llmcompressor.observers
Framework for monitoring and analyzing model behavior during compression.
Provides observers for tracking tensor statistics, activation ranges, and model behavior during compression workflows. Includes min-max observers, MSE observers, and helper utilities for quantization and other compression techniques.
Modules:
Classes:
-
MinMaxObserver
–Implements a quantization observer that calculates scale and zero point based on the
-
MovingAverageMSEObserver
–Implements a dynamic quantization observer that sets the scale and
-
Observer
–Base Observer class to be subclassed for specific implementation.
Functions:
-
get_observer_token_count
–Parse the module and return the number of tokens observed by
MinMaxObserver
Bases: Observer
Implements a quantization observer that calculates scale and zero point based on the minimum and maximum values of the tensor being observed. If averaging_constant is specified, then the scales are updated using a moving average
Methods:
-
calculate_gparam
–Generate a global scale using the observed min and max.
-
calculate_qparams
–Generate a scale and zero-point using the observed min and max.
-
calculate_updated_min_max
–Updates the observed min and max using a moving average smoothed by the
-
get_qparams_along_dim
–Calculate quantization parameters along the specified dimension
-
reset
–Reset the state of the observer, including min and maximum values
Source code in llmcompressor/observers/min_max.py
calculate_gparam
Generate a global scale using the observed min and max.
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
Returns:
-
Tensor
–updated global scale derived from the observed tensor
Source code in llmcompressor/observers/min_max.py
calculate_qparams
calculate_qparams(
observed: Tensor,
reduce_dims: Optional[Tuple[int]] = None,
tensor_id: Optional[Any] = None,
global_scale: Optional[Tensor] = None,
) -> Tuple[torch.FloatTensor, torch.IntTensor]
Generate a scale and zero-point using the observed min and max.
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
-
reduce_dims
Optional[Tuple[int]]
, default:None
) –optional tuple of dimensions to reduce along, returned scale and zero point will be shaped (1,) along the reduced dimensions
-
tensor_id
Optional[Any]
, default:None
) –Optional id if different ranges of observed tensors are passed, useful for sharding tensors by group_size
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
-
Tuple[FloatTensor, IntTensor]
–tuple of scale and zero point derived from the observed tensor
Source code in llmcompressor/observers/min_max.py
calculate_updated_min_max
calculate_updated_min_max(
observed: Tensor,
reduce_dims: Optional[Tuple[int]] = None,
tensor_id: Optional[Any] = None,
)
Updates the observed min and max using a moving average smoothed by the averaging_constant. Set the averaging_constant to 1.0 to disable averaging.
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
-
reduce_dims
Optional[Tuple[int]]
, default:None
) –optional tuple of dimensions to reduce along, returned scale and zero point will be shaped (1,) along the reduced dimensions
-
tensor_id
Optional[Any]
, default:None
) –Optional id if different ranges of observed tensors are passed, useful for sharding tensors by group_size
Returns:
- –
updated min and max values
Source code in llmcompressor/observers/min_max.py
get_qparams_along_dim
get_qparams_along_dim(
observed: Tensor,
dim: int,
tensor_id: Optional[Any] = None,
global_scale: Optional[Tensor] = None,
)
Calculate quantization parameters along the specified dimension
Source code in llmcompressor/observers/min_max.py
MovingAverageMSEObserver
MovingAverageMSEObserver(
quantization_args: QuantizationArgs,
maxshrink: float = 0.2,
patience: int = 5,
averaging_constant: float = 0.01,
grid: float = 100.0,
norm: float = 2.4,
**kwargs
)
Bases: Observer
Implements a dynamic quantization observer that sets the scale and zero point based on a moving average of the mse-clipped min and max observed values
Methods:
-
calculate_mse_min_max
–Computes the mse-clipped min and max values of the observed tensor by
-
calculate_qparams
–Updates the mse-clipped min and max values of the observed tensor using
-
calculate_updated_min_max
–Updates the mse-clipped min and max values of the observed tensor using
-
reset
–Reset the state of the observer, including min and maximum values
Source code in llmcompressor/observers/mse.py
calculate_mse_min_max
calculate_mse_min_max(
observed: Tensor,
reduce_dims: Optional[Tuple[int]] = None,
global_scale: Optional[Tensor] = None,
)
Computes the mse-clipped min and max values of the observed tensor by optimizing for quantization error
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
-
reduce_dims
Optional[Tuple[int]]
, default:None
) –optional tuple of dimensions to reduce along, returned values will be shaped (1,) along the reduced dimensions
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
- –
tuple of min and max values derived from the observed tensor
Source code in llmcompressor/observers/mse.py
calculate_qparams
calculate_qparams(
observed: Tensor,
reduce_dims: Optional[Tuple[int]] = None,
tensor_id: Optional[Any] = None,
global_scale: Optional[Tensor] = None,
) -> Tuple[FloatTensor, IntTensor]
Updates the mse-clipped min and max values of the observed tensor using a moving average smoothed by the averaging_constant
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
-
reduce_dims
Optional[Tuple[int]]
, default:None
) –optional tuple of dimensions to reduce along, returned scale and zero point will be shaped (1,) along the reduced dimensions
-
tensor_id
Optional[Any]
, default:None
) –Optional id if different ranges of observed tensors are passed, useful for sharding tensors by group_size
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
-
Tuple[FloatTensor, IntTensor]
–tuple of scale and zero point derived from the observed tensor
Source code in llmcompressor/observers/mse.py
calculate_updated_min_max
calculate_updated_min_max(
observed: Tensor,
reduce_dims: Optional[Tuple[int]] = None,
tensor_id: Optional[Any] = None,
global_scale: Optional[Tensor] = None,
) -> Tuple[FloatTensor, IntTensor]
Updates the mse-clipped min and max values of the observed tensor using a moving average smoothed by the averaging_constant
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
-
reduce_dims
Optional[Tuple[int]]
, default:None
) –optional tuple of dimensions to reduce along, returned scale and zero point will be shaped (1,) along the reduced dimensions
-
tensor_id
Optional[Any]
, default:None
) –Optional id if different ranges of observed tensors are passed, useful for sharding tensors by group_size
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
-
Tuple[FloatTensor, IntTensor]
–updated min and max values derived from the observed value
Source code in llmcompressor/observers/mse.py
Observer
Bases: InternalModule
, RegistryMixin
Base Observer class to be subclassed for specific implementation. Subclasses should override calculate_qparams
to return a scale, zero_point pair
Methods:
-
calculate_gparam
–:param observed: observed tensor to calculate quantization parameters for
-
calculate_qparams
–:param observed: observed tensor to calculate quantization parameters for
-
forward
–maps directly to get_qparams
-
get_gparam
–Function to derive a global scale parameter
-
get_qparams
–Convenience function to wrap overwritten calculate_qparams
-
post_calculate_qparams
–Run any logic specific to its observers after running calculate_qparams
-
record_observed_tokens
–Counts the number of tokens observed during the
-
reset
–Reset the state of the observer
Source code in llmcompressor/observers/base.py
calculate_gparam
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
Returns:
-
Tensor
–global scale derived from the observed tensor
Source code in llmcompressor/observers/base.py
calculate_qparams
calculate_qparams(
observed: Tensor,
reduce_dims: Optional[Tuple[int]] = None,
tensor_id: Optional[Any] = None,
global_scale: Optional[Tensor] = None,
) -> Tuple[FloatTensor, IntTensor]
Parameters:
-
observed
Tensor
) –observed tensor to calculate quantization parameters for
-
reduce_dims
Optional[Tuple[int]]
, default:None
) –optional tuple of dimensions to reduce along, returned scale and zero point will be shaped (1,) along the reduced dimensions
-
tensor_id
Optional[Any]
, default:None
) –optional id for tracking separate statistics when different ranges of observed tensors are passed, useful for sharding tensors by group_size or block quantization
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
-
Tuple[FloatTensor, IntTensor]
–tuple of scale and zero point derived from the observed tensor
Source code in llmcompressor/observers/base.py
forward
forward(
observed: Tensor,
g_idx: Optional[Tensor] = None,
global_scale: Optional[Tensor] = None,
should_calculate_gparam: bool = False,
) -> Tuple[FloatTensor, IntTensor]
maps directly to get_qparams
Parameters:
-
observed
Tensor
) –optional observed tensor from which to calculate quantization parameters
-
g_idx
Optional[Tensor]
, default:None
) –optional mapping from column index to group index
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
-
Tuple[FloatTensor, IntTensor]
–tuple of scale and zero point based on last observed value
Source code in llmcompressor/observers/base.py
get_gparam
Function to derive a global scale parameter
Parameters:
-
observed
Tensor
) –observed tensor to calculate global parameters from
Returns:
- –
derived global scale
Source code in llmcompressor/observers/base.py
get_qparams
get_qparams(
observed: Optional[Tensor] = None,
g_idx: Optional[Tensor] = None,
global_scale: Optional[Tensor] = None,
) -> Tuple[FloatTensor, IntTensor]
Convenience function to wrap overwritten calculate_qparams adds support to make observed tensor optional and support for tracking latest calculated scale and zero point
Parameters:
-
observed
Optional[Tensor]
, default:None
) –optional observed tensor to calculate quantization parameters from
-
g_idx
Optional[Tensor]
, default:None
) –optional mapping from column index to group index
-
global_scale
Optional[Tensor]
, default:None
) –optional scale to further scale local quantization scales
Returns:
-
Tuple[FloatTensor, IntTensor]
–tuple of scale and zero point based on last observed value
Source code in llmcompressor/observers/base.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
post_calculate_qparams
record_observed_tokens
Counts the number of tokens observed during the forward passes. The count is aggregated in the _num_observed_tokens attribute of the class.
Note: The batch_tensor is expected to have two dimensions (batch_size * sequence_length, num_features). This is the general shape expected by the forward pass of the expert layers in a MOE model. If the input tensor does not have two dimensions, the _num_observed_tokens attribute will be set to None.
Source code in llmcompressor/observers/base.py
get_observer_token_count
Parse the module and return the number of tokens observed by each module's observer.
Parameters:
-
module
Module
) –module to parse
Returns:
-
Counter
–counter with the number of tokens observed by each observer