Quantize linear and embedding layers in a module in-place. .. warning:: This API is experimental and may change without notice. Args: module: The PyTorch module to quantize. qlinear_config: Quantization config for linear layers ("4w", "8w", "8da4w", "8d
(
module: torch.nn.Module,
qlinear_config: Optional[str] = None,
qlinear_group_size: Optional[int] = None,
qlinear_packing_format: Optional[str] = None,
qembedding_config: Optional[str] = None,
qembedding_group_size: Optional[int] = None,
tie_word_embeddings: bool = False,
skip_incompatible_shapes: bool = False,
)
| 175 | |
| 176 | @experimental("quantize_model_ is experimental and may change without notice.") |
| 177 | def quantize_model_( |
| 178 | module: torch.nn.Module, |
| 179 | qlinear_config: Optional[str] = None, |
| 180 | qlinear_group_size: Optional[int] = None, |
| 181 | qlinear_packing_format: Optional[str] = None, |
| 182 | qembedding_config: Optional[str] = None, |
| 183 | qembedding_group_size: Optional[int] = None, |
| 184 | tie_word_embeddings: bool = False, |
| 185 | skip_incompatible_shapes: bool = False, |
| 186 | ) -> None: |
| 187 | """Quantize linear and embedding layers in a module in-place. |
| 188 | |
| 189 | .. warning:: |
| 190 | |
| 191 | This API is experimental and may change without notice. |
| 192 | |
| 193 | Args: |
| 194 | module: The PyTorch module to quantize. |
| 195 | qlinear_config: Quantization config for linear layers |
| 196 | ("4w", "8w", "8da4w", "8da8w", "fpa4w", "nvfp4"). |
| 197 | qlinear_group_size: Group size for linear quantization. |
| 198 | Defaults to 16 for nvfp4, 32 for 4w/8da4w, 0 (per-axis) for 8w/8da8w. |
| 199 | qlinear_packing_format: Packing format for linear layers |
| 200 | (e.g., "tile_packed_to_4d"). |
| 201 | qembedding_config: Quantization config for embedding layers |
| 202 | ("4w", "8w", "nvfp4"). |
| 203 | qembedding_group_size: Group size for embedding quantization. |
| 204 | Defaults to 16 for nvfp4, 32 for 4w, 0 (per-axis) for 8w. |
| 205 | tie_word_embeddings: If True and both linear and embedding use the |
| 206 | same quantization, re-tie lm_head.weight to embed_tokens.weight |
| 207 | after quantization. |
| 208 | skip_incompatible_shapes: If True, silently skip layers with |
| 209 | incompatible weight shapes. If False (default), raise RuntimeError. |
| 210 | """ |
| 211 | if not qlinear_config and not qembedding_config: |
| 212 | return |
| 213 | |
| 214 | if qlinear_group_size is None: |
| 215 | qlinear_group_size = _default_group_size(qlinear_config) |
| 216 | if qembedding_group_size is None: |
| 217 | qembedding_group_size = _default_group_size(qembedding_config) |
| 218 | |
| 219 | from torchao.quantization.quant_api import quantize_ |
| 220 | |
| 221 | # Metal (MPS) quantization uses a separate API |
| 222 | if qlinear_config == "fpa4w": |
| 223 | import torchao.experimental.ops.mps # noqa: F401 |
| 224 | from torchao.experimental.quant_api import UIntxWeightOnlyConfig |
| 225 | |
| 226 | config = UIntxWeightOnlyConfig( |
| 227 | group_size=qlinear_group_size, |
| 228 | bitwidth=4, |
| 229 | uintx_choose_qparams_algorithm="hqq", |
| 230 | ) |
| 231 | print( |
| 232 | f" Applying {qlinear_config} linear quantization " |
| 233 | f"(group_size={qlinear_group_size})..." |
| 234 | ) |