Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,34 +237,24 @@ if(WITH_NVIDIA)
find_package(CUDAToolkit REQUIRED)
endif()

# Iluvatar: CUDA-compatible device, uses `clang++` with `-x ivcore` (not `nvcc`).
# Iluvatar: CUDA-compatible device. CoreX clang++ works for `-x ivcore`
# compilation, but CMake's CUDA language path appends `-x cuda` and can crash
# the compiler. Keep CMake's CUDA language disabled for this backend and
# compile the few generated dispatch sources that need CUDA syntax via custom
# clang++ commands.
# Reference: `InfiniCore` `xmake/iluvatar.lua`.
if(WITH_ILUVATAR)
add_compile_definitions(WITH_ILUVATAR=1)
set(ILUVATAR_ARCH "ivcore20" CACHE STRING "Iluvatar GPU architecture")
find_program(CLANGXX NAMES clang++)
if(CLANGXX)
set(CMAKE_CUDA_COMPILER "${CLANGXX}" CACHE STRING "Iluvatar CUDA compiler (clang++)")
else()
set(CMAKE_CUDA_COMPILER "clang++" CACHE STRING "Iluvatar CUDA compiler (clang++)")
find_program(ILUVATAR_CUDA_COMPILER NAMES clang++ HINTS /usr/local/corex/bin)
if(NOT ILUVATAR_CUDA_COMPILER)
message(FATAL_ERROR "`WITH_ILUVATAR` is `ON` but CoreX `clang++` was not found.")
endif()
# `-x ivcore` must not be in `CMAKE_CUDA_FLAGS` — CMake passes those flags
# to both compile and link steps. During linking, `-x ivcore` causes
# `clang++` to re-parse `.o` files as source code.
set(CMAKE_CUDA_FLAGS "--cuda-gpu-arch=${ILUVATAR_ARCH} -fPIC -Wno-error=unused-variable -Wno-error=unused-private-field -Wno-unused-variable -std=c++17" CACHE STRING "Iluvatar CUDA flags")
set(CMAKE_CUDA_SEPARABLE_COMPILATION OFF CACHE BOOL "Disable RDC for Iluvatar")
set(CMAKE_CUDA_ARCHITECTURES OFF CACHE STRING "Iluvatar CUDA architectures (passed via CMAKE_CUDA_FLAGS)")
# Iluvatar does not ship `libcudadevrt`, which CMake's compiler test
# tries to link. Disable automatic CUDA runtime linking and link
# manually via `find_package(CUDAToolkit)` instead.
set(CMAKE_CUDA_RUNTIME_LIBRARY NONE)
message(STATUS "Iluvatar: CUDA compiler ${CMAKE_CUDA_COMPILER}, arch ${ILUVATAR_ARCH}")
enable_language(CUDA)
set(ILUVATAR_CUDA_FLAGS
"--cuda-gpu-arch=${ILUVATAR_ARCH};-fPIC;-Wno-error=unused-variable;-Wno-error=unused-private-field;-Wno-unused-variable;-std=c++17;--cuda-path=/usr/local/corex;-x;ivcore"
CACHE STRING "Iluvatar CUDA compiler flags")
message(STATUS "Iluvatar: CUDA compiler ${ILUVATAR_CUDA_COMPILER}, arch ${ILUVATAR_ARCH}")
find_package(CUDAToolkit REQUIRED)
# Add `-x ivcore` as a compile-only flag so it is not passed during
# linking, where it would cause `clang++` to re-parse `.o` files as
# source.
add_compile_options($<$<COMPILE_LANGUAGE:CUDA>:-x$<SEMICOLON>ivcore>)
endif()

if(WITH_METAX)
Expand Down
51 changes: 42 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,12 @@ if(WITH_ILUVATAR)

file(GLOB_RECURSE ILUVATAR_SOURCES CONFIGURE_DEPENDS ${ILUVATAR_PATTERNS})

enable_language(CUDA)

target_compile_definitions(infiniops PUBLIC WITH_ILUVATAR=1)
target_sources(infiniops PRIVATE ${ILUVATAR_SOURCES})

find_package(CUDAToolkit REQUIRED)
target_link_libraries(infiniops PUBLIC CUDA::cudart CUDA::cublas CUDA::cuda_driver)

set_target_properties(infiniops PROPERTIES
CUDA_STANDARD 17
CUDA_STANDARD_REQUIRED ON
)

list(APPEND DEVICE_LIST "iluvatar")
endif()

Expand Down Expand Up @@ -524,9 +517,49 @@ if(GENERATE_PYTHON_BINDINGS)
endif()
list(APPEND PYBIND11_COMPILE_SOURCES ${PYBIND11_DISPATCH_SOURCES})

# TODO: There might be a better solution.
if(WITH_NVIDIA OR WITH_ILUVATAR)
if(WITH_NVIDIA)
set_source_files_properties(${PYBIND11_COMPILE_SOURCES} PROPERTIES LANGUAGE CUDA)
elseif(WITH_ILUVATAR)
set(_iluvatar_dispatch_include_flags
"-I${CMAKE_CURRENT_SOURCE_DIR}"
"-I${PROJECT_SOURCE_DIR}"
"-I${PROJECT_SOURCE_DIR}/generated")
foreach(_dir IN LISTS TORCH_INCLUDE_DIRS CUDAToolkit_INCLUDE_DIRS)
list(APPEND _iluvatar_dispatch_include_flags "-I${_dir}")
endforeach()

set(_iluvatar_dispatch_defs -DWITH_ILUVATAR=1)
if(WITH_TORCH)
list(APPEND _iluvatar_dispatch_defs -DWITH_TORCH=1)
endif()
if(DEFINED TORCH_CXX11_ABI)
list(APPEND _iluvatar_dispatch_defs "-D_GLIBCXX_USE_CXX11_ABI=${TORCH_CXX11_ABI}")
endif()

set(ILUVATAR_DISPATCH_OBJECTS)
set(_iluvatar_dispatch_object_dir "${CMAKE_CURRENT_BINARY_DIR}/iluvatar_dispatch_objs")
foreach(_src IN LISTS PYBIND11_DISPATCH_SOURCES)
get_filename_component(_name "${_src}" NAME_WE)
set(_obj "${_iluvatar_dispatch_object_dir}/${_name}.o")
add_custom_command(
OUTPUT "${_obj}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${_iluvatar_dispatch_object_dir}"
COMMAND ${ILUVATAR_CUDA_COMPILER}
${_iluvatar_dispatch_defs}
${_iluvatar_dispatch_include_flags}
${ILUVATAR_CUDA_FLAGS}
-c "${_src}" -o "${_obj}"
DEPENDS "${_src}"
COMMENT "Compiling ${_name}.cc with CoreX clang++"
VERBATIM
)
list(APPEND ILUVATAR_DISPATCH_OBJECTS "${_obj}")
endforeach()

list(REMOVE_ITEM PYBIND11_COMPILE_SOURCES ${PYBIND11_DISPATCH_SOURCES})
set_source_files_properties(${ILUVATAR_DISPATCH_OBJECTS}
PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE)
list(APPEND PYBIND11_COMPILE_SOURCES ${ILUVATAR_DISPATCH_OBJECTS})
endif()

find_package(Python COMPONENTS Interpreter Development)
Expand Down
Loading