llmcompressor.modifiers.smoothquant
Modules:
Classes:
-
SmoothQuantMapping
–Dataclass for storing the mapping between an activation layer and the following
-
SmoothQuantModifier
–Implements the SmoothQuant algorithm from https://arxiv.org/abs/2211.10438. This
-
SmoothQuantScale
–Dataclass for storing the channel-wise minimum and maximum values for a layer. This
SmoothQuantMapping dataclass
Dataclass for storing the mapping between an activation layer and the following weights that must be balanced during smoothing
Parameters:
-
smooth_name
str
) –name of the activation layer
-
smooth_layer
Module
) –PyTorch module storing the activation layer
-
balance_layers
List[Module]
) –list of PyTorch modules that smooth_layer feeds into, must be balanced to offset the smoothing of smooth_layer
SmoothQuantModifier
Bases: Modifier
Implements the SmoothQuant algorithm from https://arxiv.org/abs/2211.10438. This modifier performs a channel-wise smoothing of outliers in activations, making them easier to quantize by reducing the dynamic range. The smoothing is offset by applying the inverse operation to the next layer of weights, making the weights slightly more difficult to quantize.
Because this modifier manipulates the weights of the model, it can only be used in in one-shot and not during training. Activation ranges are determined by running a small set of calibration data through the model.
example recipe:
SmoothQuantModifier:
smoothing_strength: 0.5
mappings: [
[["re:.*q_proj", "re:.*k_proj", "re:.*v_proj"], "re:.*self_attn_layer_norm"],
[["re:.*fc1"], "re:.*final_layer_norm"]
]
ignore: ["model.decoder.final_layer_norm"]
:param smoothing_strength: alpha, intensity of smoothing to perform (0-1 range) :param mappings: list activation layers to smooth, and which layers to scale the output such that activations are smoothed. Each entry of the mapping list should be a list itself, in which the first entry is a list of layers who share the same input activation (the one to be to smoothed) and the second entry is the layer whose output is scaled to achieve the smoothing. If regex is used, it matches layers with the largest overlap in module name. If not supplied the argument will be inferred from the model architecture. :param ignore: list of layers to ignore, even if they match a regex in mappings. It should match the name of layers whose outputs are scaled to achieve smoothing (the second entry of the mappings list). :param num_calibration_steps: number of samples to use for calibration, or None to use the whole dataset
Parameters:
-
calibration_function
optional function to use for the forward pass, or None to use the default tensor_module_forward
Methods:
-
on_finalize
–Clean up by clearing the scale and mapping data
-
on_initialize
–Initialize and run SmoothQuant on the given state
on_finalize
Clean up by clearing the scale and mapping data
Source code in llmcompressor/modifiers/smoothquant/base.py
on_initialize
Initialize and run SmoothQuant on the given state
Parameters:
-
state
State
) –state to run SmoothQuant on
Returns:
-
bool
–True on a successful run, False otherwise
Source code in llmcompressor/modifiers/smoothquant/base.py
SmoothQuantScale dataclass
Dataclass for storing the channel-wise minimum and maximum values for a layer. This is updated each forward pass during calibration
Parameters:
-
min_channel_vals
Tensor
) –minimum output value seen so far, per channel
-
max_channel_vals
Tensor
) –maximum output value seen so far, per channel