PyTorch is a popular Deep Learning framework and installs with the latest CUDA by default. If you haven’t upgrade NVIDIA driver or you cannot upgrade CUDA because you don’t have root access, you may need to settle down with an outdated version like CUDA 10.0. However, that means you cannot use GPU in your PyTorch models by default. How can I fix it?
This tutorial assumes you have CUDA 10.0 installed and you can run
python and a package manager like
conda. Miniconda and Anaconda are both fine. We wrote an article on how to install Miniconda.
5 Steps to Install PyTorch With CUDA 10.0
- Check if CUDA 10.0 is installed
- [For pip] Run
pip installwith specified version and
pip install torch==1.4.0 torchvision==0.5.0 -f https://download.pytorch.org/whl/cu100/torch_stable.html
Note: PyTorch only supports CUDA 10.0 up to 1.4.0. (Search
- [For conda] Run
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
- Verify PyTorch is installed
x = torch.rand(5, 3)
- Verify PyTorch is using CUDA 10.0
Verify PyTorch is installed
To insure that PyTorch has been set up properly, we will validate the installation by running a sample PyTorch script. Here we are going to create a randomly initialized tensor.
import torch print(torch.rand(5, 3))
The following output will be printed. Yours will be similar.
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
Verify if CUDA is available to PyTorch
To test whether your GPU driver and CUDA are available and accessible by PyTorch, run the following Python code to determine whether or not the CUDA driver is enabled:
import torch torch.cuda.is_available()
In case for people who are interested, the following 2 sections introduces PyTorch and CUDA.
What is PyTorch?
PyTorch is an open-source Deep Learning platform that is scalable and versatile for testing, reliable and supportive for deployment. It allows for quick, modular experimentation via an autograding component designed for fast and python-like execution. With the introduction of PyTorch 1.0, the framework now has graph-based execution, a hybrid front-end that allows for smooth mode switching, collaborative testing, and effective and secure deployment on mobile platforms.
PyTorch has 4 key features according to its homepage.
- PyTorch is production-ready: TorchScript smoothly toggles between eager and graph modes. TorchServe speeds up the production process.
- PyTorch support distributed training: The torch.collaborative interface allows for efficient distributed training and performance optimization in research and development.
- PyTorch has a robust ecosystem: It has an expansive ecosystem of tools and libraries to support applications such as computer vision and NLP.
- PyTorch has native cloud support: It is well recognized for its zero-friction development and fast scaling on key cloud providers.
What is CUDA?
CUDA is a general parallel computation architecture and programming model developed for NVIDIA graphical processing units (GPUs). Using CUDA, developers can significantly improve the speed of their computer programs by utilizing GPU resources.
In GPU-accelerated code, the sequential part of the task runs on the CPU for optimized single-threaded performance, the compute-intensive section, such as PyTorch code, runs on thousands of GPU cores in parallel through CUDA. Developers can code in common languages such as C, C++, Python while using CUDA, and implement parallelism via extensions in the form of a few simple keywords.
NVIDIA’s CUDA Toolkit includes everything you need to build GPU-accelerated software, including GPU-accelerated modules, a parser, programming resources, and the CUDA runtime.