llmcompressor.observers.base
Classes:
-
Observer–Base Observer class to be subclassed for specific implementation.
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:
-
(observedTensor) –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:
-
(observedTensor) –observed tensor to calculate quantization parameters for
-
(reduce_dimsOptional[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_idOptional[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_scaleOptional[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:
-
(observedTensor) –optional observed tensor from which to calculate quantization parameters
-
(g_idxOptional[Tensor], default:None) –optional mapping from column index to group index
-
(global_scaleOptional[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:
-
(observedTensor) –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:
-
(observedOptional[Tensor], default:None) –optional observed tensor to calculate quantization parameters from
-
(g_idxOptional[Tensor], default:None) –optional mapping from column index to group index
-
(global_scaleOptional[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.