XNNPACK 后端的对称量化方案与 PT2E 流程
介绍 ExecuTorch 中 XNNPACK 委托支持的量化方案,包括 8 位对称权重配合 8 位非对称激活,以及借助 torchao quantize_ API 对 LLM 嵌入层和线性层做低位量化。
## 量化[#](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html#quantization>) 那个...XNNPACK代理也可以用作执行对称的定量模型的后端。PyTorch模式XNNPACK后端,请使用 `XNNPACKQuantizer`. `Quantizers` 属于后端特性,这意味着 `XNNPACKQuantizer` 被配置为对模型进行量化,以利用由XNNPACK库提供的量化运算符。 ## 支持的量化计划[#](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html#supported-quantization-schemes>) XNNPACK代表支持以下量化计划: - 8 - 带有8的位对称权重 - 位对称激活(通过PT2E的四进制流). - 支持静态和动态激活. - 支持每渠道和每密度计划。 - 支持线性,演化,添加,黏液,猫,和适应性avg池2d操作器. 目前不支持在XNNPACK上只重量化. ## 8 - 使用 PT2E 流量进行位量化[#](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html#bit-quantization-using-the-pt2e-flow>) 要与 PT2E 流一起执行 8 - 位量化,在导出模型前执行以下步骤: 1. 1. 创建实例 `XnnpackQuantizer` 类别。设置量化参数。 2. 使用( U) `torch.export.export` 准备量化。 3. 调用 `prepare_pt2e` 以准备模型进行量化。 4. 对于静态量子化,使用具有代表性的样本运行制备模型,以校准量子化的拉诺激活范围. 5. 调用 `convert_pt2e` 以数量化模型。 6. 使用标准流导出并降低模型. 产出 `convert_pt2e` 是一个 PyTorch 模型,可以使用正常流量导出并降低。由于它是常规的 PyTorch 模型,它也可以用来使用标准的 PyTorch 技术来评价量化模型的准确性。 ``` import torch import torchvision.models as models from torchvision.models.mobilenetv2 import MobileNet_V2_Weights from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import XNNPACKQuantizer, get_symmetric_quantization_config from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner from executorch.backends.xnnpack.utils.configs import get_transform_passes from executorch.exir import to_edge_transform_and_lower from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e model = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval() sample_inputs = (torch.randn(1, 3, 224, 224), ) qparams = get_symmetric_quantization_config(is_per_channel=True) # (1) quantizer = XNNPACKQuantizer() quantizer.set_global(qparams) training_ep = torch.export.export(model, sample_inputs).module() # (2) prepared_model = prepare_pt2e(training_ep, quantizer) # (3) for cal_sample in [torch.randn(1, 3, 224, 224)]: # Replace with representative model inputs prepared_model(cal_sample) # (4) Calibrate quantized_model = convert_pt2e(prepared_model) # (5) et_program = to_edge_transform_and_lower( # (6) torch.export.export(quantized_model, sample_inputs), partitioner=[XnnpackPartitioner()], transform_passes=get_transform_passes(), ).to_executorch() ``` 那个... `transform_passes` 在一般降低的 API 中,舞台是可选的。对于 XNNPACK , `get_transform_passes()` 被一般推荐,因为它运行预分解图转换,将支持的图案暴露在分区器上. 见 [PyTorch 2 输出后培训量化](<https://docs.pytorch.org/ao/main/pt2e_quantization/pt2e_quant_ptq.html>) 以获取更多信息。 ## LLM 量化并量化_[#](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html#llm-quantization-with-quantize>) XNNPACK 后端也支持用 [火炬岛](<https://github.com/pytorch/ao>) 定量化_ API 。 这最常用于LLMS, 需要更高级的定量化。 由于定量化_ 不是后端意识到, 必须使用一个与 CPU / XNNPACK 兼容的配置 : - 将嵌入方式量化为 `IntxWeightOnlyConfig` (使用weight_dtype火炬.int2,火炬.int4或火炬.int8,使用PerGroup或PerAxis颗粒性). - 以 4 位重和 8bit 动态激活来量化线性层, 使用 `Int8DynamicActivationIntxWeightConfig` (使用weight_dtype =torch.int4,使用PerGroup或PerAxis颗粒性). 下面是一个简单的例子,但是,在下文中可以找到一个更详细的教程,包括对受欢迎的LLM基准的准确性评价。 [火炬文档](<https://docs.pytorch.org/ao/main/eager_tutorials/serving.html#mobile-deployment-with-executorch>). ``` from torchao.quantization.granularity import PerGroup, PerAxis from torchao.quantization.quant_api import ( IntxWeightOnlyConfig, Int8DynamicActivationIntxWeightConfig, quantize_, ) # Quantize embeddings with 8-bits, per channel embedding_config = IntxWeightOnlyConfig( weight_dtype=torch.int8, granularity=PerAxis(0), ) quantize_( eager_model, lambda m, fqn: isinstance(m, torch.nn.Embedding), ) # Quantize linear layers with 8-bit dynamic activations and 4-bit weights linear_config = Int8DynamicActivationIntxWeightConfig( weight_dtype=torch.int4, weight_granularity=PerGroup(32), ) quantize_(eager_model, linear_config) ```
查看原始来源