llmcompressor.modifiers.smoothquant.base
SmoothQuantMapping
dataclass
Dataclass for storing the mapping between an activation layer and the following weights that must be balanced during smoothing
Parameters:
Name | Type | Description | Default |
---|---|---|---|
smooth_name | str | name of the activation layer | required |
smooth_layer | Module | PyTorch module storing the activation layer | required |
balance_layers | List[Module] | list of PyTorch modules that smooth_layer feeds into, must be balanced to offset the smoothing of smooth_layer | required |
Source code in src/llmcompressor/modifiers/smoothquant/base.py
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:
Name | Type | Description | Default |
---|---|---|---|
calibration_function | optional function to use for the forward pass, or None to use the default tensor_module_forward | required |
Source code in src/llmcompressor/modifiers/smoothquant/base.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
on_finalize(state, **kwargs)
Clean up by clearing the scale and mapping data
Source code in src/llmcompressor/modifiers/smoothquant/base.py
on_initialize(state, **kwargs)
Initialize and run SmoothQuant on the given state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | state to run SmoothQuant on | required |
Returns:
Type | Description |
---|---|
bool | True on a successful run, False otherwise |
Source code in src/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:
Name | Type | Description | Default |
---|---|---|---|
min_channel_vals | Tensor | minimum output value seen so far, per channel | required |
max_channel_vals | Tensor | maximum output value seen so far, per channel | required |