三星Exynos后端通过PT2E流程实现8位静态量化

三星Exynos后端目前支持运行静态量化的8位模型。使用PT2E流程时,需要先创建EnnQuantizer并设置量化行为,再依次完成导出、准备、校准、转换和降低,最终得到可部署的量化模型。

## 量化[#](<https://docs.pytorch.org/executorch/stable/backends/samsung/samsung-quantization.html#quantization>) Exynos后端目前支持执行静态化的8 - bit模型. ## 8 - 以 PT2E 量化流量进行位数化[#](<https://docs.pytorch.org/executorch/stable/backends/samsung/samsung-quantization.html#bit-quantization-with-the-pt2e-quantization-flow>) 要与 PT2E 流一起执行 8 - 位量化,在导出模型前执行以下步骤: 1. 1. 创建实例 `EnnQuantizer` 分类和设定期望的量化行为。 2. 使用( U) `torch.export.export` 以获取源模型的图表模块。 3. 使用( U) `prepare_pt2e` 以准备模型进行量化。 4. 使用具有代表性的样品执行制成的模型,以校准分解的拉诺活化范围。 5. 使用( U) `convert_pt2e` 以数量化模型。 6. 使用标准出口流量导出并降低模型. 产出 `convert_pt2e` 是一个 PyTorch 模型,可以使用与非量化模型相同的导出流进行导出和降低。由于它是一种常规的 PyTorch 模型,它也可以用来使用标准的 PyTorch 技术来评价量化模型的精度。 下面的例子说明了如何使用PT2E的量化流对MobileNetV2模型进行定量. ``` import torch import torchvision.models as models from torchvision.models.mobilenetv2 import MobileNet_V2_Weights from executorch.backends.samsung.partition.enn_partitioner import EnnPartitioner from executorch.backends.samsung.quantizer.quantizer import EnnQuantizer, Precision 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), ) # Currently, "A8W8" is the only supported precision mode precision = "A8W8" is_per_channel = True is_qat = False quantizer = EnnQuantizer() quantizer.set_quant_params(precision, is_per_channel, is_qat) # (1) 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=[EnnPartitioner()], ).to_executorch() ``` 见 [PyTorch 2 输出后培训量化](<https://docs.pytorch.org/ao/main/pt2e_quantization/pt2e_quant_ptq.html>) 以获取更多信息。

查看原始来源