pytorch 将Tensor移动到GPU时CUDA中的内核错误

6rqinv9w  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(692)

我试图复制这个code,但我把Tensor移到了GPU上,当我在CPU上运行代码时,它运行得很好,但当我切换到GPU时,我得到了这个错误:

RuntimeError: CUDA error: initialization error
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.

我的代码是:

#!/usr/bin/python3

import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np

if torch.cuda.is_available():
    device_ = torch.device("cuda")
    print("========================\nYou are running on GPU!\n========================")
else:
    device_ = torch.device("cpu")
    print("------------------------\nYou are running on CPU!\n------------------------")

class WineDataset(Dataset):
    
    def __init__(self):
        # data loading
        xy = np.loadtxt("./dataset/wine.csv", dtype=np.float32, delimiter=",",  skiprows=1)
        self.n_samples = xy.shape[0]

        self.x = torch.from_numpy(xy[:, 1:])
        self.x = self.x.to(device_)

        self.y = torch.from_numpy(xy[:, [0]]) # n_samples, 1
        self.y = self.y.to(device_)

        print(self.x.shape, self.y.shape)
        
    def __getitem__(self, index):
        # dataset[0]
        return self.x[index], self.y[index]

    def __len__(self):
        # len(dataset)
        return self.n_samples

dataset = WineDataset()
train_loader = DataLoader(dataset=dataset, batch_size=4, shuffle=True, num_workers=2)

dataiter = iter(train_loader)
data = next(dataiter)
features, labels = data

我创建了this Colab Notebook来使我的问题可重现,你能告诉我如何在GPU上运行我的Tensor代码吗?谢谢。

6tqwzwtp

6tqwzwtp1#

引用自Pytorch论坛:RuntimeError: CUDA error: initialization error“可能会引发,如果您尝试多次初始化CUDA上下文,例如:例如,如果您正在使用多个进程”
https://discuss.pytorch.org/t/what-does-runtimeerror-cuda-driver-error-initialization-error-mean/87505
因此,您需要将num_workers更改为0
train_loader = DataLoader(dataset=dataset, batch_size=4, shuffle=True, num_workers=0)

相关问题