Skip to content

Commit

Permalink
Support XPU ABI=0 build (#531)
Browse files Browse the repository at this point in the history
# Motivation
Support XPU ABI neutral build.
Starting from compiler 2025.0, `libsycl.so` is ABI-neutral lib. Before
this, the `libsycl-preview.so` is ABI-neutral.

refer to pytorch/pytorch#130110
  • Loading branch information
guangyey authored Jul 24, 2024
1 parent d25d3d0 commit e1e195b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 84 deletions.
6 changes: 3 additions & 3 deletions cmake/BuildFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "MSVC"
set(SYCL_KERNEL_OPTIONS ${SYCL_KERNEL_OPTIONS} -fno-approx-func)
set(SYCL_KERNEL_OPTIONS ${SYCL_KERNEL_OPTIONS} -Wno-absolute-value)
set(SYCL_KERNEL_OPTIONS ${SYCL_KERNEL_OPTIONS} -no-ftz)
# Equivalent to build option -fpreview-breaking-changes for SYCL compiler.
set(SYCL_KERNEL_OPTIONS ${SYCL_KERNEL_OPTIONS} -D__INTEL_PREVIEW_BREAKING_CHANGES)
set(SYCL_KERNEL_OPTIONS ${SYCL_KERNEL_OPTIONS} -D_GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI})
endif()
# TODO: Align with PyTorch and switch to ABI=0 eventually, after
# resolving incompatible implementation in SYCL runtime.
set(SYCL_KERNEL_OPTIONS ${SYCL_KERNEL_OPTIONS} -D_GLIBCXX_USE_CXX11_ABI=1)
set(SYCL_FLAGS ${SYCL_FLAGS} ${SYCL_KERNEL_OPTIONS})

set(TORCH_XPU_OPS_FLAGS ${SYCL_HOST_FLAGS})
Expand Down
5 changes: 2 additions & 3 deletions cmake/Modules/FindSYCL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ macro(SYCL_LINK_DEVICE_OBJECTS output_file sycl_target)
OUTPUT ${output_file}
DEPENDS ${object_files}
COMMAND ${SYCL_EXECUTABLE}
-fsycl
${SYCL_device_link_flags}
-fsycl-link ${object_files}
-Xs "\"${SYCL_OFFLINE_COMPILER_FLAGS}\""
Expand Down Expand Up @@ -471,7 +470,7 @@ macro(SYCL_ADD_LIBRARY sycl_target)
target_link_libraries(
${sycl_target}
${SYCL_LINK_LIBRARIES_KEYWORD}
${SYCL_LIBRARIES})
${SYCL_LIBRARY})

set_target_properties(${sycl_target}
PROPERTIES
Expand Down Expand Up @@ -530,7 +529,7 @@ macro(SYCL_ADD_EXECUTABLE sycl_target)
target_link_libraries(
${sycl_target}
${SYCL_LINK_LIBRARIES_KEYWORD}
${SYCL_LIBRARIES})
${SYCL_LIBRARY})

set_target_properties(${sycl_target}
PROPERTIES
Expand Down
73 changes: 18 additions & 55 deletions cmake/Modules/FindSYCLToolkit.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ This will define the following variables:
#]=======================================================================]

set(SYCLTOOLKIT_FOUND False)
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
include(${TORCH_ROOT}/cmake/Modules/FindSYCLToolkit.cmake)

set(SYCL_ROOT "")
if(DEFINED ENV{SYCL_ROOT})
set(SYCL_ROOT $ENV{SYCL_ROOT})
elseif(DEFINED ENV{CMPLR_ROOT})
set(SYCL_ROOT $ENV{CMPLR_ROOT})
if(NOT SYCL_FOUND)
set(SYCLTOOLKIT_FOUND FALSE)
return()
endif()

if(SYCLTOOLKIT_FOUND)
return()
endif()
set(SYCLTOOLKIT_FOUND TRUE)

include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)

if(WIN32)
set(SYCL_EXECUTABLE_NAME icx)
else()
Expand Down Expand Up @@ -71,43 +75,6 @@ if(nocmplr)
set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
endif()

find_file(
SYCL_INCLUDE_DIR
NAMES include
HINTS ${SYCL_ROOT}
NO_DEFAULT_PATH
)

find_file(
SYCL_INCLUDE_SYCL_DIR
NAMES sycl
HINTS ${SYCL_ROOT}/include
NO_DEFAULT_PATH
)

list(APPEND SYCL_INCLUDE_DIR ${SYCL_INCLUDE_SYCL_DIR})

find_file(
SYCL_LIBRARY_DIR
NAMES lib lib64
HINTS ${SYCL_ROOT}
NO_DEFAULT_PATH
)

find_library(
SYCL_LIBRARY
NAMES sycl
HINTS ${SYCL_LIBRARY_DIR}
NO_DEFAULT_PATH
)

if((NOT SYCL_INCLUDE_DIR) OR (NOT SYCL_LIBRARY_DIR) OR (NOT SYCL_LIBRARY))
set(SYCLTOOLKIT_FOUND False)
set(SYCL_REASON_FAILURE "SYCL sdk is incomplete!!")
set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
return()
endif()

# Function to write a test case to verify SYCL features.

function(SYCL_CMPLR_TEST_WRITE src)
Expand Down Expand Up @@ -202,6 +169,13 @@ set(SYCL_FLAGS "")
set(SYCL_LINK_FLAGS "")
list(APPEND SYCL_FLAGS "-fsycl")
list(APPEND SYCL_LINK_FLAGS "-fsycl")
if(LINUX)
string(REGEX MATCH "libsycl-preview.so" is_abi_neutral ${SYCL_LIBRARY})
if(is_abi_neutral)
list(APPEND SYCL_FLAGS "-fpreview-breaking-changes")
list(APPEND SYCL_LINK_FLAGS "-fpreview-breaking-changes")
endif()
endif()

set(SYCL_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SYCL_FLAGS}")

Expand Down Expand Up @@ -249,14 +223,3 @@ endif()

message(DEBUG "The SYCL compiler is ${SYCL_COMPILER}")
message(DEBUG "The SYCL Flags are ${SYCL_FLAGS}")

# Avoid module variables conflict due to calling find_package recursively
# e.g. find_package -> add_subdirectory(entering in a sub-project) -> find_package
# find_package_handle_standard_args(
# SYCLToolkit
# FOUND_VAR SYCLTOOLKIT_FOUND
# REQUIRED_VARS SYCL_INCLUDE_DIR SYCL_LIBRARY_DIR SYCL_LIBRARY SYCL_FLAGS
# VERSION_VAR SYCL_LANGUAGE_VERSION
# REASON_FAILURE_MESSAGE "${SYCL_REASON_FAILURE}")
set(SYCLTOOLKIT_FOUND True)

18 changes: 0 additions & 18 deletions cmake/SYCL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ if(NOT SYCL_VERSION)
return()
endif()

find_library(SYCL_LIBRARIES sycl HINTS ${SYCL_LIBRARY_DIR})
# 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_LIBRARIES
NAMES "sycl${sycl_runtime_version}"
HINTS ${SYCL_LIBRARY_DIR}
)
if(SYCL_LIBRARIES STREQUAL "SYCL_LIBRARIES-NOTFOUND")
message(FATAL_ERROR "Cannot find a SYCL library on Windows")
endif()
endif()

set(SYCL_COMPILER_VERSION)
file(READ ${SYCL_VERSION} version_contents)
string(REGEX MATCHALL "__SYCL_COMPILER_VERSION +[0-9]+" VERSION_LINE "${version_contents}")
Expand Down
2 changes: 1 addition & 1 deletion src/ATen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ATen XPU sources

file(GLOB xpu_cpp "xpu/*.cpp", "native/xpu/*.cpp", "native/sparse/*.cpp")
file(GLOB xpu_cpp "xpu/*.cpp" "native/xpu/*.cpp" "native/sparse/*.cpp")
file(GLOB xpu_sycl "native/xpu/sycl/*.cpp")

list(APPEND ATen_XPU_CPP_SRCS ${xpu_cpp})
Expand Down
4 changes: 2 additions & 2 deletions src/ATen/native/xpu/sycl/AmpKernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void amp_foreach_non_finite_check_and_unscale_kernel(
}

struct AmpUpdateScaleKernelFunctor {
void operator()(sycl::item<1> item) const {
void operator()(sycl::nd_item<1> item) const {
// There is only single item/task scheduled.
if (item.get_linear_id() != 0)
if (item.get_global_linear_id() != 0)
return;

if (*found_inf_) {
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ foreach(lib ${TORCH_XPU_OPS_LIBRARIES})
target_include_directories(${lib} PUBLIC ${ATen_XPU_INCLUDE_DIRS})
target_include_directories(${lib} PUBLIC ${SYCL_INCLUDE_DIR})

target_link_libraries(${lib} PUBLIC ${SYCL_LIBRARIES})
target_link_libraries(${lib} PUBLIC ${SYCL_LIBRARY})
endforeach()

include(${TORCH_XPU_OPS_ROOT}/cmake/ClangFormat.cmake)
Expand Down
2 changes: 1 addition & 1 deletion test/sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ add_dependencies(test_sycl_build_archive sycl_simple_kernel_test)
# Instead, we use explicit linkage option '--whole-archive', which is required
# by linkage of device object modules archived in the static library. Then
# explicit linkage configuration of SYCL runtime library is required.
target_link_libraries(test_sycl_build_archive ${SYCL_LIBRARIES})
target_link_libraries(test_sycl_build_archive ${SYCL_LIBRARY})

if(INSTALL_TEST)
install(TARGETS test_sycl_build_archive DESTINATION bin)
Expand Down

0 comments on commit e1e195b

Please sign in to comment.