MCPcopy Create free account
hub / github.com/pytorch/executorch / apply_linear_quantization

Function apply_linear_quantization

examples/models/gemma4/quant_utils.py:41–62  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

39
40
41def 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
65def apply_embedding_quantization(

Callers 3

_export_audio_encoderFunction · 0.90
_export_vision_encoderFunction · 0.90
_export_text_decoderFunction · 0.90

Calls 2

quantize_model_Function · 0.90
infoMethod · 0.80

Tested by

no test coverage detected