复现
当我们使用一下代码构建推理时
pipeline(Tasks.card_detection, 'damo/cv_resnet_carddetection_scrfd34gkps', device = "cpu")
推理时会出现以下报错
File "/opt/conda/lib/python3.8/site-packages/torch/nn/parallel/data_parallel.py", line 154, in forward
raise RuntimeError("module must have its parameters and buffers "
RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu
方法
更改文件
modelscope/models/cv/face_detection/scrfd/scrfd_detect.py:51
detector = MMDataParallel(detector, device_ids = [0])
detector.device_ids=None # + 添加这行代码
原理
MMDataParallel
的forward
函数确实有实现当device_ids
为None
的情况, 即使用cpu
推理, 但是当我们设置detector = MMDataParallel(detector, device_ids = None)
是没卵用的
MMDataParallel
继承于torch
的DataParallel
, 当初始化时 调用父类初始化自动查找可以使用的cuda
设备, 重置device_ids
, 所以
只需要在初始化后重新设置device_ids
为None
即可