Categories
Python PyTorch

How to Install PyTorch with CUDA 9.2

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?

Prerequisite

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

  1. First check whether CUDA 9.2 has been installed

    cat /usr/local/cuda/version.txt

  2. [For pip] Run pip install with 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 cu92/torch- in https://download.pytorch.org/whl/torch_stable.html).

  3. [For conda] Run conda install with cudatoolkit

    conda install pytorch torchvision cudatoolkit=9.2 -c pytorch

  4. Check whether PyTorch is installed

    Open Python and run

    import torch
    x = torch.rand(5, 3)
    print(x)

  5. Verify if CUDA 9.2 is available in PyTorch

    import torch
    torch.cuda.is_available()

PyTorch versions supported for CUDA 9.2

The following PyTorch version is supported by PyTorch for CUDA 9.2.

  • 1.2.0
  • 1.3.0
  • 1.3.1
  • 1.4.0
  • 1.5.0
  • 1.5.1
  • 1.6.0
  • 1.7.0

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.

  1. PyTorch is production-ready: TorchScript smoothly toggles between eager and graph modes. TorchServe speeds up the production process.
  2. PyTorch support distributed training: The torch.collaborative interface allows for efficient distributed training and performance optimization in research and development.
  3. PyTorch has a robust ecosystem: It has an expansive ecosystem of tools and libraries to support applications such as computer vision and NLP.
  4. 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.

You can learn more about CUDA in CUDA zone and download it here: https://developer.nvidia.com/cuda-downloads.

Reference: https://pytorch.org/get-started/locally/

+2

By VarHowto Editor

Welcome to VarHowto!