设备序号无效,CUDA / TORCH

时间:2018-06-10 12:49:18

标签: python gpu pytorch

我在ubuntu 16.04中运行脚本时收到此错误。请耐心等待,我是python的新手, 我已经检查了互联网上已有的选项,但我无法修复它。

 RuntimeError: cuda runtime error (10) : invalid device ordinal at torch/csrc/cuda/Module.cpp:32

我目前正在运行此文件。

from __future__ import print_function
from models import LipRead
import torch
import toml
from training import Trainer
from validation import Validator

print("Loading options...")
with open('options.toml', 'r') as optionsFile:
options = toml.loads(optionsFile.read())

if(options["general"]["usecudnnbenchmark"] and options["general"]    ["usecudnn"]):
print("Running cudnn benchmark...")
torch.backends.cudnn.benchmark = True

#Create the model.
model = LipRead(options)

if(options["general"]["loadpretrainedmodel"]):
model.load_state_dict(torch.load(options["general"]    ["pretrainedmodelpath"]))

#Move the model to the GPU.
if(options["general"]["usecudnn"]):
model = model.cuda(options["general"]["gpuid"])

trainer = Trainer(options)
validator = Validator(options)

for epoch in range(options["training"]["startepoch"], options["training"]["epochs"]):

if(options["training"]["train"]):
    trainer.epoch(model, epoch)

if(options["validation"]["validate"]):
    validator.epoch(model)

我怀疑这个文件与弹出的错误

有关
Title = "TOML Example"

[general]
usecudnn = true
usecudnnbenchmark = true
gpuid = 0
loadpretrainedmodel = true
pretrainedmodelpath = "trainedmodel.pt"
savemodel = true
modelsavepath = "savedmodel.pt"

[input]
batchsize = 18
numworkers = 18
shuffle = true

[model]
type = "LSTM"
inputdim = 256 
hiddendim = 256
numclasses = 500
numlstms = 2

[training]
train = true
epochs = 15
startepoch = 10
statsfrequency = 1000
dataset = "/udisk/pszts-ssd/AV-ASR-data/BBC_Oxford/lipread_mp4"
learningrate = 0.003
momentum = 0.9
weightdecay = 0.0001

[validation]
validate = true
dataset = "/udisk/pszts-ssd/AV-ASR-data/BBC_Oxford/lipread_mp4"
saveaccuracy = true
accuracyfilelocation = "accuracy.txt"

错误主要在gpuid行中,因为我终于达到了。

3 个答案:

答案 0 :(得分:2)

尝试这样做

import torch
print(torch.cuda.is_available())

如果输出为False,则意味着PyTorch未检测到GPU。 我有同样的问题,并重新安装Pytorch为我工作。 您可能还想查看此https://github.com/pytorch/pytorch/issues/6098

答案 1 :(得分:0)

如果在不同数量的Cuda设备上训练了预训练模型,则可能会收到该错误。例如,在训练模型时,您使用了3台Cuda设备,现在您将同一训练过的模型加载到只有一台Cuda设备的设备上。

答案 2 :(得分:0)

预训练的权重可能会映射到其他gpuid。

#WAS
model.load_state_dict(torch.load(final_model_file, map_location={'cuda:0':'cuda:1'}))
#IS
model.load_state_dict(torch.load(final_model_file, map_location={'cuda:0':'cuda:0'}))