背景:

模式1. 训练阶段将barc 模型转换为fp16,以减少显存开销
模式2. 推理阶段直接将权重转换为fp16/int8

步骤

1.修改model.half()
2.加载预训练权重,修改为half

混合精度训练 FP16 O3

https://zhuanlan.zhihu.com/p/416942523

from apex import amp, optimizers
# Allow Amp to perform casts as required by the opt_level
model, optimizer = amp.initialize(model, optimizer, opt_level="O3")
...

loss.backward() becomes:

with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
…

bug 1

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same
解决方案:

bug 2

torch.matmul(pose_feature, self.posedirs),
RuntimeError: expected scalar type Float but found Half

参考文档

pytoch常见错误
https://blog.csdn.net/qq_40520596/article/details/106980556

更多推荐