Install PyTorch for ROCm

Install PyTorch for ROCm#

Refer to this section for the recommended PyTorch via PIP installation method, as well as Docker-based installation.

PCIe atomics

ROCm is an extension of HSA platform architecture, and shares queuing model, memory model, signaling and synchronization protocols.

Platform atomics are integral to perform queuing and signaling memory operations, where there may be multiple-writers across CPU and GPU agents.

For more details, see How ROCm uses PCIe atomics.

Install methods#

AMD recommends the PIP install method to create a PyTorch environment when working with ROCm™ for machine learning development.

Using Docker provides portability, and access to a prebuilt Docker container that has been rigorously tested within AMD. Docker also cuts down compilation time, and should perform as expected without installation issues.

AMD recommends the PIP install method to create a PyTorch environment when working with ROCm™ for machine learning development.

Check Pytorch.org for latest PIP install instructions and availability. See Compatibility matrices for support information.

Note
The latest version of Python module numpy v2.0 is incompatible with the torch wheels for this version. Downgrade to an older version is required.
Example: pip3 install numpy==1.26.4

Install PyTorch via PIP

  1. Enter the following command to unpack and begin set up.

    sudo apt install python3-pip -y
    
  2. Enter this command to update the pip wheel.

    pip3 install --upgrade pip wheel
    
  3. Enter this command to install Torch and Torchvision for ROCm AMD GPU support.

    wget https://repo.radeon.com/rocm/manylinux/rocm-rel-6.2.3/torch-2.3.0%2Brocm6.2.3-cp310-cp310-linux_x86_64.whl
    wget https://repo.radeon.com/rocm/manylinux/rocm-rel-6.2.3/torchvision-0.18.0%2Brocm6.2.3-cp310-cp310-linux_x86_64.whl
    wget https://repo.radeon.com/rocm/manylinux/rocm-rel-6.2.3/pytorch_triton_rocm-2.3.0%2Brocm6.2.3.5a02332983-cp310-cp310-linux_x86_64.whl
    pip3 uninstall torch torchvision pytorch-triton-rocm
    pip3 install torch-2.3.0+rocm6.2.3-cp310-cp310-linux_x86_64.whl torchvision-0.18.0+rocm6.2.3-cp310-cp310-linux_x86_64.whl pytorch_triton_rocm-2.3.0+rocm6.2.3.5a02332983-cp310-cp310-linux_x86_64.whl
    

    This may take several minutes.

    Important! AMD recommends proceeding with ROCm WHLs available at repo.radeon.com. The ROCm WHLs available at PyTorch.org are not tested extensively by AMD as the WHLs change regularly when the nightly builds are updated.

    Important! These specific ROCm WHLs are built for Python 3.10, and will not work on other versions of Python.

  4. Update to WSL compatible runtime lib.

    location=`pip show torch | grep Location | awk -F ": " '{print $2}'`
    cd ${location}/torch/lib/
    rm libhsa-runtime64.so*
    cp /opt/rocm/lib/libhsa-runtime64.so.1.2 libhsa-runtime64.so
    
  5. Optional step: Using a Conda environment.

    Note
    This is an optional step for users who wish to proceed with a Conda environment.
    AMD does not officially support and validate Conda usecases.

    The libhsa-runtime64.so requires installation of GCC 12.1 at minimum.
    When using a Conda environment, ImportError: version 'GLIBCXX_3.4.30' not found is likely to occur.
    Upgrade GCC for Conda using the following command.

    conda install -c conda-forge gcc=12.1.0
    

Next, verify your PyTorch installation.

Using Docker provides portability, and access to a prebuilt Docker container that has been rigorously tested within AMD. Docker also cuts down compilation time, and should perform as expected without installation issues.

Prerequisites to install PyTorch using Docker

Docker for Ubuntu® must be installed.

To install Docker for Ubuntu, enter the following command:

sudo apt install docker.io

Use Docker image with pre-installed PyTorch

Follow these steps for installing using a Docker image.

  1. Enter the following command to pull the public PyTorch Docker image.

    sudo docker pull rocm/pytorch:rocm6.1.3_ubuntu22.04_py3.10_pytorch_release-2.1.2
    

    Optional: You can also download a specific and supported configuration with different user-space ROCm versions, PyTorch versions, and supported operating systems.

    Refer to hub.docker.com/r/rocm/pytorch to download the PyTorch Docker image.

  2. Start a Docker container using the downloaded image.

    sudo docker run -it \
    --cap-add=SYS_PTRACE  \
    --security-opt seccomp=unconfined \
    --ipc=host \
    --shm-size 8G \
    --device=/dev/dxg -v /usr/lib/wsl/lib/libdxcore.so:/usr/lib/libdxcore.so -v /opt/rocm/lib/libhsa-runtime64.so.1:/opt/rocm/lib/libhsa-runtime64.so.1  \
    rocm/pytorch:rocm6.1.3_ubuntu22.04_py3.10_pytorch_release-2.1.2
    

    This will automatically download the image if it does not exist on the host. You can also pass the -v argument to mount any data directories from the host onto the container.

Next, verify the PyTorch installation.

See PyTorch Installation for ROCm for more information.

Verify PyTorch installation#

Confirm if PyTorch is correctly installed.

  1. Verify if Pytorch is installed and detecting the GPU compute device.

    python3 -c 'import torch' 2> /dev/null && echo 'Success' || echo 'Failure'
    

    Expected result:

    Success
    
  2. Enter command to test if the GPU is available.

    python3 -c 'import torch; print(torch.cuda.is_available())'
    

    Expected result:

    True
    
  3. Enter command to display installed GPU device name.

    python3 -c "import torch; print(f'device name [0]:', torch.cuda.get_device_name(0))"
    

    Expected result: Example: device name [0]: Radeon RX 7900 XTX

    device name [0]: <Supported AMD GPU>
    
  4. Enter command to display component information within the current PyTorch environment.

    python3 -m torch.utils.collect_env
    

    Expected result:

    PyTorch version
     
    ROCM used to build PyTorch
     
    OS
     
    Is CUDA available
     
    GPU model and configuration
     
    HIP runtime version
     
    MIOpen runtime version
    

Environment set-up is complete, and the system is ready for use with PyTorch to work with machine learning models, and algorithms.