专栏算法工具链QAT量化calibration阶段报错

QAT量化calibration阶段报错

已解决
默认383362025-05-12
75
3

您好,我参考J5工具链的QAT参考算法模型示例自己量化SSD模型,在指定stage为calibration运行train.py的过程中,遇到如下两个问题:

 

  1. 当calibration_trainer中的activation_calibration_observer选择mse算法时,首先在/usr/local/lib/python3.8/dist-packages/horizon_plugin_pytorch/quantization/observer_v2.py中的_compute_scale_symmetric函数下会报错RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!,我在报错的部分使用xxx=xxx.cuda()统一设备后,报错RuntimeError: [16:23:14] /home/jenkins/agent/torch1130_cu116_py38/3rdparty/hbdnn/include/hbdnn/utils.h:283: Check failed: (Device::kDevMask) == (dev_type == kDLCPU ? mshadow::cpu::kDevMask : mshadow::gpu::kDevMask),请问这个设备错误信息是什么原因?

  2. 当calibration_trainer中的activation_calibration_observer选择min_max算法时,不会出现上述设备信息报错,但是报错TypeError: The input should be QTensor, but got <class 'torch.Tensor'>, please make sure the qconfig is set correctly.Users can use the QuantStub to quantize a tensor directly.但是我在模型类内的forward函数开始已经写了x=quant(x),末尾也写了x=dequant(x),且在模型类内也写了如下的qconfig函数。请问还有哪里需要修改?谢谢!
算法工具链
征程5
评论1
0/1000
  • Huanghui
    Lv.5

    你好,如果只是想通过qat量化自己的模型,尽量还是直接使用 horizon_plugin_pytorch吧

    2025-05-14
    0
    2
    • 默认38336回复Huanghui:

      问题1已经解决,问题2您可否给出些建议?谢谢

      2025-05-14
      0
    • Huanghui回复默认38336:

      问题2,原因是有参与运算的tensor没有量化,比如模型比较复杂,上层使用了下层定义的模块,而下层内部存在一些Init的时候或计算的使用定义的常量tensor,上层虽然把forward的inputs都量化了,但是下层自己定义的tensor因为没有量化,就会存在有的量化了,有的没有量化的情况,你可以在报错的地方打印一下type看看。

      2025-05-14
      0