RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemm( handle)` with GPU only

RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemm( handle)` with GPU only

我正在研究具有一维信号的 CNN。它在 CPU 设备上工作得很好。但是,当我在 GPU 中训练模型时,出现了 CUDA 错误。我在调用 cublasCreate(handle) 时得到 RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED 后设置了 os.environ['CUDA_LAUNCH_BLOCKING'] = "1" 命令。通过这样做,发生了 cublasSgemm 错误而不是 cublasCreate 错误。 尽管 nvidia 文档怀疑硬件问题,但我可以毫无错误地使用图像训练其他 CNN。下面是我在训练模型中加载数据和设置数据的代码。

    idx = np.arange(len(dataset))  # dataset & label shuffle in once
    np.random.shuffle(idx)

    dataset = dataset[idx]
    sdnn = np.array(sdnn)[idx.astype(int)]        

    train_data, val_data = dataset[:int(0.8 * len(dataset))], dataset[int(0.8 * len(dataset)):]
    train_label, val_label = sdnn[:int(0.8 * len(sdnn))], sdnn[int(0.8 * len(sdnn)):]
    train_set = DataLoader(dataset=train_data, batch_size=opt.batch_size, num_workers=opt.workers)

    for i, data in enumerate(train_set, 0):  # data.shape = [batch_size, 3000(len(signal)), 1(channel)] tensor

        x = data.transpose(1, 2)
        label = torch.Tensor(train_label[i * opt.batch_size:i * opt.batch_size + opt.batch_size])
        x = x.to(device, non_blocking=True)
        label = label.to(device, non_blocking=True) # [batch size]
        label = label.view([len(label), 1])
        optim.zero_grad()

        # Feature of signal extract
        y_predict = model(x) # [batch size, fc3 output] # Error occurred HERE
        loss = mse(y_predict, label)

以下是此代码的错误消息。

File C:/Users/Me/Desktop/Me/Study/Project/Analysis/Regression/main.py", line 217, in Processing
    y_predict = model(x) # [batch size, fc3 output]
  File "C:\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Users\ME\Desktop\ME\Study\Project\Analysis\Regression\cnn.py", line 104, in forward
    x = self.fc1(x)
  File "C:\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\linear.py", line 91, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:\Anaconda\envs\torch\lib\site-packages\torch\nn\functional.py", line 1674, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemm( handle, opa, opb, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)`

几周来我一直试图解决这个错误,但找不到解决方案。如果您在这里看到任何错误,请告诉我。

通过部分关键字搜索,我终于得到了类似的情况。 因为稳定性,我用的是CUDA 10.2版本。参考要求将 CUDA 工具包升级到更高版本 - 在我的例子中是 11.2 - 问题解决了! 我已经处理过其他培训过程,但这一个只会导致错误。由于各种原因出现CUDA错误,改版本也算解决。

请注意,如果输入张量的维度与 nn.Linear 模块的维度不匹配,也可能导致此问题。 (ex. input.shape = (a, b) and nn.Linear(c, c, bias=False) 与 c 不匹配)。

说得对,我认为形状不匹配是引发此错误的主要原因。

我在训练图像识别模型时也遇到了这个错误,其中 - 最终 Conv2d 的输出和前 Linear 层的输入的形状不同。

If none of that works, then the best thing to do is to run a smaller version of the process on CPU and recreate the error. When running it on CPU instead of CUDA, you will get a more useful traceback that can solve your error.

答案(上面引用)中解释的一种补救措施是,禁用 gpu 尝试通过在 cpu 上执行代码(不更改任何行)来重新创建类似的情况,它应该给出更好和可以理解的错误。

P.S.: 尽管最初的问题表明他们的代码在 cpu 上执行良好,但我已经为有问题的人发布了这个答案类似的错误,而不是由于 Cuda 版本不匹配。