A semi-curated blog of computer graphics and rendering.
Installing PyTorch Geometric w.r.t. CUDA Version

I’ve been fiddling with PyTorch Geometric installation lately, and I believe we all know that although being an awesome library, sometimes it can be notoriously hard to get it to work in the first place. Especially if your CUDA version is way too low. That can be very, very problematic. So, let’s assume we have an unlisted CUDA version (11.1 in my case,) and let’s try to make it work!

Installing PyTorch

So the very first thing is to install PyTorch. And to install PyTorch (the GPU-enabled version, obviously,) you can follow this helpful link on the official website. If, however, our CUDA version is too low and becomes unlisted (again,) we can follow this link and just search for our version. In our case (CUDA 11.1,) we see that the latest supported version is torch 1.10.1.

Installing PyTorch.

So we copy & paste that into our command line and BAM! PyTorch done.

After that, try to import torch and verify that we can indeed create variables using cuda:

import torch
torch.randn(1, 1, device='cuda')

It is on device! Yay!

Wow! It is on device! Let’s go!

Advanced: Unsupported/Old OSes

For unsupported OSes, sometimes there is no precompiled PyTorch wheels, and that calls for a complete compilation from source. If such cases arise, it is very important for us to choose one compiler throughout the entire compilation (i.e. the compiler needs to be the same for both PyTorch and PyTorch Geometric.) If you are using an archaic version of, say, Ubuntu, which leads to an ancient GCC and G++, I highly recommend you to install Clang instead. For Ubuntu users, you can install Clang using llvm.sh, with a comprehensive guide available here. The Clang version can’t be too new or too old, otherwise we can’t compile PyTorch Geometric. My recommended version is clang-6.0, which works for me.

We can tell pip to use our new compiler once we have finished the installation:

export CC=clang-6.0
export CXX=clang++-6.0

# And now install PyTorch
pip install torch==...

Installing PyTorch Geometric

PyTorch Geometric (PyG for short) is more like a toolkit with four major dependencies:

  • torch_scatter
  • torch_sparse
  • torch_cluster
  • torch_spline_conv
  • pyg_lib (???)

If you have relatively new PyTorch and CUDA installed, you can just follow the guide from its official website. Otherwise, we will have to take the matter into our own hands. Note that on its official website, there is a find link for specific PyTorch and CUDA versions:

The find link.

By copy-pasting it and replacing the torch version and CUDA version with our version (PyTorch 1.10.1 + CUDA 11.1), we get https://data.pyg.org/whl/torch-1.10.1+cu111.html. And after accessing it we are presented with a list of supported versions of PyG dependencies:

The versions.

After a quick look we can see that supported versions of PyG dependencies given our CUDA and PyTorch versions are:

  • torch_cluster: 1.5.9, 1.6.0
  • torch_scatter: 2.0.9
  • torch_sparse: 0.6.12, 0.6.13
  • torch_spline_conv: 1.2.1

Now we simply need to pick their newest versions (or not as new, it’s really up to you) and pip install them.

pip install torch_scatter==2.0.9 torch_sparse==0.6.13 torch_cluster==1.6.0 torch_spline_conv==1.2.1 -f https://data.pyg.org/whl/torch-1.10.1+cu111.html
pip install torch_geometric

WARNING! It’s very important that you specify the version here, otherwise pip will completely disregard the find link and just download the newest version of the four dependencies. You might have noticed that pyg_lib is absent from the above list. I have noticed that as well, but since my task then was to simply install PyG, I disregarded the discrepency and no errors comes off it. I guess my PyTorch Geometric version is too old for the pyg_lib thing.

Advanced: PyG from Source

If you want some new thrilling adventure (or if your OS is too old, as stated above,) you can also compile PyG from source. The procedure is basically the same as above. We have to guarantee the compiler remains the same.

However, if you are using Clang as advised, you will encounter two errors.

  1. The linker will complain something about “libomp5-10”. I am not sure about this, but this seems to be due to PyTorch’s wheel’s own OpenMP library clashes with system-wide OpenMP. This can be solved by specifying the OpenMP library: CFLAGS="-fopenmp=libiomp5" pip install torch_cluster --no-cache
  2. Some weird template error at PyTorch’s variant.h. Funnily enough, I found the solution to both issues on macOS devices with Clang trying to install PyG. This reply solves this issue.

After a long wait, you should be able to build all four (five?) dependencies from source. After that, just install PyG (or build it as well.)

Conclusion

That’s it! A grand adventure of version wrangling, all due to the outdated CUDA version on the server side. But I guess that’s why it’s fun, right? To verify that PyG has been definitively successfully installed, I try to import all four dependencies independently, and try to print out its CUDA version:

import torch_scatter
print(torch_scatter.torch.version.cuda)

# ... and so on.

And that’s about it. Have fun using PyG!

+ Loading comments +
Copyleft 2023 42yeah.