评价此页

Linear#

class torch.ao.nn.quantized.Linear(in_features, out_features, bias_=True, dtype=torch.qint8)[source]#

一个量化的线性模块,输入和输出均为量化张量。我们采用与torch.nn.Linear相同的接口,请参考 https://pytorch.ac.cn/docs/stable/nn.html#torch.nn.Linear 的文档。

Linear 类似,属性将在模块创建时被随机初始化,之后会被覆盖。

变量
  • weight (Tensor) – 模块的非学习量化权重,形状为 (out_features,in_features)(\text{out\_features}, \text{in\_features})

  • bias (Tensor) – 模块的非学习偏置,形状为 (out_features)(\text{out\_features})。如果 biasTrue,则值初始化为零。

  • scale – 输出量化张量的 scale 参数,类型:double

  • zero_point – 输出量化张量的 zero_point 参数,类型:long

示例

>>> m = nn.quantized.Linear(20, 30)
>>> input = torch.randn(128, 20)
>>> input = torch.quantize_per_tensor(input, 1.0, 0, torch.quint8)
>>> output = m(input)
>>> print(output.size())
torch.Size([128, 30])
classmethod from_float(mod, use_precomputed_fake_quant=False)[source]#

从观察到的浮点模块创建量化模块

参数
  • mod (Module) – 一个浮点模块,由 torch.ao.quantization 工具生成或用户提供

  • use_precomputed_fake_quant (bool) – 如果为 True,模块将重用预计算的 fake quant 模块中的 min/max 值。

classmethod from_reference(ref_qlinear, output_scale, output_zero_point)[source]#

从参考量化模块创建 (fbgemm/qnnpack) 量化模块

参数
  • ref_qlinear (Module) – 一个参考量化线性模块,由 torch.ao.quantization 工具生成或用户提供

  • output_scale (float) – 输出张量的 scale

  • output_zero_point (int) – 输出张量的 zero point