【问题标题】:how to confirm if the weights of my pytorch model has been quantized如何确认我的 pytorch 模型的权重是否已量化
【发布时间】:2021-09-29 12:08:25
【问题描述】:

我能够使用 intel lpot(neural compressor) 成功量化用于拥抱脸文本分类的 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 模型是否已量化?

【问题讨论】:

  • 您可以通过以下方式检查权重值: print(list(model.named_pa​​rameters())) 也许无论如何都有帮助

标签: python pytorch quantization intel-lpot


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2018-09-23
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多