【现象描述】
在GPU环境上安装MindSpore,运行后报错提示设卡失败,如"SetDevice for id: 7 failed......"这类错误。
【原因分析】
此类错误一般原因:设卡的device id越界,超出了可用GPU卡总数。
【解决方法】
设卡失败后,MindSpore会有ERROR日志提示:
"SetDevice for id: index failed,... Please make sure that the 'device_id' set in context is in the range:[0, total number of GPU). If the environment variable 'CUDA_VISIBLE_DEVICES' is set, the total number of GPU will be the number set in the environment variable 'CUDA_VISIBLE_DEVICES'. For example, if export CUDA_VISIBLE_DEVICES=4,5,6, the 'device_id' can be 0,1,2 at the moment, 'device_id' starts from 0, and 'device_id'=0 means using GPU of number 4.
日志中给出了设卡样例,提示MindSpore需要的逻辑device id和物理卡号的对应关系。
- 如果是执行单卡脚本遇到设卡失败,请确保 context中设置的device_id 小于环境中GPU卡的总数(如果export了 CUDA_VISIBLE_DEVICES环境变量,则GPU卡的总数修正为CUDA_VISIBLE_DEVICES变量中设置的GPU卡的总数),例如, export CUDA_VISIBLE_DEVICES=4,5,6,则可用的卡为3张,分别对应物理环境中的4号5号和6号卡,如果在context中设置了device_id,则需要确保其小于3,可以取值0,1,2,如果设置为0,则表示使用4号GPU卡。
- 如果是执行多卡训练遇到设卡失败,请确保脚本期望执行的分布式并行数不超过物理环境中GPU卡的总数(如果export了 CUDA_VISIBLE_DEVICES环境变量,则需要保证环境变量中设置的GPU卡数不少于分布式并行数),例如执行单机8卡训练,则需要确保物理环境中至少有8张卡(如果export 了CUDA_VISIBLE_DEVICES环境变量,则需要保证CUDA_VISIBLE_DEVICES中设置的卡数不少于8张,例如, 可以export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 )。