torch.nn.DataParallel使用細節


僅使用nn.DataParallel,gpu0和gpu1、gpu0和gpu2、gpu0和gpu3等包含gpu0的組合都是可以的,其余組合不行,報錯RuntimeError: module must have its parameters and buffers on device cuda:1 (device_ids[0]) but found one of them on device: cuda:0,因為tensor是默認存在gpu0上的。

解決方法:os.environ["CUDA_VISIBLE_DEVICES"]的使用。

若想使用gpu1和gpu2,程序開頭多加一條語句 os.environ["CUDA_VISIBLE_DEVICES"] = "1,2",作用是只允許gpu1和gpu2可用,然后使用Model = nn.DataParallel(Model, device_ids=[0,1]),作用是從可用的兩個gpu中搜索第0和第1個位置的gpu。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM