Using separate cmake build directories when building from source

Hi all,

One thing that has bothered me about building pytorch from source is that I could not use multiple build directories, even though it is a cmake project. I dug around a little bit, and found a workaround for this that I wanted to share with anyone else in my situation.

If you apply this diff:

modified   tools/setup_helpers/env.py                                                                                                                                                                       
@@ -21,7 +21,7 @@ CONDA_DIR = os.path.join(os.path.dirname(sys.executable), "..")                                                                                                                           
                                                                                                                                                                                                            
 IS_64BIT = struct.calcsize("P") == 8                                                                                                                                                                       
                                                                                                                                                                                                            
-BUILD_DIR = "build"                                                                                                                                                                                        
+BUILD_DIR = os.environ.get("TORCH_CMAKE_BUILD_DIR", "build")                                                                                                                                               
                                                                                                                                                                                                            
                                                                                                                                                                                                            
 def check_env_flag(name: str, default: str = "") -> bool:                                                                                                                                                  

and then set the TORCH_CMAKE_BUILD_DIR environment variable to something other than “build”, you can share multiple build directories in the same repository. I have found this useful for testing out the same changes against multiple different versions of CUDA for my PR Add support for conditional nodes in cuda graphs. by galv · Pull Request #130386 · pytorch/pytorch · GitHub, since it depends upon CUDA 12.4 features.

Here are my two compile command lines:

TORCH_CUDA_ARCH_LIST="8.0" CUDA_HOME=/home/dgalvez/scratch/cuda/cuda-12.4 CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache CMAKE_CUDA_COMPILER_LAUNCHER=ccache REL_WITH_DEB_INFO=1 python setup.py develop 2>&1 | tee develop_cudagraphs_12.4.
TORCH_CMAKE_BUILD_DIR=build_cuda12.1 TORCH_CUDA_ARCH_LIST="8.0" CUDA_HOME=/home/dgalvez/scratch/cuda/cuda-12.1.1 CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache CMAKE_CUDA_COMPILER_LAUNCHER=ccache python setup.py develop 2>&1 | tee develop_cudagraphs_12.1.1_2.log

I have no idea if this works in all cases, but it works for me.

1 Like