【发布时间】:2021-07-25 12:47:53
【问题描述】:
我有 8 个 GPU,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 文件
目前,我能够在一个 6 核的 GPU 上成功运行它,但是当我尝试在所有 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
【问题讨论】:
-
你试过
os.environ["CUDA_VISIBLE_DEVICES"]=str(gpu_id)吗? -
只需使用
device='cuda' -
@natthaphon 成功了,谢谢 :)
-
@Ivan 我删除了另一个问题,因为这个问题有更多细节
-
是的,我明白,很抱歉。感谢您的详细回答:)
标签: python pytorch python-multiprocessing multiprocess cpu-cores