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!