diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index fcac0424e34d..f9c05379024c 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -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 diff --git a/cmake/Modules/FindMKLDNN.cmake b/cmake/Modules/FindMKLDNN.cmake index 234d361d7f5c..e774afe10e20 100644 --- a/cmake/Modules/FindMKLDNN.cmake +++ b/cmake/Modules/FindMKLDNN.cmake @@ -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() diff --git a/cmake/Modules/FindSYCLToolkit.cmake b/cmake/Modules/FindSYCLToolkit.cmake index ec46a111eaed..4a9844a5078d 100644 --- a/cmake/Modules/FindSYCLToolkit.cmake +++ b/cmake/Modules/FindSYCLToolkit.cmake @@ -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 + ) diff --git a/cmake/Summary.cmake b/cmake/Summary.cmake index 5fb53d3a321d..72be8b17d891 100644 --- a/cmake/Summary.cmake +++ b/cmake/Summary.cmake @@ -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() diff --git a/cmake/public/xpu.cmake b/cmake/public/xpu.cmake index 5395fba562ef..eb4b723da37f 100644 --- a/cmake/public/xpu.cmake +++ b/cmake/public/xpu.cmake @@ -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}")