我有8个CPU,64个CPU核(multiprocessing.cpu_count()= 64 )
我试图用一个深度学习模型来推断多个视频文件。我希望在8个GPU中的每一个上处理一些文件。对于每个GPU,我想要一个不同的6个CPU核心使用。
下面的python文件名:inference_{gpu_id}.py
Input1: GPU_id
Input2: Files to process for GPU_id
from torch.multiprocessing import Pool, Process, set_start_method
try:
set_start_method('spawn', force=True)
except RuntimeError:
pass
model = load_model(device='cuda:' + gpu_id)
def pooling_func(file):
preds = []
cap = cv2.VideoCapture(file)
while(cap.isOpened()):
ret, frame = cap.read()
count += 1
if ret == True:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
pred = model(frame)[0]
preds.append(pred)
else:
break
cap.release()
np.save(file[:-4]+'.npy', preds)
def process_files():
# all files to process on gpu_id
files = np.load(gpu_id + '_files.npy')
# I am hoping to use 6 cores for this gpu_id,
# and a different 6 cores for a different GPU id
pool = Pool(6)
r = list(tqdm(pool.imap(pooling_func, files), total = len(files)))
pool.close()
pool.join()
if __name__ == '__main__':
import multiprocessing
multiprocessing.freeze_support()
process_files()
我希望在所有GPU上同时运行inference_{gpu_id}.py文件。
目前,我可以成功地运行在一个GPU,6个核心,但当我试图运行它在所有GPU一起,只有GPU 0运行,所有其他停止提供下面的错误信息。
RuntimeError: CUDA error: invalid device ordinal.
我正在运行的脚本:
CUDA_VISIBLE_DEVICES=0 inference_0.py
CUDA_VISIBLE_DEVICES=1 inference_1.py
...
CUDA_VISIBLE_DEVICES=7 inference_7.py
发布于 2021-07-25 14:18:13
以下是对question you asked的回答,但后来删除了。
考虑到这一点,如果您没有使用CUDA_VISIBLE_DEVICES
标志,那么所有GPU都将对您的PyTorch进程可用。这意味着torch.cuda.device_count
将返回8(假设版本设置是有效的)。您可以通过torch.device
通过torch.device('cuda:0')
、torch.device('cuda:1')
、.和torch.device('cuda:8')
访问这8个GPU中的每一个。
现在,如果您只打算使用一个,并且希望将您的流程限制在一个。然后CUDA_VISIBLE_DEVICES=i
(其中i
是设备序号)将使其成为序号。在这种情况下,torch.cuda
将只能通过torch.device('cuda:0')
访问单个设备。不管实际的设备序号是什么,您通过torch.device('cuda:0')
访问它的方式都无关紧要。
如果允许访问多个设备:假设n°0、n°4和n°2,那么您将使用CUDA_VISIBLE_DEVICES=0,4,2
。因此,您通过d0 = torch.device('cuda:0')
、d1 = torch.device('cuda:1')
和d2 = torch.device('cuda:2')
引用您的cuda设备。与您用标志定义它们的顺序相同,即
d0
-> GPU n°0,d1
-> GPU n°4,d2
-> GPU n°2。
这使得您可以使用相同的代码并在不同的GPU上运行它,而不必更改您引用设备序号的底层代码。
总之,您需要查看的是运行代码所需的设备数量。在您的例子中:1
就足够了。您将使用torch.device('cuda:0')
引用它。但是,在运行代码时,需要指定cuda:0
设备是什么,并使用以下标志:
> CUDA_VISIBLE_DEVICES=0 inference.py
> CUDA_VISIBLE_DEVICES=1 inference.py
...
> CUDA_VISIBLE_DEVICES=7 inference.py
注意,'cuda'
默认为'cuda:0'
。
https://stackoverflow.com/questions/68518683
复制相似问题