首页 > 解决方案 > 如何确认我的 pytorch 模型的权重是否已量化

问题描述

我能够使用 Intel lpot(神经压缩器)成功地量化用于拥抱面部文本分类的 pytorch 模型

现在我的机器中有原始的 fp32 模型和量化的 int8 模型。为了推断,我用下面的代码加载了量化的 lpot 模型

model = AutoModelForSequenceClassification.from_pretrained('fp32/model/path')
from lpot.utils.pytorch import load  
modellpot = load("path/to/lpotmodel/", model)

我能够看到各种时间的改进,但我想确认模型权重是否已实际量化并使用 int8、fp16 等数据类型,理想情况下这应该是加速的原因。我遍历模型权重并打印权重的 dtypes,但我看到所有权重都是 fp32 类型

for param in modellpot.parameters():
  print(param.data.dtype)

输出

torch.float32
torch.float32
torch.float32
torch.float32
torch.float32
torch.float32
torch.float32
..
...

如何验证我的 pytorch 模型是否已被量化?

标签: pythonpytorchquantizationintel-lpot

解决方案


使用print(modellpot)检查模型是否被量化。例如,Linear 层将被转换为QuantizedLinear层。实际上,只有 PyTorch 支持的层才会转换为量化层,因此并非所有参数都是 int8/uint8。

当模型在每个输出中打印时,您将能够看到数据类型,例如, 如果在打印模型时执行了 int8 量化,模型输出将显示 dtype 为qint8 。


推荐阅读