1. 现象:使用os.environ[‘CUDA_VISIBLE_DEVICES’] 指定了GPU,但是模型还是只能加载在‘0’卡上。
2.原因:os.environ[‘CUDA_VISIBLE_DEVICES’] 必须在import torch之前
3.隐藏的坑:
如果import进来的其他文件中import了torch,os.environ[‘CUDA_VISIBLE_DEVICES’] 也无法生效,因为执行.py文件时会优先import其他包中的torch。
eg:
file 1:
# MyTest.py import torch print("this is MyTest.py")
file 2:
# test.py import Mytest os.environ['CUDA_VISIBLE_DEVICES'] = '1' #此时,该文件虽然没有“import torch”, 但os.environ['CUDA_VISIBLE_DEVICES'] 依然无效 #因为import MyTest时已经“import torch”了。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221267.html原文链接:https://javaforall.net
