PyTorch is a well established Deep Learning framework that supports the newest CUDA by default but what if you want to use PyTorch with CUDA 9.2? Whether you have not updated NVIDIA driver or are unable to update CUDA due to lack of root access, an outdated version like CUDA 9.2 would force you to settle down. This means that the PyTorch scripts can not be used for GPU by default. How can I fix those problems?
This guide assumes you have CUDA 9.2 enabled, so you can run python and a package manager like pip or conda. Miniconda and Anaconda are both fine but Miniconda is lightweight. We wrote an article about how to install Miniconda.
In A Nutshell
- First check whether CUDA 9.2 has been installed
- [For pip] Run
pip installwith specified version and
-f. Here we install the latest PyTorch 1.7.
pip install torch==1.7.0+cu92 torchvision==0.8.1+cu92 -f https://download.pytorch.org/whl/torch_stable.html
Note: PyTorch supports CUDA 9.2 up to the latest 1.7.0 (Search
- [For conda] Run
conda install pytorch torchvision cudatoolkit=9.2 -c pytorch
- Check whether PyTorch is installed
x = torch.rand(5, 3)
- Verify if CUDA 9.2 is available in PyTorch
PyTorch versions supported for CUDA 9.2
The following PyTorch version is supported by PyTorch for CUDA 9.2.
To install an older version, replace the version in step 2 with the version you would like to install.
Verify PyTorch is installed
We can check the installation by running a sample PyTorch script to make sure PyTorch is set up correctly. Here we create a tensor, which is initialized randomly.
import torch print(torch.rand(5, 3))
The output is printed below. Yours will be similar in some way, except for the numbers.
tensor([[0.5055, 0.4535, 0.8250], [0.7789, 0.9305, 0.2666], [0.3510, 0.6764, 0.6274], [0.4363, 0.5887, 0.6882], [0.5301, 0.0680, 0.5824]])
Verify that PyTorch has CUDA support
To test whether PyTorch can access both the GPU driver and CUDA, use the following Python code to decide whether or not the CUDA driver is enabled. This will return True.
import torch torch.cuda.is_available()
The following two parts refer the people interested to PyTorch and CUDA.
What is PyTorch?
PyTorch is an open-source Deep Learning platform that is scalable and flexible for testing, robust and supportive for deployment. It enables quick, modular experimentation via an autograding component designed for fast and python-like execution. The framework now has graph-based execution with the release of PyTorch 1.0, a hybrid front-end that allows for seamless switching of configuration, collective testing, and effective and secure delivery on mobile devices.
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 and programming paradigm built for graphics processing units ( GPUs) of NVIDIA. With CUDA, by using GPU resources, developers can dramatically increase the performance of their computer programs.
In GPU-accelerated program, the sequential portion of the function runs on the CPU for optimized single-threaded performance, while the compute-intensive component, such as PyTorch code, runs parallel via CUDA at thousands of GPU cores. When using CUDA, developers can code in popular languages such as C, C++ , Python and enforce parallelism with extensions in the form of a few simple keywords.
NVIDIA’s CUDA Toolkit provides everything you need to develop GPU accelerated applications, including GPU accelerated modules, a parser, programming resources, and the CUDA runtime.