Quantization API Reference#
Created On: Jul 25, 2020 | Last Updated On: Jun 18, 2025
torch.ao.quantization#
This module contains Eager mode quantization APIs.
Top level APIs#
Quantize the input float model with post training static quantization. |
|
Converts a float model to dynamic (i.e. |
|
Do quantization aware training and output a quantized model |
|
Prepares a copy of the model for quantization calibration or quantization-aware training. |
|
Prepares a copy of the model for quantization calibration or quantization-aware training and converts it to quantized version. |
|
Converts submodules in input module to a different module according to mapping by calling from_float method on the target module class. |
Preparing model for quantization#
Fuse a list of modules into a single module. |
|
Quantize stub module, before calibration, this is same as an observer, it will be swapped as nnq.Quantize in convert. |
|
Dequantize stub module, before calibration, this is same as identity, this will be swapped as nnq.DeQuantize in convert. |
|
A wrapper class that wraps the input module, adds QuantStub and DeQuantStub and surround the call to module with call to quant and dequant modules. |
|
Wrap the leaf child module in QuantWrapper if it has a valid qconfig Note that this function will modify the children of module inplace and it can return a new module which wraps the input module as well. |
Utility functions#
Swaps the module if it has a quantized counterpart and it has an observer attached. |
|
Propagate qconfig through the module hierarchy and assign qconfig attribute on each leaf module |
|
Define the default evaluation function. |
torch.ao.quantization.quantize_fx#
This module contains FX graph mode quantization APIs (prototype).
Prepare a model for post training quantization |
|
Prepare a model for quantization aware training |
|
Convert a calibrated or trained model to a quantized model |
|
Fuse modules like conv+bn, conv+bn+relu etc, model must be in eval mode. |
torch.ao.quantization.qconfig_mapping#
This module contains QConfigMapping for configuring FX graph mode quantization.
Mapping from model ops to |
|
Return the default QConfigMapping for post training quantization. |
|
Return the default QConfigMapping for quantization aware training. |
torch.ao.quantization.backend_config#
This module contains BackendConfig, a config object that defines how quantization is supported in a backend. Currently only used by FX Graph Mode Quantization, but we may extend Eager Mode Quantization to work with this as well.
Config that defines the set of patterns that can be quantized on a given backend, and how reference quantized models can be produced from these patterns. |
|
Config object that specifies quantization behavior for a given operator pattern. |
|
Config object that specifies the supported data types passed as arguments to quantize ops in the reference model spec, for input and output activations, weights, and biases. |
|
Config for specifying additional constraints for a given dtype, such as quantization value ranges, scale value ranges, and fixed quantization params, to be used in |
|
An enum that represents different ways of how an operator/operator pattern should be observed |
torch.ao.quantization.fx.custom_config#
This module contains a few CustomConfig classes that’s used in both eager mode and FX graph mode quantization
Custom configuration for |
|
Custom configuration for |
|
Custom configuration for |
|
torch.ao.quantization.quantizer#
torch.ao.quantization.pt2e (quantization in pytorch 2.0 export implementation)#
torch.ao.quantization.pt2e.export_utils#
Return True if the torch.nn.Module was exported, False otherwise (e.g. |
torch.ao.quantization.pt2e.lowering#
Lower a PT2E-qantized model to x86 backend. |
PT2 Export (pt2e) Numeric Debugger#
Attach numeric_debug_handle_id for all nodes in the graph module of the given ExportedProgram, like conv2d, squeeze, conv1d, etc, except for placeholder. |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
Add output loggers to node that has numeric_debug_handle |
|
For a given model, extract the tensors stats and related information for each debug handle. |
|
Given two dict mapping from debug_handle_id (int) to list of tensors return a map from debug_handle_id to NodeAccuracySummary that contains comparison information like SQNR, MSE etc. |
torch.ao.quantization.observer#
此模块包含观察者(observer),用于在校准(PTQ)或训练(QAT)期间收集数值统计信息。
基础观察者模块。 |
|
用于基于运行时的最小/最大值计算量化参数的观察者模块。 |
|
用于基于最小/最大值的移动平均值计算量化参数的观察者模块。 |
|
用于基于运行时的逐通道最小/最大值计算量化参数的观察者模块。 |
|
用于基于运行时的逐通道最小/最大值计算量化参数的观察者模块。 |
|
该模块记录张量值的运行直方图以及最小/最大值。 |
|
一个不执行任何操作的观察者,仅将其配置传递给量化模块的 |
|
该模块主要用于调试,并记录运行时期间的张量值。 |
|
一个不执行任何操作的观察者,仅将其配置传递给量化模块的 |
|
返回与观察者统计信息对应的状态字典。 |
|
给定输入模型和包含模型观察者统计信息的 state_dict,将统计信息加载回模型。 |
|
静态量化的默认观察者,通常用于调试。 |
|
默认占位符观察者,通常用于量化为 torch.float16。 |
|
默认仅调试观察者。 |
|
默认权重观察者。 |
|
默认直方图观察者,通常用于 PTQ。 |
|
默认逐通道权重观察者,通常用于支持逐通道权重量化的后端,例如 fbgemm。 |
|
动态量化的默认观察者。 |
|
浮点零点的默认观察者。 |
|
仿射量化(pytorch/ao)的观察者模块 |
|
表示量化粒度的基类。 |
|
浮点数如何映射到整数 |
|
表示量化中的逐轴粒度。 |
|
表示量化中的逐块粒度。 |
|
表示量化中的逐通道组粒度。 |
|
表示逐行粒度。 |
|
表示逐张量粒度。 |
|
表示逐 token 粒度。 |
|
PyTorch 核心中尚不存在的数据类型的占位符。 |
|
枚举,指示零点是在整数域还是浮点域 |
|
根据输入形状和粒度类型获取块大小。 |
torch.ao.quantization.fake_quantize#
此模块实现了在 QAT 期间用于执行伪量化的模块。
基础伪量化模块。 |
|
在训练时模拟量化和反量化操作。 |
|
在训练时模拟量化和反量化。 |
|
定义一个融合模块来观察张量。 |
|
激活的默认伪量化。 |
|
权重的默认伪量化。 |
|
逐通道权重的默认伪量化。 |
|
使用直方图的激活伪量化。 |
|
默认伪量化的融合版本,性能更佳。 |
|
默认权重伪量化的融合版本,性能更佳。 |
|
默认逐通道权重伪量化的融合版本,性能更佳。 |
|
禁用模块的伪量化。 |
|
启用模块的伪量化。 |
|
禁用此模块的观察。 |
|
启用此模块的观察。 |
torch.ao.quantization.qconfig#
此模块定义了 QConfig
对象,这些对象用于配置各个算子的量化设置。
通过为激活和权重分别提供设置(观察者类)来描述如何量化一个层或网络的一部分。 |
|
默认 qconfig 配置。 |
|
用于调试的默认 qconfig 配置。 |
|
用于逐通道权重量化的默认 qconfig 配置。 |
|
默认动态 qconfig。 |
|
权重量化为 torch.float16 的动态 qconfig。 |
|
激活和权重均量化为 torch.float16 的动态 qconfig。 |
|
权重逐通道量化的动态 qconfig。 |
|
权重使用浮点零点量化的动态 qconfig。 |
|
QAT 的默认 qconfig。 |
|
仅量化权重的默认 qconfig。 |
|
仅量化激活的默认 qconfig。 |
|
默认 qat_config 的融合版本,具有性能优势。 |
torch.ao.nn.intrinsic#
此模块实现了可以量化的组合(融合)模块 conv + relu。
这是一个顺序容器,它调用 Conv1d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 Conv2d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 Conv3d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 Linear 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 Conv 1d 和 Batch Norm 1d 模块。 |
|
这是一个顺序容器,它调用 Conv 2d 和 Batch Norm 2d 模块。 |
|
这是一个顺序容器,它调用 Conv 3d 和 Batch Norm 3d 模块。 |
|
这是一个顺序容器,它调用 Conv 1d、Batch Norm 1d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 Conv 2d、Batch Norm 2d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 Conv 3d、Batch Norm 3d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 BatchNorm 2d 和 ReLU 模块。 |
|
这是一个顺序容器,它调用 BatchNorm 3d 和 ReLU 模块。 |
torch.ao.nn.intrinsic.qat#
此模块实现了量化感知训练所需的融合操作版本。
一个由 Linear 和 ReLU 模块融合的 LinearReLU 模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvBn1d 模块是由 Conv1d 和 BatchNorm1d 融合的模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvBnReLU1d 模块是由 Conv1d、BatchNorm1d 和 ReLU 融合的模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvBn2d 模块是由 Conv2d 和 BatchNorm2d 融合的模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvBnReLU2d 模块是由 Conv2d、BatchNorm2d 和 ReLU 融合的模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvReLU2d 模块是由 Conv2d 和 ReLU 融合的模块,附加了用于量化感知训练的权重的 FakeQuantize 模块。 |
|
ConvBn3d 模块是由 Conv3d 和 BatchNorm3d 融合的模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvBnReLU3d 模块是由 Conv3d、BatchNorm3d 和 ReLU 融合的模块,附加了用于权重的 FakeQuantize 模块,用于量化感知训练。 |
|
ConvReLU3d 模块是由 Conv3d 和 ReLU 融合的模块,附加了用于量化感知训练的权重的 FakeQuantize 模块。 |
|
torch.ao.nn.intrinsic.quantized#
此模块实现了像 conv + relu 这样的融合操作的量化实现。没有 BatchNorm 变体,因为它们通常在推理时折叠到卷积中。
BNReLU2d 模块是 BatchNorm2d 和 ReLU 的融合模块 |
|
BNReLU3d 模块是 BatchNorm3d 和 ReLU 的融合模块 |
|
ConvReLU1d 模块是 Conv1d 和 ReLU 的融合模块 |
|
ConvReLU2d 模块是 Conv2d 和 ReLU 的融合模块 |
|
ConvReLU3d 模块是 Conv3d 和 ReLU 的融合模块 |
|
由 Linear 和 ReLU 模块融合的 LinearReLU 模块 |
torch.ao.nn.intrinsic.quantized.dynamic#
此模块实现了像 linear + relu 这样的融合操作的量化动态实现。
由 Linear 和 ReLU 模块融合的 LinearReLU 模块,可用于动态量化。 |
torch.ao.nn.qat#
此模块实现了关键的 nn 模块 **Conv2d()** 和 **Linear()** 的版本,它们在 FP32 中运行,但应用了舍入以模拟 INT8 量化的效果。
torch.ao.nn.qat.dynamic#
此模块实现了诸如 **Linear()** 等关键 nn 模块的版本,它们在 FP32 中运行,但应用了舍入以模拟 INT8 量化的效果,并在推理时动态量化。
一个附加了用于权重的 FakeQuantize 模块的 Linear 模块,用于动态量化感知训练。 |
torch.ao.nn.quantized#
此模块实现了像 ~torch.nn.Conv2d
和 torch.nn.ReLU
等 nn 层的量化版本。
逐元素应用函数 |
|
这是 |
|
这是 |
|
这是 |
|
这是 |
|
这是 |
|
这是 |
|
对由多个量化输入平面组成的量化输入信号应用一维卷积。 |
|
对由多个量化输入平面组成的量化输入信号应用二维卷积。 |
|
对由多个量化输入平面组成的量化输入信号应用三维卷积。 |
|
对由多个输入平面组成的输入图像应用 1D 转置卷积运算符。 |
|
对由多个输入平面组成的输入图像应用 2D 转置卷积运算符。 |
|
对由多个输入平面组成的输入图像应用三维转置卷积算子。 |
|
一个量化的 Embedding 模块,输入为量化的打包权重。 |
|
一个量化的 EmbeddingBag 模块,输入为量化的打包权重。 |
|
浮点运算的状态收集器类。 |
|
在 FX 图模式量化之前替换 FloatFunctional 模块的模块,因为 activation_post_process 将直接插入到顶层模块中 |
|
量化操作的包装类。 |
|
一个量化的线性模块,输入和输出均为量化张量。 |
|
这是 |
|
这是 |
|
这是 |
|
这是 |
|
这是 |
torch.ao.nn.quantized.functional#
功能接口(量化)。
此模块实现了像 ~torch.nn.functional.conv2d 和 torch.nn.functional.relu 这样的功能层的量化版本。注意: 支持量化输入。
在 区域上,以 的步长进行二维平均池化操作。 |
|
在 区域上,以 的步长进行三维平均池化操作。 |
|
对量化输入信号(由多个量化输入平面组成)应用二维自适应平均池化。 |
|
对量化输入信号(由多个量化输入平面组成)应用三维自适应平均池化。 |
|
对量化一维输入(由多个输入平面组成)应用一维卷积。 |
|
对量化二维输入(由多个输入平面组成)应用二维卷积。 |
|
对量化三维输入(由多个输入平面组成)应用三维卷积。 |
|
将输入下采样或上采样到给定的 |
|
对输入量化数据应用线性变换:。 |
|
对量化输入信号(由多个量化输入平面组成)应用一维最大池化。 |
|
对量化输入信号(由多个量化输入平面组成)应用二维最大池化。 |
|
逐元素应用量化的 CELU 函数。 |
|
的量化版本。 |
|
这是 |
|
这是 |
|
逐元素应用量化的阈值函数 |
|
这是 |
|
这是 |
|
float(input, min_, max_) -> Tensor |
|
将输入上采样到给定的 |
|
使用双线性上采样对输入进行上采样。 |
|
使用最近邻像素值对输入进行上采样。 |
torch.ao.nn.quantizable#
此模块实现了某些 nn 层的可量化版本。这些模块可以与自定义模块机制结合使用,通过向 prepare 和 convert 参数提供 custom_module_config
来实现。
可量化的长短期记忆(LSTM)。 |
|
torch.ao.nn.quantized.dynamic#
量化数据类型和量化方案#
请注意,当前算子实现仅支持 **conv** 和 **linear** 算子权重的逐通道量化。此外,输入数据通过以下方式线性映射到量化数据,反之亦然:
其中 与 clamp()
相同,而比例因子 和零点 的计算则如 MinMaxObserver
中所述,具体而言:
其中 :math:[x_\text{min}, x_\text{max}]
表示输入数据的范围,而 :math:Q_\text{min}
和 :math:Q_\text{max}
分别是量化数据类型的最小值和最大值。
请注意,选择 :math:s
和 :math:z
的方式意味着,当零处于输入数据范围之内或使用对称量化时,零将不带量化误差地表示。
可以通过 自定义 运算符 机制 <https://pytorch.ac.cn/tutorials/advanced/torch_script_custom_ops.html>
_ 实现额外的数据类型和量化方案。
torch.qscheme
— 用于描述张量量化方案的类型。支持的类型torch.per_tensor_affine
— 按张量,非对称torch.per_channel_affine
— 按通道,非对称torch.per_tensor_symmetric
— 按张量,对称torch.per_channel_symmetric
— 按通道,对称
torch.dtype
— 用于描述数据的类型。支持的类型torch.quint8
— 8 位无符号整数torch.qint8
— 8 位有符号整数torch.qint32
— 32 位有符号整数
QAT 模块。
此包正在被弃用。请改用 torch.ao.nn.qat.modules。
QAT 动态模块。
此包正在被弃用。请改用 torch.ao.nn.qat.dynamic。
此文件正在迁移到 torch/ao/quantization,并在迁移过程中保留于此以兼容。如果您要添加新的条目/功能,请将其添加到 torch/ao/quantization/fx/ 下的相应文件中,并在此处添加导入语句。
QAT 动态模块。
此包正在被弃用。请改用 torch.ao.nn.qat.dynamic。
量化模块。
- 注意:
torch.nn.quantized 命名空间正在被弃用。请改用 torch.ao.nn.quantized。
量化动态模块。
此文件正在迁移到 torch/ao/nn/quantized/dynamic,并在迁移过程中保留于此以兼容。如果您要添加新的条目/功能,请将其添加到 torch/ao/nn/quantized/dynamic 下的相应文件中,并在此处添加导入语句。