Apply TorchAO dynamic quantization to Linear layers. Uses the shared quantize_model_() from extension/llm/export/quantize.py.
(
model: nn.Module,
mode: str,
group_size: int = 128,
)
| 39 | |
| 40 | |
| 41 | def apply_linear_quantization( |
| 42 | model: nn.Module, |
| 43 | mode: str, |
| 44 | group_size: int = 128, |
| 45 | ) -> nn.Module: |
| 46 | """Apply TorchAO dynamic quantization to Linear layers. |
| 47 | |
| 48 | Uses the shared quantize_model_() from extension/llm/export/quantize.py. |
| 49 | """ |
| 50 | from executorch.extension.llm.export.quantize import quantize_model_ |
| 51 | |
| 52 | logger.info(f"Applying linear quantization: {mode}") |
| 53 | # Only pass group_size for modes that use it (8da4w). |
| 54 | # 8da8w uses PerAxis(0) — passing group_size would override to PerGroup. |
| 55 | qlinear_group_size = group_size if mode == "8da4w" else None |
| 56 | quantize_model_( |
| 57 | model, |
| 58 | qlinear_config=mode, |
| 59 | qlinear_group_size=qlinear_group_size, |
| 60 | skip_incompatible_shapes=True, |
| 61 | ) |
| 62 | return model |
| 63 | |
| 64 | |
| 65 | def apply_embedding_quantization( |
no test coverage detected