ExecuTorch量化流程概览
ExecuTorch以torchao作为量化库,把模型计算与权重从32位浮点降到8位整数,以减小内存占用、加快推理并降低功耗,适合可穿戴设备与微控制器等端侧环境。其PT2E流程分三步,先配置后端专用量化器,再量化并评估模型,最后将量化模型下沉到目标后端。
## 量化概况.[#](<https://docs.pytorch.org/executorch/stable/quantization-overview.html#quantization-overview>) 量子化是一种降低模型计算和存储重量中使用的数字精度的技术 — 通常从32-bit浮点数到8-bit整数。 这降低了模型的内存足迹,加快了推论速度,降低了功率消耗,而准确度损失往往很小。 量化对于在边缘设备上部署模型尤为重要,如可穿戴、嵌入式系统和微控制器,这些模型的计算、内存和电池容量往往有限。 通过量化模型,我们可以显著提高模型的效率,使之适合这些资源紧张的环境。 ## 在 ExecuTorch 中的量化[#](<https://docs.pytorch.org/executorch/stable/quantization-overview.html#quantization-in-executorch>) ExecuTorch 用途 [火炬岛](<https://github.com/pytorch/ao/tree/main/torchao>) 作为其定量库。这种集成使ExecuTorch能够利用PyTorch-native工具来准备、校准和转换定量模型。 ExecuTorch中的量化是后端特异性的,每个后端都根据其硬件能力定义了应该如何对模型进行定量. 大部分ExecuTorch后端使用火炬. [PT2E 量化](<https://docs.pytorch.org/ao/stable/pt2e_quantization/pt2e_quant_ptq.html>) b. 流,该流用于以火炬输出的模型。导出,并能够对每个后端进行量身定制的量化。 PT2E量化工作流程有三个主要步骤: 1. 配置后端特定量子 。 2. 准备、校准、转换和评价PyTorch中的量化模型 3. 将模型降低到目标后端 ## 1. 配置一个特定的后端量子[#](<https://docs.pytorch.org/executorch/stable/quantization-overview.html#configure-a-backend-specific-quantizer>) 每个后端都提供自己的量化符(如XNNPACKQuantizer,CoreMLQuantizer),定义如何以与目标硬件兼容的方式将量化符应用于一个模型. 这些量化符通常支持配置符,允许用户指定量化符选项,如: - 精度( 如 8 - bit 或 4 - bit) - 量化类型(如动态、静态或仅重量化) - 颗粒性(如每10个密度,每通道) - 培训后量化与量化意识培训 并非所有的量化选项都得到所有后端的支持. Consult 后端特定指南用于支持的量化模式和配置,以及如何初始化后端特定的 PT2E 量化符 : - [XNNPACK 量化](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html>) - [CoreML 量化](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-quantization.html>) - [QNN 量化](<https://docs.pytorch.org/executorch/stable/backends-qualcomm.html#step-2-optional-quantize-your-model>) - [NXP 量化](<https://docs.pytorch.org/executorch/stable/backends/nxp/nxp-quantization.html>) ## 2. 量化和评价模型[#](<https://docs.pytorch.org/executorch/stable/quantization-overview.html#quantize-and-evaluate-the-model>) 在定义了后端特定量子后,PT2E的量子化流量对所有后端是相同的,下面给出一个通用的例子,但在后端文档中给出了具体的例子: ``` from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e training_gm = torch.export.export(model, sample_inputs).module() # Prepare the model for quantization using the backend-specific quantizer instance prepared_model = prepare_pt2e(training_gm, quantizer) # Calibrate the model on representative data for sample in calibration_data: prepared_model(sample) # Convert the calibrated model to a quantized model quantized_model = convert_pt2e(prepared_model) ``` quantized_model与其它模式一样是PyTorch模型,可以针对不同的任务进行准确性评价. 任务特定基准是评价您量化模型的推荐方式,但作为粗略的替代品,您可以使用SQNR等通用错误度量来与原始模型的输出进行比较: ``` from torchao.quantization.utils import compute_error out_reference = model(sample) out_quantized = quantized_model(sample) sqnr = compute_error(out_reference, out_quantized) # SQNR error ``` 注意设备上的数值甚至对于未量化的模型也可以与PyTorch中的数值不同,精度评价也可以用py绑定或设备上进行. ## 3. 放下车型[#](<https://docs.pytorch.org/executorch/stable/quantization-overview.html#lower-the-model>) 最后一步是将quantized_model降低到理想的后端,因为你会是一个无量化的后端。 [后端特定页面](<https://docs.pytorch.org/executorch/stable/backends-overview.html>) 用于降低信息。
查看原始来源