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
107 changes: 98 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ option(WITH_MARS "Enable Mars backend" OFF)
option(WITH_CAMBRICON "Enable Cambricon backend" OFF)
option(WITH_MOORE "Enable Moore backend" OFF)
option(WITH_ASCEND "Enable Ascend backend" OFF)
option(WITH_QY "Enable Denglin QY GPU backend" OFF)

option(WITH_TORCH "Enable PyTorch C++ backend" OFF)
option(WITH_LINKED "Enable linked third-party operator backends" OFF)
Expand Down Expand Up @@ -195,9 +196,24 @@ if(AUTO_DETECT_DEVICES)
message(STATUS "Auto-detected Ascend environment.")
endif()

set(_qy_root_detect "")
foreach(_qy_env QY_ROOT QY_HOME QY_PATH)
if(NOT _qy_root_detect AND DEFINED ENV{${_qy_env}} AND
NOT "$ENV{${_qy_env}}" STREQUAL "")
set(_qy_root_detect "$ENV{${_qy_env}}")
endif()
endforeach()
if(NOT _qy_root_detect AND EXISTS "/usr/local/denglin/sdk/bin/dlcc")
set(_qy_root_detect "/usr/local/denglin")
endif()
if(_qy_root_detect AND EXISTS "${_qy_root_detect}/sdk/bin/dlcc")
set(WITH_QY ON)
message(STATUS "Auto-detected Denglin QY environment from ${_qy_root_detect}.")
endif()

if(WITH_NVIDIA)
set(_non_nvidia_gpu_detected FALSE)
foreach(_gpu_backend WITH_ILUVATAR WITH_HYGON WITH_THEAD WITH_METAX WITH_MARS WITH_CAMBRICON WITH_MOORE WITH_ASCEND)
foreach(_gpu_backend WITH_ILUVATAR WITH_HYGON WITH_THEAD WITH_METAX WITH_MARS WITH_CAMBRICON WITH_MOORE WITH_ASCEND WITH_QY)
if(${_gpu_backend})
set(_non_nvidia_gpu_detected TRUE)
endif()
Expand Down Expand Up @@ -301,29 +317,37 @@ macro(infiniops_find_torch)
# Query `torch` paths directly instead of using `find_package(Torch)`,
# which pulls in Caffe2's CMake config and may fail on platforms with
# non-standard CUDA toolchains.
# Denglin/torch may emit extra stdout lines during import; keep only the
# first line of each query so find_library HINTS stay valid.
execute_process(
COMMAND ${_TORCH_PYTHON} -c "from torch.utils.cpp_extension import include_paths; print(';'.join(include_paths()))"
COMMAND ${_TORCH_PYTHON} -c "from torch.utils.cpp_extension import include_paths; print(';'.join(include_paths()), flush=True)"
OUTPUT_VARIABLE TORCH_INCLUDE_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _torch_result
ERROR_QUIET
)

if(NOT _torch_result EQUAL 0)
message(FATAL_ERROR
"The selected Torch backend/transport requires an installed `torch` package.")
endif()
string(REGEX REPLACE "[\r\n].*" "" TORCH_INCLUDE_DIRS "${TORCH_INCLUDE_DIRS}")

execute_process(
COMMAND ${_TORCH_PYTHON} -c "from torch.utils.cpp_extension import library_paths; print(';'.join(library_paths()))"
COMMAND ${_TORCH_PYTHON} -c "from torch.utils.cpp_extension import library_paths; print(';'.join(library_paths()), flush=True)"
OUTPUT_VARIABLE _torch_lib_dirs
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
string(REGEX REPLACE "[\r\n].*" "" _torch_lib_dirs "${_torch_lib_dirs}")

execute_process(
COMMAND ${_TORCH_PYTHON} -c "import pathlib, torch; p = pathlib.Path(torch.__file__).resolve().parent.parent / 'torch.libs'; print(str(p) if p.exists() else '')"
COMMAND ${_TORCH_PYTHON} -c "import pathlib, torch; p = pathlib.Path(torch.__file__).resolve().parent.parent / 'torch.libs'; print(str(p) if p.exists() else '', flush=True)"
OUTPUT_VARIABLE _torch_private_lib_dir
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
string(REGEX REPLACE "[\r\n].*" "" _torch_private_lib_dir "${_torch_private_lib_dir}")

set(TORCH_RUNTIME_DIRS ${_torch_lib_dirs})
if(_torch_private_lib_dir)
Expand All @@ -335,7 +359,7 @@ macro(infiniops_find_torch)
find_library(C10_LIB c10 HINTS ${_torch_lib_dirs} REQUIRED)
set(TORCH_LIBRARIES ${TORCH_LIB} ${TORCH_CPU_LIB} ${C10_LIB})

if(WITH_NVIDIA OR WITH_ILUVATAR OR WITH_THEAD OR WITH_MARS)
if(WITH_NVIDIA OR WITH_ILUVATAR OR WITH_THEAD OR WITH_MARS OR WITH_QY)
find_library(TORCH_CUDA_LIB torch_cuda HINTS ${_torch_lib_dirs} REQUIRED)
find_library(C10_CUDA_LIB c10_cuda HINTS ${_torch_lib_dirs} REQUIRED)
# `torch_cuda` registers CUDA kernels through static initializers, so
Expand Down Expand Up @@ -411,14 +435,14 @@ endif()

# Only one CUDA-like GPU backend can be enabled at a time.
set(_gpu_backend_count 0)
foreach(_gpu_backend WITH_NVIDIA WITH_ILUVATAR WITH_HYGON WITH_THEAD WITH_METAX WITH_MARS WITH_MOORE WITH_ASCEND)
foreach(_gpu_backend WITH_NVIDIA WITH_ILUVATAR WITH_HYGON WITH_THEAD WITH_METAX WITH_MARS WITH_MOORE WITH_ASCEND WITH_QY)
if(${_gpu_backend})
math(EXPR _gpu_backend_count "${_gpu_backend_count} + 1")
endif()
endforeach()

if(_gpu_backend_count GREATER 1)
message(FATAL_ERROR "`WITH_NVIDIA`, `WITH_ILUVATAR`, `WITH_HYGON`, `WITH_THEAD`, `WITH_METAX`, `WITH_MARS`, `WITH_MOORE`, and `WITH_ASCEND` are mutually exclusive. Build one GPU backend at a time.")
message(FATAL_ERROR "`WITH_NVIDIA`, `WITH_ILUVATAR`, `WITH_HYGON`, `WITH_THEAD`, `WITH_METAX`, `WITH_MARS`, `WITH_MOORE`, `WITH_ASCEND`, and `WITH_QY` are mutually exclusive. Build one GPU backend at a time.")
endif()

if(WITH_NINETOOTHED AND NOT WITH_NVIDIA)
Expand Down Expand Up @@ -675,12 +699,77 @@ if(WITH_ASCEND)
message(STATUS "Using Ascend from `${ASCEND_HOME}`.")
endif()

if(WITH_QY)
add_compile_definitions(WITH_QY=1)

# Honor -DQY_ROOT / existing cache; only fall back to env / default path.
if(NOT QY_ROOT)
foreach(_qy_env QY_ROOT QY_HOME QY_PATH)
if(NOT QY_ROOT AND DEFINED ENV{${_qy_env}} AND NOT "$ENV{${_qy_env}}" STREQUAL "")
set(QY_ROOT "$ENV{${_qy_env}}")
endif()
endforeach()
if(NOT QY_ROOT AND EXISTS "/usr/local/denglin/sdk/bin/dlcc")
set(QY_ROOT "/usr/local/denglin")
endif()
if(QY_ROOT)
set(QY_ROOT "${QY_ROOT}" CACHE PATH "Denglin QY SDK root")
endif()
endif()
if(NOT QY_ROOT OR NOT EXISTS "${QY_ROOT}/sdk/bin/dlcc")
message(FATAL_ERROR
"`WITH_QY` is `ON` but Denglin QY SDK was not found. "
"Set `QY_ROOT` (expected `sdk/bin/dlcc`).")
endif()
set(QY_SDK "${QY_ROOT}/sdk")
set(ENV{QY_ROOT} "${QY_ROOT}")
set(ENV{CUDA_HOME} "${QY_SDK}")
set(ENV{CUDA_PATH} "${QY_SDK}")
set(ENV{PATH} "${QY_SDK}/bin:$ENV{PATH}")

set(CMAKE_CUDA_COMPILER
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/dlcc_wrapper.sh"
CACHE FILEPATH "Denglin QY CUDA compiler (dlcc wrapper)" FORCE)
# Keep CMAKE_CUDA_FLAGS free of --cuda-gpu-arch; the wrapper always injects
# dlgput64 and strips CMake's sm_* detection probes.
set(CMAKE_CUDA_FLAGS "--cuda-path=${QY_SDK} -std=c++17 -fPIC -O2"
CACHE STRING "QY CUDA flags" FORCE)
set(CMAKE_CUDA_SEPARABLE_COMPILATION OFF CACHE BOOL "Disable RDC for QY" FORCE)
# Skip CMake's sm_20/30/52 Clang probes; wrapper maps all arches to dlgput64.
set(CMAKE_CUDA_ARCHITECTURES "80" CACHE STRING "QY placeholder arch" FORCE)

include_directories("${QY_SDK}/include")
link_directories("${QY_SDK}/lib")
find_library(QY_CURT_LIB NAMES curt HINTS "${QY_SDK}/lib" REQUIRED)
find_library(QY_CUBLAS_LIB NAMES cublas HINTS "${QY_SDK}/lib" REQUIRED)
find_library(QY_CUBLASLT_LIB NAMES cublasLt HINTS "${QY_SDK}/lib" REQUIRED)
find_library(QY_CUDNN_LIB NAMES cudnn HINTS "${QY_SDK}/lib" REQUIRED)

# Ensure CMake Clang CUDA toolkit discovery finds nvvm/libdevice.
file(MAKE_DIRECTORY "${QY_SDK}/nvvm/libdevice")
set(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "${QY_SDK}" CACHE PATH
"QY CUDA toolkit root" FORCE)
set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "${QY_SDK}" CACHE PATH
"QY CUDA library root" FORCE)
set(CUDAToolkit_ROOT "${QY_SDK}" CACHE PATH "QY CUDA toolkit root" FORCE)

# Final link goes through the host C++ driver, which would otherwise pull
# CMake's implicit -lcudadevrt/-lcudart_static (absent on Denglin).
set(CMAKE_CUDA_RUNTIME_LIBRARY "None" CACHE STRING
"QY links curt explicitly; do not inject CUDA runtime" FORCE)

message(STATUS "QY: SDK ${QY_SDK}, compiler ${CMAKE_CUDA_COMPILER}")
enable_language(CUDA)
set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "")
endif()

# If all other platforms are not enabled, CPU is enabled by default.
if(NOT WITH_NVIDIA AND NOT WITH_ILUVATAR AND NOT WITH_HYGON AND NOT WITH_THEAD AND NOT WITH_METAX AND NOT WITH_MARS AND NOT WITH_MOORE AND NOT WITH_CAMBRICON AND NOT WITH_ASCEND)
if(NOT WITH_NVIDIA AND NOT WITH_ILUVATAR AND NOT WITH_HYGON AND NOT WITH_THEAD AND NOT WITH_METAX AND NOT WITH_MARS AND NOT WITH_MOORE AND NOT WITH_CAMBRICON AND NOT WITH_ASCEND AND NOT WITH_QY)
set(WITH_CPU ON)
endif()

if(WITH_TORCH OR WITH_METAX OR WITH_MARS OR WITH_MOORE)
if(WITH_TORCH OR WITH_METAX OR WITH_MARS OR WITH_MOORE OR WITH_QY)
set(PYBIND11_ENABLE_EXTRAS OFF)
endif()

Expand Down
2 changes: 2 additions & 0 deletions cmake/InfiniOpsConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ if(@WITH_NVIDIA@ OR @WITH_ILUVATAR@ OR @WITH_HYGON@ OR @WITH_THEAD@)
find_dependency(CUDAToolkit)
endif()

# WITH_QY links Denglin `curt`/`cublas` directly; no CUDAToolkit dependency.

include("${CMAKE_CURRENT_LIST_DIR}/InfiniOpsTargets.cmake")
check_required_components(InfiniOps)
7 changes: 6 additions & 1 deletion scripts/dev/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ source "${repo_root}/scripts/dev/platforms.sh"
usage() {
cat <<'EOF'
Usage:
scripts/dev/build.sh [cpu|nvidia|iluvatar|hygon|thead|metax|moore|cambricon|ascend|auto] [--jobs N] [--smoke]
scripts/dev/build.sh [cpu|nvidia|iluvatar|hygon|thead|metax|moore|cambricon|ascend|qy|auto] [--jobs N] [--smoke]

Examples:
scripts/dev/build.sh
Expand Down Expand Up @@ -113,6 +113,7 @@ with_cambricon="OFF"
with_metax="OFF"
with_moore="OFF"
with_ascend="OFF"
with_qy="OFF"

case "$platform" in
nvidia)
Expand All @@ -139,6 +140,9 @@ case "$platform" in
ascend)
with_ascend="ON"
;;
qy)
with_qy="ON"
;;
cpu)
;;
*)
Expand Down Expand Up @@ -198,6 +202,7 @@ cmake -S "${repo_root}" -B "${build_dir}" -G "${generator}" \
-DWITH_METAX="${with_metax}" \
-DWITH_MOORE="${with_moore}" \
-DWITH_ASCEND="${with_ascend}" \
-DWITH_QY="${with_qy}" \
-DAUTO_DETECT_DEVICES=OFF \
-DAUTO_DETECT_BACKENDS=OFF \
-DGENERATE_PYTHON_BINDINGS=ON \
Expand Down
11 changes: 11 additions & 0 deletions scripts/dev/platforms.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ INFINI_OPS_SUPPORTED_PLATFORMS=(
moore
cambricon
ascend
qy
)

infini_ops_supported_platforms_usage() {
Expand Down Expand Up @@ -87,6 +88,12 @@ _infini_ops_detect_thead() {
[[ -x "${cuda_root}/bin/nvcc" ]]
}

_infini_ops_detect_qy() {
local qy_root="${QY_ROOT:-${QY_HOME:-${QY_PATH:-/usr/local/denglin}}}"

[[ -x "${qy_root}/sdk/bin/dlcc" ]]
}

_infini_ops_detect_metax() {
if [[ -n "${MACA_PATH:-}" ]]; then
return 0
Expand Down Expand Up @@ -127,6 +134,10 @@ infini_ops_detect_platforms() {
detected+=("thead")
fi

if _infini_ops_detect_qy; then
detected+=("qy")
fi

if _infini_ops_detect_metax; then
detected+=("metax")
fi
Expand Down
78 changes: 78 additions & 0 deletions scripts/dlcc_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash
# Filter CMake/host flags unsupported or harmful when invoking Denglin `dlcc`
# as CMAKE_CUDA_COMPILER. Emulate enough of `nvcc -v` for CMake 3.22 Clang
# CUDA detection (TOP / NVVMIR_LIBRARY_DIR).
set -euo pipefail

QY_ROOT="${QY_ROOT:-${QY_HOME:-${QY_PATH:-/usr/local/denglin}}}"
DLCC="${QY_ROOT}/sdk/bin/dlcc"
SDK="${QY_ROOT}/sdk"

if [[ ! -x "${DLCC}" ]]; then
# Container image layout.
if [[ -x /usr/local/dlgpu/sdk/bin/dlcc ]]; then
QY_ROOT=/usr/local/dlgpu
DLCC=/usr/local/dlgpu/sdk/bin/dlcc
SDK=/usr/local/dlgpu/sdk
else
echo "dlcc_wrapper: dlcc not found; set QY_ROOT" >&2
exit 1
fi
fi

# CMakeDetermineCUDACompiler runs `${CMAKE_CUDA_COMPILER} -v __cmake_determine_cuda`.
if [[ "${1:-}" == "-v" ]]; then
echo "clang version 15.0.6"
echo "Cuda compilation tools, release 11.7, V11.7.0"
echo "#\$ TOP=${SDK}"
# Path need only match CMake's `nvvm/libdevice$` regex; Denglin has no nvvm tree.
echo "#\$ NVVMIR_LIBRARY_DIR=${SDK}/nvvm/libdevice"
"${DLCC}" --version || true
exit 0
fi

ARGS=()
skip_next=0
has_cuda_path=0
replaced_cudart=0
for arg in "$@"; do
if [[ "${skip_next}" -eq 1 ]]; then
skip_next=0
continue
fi
case "${arg}" in
-pthread) ;;
-B) skip_next=1 ;;
-B*) ;;
--cuda-path=*)
has_cuda_path=1
ARGS+=("${arg}")
;;
# CMake Clang detection probes sm_20/30/52; Denglin only accepts dlgput64.
--cuda-gpu-arch=sm_*) ;;
--cuda-gpu-arch=dlgput64)
ARGS+=("${arg}")
;;
--cuda-gpu-arch=*) ;;
-Xcompiler=*) ;;
# Match pre-refactor xmake/qy.lua: drop NVIDIA static CUDA libs.
-lcudadevrt|-lcudart_static|-lcudart)
if [[ "${replaced_cudart}" -eq 0 ]]; then
ARGS+=("-lcurt")
replaced_cudart=1
fi
;;
*)
ARGS+=("${arg}")
;;
esac
done

EXTRA=()
if [[ "${has_cuda_path}" -eq 0 ]]; then
EXTRA+=("--cuda-path=${SDK}")
fi
# Always compile for Denglin GPU; ignore CMake sm_* probes.
EXTRA+=("--cuda-gpu-arch=dlgput64")

exec "${DLCC}" "${EXTRA[@]}" "${ARGS[@]}"
36 changes: 33 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,36 @@ if(WITH_THEAD)
list(APPEND DEVICE_LIST "thead")
endif()

if(WITH_QY)
set(QY_PATTERNS
"native/cuda/qy/*.cc"
"native/cuda/qy/*.cpp"
"native/cuda/qy/*.cu"
)

file(GLOB_RECURSE QY_SOURCES CONFIGURE_DEPENDS ${QY_PATTERNS})

enable_language(CUDA)

target_compile_definitions(infiniops PUBLIC WITH_QY=1)
target_sources(infiniops PRIVATE ${QY_SOURCES})
target_include_directories(infiniops PUBLIC "${QY_SDK}/include")
target_link_libraries(infiniops PUBLIC
${QY_CURT_LIB}
${QY_CUBLAS_LIB}
${QY_CUBLASLT_LIB}
${QY_CUDNN_LIB})

set_target_properties(infiniops PROPERTIES
CUDA_STANDARD 17
CUDA_STANDARD_REQUIRED ON
CUDA_RUNTIME_LIBRARY None
CUDA_SEPARABLE_COMPILATION OFF
)

list(APPEND DEVICE_LIST "qy")
endif()

if(WITH_METAX)
set(METAX_PATTERNS
"native/cuda/*.cc"
Expand Down Expand Up @@ -1372,7 +1402,7 @@ if(GENERATE_OPERATOR_CALL_INSTANTIATIONS)
file(GLOB_RECURSE OPERATOR_CALL_INSTANTIATION_SOURCES CONFIGURE_DEPENDS
"${PROJECT_SOURCE_DIR}/generated/src/operator_call_instantiations_*.cc")

if(WITH_NVIDIA OR WITH_HYGON OR WITH_THEAD)
if(WITH_NVIDIA OR WITH_HYGON OR WITH_THEAD OR WITH_QY)
set_source_files_properties(${OPERATOR_CALL_INSTANTIATION_SOURCES}
PROPERTIES LANGUAGE CUDA)
target_sources(infiniops PRIVATE ${OPERATOR_CALL_INSTANTIATION_SOURCES})
Expand Down Expand Up @@ -1450,7 +1480,7 @@ if(GENERATE_OPERATOR_CALL_INSTANTIATIONS)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/operator.h"
"${PROJECT_SOURCE_DIR}/generated/bindings/operator.h" COPYONLY)
target_include_directories(infiniops PRIVATE "${PROJECT_SOURCE_DIR}")
if(WITH_NVIDIA OR WITH_HYGON OR WITH_THEAD)
if(WITH_NVIDIA OR WITH_HYGON OR WITH_THEAD OR WITH_QY)
set_source_files_properties(${OPERATOR_DISPATCH_SOURCES}
PROPERTIES LANGUAGE CUDA)
target_sources(infiniops PRIVATE ${OPERATOR_DISPATCH_SOURCES})
Expand Down Expand Up @@ -1558,7 +1588,7 @@ if(GENERATE_PYTHON_BINDINGS)
endif()
list(APPEND PYBIND11_COMPILE_SOURCES ${PYBIND11_DISPATCH_SOURCES})

if(WITH_NVIDIA OR WITH_HYGON OR WITH_THEAD)
if(WITH_NVIDIA OR WITH_HYGON OR WITH_THEAD OR WITH_QY)
set_source_files_properties(${PYBIND11_COMPILE_SOURCES} PROPERTIES LANGUAGE CUDA)
elseif(WITH_ILUVATAR)
set(_iluvatar_dispatch_include_flags
Expand Down
Loading
Loading