Python 如何在 Google Colab GPU 中安装 CUDA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50560395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:32:31  来源:igfitidea点击:

How to install CUDA in Google Colab GPU's

pythonmachine-learningcudagoogle-colaboratoryturi-create

提问by namerbenz

It seems that Google Colab GPU's doesn't come with CUDA Toolkit, how can I install CUDA in Google Colab GPU's. I am getting this error in installing mxnet in Google Colab.

似乎 Google Colab GPU 没有附带 CUDA Toolkit,我如何在 Google Colab GPU 中安装 CUDA。在 Google Colab 中安装 mxnet 时出现此错误。

Installing collected packages: mxnet
Successfully installed mxnet-1.2.0

ERROR: Incomplete installation for leveraging GPUs for computations. Please make sure you have CUDA installed and run the following line in your terminal and try again:

错误:利用 GPU 进行计算的安装不完整。请确保您已安装 CUDA 并在终端中运行以下行并重试:

pip uninstall -y mxnet && pip install mxnet-cu90==1.1.0

Adjust 'cu90' depending on your CUDA version ('cu75' and 'cu80' are also available). You can also disable GPU usage altogether by invoking turicreate.config.set_num_gpus(0). An exception has occurred, use %tb to see the full traceback.

根据您的 CUDA 版本调整“cu90”(“cu75”和“cu80”也可用)。您还可以通过调用 turicreate.config.set_num_gpus(0) 来完全禁用 GPU 使用。发生异常,请使用 %tb 查看完整的回溯。

SystemExit: 1

回答by Anwarvic

I pretty much believe that Google Colab has Cuda pre-installed... You can make sure by opening a new notebook and type !nvcc --versionwhich would return the installed Cuda version.

我几乎相信谷歌 Colab 已经预装了 Cuda……您可以通过打开一个新的笔记本并输入!nvcc --version将返回已安装的 Cuda 版本来确定。

Here is mine: enter image description here

这是我的: 在此处输入图片说明

回答by Dienow

  1. Go here: https://developer.nvidia.com/cuda-downloads
  2. Select Linux -> x86_64 -> Ubuntu -> 16.04 -> deb (local)
  3. Copy link from the download button.
  4. Now you have to compose the sequence of commands. First one will be the call to wget that will download CUDA installer from the link you saved on step 3
  5. There will be installation instruction under "Base installer" section. Copy them as well, but remove sudofrom all the lines.
  6. Preface each line with commands with !, insert into a cell and run
  7. For me the command sequence was the following:
    !wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb !dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb !apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub !apt-get update !apt-get install cuda
  8. Now finally install mxnet. As cuda version I installed above is 9.2 I had to slighly change your command: !pip install mxnet-cu92
  9. Successfully installed graphviz-0.8.3 mxnet-cu92-1.2.0
  1. 去这里:https: //developer.nvidia.com/cuda-downloads
  2. 选择 Linux -> x86_64 -> Ubuntu -> 16.04 -> deb(本地)
  3. 从下载按钮复制链接。
  4. 现在您必须编写命令序列。第一个是调用 wget,它将从您在步骤 3 中保存的链接下载 CUDA 安装程序
  5. “基本安装程序”部分将有安装说明。也复制它们,但从sudo所有行中删除。
  6. 在每行前面加上命令!,插入一个单元格并运行
  7. 对我来说,命令序列如下:
    !wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb !dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb !apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub !apt-get update !apt-get install cuda
  8. 现在终于安装mxnet。由于我上面安装的 cuda 版本是 9.2,我不得不稍微更改您的命令:!pip install mxnet-cu92
  9. Successfully installed graphviz-0.8.3 mxnet-cu92-1.2.0

回答by Ahwar

Cuda is not showing on your notebook because you have not enabled GPU in Colab.

Cuda 未显示在您的笔记本上,因为您尚未在 Colab 中启用 GPU。

The Google Colab comes with both options GPU or without GPU. You can enable or disable GPU in runtime settings

Google Colab 带有 GPU 或不带 GPU 两种选项。您可以在运行时设置中启用或禁用 GPU

Go to Menu > Runtime > Change runtime.

Change hardware acceleration to GPU.

将硬件加速更改为 GPU。

GPU Settings Screenshot

GPU 设置截图

To check if GPU is running or not, run following command

要检查 GPU 是否正在运行,请运行以下命令

!nvidia-smi

If output is like following image it means your GPU and cuda is working. You can see cuda version also.cuda confirmation screenshot

如果输出如下图所示,则表示您的 GPU 和 cuda 正在工作。您也可以看到 cuda 版本。cuda确认截图

After that to check if PyTorch is capable of using GPU, run the following code.

之后要检查 PyTorch 是否能够使用 GPU,请运行以下代码。

import torch
torch.cuda.is_available()
# Output would be True if Pytorch is using GPU otherwise it would be False.

To check if TensorFlow is capable of using GPU, run the following code.

要检查 TensorFlow 是否能够使用 GPU,请运行以下代码。

import tensorflow as tf
tf.test.gpu_device_name()
# Standard output is '/device:GPU:0'

回答by Jakub Bartczuk

If you switch to using GPU then CUDA will be available on your VM. Basically what you need to do is to match MXNet's version with installed CUDA version.

如果您改用 GPU,那么 CUDA 将在您的 VM 上可用。基本上你需要做的是将 MXNet 的版本与安装的 CUDA 版本相匹配。

Here's what I used to install MXNet on Colab:

这是我用来在 Colab 上安装 MXNet 的内容:

First check the CUDA version

首先检查CUDA版本

!cat /usr/local/lib/python3.6/dist-packages/external/local_config_cuda/cuda/cuda/cuda_config.h |\
grep TF_CUDA_VERSION

For me it outputted #define TF_CUDA_VERSION "8.0"

对我来说它输出 #define TF_CUDA_VERSION "8.0"

Then I installed MXNet with

然后我安装了 MXNet

!pip install mxnet-cu80

回答by Bright Chang

I think the easiest way here is to install mxnet-cu80. Just use the following code:

我认为这里最简单的方法是安装 mxnet-cu80。只需使用以下代码:

!pip install mxnet-cu80
import mxnet as mx

And you could check whether it works by:

您可以通过以下方式检查它是否有效:

a = mx.nd.ones((2, 3), mx.gpu())
b = a * 2 + 1
b.asnumpy()

I think colab right now just support cu80 and higher versions won't work.

我认为 colab 现在只支持 cu80 和更高版本将不起作用。

For more information, you could see the following two websites:

有关更多信息,您可以查看以下两个网站:

Google Colab Free GPU Tutorial

Google Colab 免费 GPU 教程

Installing mxnet

安装 mxnet

Happy Coding :D

快乐编码:D

回答by Niko

To run in Colab, you need CUDA 8 (mxnet 1.1.0 for cuda 9+ is broken). But Google Colab runs now 9.2. There is, however the way to uninstall 9.2, install 8.0 and then install mxnet 1.1.0 cu80.

要在 Colab 中运行,您需要 CUDA 8(用于 cuda 9+ 的 mxnet 1.1.0 已损坏)。但是 Google Colab 现在运行的是 9.2。但是,有一种方法是卸载 9.2,安装 8.0,然后安装 mxnet 1.1.0 cu80。

The complete jupyter code is here : Medium

完整的 jupyter 代码在这里:Medium

回答by Furkan

There is a guidewhich clearly explains that how to enable Cuda in Colab.

有一个指南清楚地解释了如何在 Colab 中启用 Cuda。