Files
pytorch/CMakeLists.txt
Junjie Wang 21e85424b9 [distributed] Build NCCL EP by default so TokenSwitchNCCL is enabled by default (#204363)
Human notes:
We want to enable the build of NCCL-EP on by default if both `USE_NCCL` and `USE_CUDA` are set.

## Agent details (AI-generated)

Default USE_NCCL_EP to ON so builds with CUDA and NCCL enabled also
build NCCL EP. Preserve the existing dependency conditions and explicit
USE_NCCL_EP=OFF override.

Authored with an AI assistant.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/204363
Approved by: https://github.com/kwen2501, https://github.com/Skylion007, https://github.com/crcrpar
2026-11-13 00:24:26 +00:00

1697 lines
68 KiB
CMake

# PyTorch build overview
#
# This file is the entry point of the native build. It is normally driven by
# scikit-build-core through `pip install -e . -v --no-build-isolation`
# (`spin develop`) rather than by invoking cmake directly; the exception is the
# standalone libtorch build in tools/build_libtorch.py. The stages below appear
# in the order they are executed in this file.
#
# 1. Bootstrap: forward the toolchain env vars needed before project() (cross
# compilation), call project() with the version from version.txt, then
# cmake/EnvVarForwarding.cmake turns the remaining build env vars into CMake
# cache variables. That module is the reference for the environment
# variables that configure the build: look there first for an env var to
# set, and document new ones there. The exceptions are third-party locations
# such as MKLROOT or ROCM_PATH and a handful of niche flags, which are read
# directly where they are used.
# 2. Pre-build (cmake/PreBuildSteps.cmake): initialize and verify the git
# submodules, and fetch the pinned NCCL for distributed CUDA builds.
# 3. Options: declare every build knob (USE_*, BUILD_*). An option added here
# must also be added to cmake/Summary.cmake.
# 4. Dependencies (cmake/Dependencies.cmake): find or build the third-party
# libraries and set up the accelerator toolchains (CUDA/ROCm/XPU). The
# global compile and link flags are resolved inline right after.
# 5. Code generation, all driven from caffe2/CMakeLists.txt: cmake/Codegen.cmake
# runs torchgen over native_functions.yaml to emit the ATen operator headers
# and sources, and tools/setup_helpers/generate_code.py emits the autograd
# and Python binding sources. The .pyi stubs are generated later, in
# torch/CMakeLists.txt.
# 6. Main build: add_subdirectory() of torch/headeronly, c10 and caffe2 (which
# in turn adds torch/). This builds c10, torch_cpu, the per-accelerator
# torch_cuda/torch_hip/torch_xpu, the umbrella torch library and
# torch_python, plus the C++ tests; those subdirectories also carry the
# install rules for the headers and libraries.
# 7. Export and extras: generate the Caffe2Config.cmake / Caffe2Targets.cmake
# files that downstream C++ projects consume, then the optional binaries/
# and JNI bindings, which link against the libraries built above.
# 8. Python packaging: cmake/FileMirroring.cmake copies sources into
# torchgen/packaged and into the torch/ subpackages that need files vendored
# out of third_party/, cmake/PackageData.cmake installs the non-Python
# package data (stubs, headers, templates) into torch/, and
# cmake/PostBuildSteps.cmake merges compile_commands.json and registers the
# install-time fixups (TORCH_STABLE_ONLY header wrapping, Windows runtime
# DLL and macOS OpenMP bundling).
# 9. Summary (cmake/Summary.cmake): print the resolved configuration.
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)
cmake_policy(SET CMP0126 OLD)
# Don't remove the FindCUDA module
cmake_policy(SET CMP0146 OLD)
# Prohibit in-source builds
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source build are not supported")
endif()
# Cross-compilation is configured by CMAKE_SYSTEM_NAME / CMAKE_SYSTEM_PROCESSOR,
# which must be set before project() so CMake enters cross-compile mode and
# reports the target architecture. The general environment-variable forwarding
# in cmake/EnvVarForwarding.cmake runs after project() and skips variables that
# are already defined, so project() would otherwise set these to the host values
# and the target values passed via the environment would be lost. Forward just
# the toolchain-determining variables here, before project().
foreach(_cross_var
CMAKE_SYSTEM_NAME
CMAKE_SYSTEM_PROCESSOR
CMAKE_C_COMPILER
CMAKE_CXX_COMPILER
CMAKE_SYSROOT
CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{${_cross_var}} AND NOT DEFINED ${_cross_var})
set(${_cross_var} "$ENV{${_cross_var}}")
endif()
endforeach()
# ---[ Project and semantic versioning.
# When built via scikit-build-core, these are set automatically.
# Provide fallbacks for standalone cmake invocations.
if(NOT DEFINED SKBUILD_PROJECT_NAME)
set(SKBUILD_PROJECT_NAME "torch")
endif()
if(NOT DEFINED SKBUILD_PROJECT_VERSION)
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/version.txt" SKBUILD_PROJECT_VERSION)
string(STRIP "${SKBUILD_PROJECT_VERSION}" SKBUILD_PROJECT_VERSION)
# version.txt may contain PEP 440 pre-release suffixes like "a0" that are
# not valid CMake version components. Strip everything after the last digit
# group so project(VERSION ...) still works.
string(REGEX MATCH "^[0-9]+\\.[0-9]+(\\.[0-9]+)?" SKBUILD_PROJECT_VERSION "${SKBUILD_PROJECT_VERSION}")
endif()
project(${SKBUILD_PROJECT_NAME}
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES CXX C)
# When built via scikit-build-core, Python_EXECUTABLE is already set.
# For standalone cmake invocations, find it via find_package.
find_package(Python COMPONENTS Interpreter REQUIRED)
# ---[ Where the Python packages go.
# scikit-build-core exports each wheel tree to CMake as SKBUILD_<TREE>_DIR, and
# wheel.install-dir picks which one becomes CMAKE_INSTALL_PREFIX. We set it to
# "torch" so the C++ distribution lands at torch/lib and torch/include, as the
# install rpath and torch/__init__.py both require; the prefix therefore sits
# one level below platlib, and the Python-side install rules address
# SKBUILD_PLATLIB_DIR directly instead of reaching it through the prefix.
#
# Those rules are guarded on SKBUILD, so a build scikit-build-core does not
# drive (libtorch, tools/build_libtorch.py, downstream cmake --install) installs
# the C++ distribution alone and keeps package data out of a conventional prefix
# (see gh-191102). SKBUILD is set for every scikit-build-core configure,
# editable installs included, where platlib is a per-build staging directory.
#
# The extension modules resolve the C++ tree through an $ORIGIN/lib install
# rpath and torch/__init__.py resolves bin/ and lib/ off its own __file__, so
# the prefix has to be the torch package directory. Enforce it rather than let
# a mismatch produce an install that only fails at import time.
if(SKBUILD)
get_filename_component(_torch_pkg_dir "${SKBUILD_PLATLIB_DIR}/torch" ABSOLUTE)
get_filename_component(_torch_prefix "${CMAKE_INSTALL_PREFIX}" ABSOLUTE)
if(NOT _torch_pkg_dir STREQUAL _torch_prefix)
message(FATAL_ERROR
"CMAKE_INSTALL_PREFIX must be the torch package directory: the "
"extension modules load the C++ libraries through an $ORIGIN/lib rpath "
"relative to themselves.\n"
" SKBUILD_PLATLIB_DIR : ${SKBUILD_PLATLIB_DIR}\n"
" expected prefix : ${_torch_pkg_dir}\n"
" CMAKE_INSTALL_PREFIX : ${_torch_prefix}\n"
"Keep wheel.install-dir = \"torch\" in pyproject.toml, which is what "
"makes the two agree.")
endif()
unset(_torch_pkg_dir)
unset(_torch_prefix)
endif()
# Forward environment variables to CMake variables, replicating the behavior
# previously handled by setup.py / tools/setup_helpers/cmake.py.
# When built via setup.py, these are already set as -D flags; the module
# skips variables that are already defined, so it is safe to include
# unconditionally.
include(cmake/EnvVarForwarding.cmake)
# Pre-build steps: submodule init, NCCL checkout.
# When built via setup.py, these are handled by build_deps() in setup.py;
# the module checks for existing state, so it is safe to include
# unconditionally.
include(cmake/PreBuildSteps.cmake)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(LINUX TRUE)
else()
set(LINUX FALSE)
endif()
set(CMAKE_INSTALL_MESSAGE NEVER)
# check and set CMAKE_CXX_STANDARD
string(FIND "${CMAKE_CXX_FLAGS}" "-std=c++" env_cxx_standard)
if(env_cxx_standard GREATER -1)
message(
WARNING
"C++ standard version definition detected in environment variable. "
"PyTorch requires -std=c++20. Please remove -std=c++ settings in your environment."
)
endif()
set(CMAKE_CXX_STANDARD
20
CACHE STRING
"The C++ standard whose features are requested to build this target.")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD
17
CACHE STRING
"The C standard whose features are requested to build this target.")
# ---[ Utils
include(cmake/public/utils.cmake)
# --- [ Check that minimal gcc version is 11.3+
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.3)
message(
FATAL_ERROR
"GCC-11.3 or newer is required to compile PyTorch, but found ${CMAKE_CXX_COMPILER_VERSION}"
)
endif()
# --- [ Check that minimal clang version is 16+
# Older clangs (e.g. clang-14) fail to compile C++20 ranges adaptors such as
# `std::views::filter` over associative containers when paired with libstdc++,
# see https://github.com/pytorch/pytorch/issues/184630. AppleClang uses its
# own versioning scheme and is excluded from this check.
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16)
message(
FATAL_ERROR
"Clang-16 or newer is required to compile PyTorch, but found ${CMAKE_CXX_COMPILER_VERSION}"
)
endif()
# This define is needed to preserve behavior given anticpated changes to
# cccl/thrust
# https://github.com/NVIDIA/cccl/blob/main/docs/libcudacxx/standard_api/numerics_library/complex.rst
string(APPEND CMAKE_CUDA_FLAGS
" -DLIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_LINK_WHAT_YOU_USE TRUE)
# One variable that determines whether the current cmake process is being run
# with the main Caffe2 library. This is useful for building modules - if modules
# are built with the main Caffe2 library then one does not need to do find
# caffe2 in the cmake script. One can usually guard it in some way like if(NOT
# CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO) find_package(Caffe2 REQUIRED) endif()
set(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO ON)
# Googletest's cmake files are going to set it on once they are processed. Let's
# set it at the very beginning so that the entire build is deterministic.
set(THREADS_PREFER_PTHREAD_FLAG ON)
if(NOT DEFINED BLAS_SET_BY_USER)
if(DEFINED BLAS)
set(BLAS_SET_BY_USER TRUE)
else()
message(STATUS "Not forcing any particular BLAS to be found")
set(BLAS_SET_BY_USER FALSE)
endif()
set(BLAS_SET_BY_USER
${BLAS_SET_BY_USER}
CACHE STRING
"Marks whether BLAS was manually set by user or auto-detected")
endif()
# Apple specific
if(APPLE)
# These lines are an attempt to make find_package(cuda) pick up libcuda.dylib,
# and not cuda.framework. It doesn't work all the time, but it seems to help
# for some users. TODO: replace this with a more robust fix
set(CMAKE_FIND_FRAMEWORK LAST)
set(CMAKE_FIND_APPBUNDLE LAST)
# Get clang version on macOS
execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version
OUTPUT_VARIABLE clang_full_version_string)
string(REGEX REPLACE "Apple (.*) version ([0-9]+\\.[0-9]+).*" "\\2"
CLANG_VERSION_STRING ${clang_full_version_string})
message(STATUS "CLANG_VERSION_STRING: " ${CLANG_VERSION_STRING})
if(NOT IOS)
# Determine if we can link against MPSGraph
set(MPS_FOUND OFF)
execute_process(
COMMAND bash -c "xcrun --sdk macosx --show-sdk-version"
RESULT_VARIABLE _exit_code
OUTPUT_VARIABLE _macosx_sdk_version
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(_exit_code EQUAL 0)
set(_MPS_supported_os_version OFF)
if(_macosx_sdk_version VERSION_GREATER_EQUAL 12.3)
set(_MPS_supported_os_version ON)
endif()
message(
STATUS
"sdk version: ${_macosx_sdk_version}, mps supported: ${_MPS_supported_os_version}"
)
execute_process(
COMMAND bash -c "xcrun --sdk macosx --show-sdk-path"
OUTPUT_VARIABLE _macosx_sdk_path
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(_SDK_SEARCH_PATH "${_macosx_sdk_path}/System/Library/Frameworks/")
set(_FRAMEWORK_SEARCH_PATH "/System/Library/Frameworks/")
find_library(
_MPS_fwrk_path_
NAMES MetalPerformanceShadersGraph MetalPerformanceShaders
PATHS ${_FRAMEWORK_SEARCH_PATH}
NO_DEFAULT_PATH)
find_library(
_MPS_sdk_path_
NAMES MetalPerformanceShadersGraph MetalPerformanceShaders
PATHS ${_SDK_SEARCH_PATH}
NO_DEFAULT_PATH)
if(_MPS_supported_os_version
AND _MPS_fwrk_path_
AND _MPS_sdk_path_)
set(MPS_FOUND ON)
message(STATUS "MPSGraph framework found")
else()
message(STATUS "MPSGraph framework not found")
endif()
else()
message(STATUS "MPS: unable to get MacOS sdk version")
message(STATUS "MPSGraph framework not found")
endif()
endif()
endif()
set(CPU_AARCH64 OFF)
set(CPU_INTEL OFF)
set(CPU_POWER OFF)
set(CPU_RISCV OFF)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(AMD64|x86_64)")
set(CPU_INTEL ON)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)")
# Linux reports "aarch64", macOS "arm64", and native Windows on Arm "ARM64".
# CMake MATCHES is case-sensitive, so accept each spelling.
set(CPU_AARCH64 ON)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(ppc64le)")
set(CPU_POWER ON)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv64)")
set(CPU_RISCV ON)
endif()
# For non-supported platforms, turn USE_DISTRIBUTED off by default. It is not
# tested and likely won't work without additional changes.
if(NOT LINUX AND NOT WIN32)
set(USE_DISTRIBUTED
OFF
CACHE STRING "Use distributed")
# On macOS, if USE_DISTRIBUTED is enabled (specified by the user), then make
# Gloo build with the libuv transport.
if(APPLE AND USE_DISTRIBUTED)
set(USE_LIBUV
ON
CACHE STRING "")
endif()
endif()
# ---[ Options. Note to developers: if you add an option below, make sure you
# also add it to cmake/Summary.cmake so that the summary prints out the option
# values.
include(CMakeDependentOption)
option(ATEN_NO_TEST "Do not build ATen test binaries" OFF)
option(BUILD_BINARY "Build C++ binaries" OFF)
option(BUILD_CUSTOM_PROTOBUF
"Build and use Caffe2's own protobuf under third_party" ON)
option(BUILD_PYTHON "Build Python binaries" ON)
option(BUILD_LITE_INTERPRETER "Master flag to build Lite Interpreter" OFF)
option(BUILD_SHARED_LIBS "Build libcaffe2.so" ON)
cmake_dependent_option(
CAFFE2_LINK_LOCAL_PROTOBUF "If set, build protobuf inside libcaffe2.so." ON
"BUILD_SHARED_LIBS AND BUILD_CUSTOM_PROTOBUF" OFF)
cmake_dependent_option(
CAFFE2_USE_MSVC_STATIC_RUNTIME "Using MSVC static runtime libraries" ON
"NOT BUILD_SHARED_LIBS" OFF)
option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" ON)
option(BUILD_AOT_INDUCTOR_TEST "Build C++ test binaries for aot-inductor" OFF)
option(BUILD_STATIC_RUNTIME_BENCHMARK
"Build C++ binaries for static runtime benchmarks (need gbenchmark)" OFF)
option(
BUILD_MOBILE_BENCHMARK
"Build C++ test binaries for mobile (ARM) targets(need gtest and gbenchmark)"
OFF)
option(
BUILD_MOBILE_TEST
"Build C++ test binaries for mobile (ARM) targets(need gtest and gbenchmark)"
OFF)
option(BUILD_JNI "Build JNI bindings" OFF)
option(BUILD_MOBILE_AUTOGRAD
"Build autograd function in mobile build (in development)" OFF)
cmake_dependent_option(INSTALL_TEST "Install test binaries if BUILD_TEST is on"
ON "BUILD_TEST" OFF)
option(USE_RELATIVE_PATHS "Use relative paths in generated files for ccache friendliness" OFF)
option(USE_CPP_CODE_COVERAGE "Compile C/C++ with code coverage flags" OFF)
option(USE_COLORIZE_OUTPUT "Colorize output during compilation" ON)
option(USE_ASAN "Use Address+Undefined Sanitizers" OFF)
option(USE_LSAN "Use Leak Sanitizer" OFF)
option(USE_TSAN "Use Thread Sanitizer" OFF)
# Track whether USE_CUDA was explicitly set by the user (before option() is called)
# If USE_CUDA is already defined in cache, it means user explicitly set it
if(DEFINED CACHE{USE_CUDA})
set(_USE_CUDA_EXPLICITLY_SET TRUE)
else()
set(_USE_CUDA_EXPLICITLY_SET FALSE)
endif()
# CUDA is incompatible with TSAN
cmake_dependent_option(USE_CUDA "Use CUDA" ON "NOT USE_TSAN" OFF)
# If USE_XPU is already defined in cache, it means user explicitly set it
if(DEFINED CACHE{USE_XPU})
set(_USE_XPU_EXPLICITLY_SET TRUE)
else()
set(_USE_XPU_EXPLICITLY_SET FALSE)
endif()
option(USE_XPU "Use XPU" ON)
option(USE_MTIA "Use MTIA" OFF)
cmake_dependent_option(
BUILD_LAZY_CUDA_LINALG "Build cuda linalg ops as separate library" ON
"USE_CUDA AND LINUX AND BUILD_PYTHON" OFF)
cmake_dependent_option(USE_ROCM "Use ROCm" ON "LINUX OR WIN32" OFF)
cmake_dependent_option(USE_ROCM_CK_GEMM "Use ROCm Composable Kernel for GEMMs" ON "USE_ROCM;NOT WIN32" OFF)
# Single source of truth for the GPU archs Composable Kernel GEMM supports. It
# drives both the ck_gemm build arch filter (torch_filter_ck_gemm_arches in
# cmake/public/utils.cmake) and the runtime support check / dispatch, which read
# it through the generated ATen/ROCmCKGEMMConfig.h. Add a WMMA arch (e.g.
# gfx1200) here to opt it back in without patching source.
set(TORCH_CK_GEMM_ARCH_LIST "gfx90a;gfx942;gfx950" CACHE STRING
"Semicolon-separated GPU archs Composable Kernel GEMM is built and enabled for")
cmake_dependent_option(USE_ROCM_CK_SDPA "Use ROCm Composable Kernel for SDPA" ON "USE_ROCM;NOT WIN32" OFF)
# Off by default because enabling gfx11/gfx12 adds CK SDPA code generation and
# compile cost to ROCm builds that already target those arches.
cmake_dependent_option(USE_ROCM_CK_SDPA_GFX11 "Also build ROCm Composable Kernel SDPA for gfx11 (RDNA3, RDNA3.5)" OFF "USE_ROCM_CK_SDPA" OFF)
cmake_dependent_option(USE_ROCM_CK_SDPA_GFX12 "Also build ROCm Composable Kernel SDPA for gfx12 (RDNA4)" OFF "USE_ROCM_CK_SDPA" OFF)
option(CAFFE2_STATIC_LINK_CUDA "Statically link CUDA libraries" OFF)
cmake_dependent_option(USE_CUDNN "Use cuDNN" ON "USE_CUDA" OFF)
cmake_dependent_option(USE_STATIC_CUDNN "Use cuDNN static libraries" OFF
"USE_CUDNN" OFF)
cmake_dependent_option(USE_CUSPARSELT "Use cuSPARSELt" ON "USE_CUDA" OFF)
cmake_dependent_option(USE_HIPSPARSELT "Use hipSPARSELt" ON "USE_ROCM" OFF)
cmake_dependent_option(USE_CUDSS "Use cuDSS" ON "USE_CUDA" OFF)
cmake_dependent_option(USE_CUFILE "Use cuFile" ON "USE_CUDA OR USE_ROCM;NOT WIN32" OFF)
option(USE_FBGEMM "Use FBGEMM (quantized 8-bit server operators)" ON)
option(USE_KINETO "Use Kineto profiling library" ON)
option(USE_CUPTI_SO "Use CUPTI as a shared library" ON)
option(USE_GFLAGS "Use GFLAGS" OFF)
option(USE_GLOG "Use GLOG" OFF)
option(USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
option(USE_MAGMA "Use MAGMA" ON)
option(USE_PYTORCH_METAL "Use Metal for PyTorch iOS build" OFF)
option(USE_PYTORCH_METAL_EXPORT "Export Metal models on MacOSX desktop" OFF)
option(USE_NATIVE_ARCH "Use -march=native" OFF)
cmake_dependent_option(USE_MPS "Use MPS for macOS build" ON "MPS_FOUND" OFF)
option(USE_DISTRIBUTED "Use distributed" ON)
cmake_dependent_option(USE_NCCL "Use NCCL" ON
"USE_DISTRIBUTED;USE_CUDA OR USE_ROCM;UNIX;NOT APPLE" OFF)
cmake_dependent_option(USE_XCCL "Use XCCL" ON
"USE_XPU;UNIX;NOT APPLE" OFF)
cmake_dependent_option(USE_RCCL "Use RCCL" ON "USE_NCCL;NOT WIN32" OFF)
cmake_dependent_option(USE_STATIC_NCCL "Use static NCCL" OFF "USE_NCCL" OFF)
cmake_dependent_option(USE_SYSTEM_NCCL "Use system-wide NCCL" OFF "USE_NCCL"
OFF)
# Builds NCCL-EP if true.
cmake_dependent_option(USE_NCCL_EP "Build NCCL EP library" ON
"USE_NCCL;USE_CUDA" OFF)
cmake_dependent_option(USE_NVSHMEM "Use NVSHMEM" ON
"USE_DISTRIBUTED;USE_CUDA OR USE_ROCM;UNIX;NOT APPLE" OFF)
# NNPACK has been removed; still reported via caffe2/core/macros.h.in
set(USE_NNPACK OFF)
cmake_dependent_option(USE_NUMA "Use NUMA. Only available on Linux." ON "LINUX"
OFF)
cmake_dependent_option(USE_NVRTC "Use NVRTC. Only available if USE_CUDA is on."
ON "USE_CUDA" OFF)
option(USE_NUMPY "Use NumPy" ON)
option(USE_OPENMP "Use OpenMP for parallel code" ON)
option(USE_PRECOMPILED_HEADERS "Use pre-compiled headers to accelerate build."
OFF)
option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" ON)
option(USE_EIGEN_SPARSE "Use Eigen Sparse Matrices" OFF)
option(USE_SYSTEM_EIGEN_INSTALL
"Use system Eigen instead of the one under third_party" OFF)
cmake_dependent_option(
USE_VALGRIND "Use Valgrind. Only available on Linux." ON
"LINUX" OFF)
if(NOT DEFINED USE_VULKAN)
cmake_dependent_option(USE_VULKAN "Use Vulkan GPU backend" ON "ANDROID" OFF)
endif()
option(USE_SOURCE_DEBUG_ON_MOBILE "Enable" ON)
option(USE_LITE_INTERPRETER_PROFILER "Enable" ON)
option(USE_VULKAN_FP16_INFERENCE "Vulkan - Use fp16 inference" OFF)
option(USE_VULKAN_RELAXED_PRECISION
"Vulkan - Use relaxed precision math in the kernels (mediump)" OFF)
# option USE_XNNPACK: try to enable xnnpack by default.
option(USE_XNNPACK "Use XNNPACK" OFF)
option(USE_ROCM_KERNEL_ASSERT "Use Kernel Assert for ROCm" OFF)
# Ensure that an ITT build is the default for x86 CPUs
cmake_dependent_option(USE_ITT "Use Intel(R) VTune Profiler ITT functionality"
ON "CPU_INTEL" OFF)
# Ensure that an MKLDNN build is the default for x86 and AArch64 CPUs
if(CPU_INTEL OR CPU_AARCH64)
set(USE_MKLDNN_DEFAULT ON)
else()
set(USE_MKLDNN_DEFAULT OFF)
endif()
cmake_dependent_option(
USE_MKLDNN "Use MKLDNN. Only available on x86, x86_64, AArch64, ppc64le and riscv64."
"${USE_MKLDNN_DEFAULT}" "CPU_INTEL OR CPU_AARCH64 OR CPU_POWER OR CPU_RISCV" OFF)
cmake_dependent_option(
USE_MKLDNN_ACL "Use Compute Library for the Arm architecture." OFF
"USE_MKLDNN AND CPU_AARCH64" OFF)
option(USE_STATIC_MKL "Prefer to link with MKL statically (Unix only)" OFF)
cmake_dependent_option(
USE_MPI "Use MPI for Caffe2. Only available if USE_DISTRIBUTED is on." ON
"USE_DISTRIBUTED" OFF)
cmake_dependent_option(
USE_UCC "Use UCC. Only available if USE_DISTRIBUTED is on." OFF
"USE_DISTRIBUTED" OFF)
cmake_dependent_option(USE_SYSTEM_UCC "Use system-wide UCC" OFF "USE_UCC" OFF)
cmake_dependent_option(USE_C10D_UCC "USE C10D UCC" ON "USE_DISTRIBUTED;USE_UCC"
OFF)
cmake_dependent_option(
USE_GLOO "Use Gloo. Only available if USE_DISTRIBUTED is on." ON
"USE_DISTRIBUTED" OFF)
cmake_dependent_option(
USE_GLOO_WITH_OPENSSL "Use Gloo with OpenSSL. Only available if USE_GLOO is on." OFF
"USE_GLOO AND LINUX AND NOT INTERN_BUILD_MOBILE" OFF)
cmake_dependent_option(
USE_GLOO_IBVERBS "Use Gloo with ibverbs backend. Only available if USE_GLOO is on." OFF
"USE_GLOO AND LINUX AND NOT INTERN_BUILD_MOBILE" OFF)
cmake_dependent_option(
USE_C10D_GLOO "USE C10D GLOO" ON "USE_DISTRIBUTED;USE_GLOO" OFF)
cmake_dependent_option(
USE_C10D_NCCL "USE C10D NCCL" ON "USE_DISTRIBUTED;USE_NCCL" OFF)
cmake_dependent_option(
USE_C10D_XCCL "USE C10D XCCL" ON "USE_DISTRIBUTED;USE_XCCL" OFF)
cmake_dependent_option(
USE_C10D_MPI "USE C10D MPI" ON "USE_DISTRIBUTED;USE_MPI" OFF)
cmake_dependent_option(
USE_TENSORPIPE "Use TensorPipe. Only available if USE_DISTRIBUTED is on." ON
"USE_DISTRIBUTED AND NOT WIN32" OFF)
option(ONNX_ML "Enable traditional ONNX ML API." ON)
option(HAVE_SOVERSION "Whether to add SOVERSION to the shared objects" OFF)
option(BUILD_LIBTORCH_CPU_WITH_DEBUG
"Enable RelWithDebInfo for libtorch_cpu target only" OFF)
cmake_dependent_option(
USE_CCACHE "Attempt using [S]CCache to wrap the compilation" ON "UNIX" OFF)
option(WERROR "Build with -Werror supported by the compiler" OFF)
option(
DEBUG_CUDA
"When compiling DEBUG, also attempt to compile CUDA with debug flags (may cause nvcc to OOM)"
OFF)
option(USE_COREML_DELEGATE "Use the CoreML backend through delegate APIs" OFF)
option(USE_PER_OPERATOR_HEADERS
"Whether ATen should generate separate headers for each operator" ON)
cmake_dependent_option(
BUILD_LAZY_TS_BACKEND
"Build the lazy Torchscript backend, not compatible with mobile builds" ON
"NOT INTERN_BUILD_MOBILE" OFF)
cmake_dependent_option(BUILD_BUNDLE_PTXAS "Bundle PTX into torch/bin folder"
OFF "USE_CUDA" OFF)
cmake_dependent_option(
USE_CMAKE_INSTRUMENTATION
"Use cmake-instrumentation to collect build metrics"
OFF # disabled by default because it causes CI timeouts
"CMAKE_VERSION VERSION_GREATER_EQUAL 4.3"
OFF
)
if(USE_CMAKE_INSTRUMENTATION)
cmake_instrumentation(
API_VERSION 1
DATA_VERSION 1.0
HOOKS postCMakeBuild
OPTIONS trace
CALLBACK "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/build_trace_summary.py" --root "${CMAKE_CURRENT_SOURCE_DIR}" --index
)
endif()
cmake_dependent_option(USE_KLEIDIAI "Use KleidiAI for the ARM CPU & AARCH64 architecture." ON
"CPU_AARCH64" OFF)
# prioritized text linker, ON by default for AArch64+Linux, option visible to all AArch64, x86 and ppc64le.
set(USE_PRIORITIZED_TEXT_DEFAULT OFF)
if(LINUX AND CPU_AARCH64)
set(USE_PRIORITIZED_TEXT_DEFAULT ON)
endif()
cmake_dependent_option(USE_PRIORITIZED_TEXT_FOR_LD "Use prioritized text linker for ld."
"${USE_PRIORITIZED_TEXT_DEFAULT}" "CPU_INTEL OR CPU_AARCH64 OR CPU_POWER" OFF)
# LLVM BOLT post-link binary optimization. Visible on AArch64, x86 and ppc64le.
# Defaults ON for aarch64+linux+CUDA builds that also apply the linker script
# optimization and a suitable llvm-bolt (>=21) is available. Depends on the
# linker script because the committed profiles were collected with it enabled.
function(check_llvm_bolt_version var item)
execute_process(COMMAND ${item} --version OUTPUT_VARIABLE _cmd_out)
string(REGEX MATCH "[0-9]+\.[0-9]+\.[0-9]+" _bolt_version "${_cmd_out}")
if(${_bolt_version} VERSION_LESS 21)
set(${var} FALSE PARENT_SCOPE)
endif()
endfunction()
find_program(LLVM_BOLT_EXECUTABLE NAMES llvm-bolt VALIDATOR check_llvm_bolt_version)
set(USE_LLVM_BOLT_DEFAULT OFF)
if(LINUX AND USE_CUDA AND CPU_AARCH64 AND USE_PRIORITIZED_TEXT_FOR_LD AND LLVM_BOLT_EXECUTABLE)
set(USE_LLVM_BOLT_DEFAULT ON)
endif()
cmake_dependent_option(USE_LLVM_BOLT "Optimize libraries with LLVM BOLT."
"${USE_LLVM_BOLT_DEFAULT}" "CPU_INTEL OR CPU_AARCH64 OR CPU_POWER" OFF)
set(LLVM_BOLT_PROFILES_DIR "" CACHE PATH
"Directory containing LLVM BOLT profiles; empty uses bundled profiles.")
# Enable third party mimalloc library to improve memory allocation performance
# on Windows and AArch64.
set(USE_MIMALLOC_DEFAULT OFF)
if(WIN32 OR (CPU_AARCH64 AND NOT APPLE))
set(USE_MIMALLOC_DEFAULT ON)
endif()
option(USE_MIMALLOC "Use mimalloc" "${USE_MIMALLOC_DEFAULT}")
option(USE_MIMALLOC_ON_MKL "Use mimalloc on MKL" OFF)
if(USE_MIMALLOC_DEFAULT)
# Disable OPT flags for mimalloc on aarch64 as it breaks ARMv8.0
# See https://github.com/pytorch/pytorch/issues/174344
if(CPU_AARCH64)
set(MI_NO_OPT_ARCH ON)
endif()
# Not enable USE_MIMALLOC_ON_MKL due to it caused issue:
# https://github.com/pytorch/pytorch/issues/138994
# Will turn on when we can fix USE_STATIC_MKL lost functionality:
# https://github.com/pytorch/pytorch/pull/138996
# set(USE_MIMALLOC_ON_MKL ON)
endif()
if(USE_CCACHE)
find_program(CCACHE_PROGRAM ccache)
find_program(SCCACHE_EXECUTABLE sccache)
# torch-xpu-ops consumes CMAKE_SYCL_COMPILER_LAUNCHER directly; this does not
# require CMake to provide native SYCL language support.
if(CCACHE_PROGRAM)
foreach(LANG CXX C CUDA SYCL)
set(CMAKE_${LANG}_COMPILER_LAUNCHER
"${CCACHE_PROGRAM}"
CACHE STRING "${LANG} compiler launcher")
endforeach()
elseif(SCCACHE_EXECUTABLE)
foreach(LANG CXX C CUDA SYCL)
set(CMAKE_${LANG}_COMPILER_LAUNCHER
"${SCCACHE_EXECUTABLE}"
CACHE STRING "${LANG} compiler launcher")
endforeach()
else()
message(
STATUS
"Could not find neither ccache nor sccache. Consider installing ccache to speed up compilation."
)
endif()
endif()
# Since TensorPipe does not support Windows, set it to OFF when WIN32 detected
# On Windows platform, if user does not install libuv in build conda env and
# does not set libuv_ROOT environment variable. Set USE_DISTRIBUTED to OFF.
if(WIN32)
set(USE_TENSORPIPE OFF)
message(WARNING "TensorPipe cannot be used on Windows. Set it to OFF")
set(USE_KLEIDIAI OFF)
message(WARNING "KleidiAI cannot be used on Windows. Set it to OFF")
if(USE_DISTRIBUTED AND NOT DEFINED ENV{libuv_ROOT})
find_library(
libuv_tmp_LIBRARY
NAMES uv libuv
HINTS $ENV{CONDA_PREFIX}\\Library $ENV{PREFIX}\\Library
PATH_SUFFIXES lib
NO_DEFAULT_PATH)
if(NOT libuv_tmp_LIBRARY)
set(USE_DISTRIBUTED OFF)
set(USE_GLOO OFF)
message(
WARNING
"Libuv is not installed in current conda env. Set USE_DISTRIBUTED to OFF. "
"Please run command 'conda install -c conda-forge libuv=1.51' to install libuv."
)
else()
set(ENV{libuv_ROOT} ${libuv_tmp_LIBRARY}/../../)
endif()
endif()
endif()
if(USE_GLOO_WITH_OPENSSL)
set(USE_TCP_OPENSSL_LOAD
ON
CACHE STRING "")
endif()
# Linux distributions do not want too many embedded sources, in that sense we
# need to be able to build pytorch with an (almost) empty third_party directory.
# USE_SYSTEM_LIBS is a shortcut variable to toggle all the # USE_SYSTEM_*
# variables on. Individual USE_SYSTEM_* variables can be toggled with
# USE_SYSTEM_LIBS being "OFF".
option(USE_SYSTEM_LIBS "Use all available system-provided libraries." OFF)
option(USE_SYSTEM_CPUINFO "Use system-provided cpuinfo." OFF)
option(USE_SYSTEM_SLEEF "Use system-provided sleef." OFF)
option(USE_SYSTEM_GLOO "Use system-provided gloo." OFF)
option(USE_SYSTEM_FP16 "Use system-provided fp16." OFF)
option(USE_SYSTEM_PYBIND11 "Use system-provided PyBind11." OFF)
option(USE_SYSTEM_PTHREADPOOL "Use system-provided pthreadpool." OFF)
option(USE_SYSTEM_PSIMD "Use system-provided psimd." OFF)
option(USE_SYSTEM_FXDIV "Use system-provided fxdiv." OFF)
option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF)
option(USE_SYSTEM_ONNX "Use system-provided onnx." OFF)
option(USE_SYSTEM_XNNPACK "Use system-provided xnnpack." OFF)
option(USE_SYSTEM_NVTX "Use system-provided nvtx." OFF)
option(USE_GOLD_LINKER "Use ld.gold to link" OFF)
if(USE_SYSTEM_LIBS)
set(USE_SYSTEM_CPUINFO ON)
set(USE_SYSTEM_SLEEF ON)
set(USE_SYSTEM_GLOO ON)
set(BUILD_CUSTOM_PROTOBUF OFF)
set(USE_SYSTEM_EIGEN_INSTALL ON)
set(USE_SYSTEM_FP16 ON)
set(USE_SYSTEM_PTHREADPOOL ON)
set(USE_SYSTEM_PSIMD ON)
set(USE_SYSTEM_FXDIV ON)
set(USE_SYSTEM_BENCHMARK ON)
set(USE_SYSTEM_ONNX ON)
set(USE_SYSTEM_XNNPACK ON)
set(USE_SYSTEM_PYBIND11 ON)
if(USE_NCCL)
set(USE_SYSTEM_NCCL ON)
endif()
set(USE_SYSTEM_NVTX ON)
endif()
# /Z7 override option When generating debug symbols, CMake default to use the
# flag /Zi. However, it is not compatible with sccache. So we rewrite it off.
# But some users don't use sccache; this override is for them.
cmake_dependent_option(
MSVC_Z7_OVERRIDE
"Work around sccache bug by replacing /Zi and /ZI with /Z7 when using MSVC (if you are not using sccache, you can turn this OFF)"
ON
"MSVC"
OFF)
if(NOT USE_SYSTEM_ONNX)
set(ONNX_NAMESPACE
"onnx_torch"
CACHE
STRING
"A namespace for ONNX; needed to build with other frameworks that share ONNX."
)
else()
set(ONNX_NAMESPACE
"onnx"
CACHE
STRING
"A namespace for ONNX; needed to build with other frameworks that share ONNX."
)
endif()
set(SELECTED_OP_LIST
""
CACHE
STRING
"Path to the yaml file that contains the list of operators to include for custom build. Include all operators by default."
)
option(
STATIC_DISPATCH_BACKEND
"Name of the backend for which static dispatch code is generated, e.g.: CPU."
"")
option(
USE_LIGHTWEIGHT_DISPATCH
"Enable codegen unboxing for ATen ops, need to work with static dispatch in order to work properly."
OFF)
if(USE_LIGHTWEIGHT_DISPATCH AND NOT STATIC_DISPATCH_BACKEND)
message(
FATAL_ERROR
"Need to enable static dispatch after enabling USE_LIGHTWEIGHT_DISPATCH.")
endif()
option(TRACING_BASED
"Master flag to build Lite Interpreter with tracing build option" OFF)
# This is a fix for a rare build issue on Ubuntu: symbol lookup error:
# miniconda3/envs/pytorch-py3.7/lib/libmkl_intel_lp64.so: undefined symbol:
# mkl_blas_dsyrk
# https://software.intel.com/en-us/articles/symbol-lookup-error-when-linking-intel-mkl-with-gcc-on-ubuntu
if(LINUX)
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-as-needed")
set(ENV_LDFLAGS "$ENV{LDFLAGS}")
string(STRIP "${ENV_LDFLAGS}" ENV_LDFLAGS)
# Do not append linker flags passed via env var if they already there
if(NOT ${CMAKE_SHARED_LINKER_FLAGS} MATCHES "${ENV_LDFLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} ${ENV_LDFLAGS}")
endif()
endif()
if(MSVC)
# MSVC by default does not apply the correct __cplusplus version as specified
# by the C++ standard because MSVC is not a completely compliant
# implementation. This option forces MSVC to use the appropriate value given
# the requested --std option. This fixes a compilation issue mismatch between
# GCC/Clang and MSVC.
#
# See: *
# https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170
# * https://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler /Zc:__cplusplus -Xcompiler /Zc:preprocessor")
# Since VS 2022 17.10 (MSVC 14.40) std::mutex has a constexpr constructor
# that zero-initializes the mutex and relies on msvcp140.dll >= 14.40 to
# cope with that layout. If an older msvcp140.dll is already loaded in the
# process (stale VC redist, or a copy bundled by another package such as
# PyQt), the first lock during c10.dll static init faults inside DllMain and
# `import torch` fails with WinError 1114. Opt back into the classic
# constructor so the wheels work with any 14.x runtime.
# See https://github.com/pytorch/pytorch/issues/169429 and
# https://github.com/microsoft/STL/issues/4730
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR")
set(CMAKE_NINJA_CMCLDEPS_RC OFF)
if(MSVC_Z7_OVERRIDE)
# CMake set debug flags to use /Z7
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug,RelWithDebInfo>:Embedded>")
endif()
foreach(
flag_var
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${CAFFE2_USE_MSVC_STATIC_RUNTIME})
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
else()
if(${flag_var} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${flag_var} "${${flag_var}}")
endif()
endif()
# /bigobj increases number of sections in .obj file, which is needed to link
# against libraries in Python 2.7 under Windows For Visual Studio
# generators, if /MP is not added, then we may need to add /MP to the flags.
# For other generators like ninja, we don't need to add /MP because it is
# already handled by the generator itself.
if(CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT ${flag_var} MATCHES
"/MP")
set(${flag_var} "${${flag_var}} /MP /bigobj")
else()
set(${flag_var} "${${flag_var}} /bigobj")
endif()
endforeach(flag_var)
foreach(flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL)
if(${flag_var} MATCHES "/Z[iI7]")
string(REGEX REPLACE "/Z[iI7]" "" ${flag_var} "${${flag_var}}")
endif()
endforeach(flag_var)
foreach(
flag_var
CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_SHARED_LINKER_FLAGS_DEBUG
CMAKE_STATIC_LINKER_FLAGS_DEBUG
CMAKE_EXE_LINKER_FLAGS_DEBUG
CMAKE_MODULE_LINKER_FLAGS_DEBUG)
# Switch off incremental linking in debug/relwithdebinfo builds
if(${flag_var} MATCHES "/INCREMENTAL" AND NOT ${flag_var} MATCHES
"/INCREMENTAL:NO")
string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" ${flag_var}
"${${flag_var}}")
endif()
endforeach(flag_var)
foreach(flag_var CMAKE_SHARED_LINKER_FLAGS CMAKE_STATIC_LINKER_FLAGS
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
string(APPEND ${flag_var} " /ignore:4049 /ignore:4217 /ignore:4099")
endforeach(flag_var)
foreach(flag_var CMAKE_SHARED_LINKER_FLAGS)
# https://github.com/pytorch/pytorch/issues/91933: Don't set the manifest
# filename explicitly helps fix the linker error when linking
# torch_python.dll. The manifest file would still be there in the correct
# format torch_python.dll.manifest
if(${flag_var} MATCHES "/MANIFESTFILE:.*\\.manifest")
string(REGEX REPLACE "/MANIFESTFILE:.*\\.manifest" "" ${flag_var}
"${${flag_var}}")
endif()
endforeach(flag_var)
# Try harder
string(APPEND CMAKE_CUDA_FLAGS " -Xcompiler /w -w")
string(APPEND CMAKE_CXX_FLAGS " /FS")
string(APPEND CMAKE_CUDA_FLAGS " -Xcompiler /FS")
endif(MSVC)
string(APPEND CMAKE_CUDA_FLAGS " -Xfatbin -compress-all")
# Set linker max-page-size to 64KiB on AArch64 Linux
# And stub group size to 32MiB to workaround R_AARCH64_CALL26
# relocation errors
if(LINUX AND CPU_AARCH64)
add_link_options_if_supported("-z,max-page-size=0x10000")
add_link_options_if_supported("--stub-group-size=0x2000000")
endif()
# Set INTERN_BUILD_MOBILE for all mobile builds. Components that are not
# applicable to mobile are disabled by this variable. Setting
# `BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN` environment variable can force it
# to do mobile build with host toolchain - which is useful for testing purpose.
if(ANDROID
OR IOS
OR DEFINED ENV{BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN})
set(INTERN_BUILD_MOBILE ON)
message(WARNING "INTERN_BUILD_MOBILE is on, disabling BUILD_LAZY_TS_BACKEND")
set(BUILD_LAZY_TS_BACKEND OFF)
set(USE_KLEIDIAI OFF)
message(WARNING "KleidiAI cannot be used on Mobile builds. Set it to OFF")
# Set -ffunction-sections and -fdata-sections so that each method has its own
# text section. This allows the linker to remove unused section when the flag
# -Wl,-gc-sections is provided at link time.
string(APPEND CMAKE_CXX_FLAGS " -ffunction-sections")
string(APPEND CMAKE_C_FLAGS " -ffunction-sections")
string(APPEND CMAKE_CXX_FLAGS " -fdata-sections")
string(APPEND CMAKE_C_FLAGS " -fdata-sections")
# Please note that the use of the following flags is required when linking
# against libtorch_cpu.a for mobile builds. -Wl,--whole-archive -ltorch_cpu
# -Wl,--no-whole-archive
#
# This allows global constructors to be included and run. Global constructors
# are used for operator/kernel registration with the PyTorch Dispatcher.
if(DEFINED ENV{BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN})
# C10_MOBILE is derived from Android/iOS toolchain macros in
# c10/macros/Macros.h, so it needs to be explicitly set here.
string(APPEND CMAKE_CXX_FLAGS " -DC10_MOBILE")
endif()
if(DEFINED ENV{PYTORCH_MOBILE_TRIM_DISPATCH_KEY_SET})
# If PYTORCH_MOBILE_TRIM_DISPATCH_KEY_SET is defined (env var), then define
# C10_MOBILE_TRIM_DISPATCH_KEYS, which limits the number of dispatch keys in
# OperatorEntry::dispatchTable_ to reduce peak memory during library
# initialization.
string(APPEND CMAKE_CXX_FLAGS " -DC10_MOBILE_TRIM_DISPATCH_KEYS")
endif()
endif()
# INTERN_BUILD_ATEN_OPS is used to control whether to build ATen/TH operators.
set(INTERN_BUILD_ATEN_OPS ON)
if(NOT DEFINED USE_BLAS)
set(USE_BLAS ON)
endif()
# Prioritized Text Linker Optimization
if(USE_PRIORITIZED_TEXT_FOR_LD)
append_cxx_flag_if_supported("-ffunction-sections" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-fdata-sections" CMAKE_CXX_FLAGS)
append_c_flag_if_supported("-ffunction-sections" CMAKE_C_FLAGS)
append_c_flag_if_supported("-fdata-sections" CMAKE_C_FLAGS)
set(LINKER_SCRIPT_FILE_IN "${CMAKE_SOURCE_DIR}/cmake/prioritized_text.txt")
set(LINKER_SCRIPT_FILE_OUT "${CMAKE_SOURCE_DIR}/cmake/linker_script.ld")
set(LINKER_SCRIPT_PATH "${CMAKE_SOURCE_DIR}/tools/setup_helpers/generate_linker_script.py")
add_custom_command(
OUTPUT "${LINKER_SCRIPT_FILE_OUT}"
COMMAND ${Python_EXECUTABLE}
"${LINKER_SCRIPT_PATH}"
--filein "${LINKER_SCRIPT_FILE_IN}"
--fout "${LINKER_SCRIPT_FILE_OUT}"
DEPENDS "${LINKER_SCRIPT_PATH}"
"${LINKER_SCRIPT_FILE_IN}"
VERBATIM
)
add_custom_target(generate_linker_script DEPENDS "${LINKER_SCRIPT_FILE_OUT}")
elseif(LINUX AND CPU_AARCH64)
message(WARNING [[
It is strongly recommend to enable linker script optimization for all AArch64 Linux builds.
To do so please export USE_PRIORITIZED_TEXT_FOR_LD=1
]])
endif()
if(USE_LLVM_BOLT)
# In addition to the flags below, BOLT also requires --emit-relocs for the
# linker. Since --emit-relocs increases the binary size, we limit the use to
# libraries that are actually optimized by BOLT instead of applying globally
# (see torch_optimize_layout_if_enabled)
list(APPEND _bolt_flags
"-fno-plt" # shows a perf boost in NVIDIA internal CI
"-fno-reorder-blocks-and-partition" # required by BOLT when using GCC>=8
)
foreach(_flag IN LISTS _bolt_flags)
append_c_flag_if_supported("${_flag}" CMAKE_C_FLAGS)
append_cxx_flag_if_supported("${_flag}" CMAKE_CXX_FLAGS)
endforeach()
if(NOT LLVM_BOLT_PROFILES_DIR)
# Extract the committed BOLT profiles for the build-time optimization
# step (torch_optimize_layout_if_enabled in cmake/public/utils.cmake).
set(LLVM_BOLT_PROFILES_DIR "${CMAKE_BINARY_DIR}/bolt_profiles")
file(REMOVE_RECURSE "${LLVM_BOLT_PROFILES_DIR}")
file(ARCHIVE_EXTRACT INPUT "${PROJECT_SOURCE_DIR}/cmake/bolt_profiles/torch.tar.zst"
DESTINATION "${LLVM_BOLT_PROFILES_DIR}")
endif()
add_custom_target(bolt_profile_quality_summary ALL
COMMAND "${Python_EXECUTABLE}"
"${PROJECT_SOURCE_DIR}/tools/bolt_profile_quality.py"
"$<TARGET_PROPERTY:bolt_profile_quality_summary,SCRIPT_ARGS>"
COMMENT "Summarizing BOLT profile quality"
VERBATIM
USES_TERMINAL
COMMAND_EXPAND_LISTS
)
endif()
# Build libtorch mobile library, which contains ATen/TH ops and native support
# for TorchScript model, but doesn't contain not-yet-unified caffe2 ops;
if(INTERN_BUILD_MOBILE)
if(NOT BUILD_SHARED_LIBS AND NOT "${SELECTED_OP_LIST}" STREQUAL "")
string(APPEND CMAKE_CXX_FLAGS " -DNO_EXPORT")
endif()
if(BUILD_MOBILE_AUTOGRAD)
set(INTERN_DISABLE_AUTOGRAD OFF)
else()
set(INTERN_DISABLE_AUTOGRAD ON)
endif()
set(BUILD_PYTHON OFF)
set(USE_DISTRIBUTED OFF)
set(NO_API ON)
set(USE_FBGEMM OFF)
set(INTERN_DISABLE_ONNX ON)
if(USE_BLAS)
set(INTERN_USE_EIGEN_BLAS ON)
else()
set(INTERN_USE_EIGEN_BLAS OFF)
endif()
# Disable developing mobile interpreter for actual mobile build. Enable it
# elsewhere to capture build error.
set(INTERN_DISABLE_MOBILE_INTERP ON)
endif()
# ---[ Version numbers for generated libraries
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS version.txt)
file(READ version.txt TORCH_DEFAULT_VERSION)
# Strip trailing newline
string(REGEX REPLACE "\n$" "" TORCH_DEFAULT_VERSION "${TORCH_DEFAULT_VERSION}")
if("${TORCH_DEFAULT_VERSION} " STREQUAL " ")
message(WARNING "Could not get version from base 'version.txt'")
# If we can't get the version from the version file we should probably set it
# to something non-sensical like 0.0.0
set(TORCH_DEFAULT_VERSION "0.0.0")
endif()
set(TORCH_BUILD_VERSION
"${TORCH_DEFAULT_VERSION}"
CACHE STRING "Torch build version" FORCE)
if(DEFINED ENV{PYTORCH_BUILD_VERSION})
set(TORCH_BUILD_VERSION
"$ENV{PYTORCH_BUILD_VERSION}"
CACHE STRING "Torch build version" FORCE)
endif()
caffe2_parse_version_str(TORCH ${TORCH_BUILD_VERSION})
set(TORCH_SOVERSION "${TORCH_VERSION_MAJOR}.${TORCH_VERSION_MINOR}")
# ---[ CMake scripts + modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
# ---[ CMake build directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
enable_testing()
# ---[ Build variables set within the cmake tree
include(cmake/BuildVariables.cmake)
set(CAFFE2_ALLOWLIST
""
CACHE STRING "A allowlist file of files that one should build.")
# Set default build type
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set - defaulting to Release")
set(CMAKE_BUILD_TYPE
"Release"
CACHE
STRING
"Choose the type of build from: Debug Release RelWithDebInfo MinSizeRel Coverage."
FORCE)
endif()
# The below means we are cross compiling for arm64 or x86_64 on MacOSX
if(NOT IOS
AND CMAKE_SYSTEM_NAME STREQUAL "Darwin"
AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$")
set(CROSS_COMPILING_MACOSX TRUE)
# We need to compile a universal protoc to not fail protobuf build We set
# CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY (vs executable) to succeed
# the cmake compiler check for cross-compiling
set(protoc_build_command
"./scripts/build_host_protoc.sh --other-flags -DCMAKE_OSX_ARCHITECTURES=\"x86_64;arm64\" -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY -DCMAKE_C_COMPILER_WORKS=1 -DCMAKE_CXX_COMPILER_WORKS=1 -DCMAKE_POLICY_VERSION_MINIMUM=3.5"
)
# We write to a temp scriptfile because CMake COMMAND dislikes double quotes
# in commands
file(WRITE ${PROJECT_SOURCE_DIR}/tmp_protoc_script.sh
"#!/bin/bash\n${protoc_build_command}")
file(
COPY ${PROJECT_SOURCE_DIR}/tmp_protoc_script.sh
DESTINATION ${PROJECT_SOURCE_DIR}/scripts/
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
execute_process(
COMMAND ./scripts/tmp_protoc_script.sh
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE BUILD_HOST_PROTOC_RESULT)
file(REMOVE ${PROJECT_SOURCE_DIR}/tmp_protoc_script.sh
${PROJECT_SOURCE_DIR}/scripts/tmp_protoc_script.sh)
if(NOT BUILD_HOST_PROTOC_RESULT EQUAL "0")
message(FATAL_ERROR "Could not compile universal protoc.")
endif()
set(PROTOBUF_PROTOC_EXECUTABLE
"${PROJECT_SOURCE_DIR}/build_host_protoc/bin/protoc")
set(CAFFE2_CUSTOM_PROTOC_EXECUTABLE
"${PROJECT_SOURCE_DIR}/build_host_protoc/bin/protoc")
endif()
# ---[ Misc checks to cope with various compiler modes
include(cmake/MiscCheck.cmake)
# External projects
include(ExternalProject)
# ---[ Dependencies ---[ FBGEMM doesn't work on x86 32bit and
# CMAKE_SYSTEM_PROCESSOR thinks its 64bit
if(USE_FBGEMM AND (NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|x86_64|aarch64)$"
OR CMAKE_SIZEOF_VOID_P EQUAL 4))
message(WARNING
"64-bit x86 or aarch64 required for FBGEMM. "
"Not compiling with FBGEMM. "
"Turn this warning off by USE_FBGEMM=OFF.")
set(USE_FBGEMM OFF)
endif()
set(BUILD_ONEDNN_GRAPH OFF)
if(MSVC)
# The source code is in utf-8 encoding
append_cxx_flag_if_supported("/utf-8" CMAKE_CXX_FLAGS)
endif()
# Note for ROCM platform: 1. USE_ROCM is always ON until
# include(cmake/Dependencies.cmake) 2. USE_CUDA will become OFF during
# re-configuration Truth Table: CUDA 1st pass: USE_CUDA=True;USE_ROCM=True,
# FLASH evaluates to ON by default CUDA 2nd pass: USE_CUDA=True;USE_ROCM=False,
# FLASH evaluates to ON by default ROCM 1st pass: USE_CUDA=True;USE_ROCM=True,
# FLASH evaluates to ON by default ROCM 2nd pass: USE_CUDA=False;USE_ROCM=True,
# FLASH evaluates to ON by default CPU 1st pass: USE_CUDA=False(Cmd
# Option);USE_ROCM=True, FLASH evaluates to OFF by default CPU 2nd pass:
# USE_CUDA=False(Cmd Option);USE_ROCM=False, FLASH evaluates to OFF by default
# Thus we cannot tell ROCM 2nd pass and CPU 1st pass
#
# The only solution is to include(cmake/Dependencies.cmake), and defer the
# aotriton build decision later.
include(cmake/Dependencies.cmake)
cmake_dependent_option(
USE_FLASH_ATTENTION
"Whether to build the flash_attention kernel for scaled dot product attention.\
Will be disabled if not supported by the platform"
ON
"USE_CUDA OR USE_ROCM"
OFF)
# Flash Attention needs sm80+; disable it if no resolved arch reaches that.
if(USE_FLASH_ATTENTION AND USE_CUDA AND NOT USE_ROCM)
string(REPLACE " " ";" _flash_attn_archs "${NVCC_FLAGS_EXTRA_readable}")
set(_flash_attn_has_sm80 OFF)
foreach(_arch IN LISTS _flash_attn_archs)
if(_arch MATCHES "^(sm|compute)_([0-9]+)" AND CMAKE_MATCH_2 GREATER_EQUAL 80)
set(_flash_attn_has_sm80 ON)
endif()
endforeach()
if(_flash_attn_archs AND NOT _flash_attn_has_sm80)
message(STATUS
"Disabling USE_FLASH_ATTENTION: it needs a CUDA architecture >= 8.0, "
"but the resolved CUDA architectures are '${_flash_attn_archs}'")
set(USE_FLASH_ATTENTION OFF)
endif()
endif()
IF(USE_ROCM AND ("gfx942" IN_LIST PYTORCH_ROCM_ARCH OR "gfx950" IN_LIST PYTORCH_ROCM_ARCH))
message(WARNING "Setting USE_MSLK for gfx942/gfx950 to ON by default, doing ROCM build")
set(USE_MSLK_DEFAULT ON)
elseif(USE_CUDA AND "$ENV{TORCH_CUDA_ARCH_LIST}" MATCHES "10.0" AND CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 12.8 AND NOT WIN32)
message(STATUS "Setting USE_MSLK to ON by default , doing CUDA build for SM100a")
set(USE_MSLK_DEFAULT ON)
else()
set(USE_MSLK_DEFAULT OFF)
endif()
cmake_dependent_option(
USE_MSLK
"Whether to build MSLK quantized GEMM kernels.\
Will be disabled if not supported by the platform"
${USE_MSLK_DEFAULT}
"(USE_CUDA AND NOT MSVC) OR USE_ROCM"
OFF)
# CAVEAT: Again, Flash Attention2 will error while building for sm52 while Mem
# Eff Attention won't
cmake_dependent_option(
USE_MEM_EFF_ATTENTION
"Enable memory-efficient attention for scaled dot product attention.\
Will be disabled if not supported by the platform" ON
"USE_CUDA OR USE_ROCM" OFF)
#
# Cannot be put into Dependencies.cmake due circular dependency:
# USE_FLASH_ATTENTION -> USE_ROCM -> Dependencies.cmake -> aotriton.cmake
#
if(USE_ROCM)
if(USE_FLASH_ATTENTION OR USE_MEM_EFF_ATTENTION)
include(cmake/External/aotriton.cmake)
endif()
endif()
if(DEBUG_CUDA)
string(APPEND CMAKE_CUDA_FLAGS_DEBUG " -lineinfo")
string(APPEND CMAKE_CUDA_FLAGS_RELWITHDEBINFO " -lineinfo")
endif(DEBUG_CUDA)
if(USE_FBGEMM)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_FBGEMM")
endif()
if(USE_MSLK)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_MSLK")
endif()
if(USE_PYTORCH_QNNPACK)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_QNNPACK")
endif()
# Enable sleef on macOS with Apple silicon by default
if((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") AND ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64"))
message(STATUS "Running on macOS with Apple silicon")
string(APPEND CMAKE_CXX_FLAGS " -DAT_BUILD_ARM_VEC256_WITH_SLEEF")
add_compile_definitions(AT_BUILD_ARM_VEC256_WITH_SLEEF)
endif()
# Enable sleef on Arm(R) architecture by default (except Android)
if((NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Android")
AND("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "aarch64"))
string(APPEND CMAKE_CXX_FLAGS " -DAT_BUILD_ARM_VEC256_WITH_SLEEF")
add_compile_definitions(AT_BUILD_ARM_VEC256_WITH_SLEEF)
endif()
if(USE_XNNPACK)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_XNNPACK")
endif()
if(USE_VULKAN)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN")
string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_API")
if(USE_VULKAN_FP16_INFERENCE)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_FP16_INFERENCE")
endif()
if(USE_VULKAN_RELAXED_PRECISION)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_RELAXED_PRECISION")
endif()
endif()
if(BUILD_LITE_INTERPRETER)
string(APPEND CMAKE_CXX_FLAGS " -DBUILD_LITE_INTERPRETER")
endif()
if(TRACING_BASED)
string(APPEND CMAKE_CXX_FLAGS " -DTRACING_BASED")
endif()
if(USE_PYTORCH_METAL)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_METAL")
endif()
if(USE_PYTORCH_METAL_EXPORT)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_METAL_EXPORT")
endif()
if(USE_SOURCE_DEBUG_ON_MOBILE)
string(APPEND CMAKE_CXX_FLAGS " -DSYMBOLICATE_MOBILE_DEBUG_HANDLE")
endif()
if(BUILD_LITE_INTERPRETER AND USE_LITE_INTERPRETER_PROFILER)
string(APPEND CMAKE_CXX_FLAGS " -DEDGE_PROFILER_USE_KINETO")
endif()
if(USE_COREML_DELEGATE)
string(APPEND CMAKE_CXX_FLAGS " -DUSE_COREML_DELEGATE")
endif()
# ---[ Allowlist file if allowlist is specified
include(cmake/Allowlist.cmake)
# ---[ Build flags Re-include to override append_cxx_flag_if_supported from
# third_party/FBGEMM
include(cmake/public/utils.cmake)
if(USE_COLORIZE_OUTPUT)
set(CMAKE_COLOR_DIAGNOSTICS ON)
endif()
if(NOT MSVC)
string(APPEND CMAKE_CXX_FLAGS " -O2 -fPIC")
# This prevents use of `c10::optional`, `c10::nullopt` etc within the codebase
string(APPEND CMAKE_CXX_FLAGS " -DC10_NODEPRECATED")
string(APPEND CMAKE_CUDA_FLAGS " -DC10_NODEPRECATED")
string(APPEND CMAKE_OBJCXX_FLAGS " -DC10_NODEPRECATED")
# Eigen fails to build with some versions, so convert this to a warning
# Details at http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1459
string(APPEND CMAKE_CXX_FLAGS " -Wall")
string(APPEND CMAKE_CXX_FLAGS " -Wextra")
append_cxx_flag_if_supported("-Werror=return-type" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=non-virtual-dtor" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=braced-scalar-init" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=range-loop-construct" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=bool-operation" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wnarrowing" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-missing-field-initializers"
CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unknown-pragmas" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-unused-parameter" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-strict-overflow" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-strict-aliasing" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-stringop-overflow" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wvla-extension" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wsuggest-override" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wnewline-eof" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Winconsistent-missing-override"
CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Winconsistent-missing-destructor-override"
CMAKE_CXX_FLAGS)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_CXX_FLAGS " -Wno-pass-failed")
endif()
if(CMAKE_COMPILER_IS_GNUCXX)
# Suppress "The ABI for passing parameters with 64-byte alignment has
# changed in GCC 4.6"
string(APPEND CMAKE_CXX_FLAGS " -Wno-psabi")
endif()
# Use ld.gold if available, fall back to ld.bfd (the default ld) if not
if(USE_GOLD_LINKER)
if(USE_DISTRIBUTED AND USE_MPI)
# Same issue as here with default MPI on Ubuntu
# https://bugs.launchpad.net/ubuntu/+source/deal.ii/+bug/1841577
message(WARNING "Refusing to use gold when USE_MPI=1")
else()
execute_process(
COMMAND "${CMAKE_C_COMPILER}" -fuse-ld=gold -Wl,--version
ERROR_QUIET
OUTPUT_VARIABLE LD_VERSION)
if(NOT "${LD_VERSION}" MATCHES "GNU gold")
message(
WARNING
"USE_GOLD_LINKER was set but ld.gold isn't available, turning it off"
)
set(USE_GOLD_LINKER OFF)
else()
message(STATUS "ld.gold is available, using it to link")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_MODULE_LINKER_FLAGS
"${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=gold")
endif()
endif()
endif()
append_cxx_flag_if_supported("-Wno-error=old-style-cast" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wconstant-conversion" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-aligned-allocation-unavailable"
CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Qunused-arguments" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-faligned-new" CMAKE_CXX_FLAGS)
if(WERROR)
append_cxx_flag_if_supported("-Werror" CMAKE_CXX_FLAGS)
if(NOT COMPILER_SUPPORT_WERROR)
set(WERROR FALSE)
endif()
endif()
append_cxx_flag_if_supported("-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-fstandalone-debug" CMAKE_CXX_FLAGS_DEBUG)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_BUILD_TYPE MATCHES Debug)
message(WARNING "Applying -Og optimization for aarch64 GCC debug build to workaround ICE")
endif()
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer -Og")
else()
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
endif()
# aarch64 C++ stack unwinding uses frame-pointer chain walking, so frame
# pointers must be present in all build types. The cost is negligible on
# aarch64 (31 GPRs vs x86-64's 16, so dedicating x29 rarely spills).
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
append_cxx_flag_if_supported("-fno-omit-frame-pointer" CMAKE_CXX_FLAGS)
endif()
append_cxx_flag_if_supported("-fno-math-errno" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-fno-trapping-math" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Werror=format" CMAKE_CXX_FLAGS)
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13)
append_cxx_flag_if_supported("-Wno-dangling-reference" CMAKE_CXX_FLAGS)
append_cxx_flag_if_supported("-Wno-error=dangling-reference" CMAKE_CXX_FLAGS)
endif()
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12)
# Suppress GCC's warning without changing the value of
# std::hardware_destructive_interference_size in public headers.
append_cxx_flag_if_supported("-Wno-interference-size" CMAKE_CXX_FLAGS)
endif()
# needed for compat with newer versions of clang that use C++20 mangling rules
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 18)
append_cxx_flag_if_supported("-fclang-abi-compat=17" CMAKE_CXX_FLAGS)
endif()
else()
# Define export functions for AOTI.
add_compile_definitions(EXPORT_AOTI_FUNCTIONS)
# skip unwanted includes from windows.h
add_compile_definitions(WIN32_LEAN_AND_MEAN)
# Windows SDK broke compatibility since version 25131, but introduced this
# define for backward compatibility.
add_compile_definitions(_UCRT_LEGACY_INFINITY)
# disable min/max macros
add_compile_definitions(NOMINMAX)
# Turn off these warnings on Windows. destructor was implicitly defined as
# delete
append_cxx_flag_if_supported("/wd4624" CMAKE_CXX_FLAGS)
# unknown pragma
append_cxx_flag_if_supported("/wd4068" CMAKE_CXX_FLAGS)
# unexpected tokens following preprocessor directive - expected a newline
append_cxx_flag_if_supported("/wd4067" CMAKE_CXX_FLAGS)
# conversion from 'size_t' to 'unsigned int', possible loss of data
append_cxx_flag_if_supported("/wd4267" CMAKE_CXX_FLAGS)
# no suitable definition provided for explicit template instantiation request
append_cxx_flag_if_supported("/wd4661" CMAKE_CXX_FLAGS)
# recursive on all control paths, function will cause runtime stack overflow
append_cxx_flag_if_supported("/wd4717" CMAKE_CXX_FLAGS)
# conversion from '_Ty' to '_Ty', possible loss of data
append_cxx_flag_if_supported("/wd4244" CMAKE_CXX_FLAGS)
# unsafe use of type 'bool' in operation
append_cxx_flag_if_supported("/wd4804" CMAKE_CXX_FLAGS)
# inconsistent dll linkage
append_cxx_flag_if_supported("/wd4273" CMAKE_CXX_FLAGS)
endif()
# Add code coverage flags to supported compilers
if(USE_CPP_CODE_COVERAGE)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path")
string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path")
string(APPEND CMAKE_OBJCXX_FLAGS " --coverage -fprofile-abs-path")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
string(APPEND CMAKE_CXX_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
string(APPEND CMAKE_OBJCXX_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
else()
message(
FATAL_ERROR
"Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported")
endif()
endif()
if(APPLE)
if(USE_MPS)
string(APPEND CMAKE_OBJCXX_FLAGS " -DUSE_MPS -fno-objc-arc")
string(APPEND CMAKE_CXX_FLAGS " -DUSE_MPS")
string(
APPEND
CMAKE_SHARED_LINKER_FLAGS
" -weak_framework Foundation -weak_framework MetalPerformanceShaders -weak_framework MetalPerformanceShadersGraph -weak_framework Metal -weak_framework IOKit"
)
# To suppress MPSGraph availability warnings
append_cxx_flag_if_supported("-Wno-unguarded-availability-new"
CMAKE_OBJCXX_FLAGS)
endif()
append_cxx_flag_if_supported("-Wno-missing-braces" CMAKE_CXX_FLAGS)
endif()
if(EMSCRIPTEN)
string(
APPEND
CMAKE_CXX_FLAGS
" -Wno-implicit-function-declaration -DEMSCRIPTEN -s DISABLE_EXCEPTION_CATCHING=0"
)
endif()
append_cxx_flag_if_supported("-Wno-stringop-overflow" CMAKE_CXX_FLAGS)
if(ANDROID AND (NOT ANDROID_DEBUG_SYMBOLS))
if(CMAKE_COMPILER_IS_GNUCXX)
string(APPEND CMAKE_CXX_FLAGS " -s")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_CXX_FLAGS " -g0")
else()
string(APPEND CMAKE_EXE_LINKER_FLAGS " -s")
endif()
endif()
if(NOT APPLE AND UNIX)
list(APPEND Caffe2_DEPENDENCY_LIBS dl)
endif()
# Prefix path to Caffe2 headers. If a directory containing installed Caffe2
# headers was inadvertently added to the list of include directories, prefixing
# PROJECT_SOURCE_DIR means this source tree always takes precedence.
include_directories(BEFORE ${PROJECT_SOURCE_DIR})
# Prefix path to generated Caffe2 headers. These need to take precedence over
# their empty counterparts located in PROJECT_SOURCE_DIR.
include_directories(BEFORE ${PROJECT_BINARY_DIR})
include_directories(BEFORE ${PROJECT_SOURCE_DIR}/aten/src/)
include_directories(BEFORE ${CMAKE_BINARY_DIR}/aten/src/)
if(USE_MIMALLOC)
set(MI_OVERRIDE OFF)
set(MI_BUILD_SHARED OFF)
set(MI_BUILD_OBJECT OFF)
set(MI_BUILD_TESTS OFF)
add_compile_definitions(USE_MIMALLOC)
add_subdirectory(third_party/mimalloc)
include_directories(third_party/mimalloc/include)
endif()
if(USE_MIMALLOC AND USE_MIMALLOC_ON_MKL)
add_compile_definitions(USE_MIMALLOC_ON_MKL)
endif()
# ---[ Main build
add_subdirectory(torch/headeronly) # headeronly headers
add_subdirectory(c10)
add_subdirectory(caffe2)
# ---[ CMake related files Uninstall option.
if(NOT TARGET caffe2_uninstall)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake IMMEDIATE @ONLY)
add_custom_target(
caffe2_uninstall COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
# ---[ Make configuration files for cmake to allow dependent libraries easier
# access to Caffe2.
if((NOT USE_GLOG)
OR(NOT USE_GFLAGS)
OR BUILD_CUSTOM_PROTOBUF)
message(WARNING "Generated cmake files are only fully tested if one builds "
"with system glog, gflags, and protobuf. Other settings may "
"generate files that are not well tested.")
endif()
# Note(jiayq): when building static libraries, all PRIVATE dependencies will
# also become interface libraries, and as a result if there are any dependency
# libraries that are not exported, the following install export script will
# fail. As a result, we will only provide the targets cmake files for shared lib
# installation. For more info, read:
# https://cmake.org/pipermail/cmake/2016-May/063400.html
if(BUILD_SHARED_LIBS)
configure_file(${PROJECT_SOURCE_DIR}/cmake/Caffe2Config.cmake.in
${PROJECT_BINARY_DIR}/Caffe2Config.cmake @ONLY)
install(
FILES ${PROJECT_BINARY_DIR}/Caffe2Config.cmake
DESTINATION share/cmake/Caffe2
COMPONENT dev)
install(
FILES ${PROJECT_SOURCE_DIR}/cmake/public/cuda.cmake
${PROJECT_SOURCE_DIR}/cmake/public/xpu.cmake
${PROJECT_SOURCE_DIR}/cmake/public/glog.cmake
${PROJECT_SOURCE_DIR}/cmake/public/gflags.cmake
${PROJECT_SOURCE_DIR}/cmake/public/mkl.cmake
${PROJECT_SOURCE_DIR}/cmake/public/mkldnn.cmake
${PROJECT_SOURCE_DIR}/cmake/public/protobuf.cmake
${PROJECT_SOURCE_DIR}/cmake/public/utils.cmake
${PROJECT_SOURCE_DIR}/cmake/public/LoadHIP.cmake
DESTINATION share/cmake/Caffe2/public
COMPONENT dev)
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/cmake/Modules_CUDA_fix
DESTINATION share/cmake/Caffe2/
COMPONENT dev)
install(
FILES ${PROJECT_SOURCE_DIR}/cmake/Modules/FindCUDAToolkit.cmake
DESTINATION share/cmake/Caffe2/
COMPONENT dev)
install(
FILES ${PROJECT_SOURCE_DIR}/cmake/Modules/FindCUSPARSELT.cmake
DESTINATION share/cmake/Caffe2/
COMPONENT dev)
install(
FILES ${PROJECT_SOURCE_DIR}/cmake/Modules/FindCUDSS.cmake
DESTINATION share/cmake/Caffe2/
COMPONENT dev)
install(
FILES ${PROJECT_SOURCE_DIR}/cmake/Modules/FindSYCLToolkit.cmake
DESTINATION share/cmake/Caffe2/
COMPONENT dev)
if(NOT BUILD_LIBTORCHLESS)
install(
EXPORT Caffe2Targets
DESTINATION share/cmake/Caffe2
FILE Caffe2Targets.cmake
COMPONENT dev)
endif()
else()
message(WARNING "Generated cmake files are only available when building "
"shared libs.")
endif()
# ---[ Binaries Binaries will be built after the Caffe2 main libraries and the
# modules are built. For the binaries, they will be linked to the Caffe2 main
# libraries, as well as all the modules that are built with Caffe2 (the ones
# built in the previous Modules section above).
if(BUILD_BINARY)
add_subdirectory(binaries)
endif()
# ---[ JNI
if(BUILD_JNI)
if(NOT MSVC)
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-variable")
endif()
set(BUILD_LIBTORCH_WITH_JNI 1)
set(FBJNI_SKIP_TESTS 1)
add_subdirectory(android/pytorch_android)
endif()
# Parse custom debug info
if(DEFINED USE_CUSTOM_DEBINFO)
string(REPLACE ";" " " SOURCE_FILES "${USE_CUSTOM_DEBINFO}")
message(STATUS "Source files with custom debug infos: ${SOURCE_FILES}")
string(REGEX REPLACE " +" ";" SOURCE_FILES_LIST "${SOURCE_FILES}")
# Add debug info for each source file
foreach(SOURCE_FILE ${SOURCE_FILES_LIST})
# We have to specify the scope here. We do this by specifying the targets we
# care about and caffe2/ for all test targets defined there
if(BUILD_LIBTORCHLESS)
caffe2_update_option(USE_CUDA OFF)
set(ALL_PT_TARGETS "torch_python;${C10_LIB};${TORCH_CPU_LIB};${TORCH_LIB}")
else()
# @todo test if we can remove this
set(ALL_PT_TARGETS "torch_python;c10;torch_cpu;torch")
endif()
set_property(SOURCE ${SOURCE_FILE}
DIRECTORY "caffe2/" TARGET_DIRECTORY ${ALL_PT_TARGETS}
APPEND PROPERTY COMPILE_OPTIONS "-g")
endforeach()
# Link everything with debug info when any file is in debug mode
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -g")
endif()
# Bundle PTXAS if needed
if(BUILD_BUNDLE_PTXAS AND USE_CUDA)
message(STATUS "Copying PTXAS into the bin folder")
file(COPY "${CUDAToolkit_BIN_DIR}/ptxas"
DESTINATION "${PROJECT_BINARY_DIR}")
install(PROGRAMS "${PROJECT_BINARY_DIR}/ptxas"
DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif()
if(SKBUILD)
# File mirroring: copy source files into torchgen/packaged/, vendored
# inductor templates, and symlink-replacement copies. Previously handled
# by setup.py.
include(cmake/FileMirroring.cmake)
# Package data: install non-Python data files (type stubs, templates,
# headers, etc.) into the torch package. Previously declared via
# setup.py package_data.
include(cmake/PackageData.cmake)
# setuptools-era top_level.txt; without it importlib.metadata.packages_distributions()
# stats every RECORD entry to infer top-level names. Keep in sync with
# [tool.scikit-build.wheel] packages in pyproject.toml.
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/top_level.txt" "functorch\ntorch\ntorchgen\n")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/top_level.txt"
DESTINATION "${SKBUILD_METADATA_DIR}")
endif()
include(cmake/PostBuildSteps.cmake)
include(cmake/Summary.cmake)
caffe2_print_configuration_summary()