MLX 后端基于 TorchAO 的权重量化流程

介绍 ExecuTorch 的 MLX 后端如何借助 TorchAO 对模型做仅权重量化,以缩小模型体积并提升 Apple Silicon 上大语言模型的推理表现,量化在 torch.export 之前就地施加于 eager 模型。

## 量化[#](<https://docs.pytorch.org/executorch/stable/backends/mlx/mlx-quantization.html#quantization>) MLX 后端支持通过 [TorchAO](<https://github.com/pytorch/ao>) 用于降低模型大小和改善推导性能,特别是用于苹果硅上的LLMs。 量化应用在现场的急切模型中 **在此之前** `torch.export()`. ## `quantize_`[#](<https://docs.pytorch.org/executorch/stable/backends/mlx/mlx-quantization.html#quantize>) MLX后端使用 TorchAO 的 [`quantize_`](<https://github.com/pytorch/ao/blob/main/torchao/quantization/quant_api.py>) 头罩下方的API。您可以直接调用它来全面控制量化配置和颗粒化。 关键 TorchAO 配置是 : - [`IntxWeightOnlyConfig`](<https://github.com/pytorch/ao/blob/main/torchao/quantization/quant_api.py>) – 对于INT2 / INT4 / INT8 重量只量化,每组颗粒性(组大小为32,64,128). - [`ExportableNVFP4Config`](<https://github.com/pytorch/executorch/blob/main/extension/llm/export/nvfp4.py>) 用于 NVFP4 仅重量化 ``` import torch from torchao.quantization.quant_api import quantize_, IntxWeightOnlyConfig from torchao.quantization.granularity import PerGroup # INT4 weight-only quantization for linear layers (group_size=32) quantize_( model, IntxWeightOnlyConfig(weight_dtype=torch.int4, granularity=PerGroup(32)), filter_fn=lambda m, fqn: isinstance(m, torch.nn.Linear), ) # INT8 weight-only quantization for embedding layers (group_size=128) quantize_( model, IntxWeightOnlyConfig(weight_dtype=torch.int8, granularity=PerGroup(128)), filter_fn=lambda m, fqn: isinstance(m, torch.nn.Embedding), ) ``` ## `quantize_model_`[#](<https://docs.pytorch.org/executorch/stable/backends/mlx/mlx-quantization.html#quantize-model>) 为方便起见,ExecuTorch提供 `quantize_model_` 关闭 `quantize_` 带有常见的 LLM 量化配置的合理默认值 : ``` from executorch.extension.llm.export.quantize import quantize_model_ # Quantize linear layers with INT4, embedding layers with INT8 # Note: 8w defaults to per-axis grouping, which MLX does not support. # Always pass an explicit group size when using 8w with MLX. quantize_model_(model, qlinear_config="4w", qembedding_config="8w", qembedding_group_size=128) ``` ### 支持的配置[#](<https://docs.pytorch.org/executorch/stable/backends/mlx/mlx-quantization.html#supported-configs>) | 配置 | 说明 | | --- | --- | | `4w` | INT4 仅重量化(每组) | | `8w` | INT8 仅重量化(每组) | | `nvfp4` | NVIDIA FP4 仅重量化 | 这些可以独立应用于线性层和嵌入层. ### 使用 LLM 导出脚本[#](<https://docs.pytorch.org/executorch/stable/backends/mlx/mlx-quantization.html#using-the-llm-export-script>) 最简单的导出量化模型的方法就是通过 `export_llm_hf` 脚本, 它调用 `quantize_model_` 内部 : ``` # INT4 quantization for both linear and embedding layers python -m executorch.backends.mlx.examples.llm.export_llm_hf \ --model-id "unsloth/Llama-3.2-1B-Instruct" \ --output llama_int4.pte \ --use-custom-sdpa \ --use-custom-kv-cache \ --qlinear 4w \ --qembedding 4w # INT8 quantization for linear layers only # Note: --qlinear-group-size is required for 8w (default is per-axis, which MLX does not support) python -m executorch.backends.mlx.examples.llm.export_llm_hf \ --model-id "unsloth/Llama-3.2-1B-Instruct" \ --output llama_int8.pte \ --use-custom-sdpa \ --use-custom-kv-cache \ --qlinear 8w \ --qlinear-group-size 128 ``` ### CLI 量化选项[#](<https://docs.pytorch.org/executorch/stable/backends/mlx/mlx-quantization.html#cli-quantization-options>) | 选项 | 默认 | 说明 | | --- | --- | --- | | `--qlinear` | 无 | 线性层的量化( )`4w`, `8w`, `nvfp4`) | | `--qembedding` | 无 | 嵌入层的量化( Q)`4w`, `8w`, `nvfp4`) | | `--qlinear-group-size` | 取决于配置 | 线性层定量的组大小( 32, 64, 或 128) 。 默认为 32 `4w`,16 用于 `nvfp4`. **所需经费 `8w`** (默认为每轴,MLX不支持). | | `--qembedding-group-size` | 取决于配置 | 嵌入层整数的组大小( 32, 64, 或 128) 。 默认为 32 。 `4w`,16 用于 `nvfp4`. **所需经费 `8w`** (默认为每轴,MLX不支持). | | `--no-tie-word-embeddings` | 虚假 | 禁用 lm_head 重排在 Quantization 后嵌入 |

查看原始来源