[Cmake] Make PyTorch buildable by CMake-4.x (#150203)

By turning on compatibility mode for protobuf, nnpack, PSimd and FP16, ittapi, TensorPipe and Gloo
Update CMake requirements

 Revert 0ece461ccafe5649d2d0f058ff5477765fd56499 and b0901d62ae2c2e909f91401eacebf3731df20cbe to test that it actually works

TODO:
  - Update/get rid of those libraries

Fixes https://github.com/pytorch/pytorch/issues/150149

Pull Request resolved: https://github.com/pytorch/pytorch/pull/150203
Approved by: https://github.com/clee2000
This commit is contained in:
Nikita Shulga
2025-03-29 01:39:13 +00:00
committed by PyTorch MergeBot
parent edb6f1b7a8
commit 493c7fa66f
13 changed files with 64 additions and 17 deletions

View File

@ -80,7 +80,7 @@ if [ -n "$ANACONDA_PYTHON_VERSION" ]; then
# following builds that we know should use conda. Specifically, Ubuntu bionic # following builds that we know should use conda. Specifically, Ubuntu bionic
# and focal cannot find conda mkl with stock cmake, so we need a cmake from conda # and focal cannot find conda mkl with stock cmake, so we need a cmake from conda
if [ -n "${CONDA_CMAKE}" ]; then if [ -n "${CONDA_CMAKE}" ]; then
conda_install cmake==3.31.2 conda_install cmake
fi fi
# Magma package names are concatenation of CUDA major and minor ignoring revision # Magma package names are concatenation of CUDA major and minor ignoring revision

View File

@ -9,12 +9,12 @@ FOR %%v IN (%DESIRED_PYTHON%) DO (
set PYTHON_VERSION_STR=%%v set PYTHON_VERSION_STR=%%v
set PYTHON_VERSION_STR=!PYTHON_VERSION_STR:.=! set PYTHON_VERSION_STR=!PYTHON_VERSION_STR:.=!
conda remove -n py!PYTHON_VERSION_STR! --all -y || rmdir %CONDA_HOME%\envs\py!PYTHON_VERSION_STR! /s conda remove -n py!PYTHON_VERSION_STR! --all -y || rmdir %CONDA_HOME%\envs\py!PYTHON_VERSION_STR! /s
if "%%v" == "3.9" call conda create -n py!PYTHON_VERSION_STR! -y numpy=2.0.1 boto3 cmake=3.31.2 ninja typing_extensions setuptools=72.1.0 python=%%v if "%%v" == "3.9" call conda create -n py!PYTHON_VERSION_STR! -y numpy=2.0.1 boto3 cmake ninja typing_extensions setuptools=72.1.0 python=%%v
if "%%v" == "3.10" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.0.1 boto3 cmake=3.31.2 ninja typing_extensions setuptools=72.1.0 python=%%v if "%%v" == "3.10" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.0.1 boto3 cmake ninja typing_extensions setuptools=72.1.0 python=%%v
if "%%v" == "3.11" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.0.1 boto3 cmake=3.31.2 ninja typing_extensions setuptools=72.1.0 python=%%v if "%%v" == "3.11" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.0.1 boto3 cmake ninja typing_extensions setuptools=72.1.0 python=%%v
if "%%v" == "3.12" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.0.1 boto3 cmake=3.31.2 ninja typing_extensions setuptools=72.1.0 python=%%v if "%%v" == "3.12" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.0.1 boto3 cmake ninja typing_extensions setuptools=72.1.0 python=%%v
if "%%v" == "3.13" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.1.2 boto3 cmake=3.31.2 ninja typing_extensions setuptools=72.1.0 python=%%v if "%%v" == "3.13" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.1.2 boto3 cmake ninja typing_extensions setuptools=72.1.0 python=%%v
if "%%v" == "3.13t" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.1.2 boto3 cmake=3.31.2 ninja typing_extensions setuptools=72.1.0 python-freethreading python=3.13 if "%%v" == "3.13t" call conda create -n py!PYTHON_VERSION_STR! -y -c=conda-forge numpy=2.1.2 boto3 cmake ninja typing_extensions setuptools=72.1.0 python-freethreading python=3.13
call conda run -n py!PYTHON_VERSION_STR! pip install pyyaml call conda run -n py!PYTHON_VERSION_STR! pip install pyyaml
call conda run -n py!PYTHON_VERSION_STR! pip install mkl-include call conda run -n py!PYTHON_VERSION_STR! pip install mkl-include
call conda run -n py!PYTHON_VERSION_STR! pip install mkl-static call conda run -n py!PYTHON_VERSION_STR! pip install mkl-static

View File

@ -391,6 +391,12 @@ elseif(NOT TARGET fxdiv AND USE_SYSTEM_FXDIV)
endif() endif()
target_link_libraries(pytorch_qnnpack PRIVATE fxdiv) target_link_libraries(pytorch_qnnpack PRIVATE fxdiv)
# -- [ CMake-4 compat mode
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0" AND NOT (USE_SYSTEM_PSIMD OR USE_SYSTEM_FP16))
message(WARNING "Ancient psimd/FP16 forces CMake compatibility")
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
endif()
# ---[ Configure psimd # ---[ Configure psimd
if(NOT TARGET psimd AND NOT USE_SYSTEM_PSIMD) if(NOT TARGET psimd AND NOT USE_SYSTEM_PSIMD)
add_subdirectory( add_subdirectory(
@ -423,6 +429,11 @@ elseif(NOT TARGET fp16 AND USE_SYSTEM_FP16)
endif() endif()
target_link_libraries(pytorch_qnnpack PRIVATE fp16) target_link_libraries(pytorch_qnnpack PRIVATE fp16)
# -- [ Undo cmake-4 compat mode
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
unset(CMAKE_POLICY_VERSION_MINIMUM)
endif()
install(TARGETS pytorch_qnnpack install(TARGETS pytorch_qnnpack
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

View File

@ -784,7 +784,14 @@ if(USE_NUMA)
endif() endif()
if(USE_ITT) if(USE_ITT)
find_package(ITT) if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
message(WARNING "ITT is only cmake-2.8 compatible")
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
find_package(ITT)
unset(CMAKE_POLICY_VERSION_MINIMUM)
else()
find_package(ITT)
endif()
if(ITT_FOUND) if(ITT_FOUND)
include_directories(SYSTEM ${ITT_INCLUDE_DIR}) include_directories(SYSTEM ${ITT_INCLUDE_DIR})
list(APPEND Caffe2_DEPENDENCY_LIBS ${ITT_LIBRARIES}) list(APPEND Caffe2_DEPENDENCY_LIBS ${ITT_LIBRARIES})
@ -1150,7 +1157,14 @@ if(USE_DISTRIBUTED AND USE_TENSORPIPE)
# Tensorpipe uses cuda_add_library # Tensorpipe uses cuda_add_library
torch_update_find_cuda_flags() torch_update_find_cuda_flags()
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
message(WARNING "Archived TensorPipe forces CMake compatibility mode")
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/tensorpipe) add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/tensorpipe)
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
unset(CMAKE_POLICY_VERSION_MINIMUM)
endif()
list(APPEND Caffe2_DEPENDENCY_LIBS tensorpipe) list(APPEND Caffe2_DEPENDENCY_LIBS tensorpipe)
list(APPEND Caffe2_DEPENDENCY_LIBS nlohmann) list(APPEND Caffe2_DEPENDENCY_LIBS nlohmann)
@ -1192,7 +1206,15 @@ if(USE_GLOO)
set(NCCL_EXTERNAL ON) set(NCCL_EXTERNAL ON)
endif() endif()
set(GLOO_USE_CUDA_TOOLKIT ON CACHE BOOL "" FORCE) set(GLOO_USE_CUDA_TOOLKIT ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo) if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
# Remove me when https://github.com/facebookincubator/gloo/pull/424 is landed
message(WARNING "Downgrading cmake-policy-version for gloo build")
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
unset(CMAKE_POLICY_VERSION_MINIMUM)
else()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
endif()
# Here is a little bit hacky. We have to put PROJECT_BINARY_DIR in front # Here is a little bit hacky. We have to put PROJECT_BINARY_DIR in front
# of PROJECT_SOURCE_DIR with/without conda system. The reason is that # of PROJECT_SOURCE_DIR with/without conda system. The reason is that
# gloo generates a new config.h in the binary diretory. # gloo generates a new config.h in the binary diretory.

View File

@ -62,9 +62,16 @@ if(ANDROID OR IOS OR ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR ${CMAKE_SYSTEM_NAM
set(NNPACK_LIBRARY_TYPE "static" CACHE STRING "") set(NNPACK_LIBRARY_TYPE "static" CACHE STRING "")
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "") set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "") set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
message(WARNING "Ancient nnpack forces CMake compatibility")
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
endif()
add_subdirectory( add_subdirectory(
"${NNPACK_SOURCE_DIR}" "${NNPACK_SOURCE_DIR}"
"${CONFU_DEPENDENCIES_BINARY_DIR}/NNPACK") "${CONFU_DEPENDENCIES_BINARY_DIR}/NNPACK")
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
unset(CMAKE_POLICY_VERSION_MINIMUM)
endif()
# We build static versions of nnpack and pthreadpool but link # We build static versions of nnpack and pthreadpool but link
# them into a shared library for Caffe2, so they need PIC. # them into a shared library for Caffe2, so they need PIC.
set_property(TARGET nnpack PROPERTY POSITION_INDEPENDENT_CODE ON) set_property(TARGET nnpack PROPERTY POSITION_INDEPENDENT_CODE ON)

View File

@ -52,7 +52,14 @@ macro(custom_protobuf_find)
endif(MSVC_Z7_OVERRIDE) endif(MSVC_Z7_OVERRIDE)
endif(MSVC) endif(MSVC)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/protobuf/cmake) if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.0.0")
message(WARNING "Ancient protobuf forces CMake compatibility")
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/protobuf/cmake)
unset(CMAKE_POLICY_VERSION_MINIMUM)
else()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/protobuf/cmake)
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE}) set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE})

View File

@ -1,6 +1,6 @@
# Python dependencies required for development # Python dependencies required for development
astunparse astunparse
cmake==3.31.6 # Temp pin until we support 4.0.0? cmake
expecttest>=0.3.0 expecttest>=0.3.0
filelock filelock
fsspec fsspec

View File

@ -1,5 +1,5 @@
# Basic CMake setup # Basic CMake setup
cmake_minimum_required(VERSION 3.1 FATAL_ERROR) cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(custom_backend) project(custom_backend)
if(USE_ROCM) if(USE_ROCM)

View File

@ -1,5 +1,5 @@
# Basic CMake setup # Basic CMake setup
cmake_minimum_required(VERSION 3.1 FATAL_ERROR) cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(custom_ops) project(custom_ops)
if(USE_ROCM) if(USE_ROCM)

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.15)
set(TORCH_ROOT ${CMAKE_CURRENT_LIST_DIR}/../..) set(TORCH_ROOT ${CMAKE_CURRENT_LIST_DIR}/../..)
set(TEST_ROOT ${TORCH_ROOT}/test/edge) set(TEST_ROOT ${TORCH_ROOT}/test/edge)

View File

@ -1,5 +1,5 @@
# Basic CMake setup # Basic CMake setup
cmake_minimum_required(VERSION 3.1 FATAL_ERROR) cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(jit_hooks) project(jit_hooks)
if(USE_ROCM) if(USE_ROCM)

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.15)
project(custom_build_project) project(custom_build_project)

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.15)
set(TORCH_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../..) set(TORCH_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../..)
set(TEST_ROOT ${TORCH_ROOT}/test/mobile/lightweight_dispatch) set(TEST_ROOT ${TORCH_ROOT}/test/mobile/lightweight_dispatch)