Skip to content

Commit

Permalink
Support XPU ABI=0 build (pytorch#130110)
Browse files Browse the repository at this point in the history
# Motivation
This PR intends to support ABI=0 build for XPU backend.

# Additional Context
The major change is adding a compilation option `-D__INTEL_PREVIEW_BREAKING_CHANGES` for the host compiler(gcc) and `-fpreview-breaking-changes` for XPU device kernel code compiler(icpx), why?
Because we use
- gcc to compile host code and link SYCL runtime. So we need to pass `-D__INTEL_PREVIEW_BREAKING_CHANGES` to tell the host compiler invoking the ABI-neutral API included in SYCL. And
- use icpx to compile device kernel code and link SYCL runtime. So we need to pass `-fpreview-breaking-changes` to tell the device kernel compiler building ABI-neutral code. Besides,
- `libsycl-preview.so` is an ABI-neutral library but `libsycl.so` is not.

This PR depends on pytorch#131643.

Pull Request resolved: pytorch#130110
Approved by: https://github.com/EikanWang, https://github.com/gujinghui, https://github.com/albanD
  • Loading branch information
guangyey authored and pytorchmergebot committed Aug 1, 2024
1 parent 997f64a commit 92bebb4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ If you would like to compile PyTorch with [new C++ ABI](https://gcc.gnu.org/onli
export _GLIBCXX_USE_CXX11_ABI=1
```

Please **note** that starting from PyTorch 2.5, the PyTorch build with XPU supports both new and old C++ ABIs. Previously, XPU only supported the new C++ ABI. If you want to compile with Intel GPU support, please follow [Intel GPU Support](#intel-gpu-support).

If you're compiling for AMD ROCm then first run this command:
```bash
# Only run this if you're compiling for ROCm
Expand Down
30 changes: 15 additions & 15 deletions c10/xpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ set(C10_XPU_HEADERS
impl/XPUGuardImpl.h
)
if(NOT BUILD_LIBTORCHLESS)
add_library(c10_xpu ${C10_XPU_SRCS} ${C10_XPU_HEADERS})
target_compile_options(c10_xpu PRIVATE "-DC10_XPU_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10_xpu PRIVATE "-fvisibility=hidden")
endif()

# ---[ Dependency of c10_xpu
target_link_libraries(c10_xpu PUBLIC c10 torch::xpurt)
target_include_directories(
c10_xpu PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)
add_library(c10_xpu ${C10_XPU_SRCS} ${C10_XPU_HEADERS})
target_compile_options(c10_xpu PRIVATE "-DC10_XPU_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10_xpu PRIVATE "-fvisibility=hidden")
endif()

# ---[ Dependency of c10_xpu
target_link_libraries(c10_xpu PUBLIC c10 torch::xpurt)
target_include_directories(
c10_xpu PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)
install(TARGETS c10_xpu EXPORT Caffe2Targets DESTINATION lib)
set(C10_XPU_LIB c10_xpu)
add_subdirectory(test)
Expand Down
4 changes: 4 additions & 0 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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()
endif()

Expand Down
6 changes: 5 additions & 1 deletion cmake/Modules/FindMKLDNN.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ IF(NOT MKLDNN_FOUND)
set(DNNL_MAKE_COMMAND "cmake" "--build" ".")
include(ProcessorCount)
ProcessorCount(proc_cnt)
if ((DEFINED ENV{MAX_JOBS}) AND ("$ENV{MAX_JOBS}" LESS_EQUAL ${proc_cnt}))
if((DEFINED ENV{MAX_JOBS}) AND ("$ENV{MAX_JOBS}" LESS_EQUAL ${proc_cnt}))
list(APPEND DNNL_MAKE_COMMAND "-j" "$ENV{MAX_JOBS}")
if(CMAKE_GENERATOR MATCHES "Make|Ninja")
list(APPEND DNNL_MAKE_COMMAND "--" "-l" "$ENV{MAX_JOBS}")
endif()
endif()
if(LINUX)
set(ABI_NEUTRAL_FLAGS -fpreview-breaking-changes)
endif()
ExternalProject_Add(xpu_mkldnn_proj
SOURCE_DIR ${MKLDNN_ROOT}
PREFIX ${XPU_MKLDNN_DIR_PREFIX}
BUILD_IN_SOURCE 0
CMAKE_ARGS -DCMAKE_C_COMPILER=icx
-DCMAKE_CXX_COMPILER=${SYCL_CXX_DRIVER}
-DCMAKE_CXX_FLAGS=${ABI_NEUTRAL_FLAGS}
-DDNNL_GPU_RUNTIME=SYCL
-DDNNL_CPU_RUNTIME=THREADPOOL
-DDNNL_BUILD_TESTS=OFF
Expand Down
2 changes: 1 addition & 1 deletion cmake/Modules/FindSYCLToolkit.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ find_file(
# Find SYCL library fullname.
find_library(
SYCL_LIBRARY
NAMES sycl
NAMES sycl-preview
HINTS ${SYCL_LIBRARY_DIR}
NO_DEFAULT_PATH
)
Expand Down

0 comments on commit 92bebb4

Please sign in to comment.