Add XPU compiler version control in cmake to keep BC (#139258)

# Motivation
This PR aims to maintain backward compatibility when building PyTorch XPU with the old and new compilers.

# Additional Context
The details are described here. The new compiler (2025.0.0) has some breaking changes compared with the old compiler(2024.1), for examples:
1. On Windows, sycl library is named `sycl7.lib` in the old compiler but is named `sycl.lib` in the new compiler.
2. On Linux, in order to support ABI=0, we have to link `libsycl-preview.so` in the old compiler but we could link `libsycl.so` in the new compiler to have the same ABI compatibility.
3. We added a macro `SYCL_COMPILER_VERSION` to support our new code has good backward compatibility with the old compiler. Now the new feature(Event elapsed_time, memory summary, and device architecture property) introduced by the new compiler will be controlled within the macro `SYCL_COMPILER_VERSION`.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/139258
Approved by: https://github.com/EikanWang, https://github.com/atalman, https://github.com/gujinghui
This commit is contained in:
Yu, Guangye
2024-11-09 10:35:52 +00:00
committed by PyTorch MergeBot
parent 191971e01d
commit 8051ee802c
5 changed files with 78 additions and 35 deletions

View File

@ -95,11 +95,10 @@ if(USE_XPU)
message(WARNING "Not compiling with XPU. Could NOT find SYCL."
"Suppress this warning with -DUSE_XPU=OFF.")
caffe2_update_option(USE_XPU OFF)
else()
if(LINUX)
string(APPEND CMAKE_CXX_FLAGS " -D__INTEL_PREVIEW_BREAKING_CHANGES")
endif()
endif()
foreach(flag ${XPU_HOST_CXX_FLAGS})
add_definitions(${flag})
endforeach()
endif()
# ---[ Custom Protobuf

View File

@ -42,8 +42,8 @@ IF(NOT MKLDNN_FOUND)
list(APPEND DNNL_MAKE_COMMAND "--" "-l" "$ENV{MAX_JOBS}")
endif()
endif()
if(LINUX)
set(DNNL_CXX_FLAGS "-DCMAKE_CXX_FLAGS=-fpreview-breaking-changes")
if(XPU_DEVICE_CXX_FLAGS)
set(DNNL_CXX_FLAGS "-DCMAKE_CXX_FLAGS=${XPU_DEVICE_CXX_FLAGS}")
else()
set(DNNL_CXX_FLAGS "")
endif()

View File

@ -3,6 +3,7 @@
# SYCL_INCLUDE_DIR : Include directories needed to use SYCL.
# SYCL_LIBRARY_DIR The path to the SYCL library.
# SYCL_LIBRARY : SYCL library fullname.
# SYCL_COMPILER_VERSION : SYCL compiler version.
include(FindPackageHandleStandardArgs)
@ -21,6 +22,39 @@ if(nosyclfound)
return()
endif()
# Find SYCL compiler executable.
find_program(
SYCL_COMPILER
NAMES icx
PATHS "${SYCL_ROOT}"
PATH_SUFFIXES bin bin64
NO_DEFAULT_PATH
)
function(parse_sycl_compiler_version version_number)
# Execute the SYCL compiler with the --version flag to match the version string.
execute_process(COMMAND ${SYCL_COMPILER} --version OUTPUT_VARIABLE SYCL_VERSION_STRING)
string(REGEX REPLACE "Intel\\(R\\) (.*) Compiler ([0-9]+\\.[0-9]+\\.[0-9]+) (.*)" "\\2"
SYCL_VERSION_STRING_MATCH ${SYCL_VERSION_STRING})
string(REPLACE "." ";" SYCL_VERSION_LIST ${SYCL_VERSION_STRING_MATCH})
# Split the version number list into major, minor, and patch components.
list(GET SYCL_VERSION_LIST 0 VERSION_MAJOR)
list(GET SYCL_VERSION_LIST 1 VERSION_MINOR)
list(GET SYCL_VERSION_LIST 2 VERSION_PATCH)
# Calculate the version number in the format XXXXYYZZ, using the formula (major * 10000 + minor * 100 + patch).
math(EXPR VERSION_NUMBER_MATCH "${VERSION_MAJOR} * 10000 + ${VERSION_MINOR} * 100 + ${VERSION_PATCH}")
set(${version_number} "${VERSION_NUMBER_MATCH}" PARENT_SCOPE)
endfunction()
parse_sycl_compiler_version(SYCL_COMPILER_VERSION)
if(NOT SYCL_COMPILER_VERSION)
set(SYCL_FOUND False)
set(SYCL_REASON_FAILURE "Cannot parse sycl compiler version to get SYCL_COMPILER_VERSION!")
set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
return()
endif()
# Find include path from binary.
find_file(
SYCL_INCLUDE_DIR
@ -48,36 +82,32 @@ find_file(
NO_DEFAULT_PATH
)
# Find SYCL library fullname.
# Don't use if(LINUX) here since this requires cmake>=3.25 and file is installed
# and used by other projects.
# See: https://cmake.org/cmake/help/v3.25/variable/LINUX.html
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_library(
SYCL_LIBRARY
NAMES sycl-preview
HINTS ${SYCL_LIBRARY_DIR}
NO_DEFAULT_PATH
)
endif()
# On Windows, currently there's no sycl.lib. Only sycl7.lib with version suffix,
# where the current version of the SYCL runtime is 7.
# Until oneAPI adds support to sycl.lib without the version suffix,
# sycl_runtime_version needs to be hardcoded and uplifted when SYCL runtime version uplifts.
# TODO: remove this when sycl.lib is supported on Windows
if(WIN32)
set(sycl_runtime_version 7)
find_library(
SYCL_LIBRARY
NAMES "sycl${sycl_runtime_version}"
HINTS ${SYCL_LIBRARY_DIR}
NO_DEFAULT_PATH
)
if(SYCL_LIBRARY STREQUAL "SYCL_LIBRARY-NOTFOUND")
message(FATAL_ERROR "Cannot find a SYCL library on Windows")
# Define the old version of SYCL toolkit that is compatible with the current version of PyTorch.
set(PYTORCH_2_5_SYCL_TOOLKIT_VERSION 20249999)
# By default, we use libsycl.so on Linux and sycl.lib on Windows as the SYCL library name.
if (SYCL_COMPILER_VERSION VERSION_LESS_EQUAL PYTORCH_2_5_SYCL_TOOLKIT_VERSION)
# Don't use if(LINUX) here since this requires cmake>=3.25 and file is installed
# and used by other projects.
# See: https://cmake.org/cmake/help/v3.25/variable/LINUX.html
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(sycl_lib_suffix "-preview")
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
# On Windows, the SYCL library is named sycl7.lib until PYTORCH_2_5_SYCL_TOOLKIT_VERSION.
# sycl.lib is supported in the later version.
set(sycl_lib_sufix "7")
endif()
endif()
# Find SYCL library fullname.
find_library(
SYCL_LIBRARY
NAMES "sycl${sycl_lib_suffix}"
HINTS ${SYCL_LIBRARY_DIR}
NO_DEFAULT_PATH
)
# Find OpenCL library fullname, which is a dependency of oneDNN.
find_library(
OCL_LIBRARY
NAMES OpenCL
@ -85,7 +115,7 @@ find_library(
NO_DEFAULT_PATH
)
if((NOT SYCL_INCLUDE_DIR) OR (NOT SYCL_LIBRARY_DIR) OR (NOT SYCL_LIBRARY))
if((NOT SYCL_LIBRARY) OR (NOT OCL_LIBRARY))
set(SYCL_FOUND False)
set(SYCL_REASON_FAILURE "SYCL library is incomplete!!")
set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
@ -96,4 +126,6 @@ find_package_handle_standard_args(
SYCL
FOUND_VAR SYCL_FOUND
REQUIRED_VARS SYCL_INCLUDE_DIR SYCL_LIBRARY_DIR SYCL_LIBRARY
REASON_FAILURE_MESSAGE "${SYCL_REASON_FAILURE}")
REASON_FAILURE_MESSAGE "${SYCL_REASON_FAILURE}"
VERSION_VAR SYCL_COMPILER_VERSION
)

View File

@ -122,6 +122,7 @@ function(caffe2_print_configuration_summary)
endif()
message(STATUS " USE_XPU : ${USE_XPU}")
if(${USE_XPU})
message(STATUS " SYCL compiler version : ${SYCL_COMPILER_VERSION}")
message(STATUS " SYCL include path : ${SYCL_INCLUDE_DIR}")
message(STATUS " SYCL library : ${SYCL_LIBRARY}")
endif()

View File

@ -5,6 +5,9 @@ if(TARGET torch::xpurt)
return()
endif()
set(XPU_HOST_CXX_FLAGS)
set(XPU_DEVICE_CXX_FLAGS)
# Find SYCL library.
find_package(SYCLToolkit REQUIRED)
if(NOT SYCL_FOUND)
@ -33,3 +36,11 @@ set_property(
torch_xpu_get_arch_list(XPU_ARCH_FLAGS)
# propagate to torch-xpu-ops
set(TORCH_XPU_ARCH_LIST ${XPU_ARCH_FLAGS})
if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND SYCL_COMPILER_VERSION VERSION_LESS_EQUAL PYTORCH_2_5_SYCL_TOOLKIT_VERSION)
# for ABI compatibility on Linux
string(APPEND XPU_HOST_CXX_FLAGS " -D__INTEL_PREVIEW_BREAKING_CHANGES")
string(APPEND XPU_DEVICE_CXX_FLAGS " -fpreview-breaking-changes")
endif()
string(APPEND XPU_HOST_CXX_FLAGS " -DSYCL_COMPILER_VERSION=${SYCL_COMPILER_VERSION}")