Request for assistance: Building PyTorch 2.5.1 (libtorch + main PyTorch) with CUDA 12.6.3 and cuDNN 9.5.1

Hello PyTorch Team and Community,

I would like to request guidance on the correct method to build PyTorch 2.5.1 (both libtorch and the main PyTorch) from source, ensuring compatibility with CUDA 12.6.3 and cuDNN 9.5.1.

Machine Specifications

•	OS: Ubuntu 22.04.5 LTS
•	CPU: Intel i7-9700K
•	Memory: 64 GB
•	GPU: RTX 4060 Ti (16 GB VRAM)
•	CUDA/cuDNN: CUDA 12.6.3 and cuDNN 9.5.1, both installed system-wide (not through Miniconda or other package managers).
•	Python Environment: Miniconda3 with Python 3.11.10 (virtual environment).

My goal is to build PyTorch from source for use in image generation AI workflows (e.g., ComfyUI).

Context and Current Process

I want to confirm the correct procedure for building PyTorch (including libtorch) with cmake from source, targeting CUDA 12.6.3 and cuDNN 9.5.1, and subsequently packaging the build as a .whl file for installation in my virtual environment.

Previously, I followed this process:
1. Set the environment variable export BUILD_LIBTORCH_WHL=1 and built libtorch (TORCH_NO_PYTHON) with cmake, configuring it for CUDA 12.6.2 and cuDNN 9.5.1.
2. Used the following command to build and create a wheel file:

python -m build --wheel -Ccmake.define.BUILD_CMAKE_FROM_SOURCE=ON

3.	Deleted the build directory using rm -rf build and cleaned up with python setup.py clean.
4.	Then, I used this built version of libtorch as a dependency for building the main PyTorch. Before doing so, I set export BUILD_PYTHON_ONLY=1, configured CUDA 12.6.2 and cuDNN 9.5.1 with cmake again (using the same settings as the libtorch build), and attempted to build with the same command:

python -m build --wheel -Ccmake.define.BUILD_CMAKE_FROM_SOURCE=ON

However, this approach often results in errors during the main PyTorch build phase, such as torch_python not being found. To work around this, I have removed the BUILD_PYTHON_ONLY flag and rebuilt PyTorch using the following command instead:

python -m build

While this allows me to build a package compatible with CUDA 12.6.2 and cuDNN 9.5.1 for my virtual environment, I suspect this approach is incorrect, especially for integrating libtorch properly into the main PyTorch build.

Questions

1.	What is the correct and complete method to build PyTorch 2.5.1, including both libtorch and the main PyTorch, from source, targeting CUDA 12.6.3 and cuDNN 9.5.1?
2.	Is it correct to assume that libtorch must be included as part of the main PyTorch build to consider the package complete?
3.	Are there additional dependencies, environment variables, or specific cmake options required for this process?

If further information is needed regarding my build environment, configuration, or errors encountered, please let me know, and I will provide additional details.

Thank you for your assistance!

1 Like

I was able to build pytorch with CUDA12.8.1+cuDNN9.8.0. This problem was solved.

Can you share the procedures to build libtorch and the python wheel? I want one for c++ and one for python. Thanks a million

1 Like

Hi, I can try to share what worked for me, but my final successful build was on a fairly different environment than the original one in this thread.

Because of that, I do not want to post a very large procedure that may not match your system.

Could you clarify what you want most?

1. libtorch (C++) build steps

2. Python wheel build steps

3. dependency setup

4. specific build error troubleshooting

If possible, please also share your OS, Python version, CUDA, cuDNN, compiler, and whether you use Conda or a native venv.

I’m integrating pytorch with our existing infrastructure (based on tensorflow). Currently, I removed tensorflow’s GPU capability(by defining TF_NEED_CUDA=0 in bazel) and successfully make torch and tensorflow work together in a single process. To make dependencies consistent. I’m trying to recompile libtorch and pytorch wheel using clang20, cuda 12.8, cudnn 9.8. python 3.10, Ubuntu 22.04, native venv.

After googling, I didn’t find a clear step to build both libtorch and python wheel. We need both and need recompiling to make sure the artifact is consistent with our existing environment.

By the way, I have built libtorch with bazel (with tens of patches). I’m afraid that torch wheel need hundreds of patches with bazel :smiley: