赞
踩
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”了。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。