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
58 changes: 54 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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(AUTO_DETECT_DEVICES "Automatically detect available devices" OFF)
option(INFINI_RT_BUILD_TESTING "Build InfiniRT tests" OFF)
Expand Down Expand Up @@ -121,9 +122,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 All @@ -140,7 +156,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)

# 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_CAMBRICON WITH_ASCEND)
foreach(_gpu_backend WITH_NVIDIA WITH_ILUVATAR WITH_HYGON WITH_THEAD WITH_METAX WITH_MARS WITH_MOORE WITH_CAMBRICON WITH_ASCEND WITH_QY)
if(${_gpu_backend})
math(EXPR _gpu_backend_count "${_gpu_backend_count} + 1")
endif()
Expand Down Expand Up @@ -348,8 +364,39 @@ if(WITH_ASCEND)
message(STATUS "Using Ascend from `${ASCEND_HOME}`.")
endif()

if(WITH_QY)
add_compile_definitions(WITH_QY=1)

set(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(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 `${QY_ROOT}/sdk/bin/dlcc`).")
endif()
set(QY_ROOT "${QY_ROOT}" CACHE PATH "Denglin QY SDK root" FORCE)
set(QY_SDK "${QY_ROOT}/sdk")

# InfiniRT QY is header-only against the Denglin CUDA-compatible runtime.
# Link `curt` directly (MetaX-style) rather than requiring CMake CUDA + nvcc.
include_directories("${QY_SDK}/include")
link_directories("${QY_SDK}/lib")
find_library(QY_CURT_LIB NAMES curt HINTS "${QY_SDK}/lib" REQUIRED)
set(ENV{QY_ROOT} "${QY_ROOT}")
set(ENV{CUDA_HOME} "${QY_SDK}")
set(ENV{CUDA_PATH} "${QY_SDK}")
message(STATUS "QY: SDK ${QY_SDK}, runtime ${QY_CURT_LIB}")
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)
add_compile_definitions(WITH_CPU=1)
endif()
Expand Down Expand Up @@ -385,6 +432,9 @@ endif()
if(WITH_ASCEND)
list(APPEND INFINI_RT_PUBLIC_HEADER_DEVICES ascend)
endif()
if(WITH_QY)
list(APPEND INFINI_RT_PUBLIC_HEADER_DEVICES qy)
endif()

find_package(Python COMPONENTS Interpreter REQUIRED)
execute_process(
Expand All @@ -408,7 +458,7 @@ if(WITH_NVIDIA OR WITH_ILUVATAR OR WITH_HYGON OR WITH_THEAD)
endif()

set(INFINI_RT_PACKAGE_VENDOR_BACKEND "")
foreach(_backend METAX MARS MOORE CAMBRICON ASCEND)
foreach(_backend METAX MARS MOORE CAMBRICON ASCEND QY)
if(WITH_${_backend})
set(INFINI_RT_PACKAGE_VENDOR_BACKEND "${_backend}")
endif()
Expand Down
27 changes: 27 additions & 0 deletions cmake/InfiniRTConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(InfiniRT_WITH_MARS @WITH_MARS@)
set(InfiniRT_WITH_MOORE @WITH_MOORE@)
set(InfiniRT_WITH_CAMBRICON @WITH_CAMBRICON@)
set(InfiniRT_WITH_ASCEND @WITH_ASCEND@)
set(InfiniRT_WITH_QY @WITH_QY@)

if(@INFINI_RT_PACKAGE_NEEDS_CUDATOOLKIT@)
find_dependency(CUDAToolkit)
Expand Down Expand Up @@ -186,6 +187,32 @@ elseif(_InfiniRT_VENDOR_BACKEND STREQUAL "ASCEND")
_infinirt_define_backend_runtime(
"${_InfiniRT_ASCEND_INCLUDE_DIRS}"
"${_InfiniRT_BACKEND_RUNTIME_LIBRARY};${_InfiniRT_BACKEND_DRIVER_LIBRARY};${CMAKE_DL_LIBS}")
elseif(_InfiniRT_VENDOR_BACKEND STREQUAL "QY")
set(_InfiniRT_ROOT_HINTS "${InfiniRT_QY_ROOT}")
foreach(_InfiniRT_ENV QY_ROOT QY_HOME QY_PATH)
if(DEFINED ENV{${_InfiniRT_ENV}})
list(APPEND _InfiniRT_ROOT_HINTS "$ENV{${_InfiniRT_ENV}}")
endif()
endforeach()
list(APPEND _InfiniRT_ROOT_HINTS /usr/local/denglin)
find_path(_InfiniRT_BACKEND_INCLUDE_DIR
NAMES cuda_runtime.h
HINTS ${_InfiniRT_ROOT_HINTS}
PATH_SUFFIXES sdk/include include)
find_library(_InfiniRT_BACKEND_RUNTIME_LIBRARY
NAMES curt
HINTS ${_InfiniRT_ROOT_HINTS}
PATH_SUFFIXES sdk/lib lib lib64)
if(NOT _InfiniRT_BACKEND_INCLUDE_DIR OR
NOT _InfiniRT_BACKEND_RUNTIME_LIBRARY)
set(InfiniRT_FOUND FALSE)
set(InfiniRT_NOT_FOUND_MESSAGE
"Denglin QY SDK not found. Set InfiniRT_QY_ROOT or QY_ROOT.")
return()
endif()
_infinirt_define_backend_runtime(
"${_InfiniRT_BACKEND_INCLUDE_DIR}"
"${_InfiniRT_BACKEND_RUNTIME_LIBRARY}")
endif()

include("${CMAKE_CURRENT_LIST_DIR}/InfiniRTTargets.cmake")
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[@]}"
7 changes: 7 additions & 0 deletions scripts/generate_public_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
("ascend", "device_.h", "native/ascend/device_.h"),
("ascend", "runtime_.h", "native/ascend/runtime_.h"),
),
"qy": (
("qy", "data_type_.h", "native/cuda/qy/data_type_.h"),
("qy", "device_.h", "native/cuda/qy/device_.h"),
("qy", "runtime_.h", "native/cuda/qy/runtime_.h"),
),
}

_DEVICE_TYPES = {
Expand All @@ -70,6 +75,7 @@
"moore": "Device::Type::kMoore",
"cambricon": "Device::Type::kCambricon",
"ascend": "Device::Type::kAscend",
"qy": "Device::Type::kQy",
}

_DEFAULT_DEVICE_PRIORITY = (
Expand All @@ -82,6 +88,7 @@
"moore",
"cambricon",
"ascend",
"qy",
"cpu",
)

Expand Down
1 change: 1 addition & 0 deletions scripts/run_performance_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
("WITH_MOORE", "moore"),
("WITH_CAMBRICON", "cambricon"),
("WITH_ASCEND", "ascend"),
("WITH_QY", "qy"),
("WITH_CPU", "cpu"),
)

Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ if(WITH_CAMBRICON)
"${CAMBRICON_RUNTIME_LIB}")
endif()

if(WITH_QY)
target_compile_definitions(infinirt PUBLIC WITH_QY=1)
_infinirt_use_backend_runtime(
"${QY_SDK}/include"
"${QY_CURT_LIB}")
endif()

if(WITH_ASCEND)
# ASCEND_HOME is set by the top-level CMakeLists.txt.
target_compile_definitions(infinirt PUBLIC WITH_ASCEND=1)
Expand Down
5 changes: 4 additions & 1 deletion src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Device {
kHygon = 7,
kThead = 8,
kMars = 9,
kQy = 10,
kCount
};

Expand Down Expand Up @@ -68,6 +69,7 @@ class Device {
{Type::kHygon, "hygon"},
{Type::kThead, "thead"},
{Type::kMars, "mars"},
{Type::kQy, "qy"},
}}};

static constexpr ConstexprMap<std::string_view, Device::Type,
Expand All @@ -83,6 +85,7 @@ class Device {
{"hygon", Type::kHygon},
{"thead", Type::kThead},
{"mars", Type::kMars},
{"qy", Type::kQy},
}}};

int index_{0};
Expand All @@ -98,7 +101,7 @@ using AllDeviceTypes =
List<Device::Type::kCpu, Device::Type::kNvidia, Device::Type::kCambricon,
Device::Type::kAscend, Device::Type::kMetax, Device::Type::kMoore,
Device::Type::kIluvatar, Device::Type::kHygon, Device::Type::kThead,
Device::Type::kMars>;
Device::Type::kMars, Device::Type::kQy>;

// Deferred computation of active devices. The `Filter` and `FilterList`
// evaluation are nested inside a class template so that `DeviceEnabled`
Expand Down
30 changes: 30 additions & 0 deletions src/native/cuda/qy/data_type_.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef INFINI_RT_QY_DATA_TYPE__H_
#define INFINI_RT_QY_DATA_TYPE__H_

// clang-format off
#include <cuda_bf16.h>
#include <cuda_fp16.h>
// clang-format on

#include "data_type.h"
#include "native/cuda/qy/device_.h"

namespace infini::rt {

using cuda_bfloat16 = nv_bfloat16;

using cuda_bfloat162 = nv_bfloat162;

template <>
struct TypeMap<Device::Type::kQy, DataType::kFloat16> {
using type = half;
};

template <>
struct TypeMap<Device::Type::kQy, DataType::kBFloat16> {
using type = __nv_bfloat16;
};

} // namespace infini::rt

#endif
13 changes: 13 additions & 0 deletions src/native/cuda/qy/device_.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef INFINI_RT_QY_DEVICE__H_
#define INFINI_RT_QY_DEVICE__H_

#include "device.h"

namespace infini::rt {

template <>
struct DeviceEnabled<Device::Type::kQy> : std::true_type {};

} // namespace infini::rt

#endif
Loading
Loading