导入
复制
导入
警告
from 打字
导入
任何,
可选,
联合
导入
火炬
from torch.fx 导入
图模块
from torch.fx 图形模块
导入
_用户保留属性键
from .后端配置
导入
后端配置,
获取 TensorRT 后端配置 # noqa: F401
from .fx 转换
导入
转换
from .fx.自定义配置
导入 ConvertCustomConfig,
融合自定义配置,
准备自定义配置
from .fx.熔合
导入
熔合 # noqa: F401
from .fx.图形模块
导入
观察图模块 # noqa: F401
from .fx.准备
导入
准备 # noqa: F401
from .fx.追踪器
导入
量化追踪器,
范围,
范围上下文管理器 # noqa: F401
from .fx.utils 导入 ( # noqa: F401
获取自定义模块类键,
获取跳过的模块名称和类,
)
from .qconfig_mapping 导入
QConfig 映射
def 将保留的属性附加到模型(
模型:
联盟[GraphModule,
火把.
神经网络.
模块
],
保留属性:
字典[
字符串,
任何
],
) -> 无:
将保留的属性存储到 model.meta 中,以便在 deepcopy 过程中保留
模型.
元数据[
用户保留属性键] =
复制.
复制(
保留属性)
忽略[操作符,索引,赋值]
设置保留的属性在模型中,以便用户在调用
模型.attr,就像在调用 fx 图模式量化之前一样
for 属性名称,
属性
在
模型.
元数据[
用户保留属性键].
项目():
忽略[索引,联合属性]
setattr(模型,
属性名称,
属性)
def _检查是否为图模块(
模型:
火把.
神经网络.
模块) ->
无:
如果
不 isinstance(
模型, GraphModule):
raise ValueError(
"输入模型必须是一个 GraphModule,"
+ "类型为:"
+ 字符串(
类型(
模型))
+ "请制作 "
+ "务必遵循教程。"
)
def _如果节点不存在则附加元数据_(
模型: GraphModule) ->
无:
"""如果图中所有节点不存在元字段,则附加元字段,
元字段是一个存储节点某些元信息的字段,例如
节点的输出数据类型和形状信息,这个字段仅存在
如果程序被 make_fx 捕获(用于 quantize_pt2e 流),如果
程序被 torch.fx 符号跟踪捕获,则此字段可能不存在,
因此我们在这里添加它,以避免到处检查这一点
"文档"
for 节点
在
模型.
图.
节点:
如果
不
有属性(node,
元数据):
node.元数据 = {}
def 将_ff 与_fxff 交换(
模型:
火把.
神经网络.
模块) ->
无:
r将 FloatFunctional 与 FXFloatFunctional 交换
需要交换的模块 =
输入文本为空,请提供需要翻译的文本
for 名称,
模块
在
模型.
命名子项():
如果 isinstance(
模块,
火把.ao.
神经网络.
量化.
浮点功能):
可交换模块.append(
名称)
否则:
_将_swap_ff_with_fxff 交换(
模块)
for 名称
在
可交换模块:
删除
模型.
模块[
名称]
模型.
模块[
名称] =
火把.ao.
神经网络.
量化.
FXFloat 功能()
def _融合_fx(
模型: GraphModule,
是否是 QAT:
布尔,
融合自定义配置:
联盟[
融合自定义配置,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r内部辅助函数,用于在量化前融合模块
参数:
模型:来自符号跟踪的 GraphModule 对象(torch.fx.symbolic_trace)
"文档"
_check_is_graph_module(模型)
返回
融合(
模型,
是否是 QAT,
熔合自定义配置, backend_config
) # type: ignore[operator]
def _准备_fx(
模型:
火把.
神经网络.
模块,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何]],
是否是 QAT:
布尔,
示例输入:
元组[
任何, ...
],
准备自定义配置:
联盟[
准备自定义配置,
字典[
字符串,
任何
],
无] =
无,
_均衡配置:
可选[
联盟[
QConfig 映射,
字典[
字符串,
任何]]] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
是否为独立模块:
布尔 =
错误,
) -> GraphModule:
r"""准备_fx 的内部辅助函数
参数:
`模型`, `qconfig_mapping`, `prepare_custom_config`, `_equalization_config`:
请参阅文档了解 :func:`~torch.ao.quantization.prepare_fx`
`is_standalone_module`: 一个布尔标志,表示我们是否
对独立模块进行量化,或者不是,独立模块
是父模块中未内联的子模块
父模块的前向图中,
独立模块的量化方式描述如下:
func:`~torch.ao.quantization._prepare_standalone_module_fx`
"文档"
如果
准备自定义配置
是
无:
准备自定义配置 =
准备自定义配置()
如果
_均衡配置_
是
无:
_均衡配置_ =
QConfig 映射()
如果 isinstance(
准备自定义配置,
字典):
警告.
警告(
"将 prepare_custom_config_dict 传递给 prepare 已被弃用且将不再支持"
"在未来的版本中。请传递 PrepareCustomConfig。",
未来警告,
栈级别=3,
)
prepare_custom_config = 准备自定义配置.from_dict(
准备自定义配置)
将 FloatFunctional 替换为 FXFloatFunctional
_将_ff 替换为_fxff(
模型)
跳过的模块名称,
跳过的模块类 =
获取跳过的模块名称和类(
准备自定义配置,
独立模块
)
保留属性名称 =
准备自定义配置.
保留属性
保留属性 = {
属性: getattr(
模型,
属性)
for 属性
在
保留属性名称
如果
有属性(
模型,
属性)
}
# 符号化追踪模型
追踪器 =
量化追踪器(
跳过的模块名称,
跳过的模块类) # type: ignore[arg-type]
图模块 = GraphModule(
模型,
追踪器.
跟踪(
模型))
将元数据附加到节点(如果不存在)(
图模块)
合并自定义配置 =
融合自定义配置().
设置保留属性(
准备自定义配置.
保留属性
)
图模块 =
_融合效果(
图模块,
是否是 QAT,
融合自定义配置,
后端配置)
准备好的 = prepare(
图模块,
QConfig 映射,
是否是 QAT,
追踪器.node_name_to_scope,
示例输入=
示例输入,
准备自定义配置=
准备自定义配置,
_均衡配置=
_均衡配置,
后端配置=
后端配置,
独立模块=
独立模块,
) # type: ignore[operator]
将保留属性附加到模型(
准备好,
保留属性)
返回
准备
def _准备独立模块_fx(
模型:
火把.
神经网络.
模块,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何]],
是否是 QAT:
布尔,
示例输入:
元组[
任何, ...
],
准备自定义配置:
联盟[
准备自定义配置,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r[仅限内部使用] 准备一个独立模块,以便在量化时使用
父模块。
standalone_module 表示它是一个不在父模块中内联的子模块。
将分别作为一个单元进行量化。
独立模块的观察方式由 `input_quantized_idxs` 和 `output_quantized_idxs` 指定,
在 `prepare_custom_config` 中为独立模块
返回:
* 模型(GraphModule):准备好的独立模块。它具有以下属性
模型元数据:
`standalone_module_input_quantized_idxs(List[Int])`: 一个 Int 列表
图输入的索引,预期将进行量化
与提供的 input_quantized_idxs 配置相同
独立模块
* `standalone_module_output_quantized_idxs(List[Int])`: 一个整数列表
索引,用于表示图输出的量化
与提供的 input_quantized_idxs 配置相同
独立模块
"文档"
返回
_准备_fx(
模型,
QConfig 映射,
是否是 QAT,
示例输入,
准备自定义配置,
后端配置=
后端配置,
是否为独立模块=
是,
)
[文档]def fuse_fx(
模型: torch.nn.Module,
fuse_custom_config: Union[FuseCustomConfig, dict[str, Any], None] = None,
backend_config: Union[BackendConfig, dict[str, Any], None] = None,
) -> GraphModule:
将模块如 conv+bn、conv+bn+relu 等融合,模型必须处于评估模式。
融合规则定义在 torch.ao.quantization.fx.fusion_pattern.py 中
参数:
* `model` (torch.nn.Module): 一个 torch.nn.Module 模型
* `fuse_custom_config` (FuseCustomConfig): fuse_fx 的自定义配置
有关详细信息,请参阅 :class:`~torch.ao.quantization.fx.custom_config.FuseCustomConfig`
示例::
from torch.ao.quantization import fuse_fx
m = Model().eval()
m = fuse_fx(m)
"""
如果 fuse_custom_config 为 None:
fuse_custom_config = FuseCustomConfig()
如果 isinstance(fuse_custom_config, dict):
warnings.warn(
传递 fuse_custom_config_dict 到 fuse 已过时,将不再支持
在未来的版本中。请传入一个 FuseCustomConfig。
未来警告
stacklevel=2
)
fuse_custom_config = FuseCustomConfig.from_dict(fuse_custom_config)
torch._C._log_api_usage_once("quantization_api.quantize_fx.fuse_fx")
preserved_attr_names = fuse_custom_config.preserved_attributes
preserved_attrs = {
attr = getattr(model, attr)
for attr in preserved_attr_names
if hasattr(model, attr)
}
graph_module = torch.fx.symbolic_trace(model)
_attach_meta_to_node_if_not_exist(graph_module)
graph_module = _fuse_fx(graph_module, False, fuse_custom_config, backend_config)
将保留属性附加到模型(graph_module, preserved_attrs)
返回 graph_module
[文档]def
准备 fx(
模型:
火把.
神经网络.
模块,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何]],
示例输入:
元组[
任何, ...
],
准备自定义配置:
联盟[
准备自定义配置,
字典[
字符串,
任何
],
无] =
无,
_均衡配置:
可选[
联盟[
QConfig 映射,
字典[
字符串,
任何]]] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r""" 准备模型进行后训练量化
参数:
* `model` (torch.nn.Module): torch.nn.Module 模型
`qconfig_mapping` (QConfigMapping):用于配置模型量化方式的 QConfigMapping 对象,详见 :class:`~torch.ao.quantization.qconfig_mapping.QConfigMapping`
量化,详细信息请参阅 :class:`~torch.ao.quantization.qconfig_mapping.QConfigMapping`
有关更多信息
* `example_inputs` (Tuple[Any, ...]):模型前向函数的示例输入
位置参数的元组(关键字参数也可以作为位置参数传递)
* `prepare_custom_config`(PrepareCustomConfig):量化工具的自定义配置
有关详细信息,请参阅:class:`~torch.ao.quantization.fx.custom_config.PrepareCustomConfig`
* `_equalization_config`:指定如何在模型上执行均衡的配置
后端配置(BackendConfig):指定操作符如何量化的配置
在后端,这包括如何观察操作员,
支持的融合模式,如何插入量化/去量化操作,
支持的数据类型等。有关详细信息,请参阅::class:`~torch.ao.quantization.backend_config.BackendConfig`
返回:
配置了 qconfig_mapping 的观察器的 GraphModule,准备进行校准
示例::
导入 torch
从 torch.ao.quantization 导入 get_default_qconfig_mapping
从 torch.ao.quantization.quantize_fx 导入 prepare_fx
class Submodule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(5, 5)
def forward(self, x):
x = self.linear(x)
return x
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(5, 5)
self.sub = Submodule()
def forward(self, x):
x = self.linear(x)
x = self.sub(x) + x
return x
初始化浮点模型
float_model = M().eval()
# 定义校准函数
def 校准(model, data_loader):
model.eval()
with torch.no_grad():
for image, target in data_loader:
模型(图像)
# qconfig 是为特定操作插入观察者的配置
# 操作符
# qconfig = get_default_qconfig("fbgemm")
自定义 qconfig 的示例:
qconfig = torch.ao.quantization.QConfig(
激活=MinMaxObserver.with_args(dtype=torch.qint8),
权重=MinMaxObserver.with_args(dtype=torch.qint8))
`激活`和`权重`是观察器模块的构造函数
`qconfig_mapping`是一个量化配置集合,用户可以为每个操作(torch 操作调用、功能调用、模块调用)设置 qconfig
通过`qconfig_mapping`在模型中设置每个操作器的 qconfig
通过`qconfig_mapping`在模型中设置每个操作器的 qconfig
获取最适合模型的 qconfig_mapping
针对目标"fbgemm"后端
qconfig_mapping = get_default_qconfig_mapping("fbgemm")
我们可以以不同的方式自定义 qconfig_mapping
例如设置全局 qconfig,这意味着我们将使用相同的 qconfig
# 模型中所有操作符,这可以被其他设置覆盖
# qconfig_mapping = QConfigMapping().设置全局(qconfig)
例如,使用特定的 qconfig 对线性子模块进行量化
# qconfig_mapping = QConfigMapping().set_module_name("linear", qconfig)
# 例如,使用特定 qconfig 量化所有 nn.Linear 模块
# qconfig_mapping = QConfigMapping().set_object_type(torch.nn.Linear, qconfig)
# 更完整的列表,请参阅 :class:`torch.ao.quantization.QConfigMapping` 的文档字符串
参数
example_inputs 是一个输入元组,用于推断模型输出类型
目前未使用,但请确保 model(*example_inputs) 运行
目前它还没有被使用,但请确保 model(*example_inputs) 运行
example_inputs = (torch.randn(1, 3, 224, 224),)
# TODO: 在我们将 backend_config 分割为 fbgemm 和 qnnpack 之后添加 backend_config
# 例如:backend_config = get_default_backend_config("fbgemm")
# `prepare_fx` 根据 qconfig_mapping 在模型中插入观察者
# 后端配置。如果 qconfig_mapping 中操作员的配置
# 在后端配置中受支持(意味着它由目标
# 硬件支持),我们将根据 qconfig_mapping
# 插入观察器模块;否则,qconfig_mapping 中的配置将被忽略
#
示例:
在 qconfig_mapping 中,用户将线性模块设置为使用 quint8 进行量化
激活和 qint8 权重
# qconfig = torch.ao.quantization.QConfig(
# observer=MinMaxObserver.with_args(dtype=torch.quint8),
# weight=MinMaxObserver.with-args(dtype=torch.qint8))
# 注意:当前 qconfig API 不支持设置输出观察器,但我们可能将其扩展以支持更精细的控制
# 我们可能将其扩展以支持这些更精细的控制
# 未来
#
# qconfig_mapping = QConfigMapping().set_object_type(torch.nn.Linear, qconfig)
# 在后端配置中,线性模块也支持此配置:
# weighted_int8_dtype_config = DTypeConfig(
# input_dtype=torch.quint8,
# output_dtype=torch.quint8,
# weight_dtype=torch.qint8,
# bias_type=torch.float)
# linear_pattern_config = BackendPatternConfig(torch.nn.Linear)
# .设置观察类型为 ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT)
# .添加数据类型配置 weighted_int8_dtype_config)
# ...
# backend_config = BackendConfig().设置后端模式配置 linear_pattern_config)
`prepare_fx` 将检查用户在 qconfig_mapping 中请求的设置
# 由 backend_config 支持,并插入观察者和伪量化模块
在模型中
准备好的模型 = 准备_fx(float_model, qconfig_mapping, example_inputs)
运行校准
calibrate(prepared_model, sample_inference_data)
"文档"
火把._C._log_api_usage_once(
"量化 API.quantize_fx.prepare_fx")
返回 _prepare_fx(
模型,
QConfig 映射,
错误, # is_qat
示例输入,
准备自定义配置,
等化配置,
后端配置,
)
[文档]def
准备 QAT 效果(
模型:
火把.
神经网络.
模块,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何]],
示例输入:
元组[
任何, ...
],
准备自定义配置:
联盟[
准备自定义配置,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r为量化感知训练准备模型
参数:
* `model` (torch.nn.Module): torch.nn.Module 模型
* `qconfig_mapping` (QConfigMapping): 查看 :func:`~torch.ao.quantization.prepare_fx`
* `example_inputs` (Tuple[Any, ...]): 查看 :func:`~torch.ao.quantization.prepare_fx`
* `prepare_custom_config` (PrepareCustomConfig): 查看 :func:`~torch.ao.quantization.prepare_fx`
* `backend_config` (BackendConfig): 查看 :func:`~torch.ao.quantization.prepare_fx`
返回:
具有模拟量化模块的 GraphModule(由 qconfig_mapping 和 backend_config 配置),已准备好进行
量化感知训练
示例::
导入 torch
从 torch.ao.quantization 导入 get_default_qat_qconfig_mapping
从 torch.ao.quantization.quantize_fx 导入 prepare_qat_fx
class Submodule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(5, 5)
def forward(self, x):
x = self.linear(x)
return x
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(5, 5)
self.sub = Submodule()
def forward(self, x):
x = self.linear(x)
x = self.sub(x) + x
return x
初始化浮点模型
float_model = M().train()
# (可选,但推荐) 从预训练模型加载权重
# float_model.load_weights(...)
# 定义量化感知训练的训练循环
def train_loop(model, train_data):
model.train()
对于图像,目标在数据加载器中:
...
# qconfig 是为特定操作插入观察者的配置
# 操作符
# qconfig = get_default_qconfig("fbgemm")
自定义 qconfig 的示例:
qconfig = torch.ao.quantization.QConfig(
激活=FakeQuantize.with_args(observer=MinMaxObserver.with_args(dtype=torch.qint8)),
权重=FakeQuantize.with_args(observer=MinMaxObserver.with_args(dtype=torch.qint8)))
`激活`和`权重`是观察器模块的构造函数
`qconfig_mapping`是一个量化配置集合,用户可以为每个操作(torch 操作调用、功能调用、模块调用)设置 qconfig
通过`qconfig_mapping`在模型中设置每个操作器的 qconfig
通过`qconfig_mapping`在模型中设置每个操作器的 qconfig
获取最适合模型的 qconfig_mapping
针对目标"fbgemm"后端
qconfig_mapping = get_default_qat_qconfig("fbgemm")
我们可以以不同的方式自定义 qconfig_mapping,请查看
# :func:`~torch.ao.quantization.prepare_fx` 的文档字符串,用于不同配置方式
# 如何配置此功能
# example_inputs 是一个输入元组,用于推断模型输出的类型
# 的示例
# 目前未使用,但请确保模型(*example_inputs)运行
example_inputs = (torch.randn(1, 3, 224, 224),)
# TODO: 在我们将 backend_config 分割为 fbgemm 和 qnnpack 之后添加 backend_config
# 例如 backend_config = get_default_backend_config("fbgemm")
`prepare_qat_fx`根据 qconfig_mapping 在模型中插入观察者
`backend_config`,如果 qconfig_mapping 中操作符的配置在`backend_config`中受支持(意味着它由目标硬件支持),我们将根据 qconfig_mapping 插入 fake_quantize 模块
`backend_config`(意味着它由目标硬件支持),我们将根据 qconfig_mapping 插入 fake_quantize 模块
我们将根据 qconfig_mapping 插入 fake_quantize 模块
# 否则将忽略 qconfig_mapping 中的配置
# 请参阅 :func:`~torch.ao.quantization.prepare_fx` 以获取详细说明
# qconfig_mapping 如何与 backend_config 交互
prepared_model = prepare_qat_fx(float_model, qconfig_mapping, example_inputs)
运行训练
train_loop(prepared_model, train_loop)
"文档"
火把._C._log_api_usage_once(
quantization_api.quantize_fx.prepare_qat_fx)
返回
准备效果(
模型,
QConfig 映射,
是,
# 是否为 QAT
示例输入,
准备自定义配置,
后端配置=
后端配置,
)
def _转换效果(
图模块: GraphModule,
参考项:
布尔,
转换自定义配置:
联盟[ConvertCustomConfig,
字典[
字符串,
任何
],
无] =
无,
是否为独立模块:
布尔 =
错误,
移除_qconfig:
布尔 =
是,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
已分解:
布尔 =
错误,
保持原始权重:
布尔 =
错误,
) -> GraphModule:
`is_standalone_module`: 请参阅文档在 :func:`~torch.ao.quantization.prepare_standalone_module_fx` 中
如果
转换自定义配置
是
无:
转换自定义配置 = ConvertCustomConfig()
如果 isinstance(
转换自定义配置,
字典):
警告.
警告(
将 convert_custom_config_dict 传递给 convert 已被弃用且将不再受支持
在未来的版本中。请传递 ConvertCustomConfig。,
未来警告,
栈级别=3,
)
转换自定义配置 = ConvertCustomConfig.
从字典转换(
转换自定义配置)
_检查是否为图模块(
图模块)
保留属性名称 =
转换自定义配置.
保留属性
保留属性 = {
属性: getattr(
图模块,
属性)
for 属性
在
保留属性名称
如果
有属性(
图模块,
属性)
}
量化 =
转换(
图模块,
参考项,
转换自定义配置,
是否为独立模块,
移除_qconfig_flag 标志=
删除 qconfig,
QConfig 映射=
QConfig 映射,
后端配置=
后端配置,
是否分解=
是否分解,
保留原始权重=
保持原始权重,
)
将保留的属性附加到模型(
量化,
保留属性)
返回
量化
[文档]def
转换_fx(
图模块: GraphModule,
转换自定义配置:
联盟[ConvertCustomConfig,
字典[
字符串,
任何
],
无] =
无,
_移除_qconfig:
布尔 =
是,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
保留原始权重:
布尔 =
错误,
) -> GraphModule:
r将校准或训练好的模型转换为量化模型
参数:
* `graph_module` (torch.fx.GraphModule):准备并校准/训练好的模型(GraphModule)
* `convert_custom_config` (ConvertCustomConfig):转换函数的自定义配置。
有关更多详细信息,请参阅 :class:`~torch.ao.quantization.fx.custom_config.ConvertCustomConfig`
* `_remove_qconfig` (bool): 选项用于在转换后从模型中移除 qconfig 属性。
* `qconfig_mapping` (QConfigMapping): 指定如何进行模型量化的配置。
密钥必须包括传递给 `prepare_fx` 或 `prepare_qat_fx` 的 qconfig_mapping 中的那些
使用相同的值或 `None`。可以指定额外的键,并将值设置为 `None`。
对于每个值设置为 None 的条目,我们跳过在模型中对该条目进行量化::
qconfig_mapping = QConfigMapping
.set_global(qconfig_from_prepare)
.set_object_type(torch.nn.functional.add, None) # 跳过量化 torch.nn.functional.add
.set_object_type(torch.nn.functional.linear, qconfig_from_prepare)
.set_module_name("foo.bar", None) # 跳过量化模块 "foo.bar"
后端配置(BackendConfig):后端配置,描述了如何
操作符应在后端进行量化,这包括量化
模式支持(静态/动态/仅权重),数据类型支持(quint8/qint8 等)。
每个操作符和融合操作符的观察者放置。
请参阅:class:`~torch.ao.quantization.backend_config.BackendConfig` 以获取更多详细信息
返回:
量化模型(torch.nn.Module)
示例::
# prepared_model:经过 prepare_fx/prepare_qat_fx 和校准/训练后的模型
# convert_fx 将校准/训练后的模型转换为量化模型
目标硬件,这包括首先将模型转换为参考
量化模型,然后将参考量化模型降低到后端
目前,支持的后端包括 fbgemm (onednn)、qnnpack (xnnpack)
它们共享同一组量化算子,因此我们使用相同的
# 降低过程
#
# backend_config 定义了相应的参考量化模块
# 模型中的加权模块,例如 nn.Linear
# TODO:在我们将 backend_config 拆分为 fbgemm 和 qnnpack 之后添加 backend_config
backend_config = 获取默认后端配置("fbgemm")
quantized_model = 将准备好的模型转换为量化模型
"文档"
火把._C._log_api_usage_once(
"量化 API.量化 fx.convert_fx")
返回 _convert_fx(
图模块,
参考项=
错误,
转换自定义配置=
转换自定义配置,
_移除_qconfig=
_移除_qconfig,
QConfig 映射=
QConfig 映射,
后端配置=
后端配置,
保留原始权重=
保留原始权重,
)
def 转换为参考 FX(
图模块: GraphModule,
转换自定义配置:
联盟[ConvertCustomConfig,
字典[
字符串,
任何
],
无] =
无,
_移除_qconfig:
布尔 =
是,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r将校准或训练的模型转换为参考量化模型,
更多详细信息,请参阅 https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md
参考量化模型是量化模型的标准表示
通过 FX Graph 模式量化,它可以进一步降低以在目标上运行
硬件,如加速器
参数:
* `graph_module` (GraphModule):一个准备就绪并校准/训练过的模型(GraphModule)
* `convert_custom_config` (ConvertCustomConfig):convert 函数的自定义配置。
更多详情请参阅::func:`~torch.ao.quantization.quantize_fx.convert_fx`。
* `_remove_qconfig` (bool):在转换后从模型中移除 qconfig 属性的选项。
* `qconfig_mapping` (QConfigMapping):指定如何将模型转换为量化的配置。
查看 :func:`~torch.ao.quantization.quantize_fx.convert_fx` 获取更多详细信息。
* `backend_config` (BackendConfig):后端配置,用于描述后端中算子应该如何量化。查看
func:`~torch.ao.quantization.quantize_fx.convert_fx` 获取更多详细信息。
查看 :func:`~torch.ao.quantization.quantize_fx.convert_fx` 获取更多详细信息。
返回:
参考量化模型(GraphModule)
示例::
# 准备后的模型:经过 prepare_fx/prepare_qat_fx 和校准/训练后的模型
# TODO:在我们将 backend_config 分割为 fbgemm 和 qnnpack 之后添加 backend_config
# 例如:backend_config = get_default_backend_config("fbgemm")
reference_quantized_model = convert_to_reference_fx(prepared_model)
"文档"
火把._C._log_api_usage_once(
quantization_api.quantize_fx.convert_to_reference_fx)
返回 _convert_fx(
图模块,
参考项=
是,
转换自定义配置=
转换自定义配置,
_移除 qconfig=
_移除 qconfig,
QConfig 映射=
QConfig 映射,
后端配置=
后端配置,
)
def 将转换为参考分解的 FX(
图模块: GraphModule,
转换自定义配置:
联盟[ConvertCustomConfig,
字典[
字符串,
任何
],
无] =
无,
QConfig 映射:
联盟[
QConfig 映射,
字典[
字符串,
任何
],
无] =
无,
后端配置:
联盟[
后端配置,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r将校准或训练的模型转换为参考量化模型,具有分解表示的量化张量
量化张量的分解表示
请参阅 https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md 以获取更多详细信息,
参考量化模型是 FX 图模式量化提供的一个量化模型的标准表示,
它可以被进一步降低以在目标硬件上运行,例如加速器,
如同加速器这样的目标硬件。
注意:这不是公开 API
参数:
* `graph_module` (GraphModule):一个准备并校准/训练好的模型(GraphModule)
* `convert_custom_config` (ConvertCustomConfig):convert 函数的定制配置。
更多详情请参阅::func:`~torch.ao.quantization.quantize_fx.convert_fx`
* `_remove_qconfig` (bool): 选项用于在转换后从模型中移除 qconfig 属性。
* `qconfig_mapping` (QConfigMapping): 指定如何进行量化转换的配置。
查看更多详情::func:`~torch.ao.quantization.quantize_fx.convert_fx`。
* `backend_config` (BackendConfig):后端配置,用于描述后端如何
操作符应在后端进行量化。详见
func:`~torch.ao.quantization.quantize_fx.convert_fx` 以获取更多详细信息。
返回:
一个参考量化模型(GraphModule),其中操作符与分解量化张量一起工作
示例::
# 准备模型:经过 prepare_fx/prepare_qat_fx 和校准/训练后的模型
# TODO: 在我们将 backend_config 分割为 fbgemm 和 qnnpack 之后添加 backend_config
# 例如,backend_config = get_default_backend_config("fbgemm")
reference_quantized_model = _convert_to_reference_decomposed_fx(prepared_model)
"文档"
火把._C._log_api_usage_once(
"quantization_api.quantize_fx._convert_to_reference_decomposed_fx"
)
返回
转换_fx(
图模块,
参考项=
是,
转换自定义配置=
转换自定义配置,
_移除_qconfig=
错误,
QConfig 映射=
QConfig 映射,
后端配置=
后端配置,
已分解=
是,
)
def _转换独立模块_fx(
图模块: GraphModule,
参考项:
布尔 =
错误,
转换自定义配置:
联盟[ConvertCustomConfig,
字典[
字符串,
任何
],
无] =
无,
) -> GraphModule:
r[仅内部使用] 将由 :func:`~torch.ao.quantization.prepare_standalone_module_fx` 产生的模型进行转换
将其转换为量化模型
返回一个量化独立模块,无论输入/输出是否量化
指定为 prepare_custom_config,与
输入量化索引,输出量化索引,请
查看文档以获取 prepare_fx 的详细信息
"文档"
返回
_转换_fx(
图模块,
参考项,
转换自定义配置,
是否为独立模块=
是,
)