python - 如何确认我的 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 模型是否已被量化?
解决方案
使用print(modellpot)检查模型是否被量化。例如,Linear 层将被转换为QuantizedLinear层。实际上,只有 PyTorch 支持的层才会转换为量化层,因此并非所有参数都是 int8/uint8。
当模型在每个输出中打印时,您将能够看到数据类型,例如, 如果在打印模型时执行了 int8 量化,模型输出将显示 dtype 为qint8 。
推荐阅读
- java - 在“普通”Java 应用程序和 Web 应用程序中运行良好的库关闭例程
- ruby-on-rails - 如何在dependecy gem中添加调试器(binding.pry)?
- c# - 为什么我的 DLL 在使用其他库(例如 curl、curpp、cpprestsdk)编译后无法工作?
- angular - 从回调函数角度调用组件的函数
- android - 如何处理带有 html 内容的请求?
- ios - 在 iOS 开发的应用导航中轮到谷歌地图
- javascript - 画布动画圈不显示
- php - PHP的垃圾收集器不起作用?
- python - 最大刻度数和滚动 X
- python - 在函数图中添加箭头以显示其方向