【问题标题】:How to install CUDA in Google Colab - Cannot initialize CUDA without ATen_cuda library如何在 Google Colab 中安装 CUDA - 没有 ATen_cuda 库无法初始化 CUDA
【发布时间】:2019-05-25 13:31:08
【问题描述】:

我正在尝试在 Goolge Colab 中使用 cuda,但在运行我的程序时出现以下错误。

RuntimeError:没有 ATen_cuda 库无法初始化 CUDA。 PyTorch 将其后端拆分为两个共享库:一个 CPU 库和一个 CUDA 库;发生此错误是因为您尝试使用某些 CUDA 功能,但由于某种原因动态链接器尚未加载 CUDA 库。必须加载 CUDA 库,即使您不直接使用 CUDA 库中的任何符号!一个常见的罪魁祸首是链接参数中缺少 -Wl,--no-as-needed;如果您不依赖任何符号,许多动态链接器将删除动态库依赖项。您可以通过在二进制文件上使用 ldd 来检查是否发生了这种情况,以查看是否存在对 *_cuda.so 库的依赖。

我安装了以下库。

from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/'
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-

{platform}-linux_x86_64.whl torchvision
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import matplotlib.pyplot as plt
import time
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
!pip install Pillow==5.3.0
# import the new one
import PIL

我正在尝试运行以下代码。

for device in ['cpu', 'cuda']:

    criterion = nn.NLLLoss()
    # Only train the classifier parameters, feature parameters are frozen
    optimizer = optim.Adam(model.classifier.parameters(), lr=0.001)

    model.to(device)

    for ii, (inputs, labels) in enumerate(trainloader):

        # Move input and label tensors to the GPU
        inputs, labels = inputs.to(device), labels.to(device)

        start = time.time()

        outputs = model.forward(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        if ii==3:
            break

    print(f"Device = {device}; Time per batch: {(time.time() - start)/3:.3f} seconds")

【问题讨论】:

    标签: python runtime-error pytorch google-colaboratory


    【解决方案1】:

    您是否选择了运行时作为 GPU? 检查运行时> 更改运行时类型> 选择硬件加速器作为 GPU

    【讨论】:

      【解决方案2】:

      您是否尝试过以下方法?

      Go to Menu > Runtime > Change runtime.

      Change hardware acceleration to GPU.

      How to install CUDA in Google Colab GPU's

      【讨论】:

        猜你喜欢
        • 2018-11-06
        • 2018-11-06
        • 2020-05-30
        • 1970-01-01
        • 2022-12-21
        • 2013-09-02
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多