常见故障

import 出错

错误一:Cannot find the extension library(_C.so)

解决方法:

  • 确定 horizon_plugin_pytorch 版本和 cuda 版本是对应的。
  • 在 python3 中,找到 horizon_plugin_pytorch 的执行路径,检测该目录下是否有 .so 文件。可能同时存在多个 horizon_plugin_pytorch 的版本,需要卸载只保留一个需要的版本。

错误二:RuntimeError: Cannot load custom ops. Please rebuild the horizon_plugin_pytorch

解决方法:确认本地 CUDA 环境是否正常,如路径、版本等。

Module 不支持 deepcopy

某些框架(例如:PyTorch Lightning)对 torch 原生 module 做了二次封装,但不支持 deepcopy。

解决方法:为模型实现 __deepcopy__ 方法。

class Model(Module):
    ...
    def __deepcopy__(self, memo):
        new_model = Model()
        new_model.xxx = self.xxx
        return new_model

RuntimeError: Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment

该报错通常发生在 deepcopy torch.nn.Module 的时候。通常是 forward 过程中将某些中间结果存在了模型中,模型中存在 non-leaf tensor 导致 deepcopy 报错。

class Model(torch.nn.Module):
    ...
    def forward(self, x):
        # x 为某个需要梯度的输入 x.requires_grad = True
        y = x + 1

        # y 为 non-leaf tensor,赋值给模型的某个属性后,deepcopy model 就会报错
        self.yy = y

解决方法:检查模型中,以及 forward 之后,是否有某些属性是 non-leaf tensor。在保存这些 tensor 前,确保进行了 detach 操作。

class Model(torch.nn.Module):
    ...
    def forward(self, x):
        # x 为某个需要梯度的输入 x.requires_grad = True
        y = x + 1

        # y 为 non-leaf tensor,保存到模型中时先进行 detach
        self.yy = y.detach()

export 时报错:IndexError: list index out of range

该报错通常是因为模型输出中存在 QTensor。

解决方法:检查模型输出,确保所有输出都经过了 dequant。