From 74d32e36b1bd88b87a420f76168fb1a12dd1dd1c Mon Sep 17 00:00:00 2001 From: rubik Date: Wed, 18 Nov 2026 16:54:20 +0800 Subject: [PATCH] feat(kunlun): add Kunlun XPU runtime backend Register Kunlun as device type 10 (kKunlun) and wire the XRE runtime as a native backend: - cmake: WITH_KUNLUN option with device autodetection (/dev/xpu*); SDK root accepts a bare XRE tree or a torch_xmlir package root - src/native/kunlun: device_/runtime_/data_type_ shims over the xpu_* C API; runtime_ carries its own stream create/destroy/synchronize and a device-wide xpu_wait() as DeviceSynchronize (XRE has no whole-device sync) - link libxpurt.so; expose only the xpu/kernel XTDK header subtree to host compilation --- CMakeLists.txt | 34 ++++- scripts/generate_public_headers.py | 7 + src/CMakeLists.txt | 22 +++ src/device.h | 5 +- src/native/kunlun/data_type_.h | 55 +++++++ src/native/kunlun/device_.h | 13 ++ src/native/kunlun/runtime_.h | 223 +++++++++++++++++++++++++++++ 7 files changed, 355 insertions(+), 4 deletions(-) create mode 100644 src/native/kunlun/data_type_.h create mode 100644 src/native/kunlun/device_.h create mode 100644 src/native/kunlun/runtime_.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 270f4bd..eeac9d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_KUNLUN "Enable Kunlun XPU backend" OFF) option(AUTO_DETECT_DEVICES "Automatically detect available devices" OFF) option(INFINI_RT_BUILD_TESTING "Build InfiniRT tests" OFF) @@ -123,7 +124,7 @@ if(AUTO_DETECT_DEVICES) 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_KUNLUN) if(${_gpu_backend}) set(_non_nvidia_gpu_detected TRUE) endif() @@ -140,7 +141,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_KUNLUN) if(${_gpu_backend}) math(EXPR _gpu_backend_count "${_gpu_backend_count} + 1") endif() @@ -348,8 +349,32 @@ if(WITH_ASCEND) message(STATUS "Using Ascend from `${ASCEND_HOME}`.") endif() +if(WITH_KUNLUN) + add_compile_definitions(WITH_KUNLUN=1) + if(NOT DEFINED KUNLUN_HOME) + if(DEFINED ENV{KUNLUN_HOME} AND NOT "$ENV{KUNLUN_HOME}" STREQUAL "") + set(KUNLUN_HOME "$ENV{KUNLUN_HOME}" CACHE PATH "Kunlun XPU SDK root") + else() + set(KUNLUN_HOME "/usr/local/xpu" CACHE PATH "Kunlun XPU SDK root") + endif() + endif() + # The SDK root must contain an XRE runtime tree: either directly + # (`/include/xpu/runtime.h`, `/so/libxpurt.so` as in + # `/usr/local/xpu`) or under an `xre/` subdirectory (as in a + # `torch_xmlir` package root). + if(NOT EXISTS "${KUNLUN_HOME}/include/xpu/runtime.h" AND EXISTS "${KUNLUN_HOME}/xre/include/xpu/runtime.h") + set(KUNLUN_XRE_ROOT "${KUNLUN_HOME}/xre") + else() + set(KUNLUN_XRE_ROOT "${KUNLUN_HOME}") + endif() + if(NOT EXISTS "${KUNLUN_XRE_ROOT}/include/xpu/runtime.h") + message(FATAL_ERROR "WITH_KUNLUN is ON but XRE runtime headers were not found under `${KUNLUN_XRE_ROOT}`. Set KUNLUN_HOME to the Kunlun XPU SDK root.") + endif() + message(STATUS "Using Kunlun XRE from `${KUNLUN_XRE_ROOT}`.") +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_KUNLUN) set(WITH_CPU ON) add_compile_definitions(WITH_CPU=1) endif() @@ -385,6 +410,9 @@ endif() if(WITH_ASCEND) list(APPEND INFINI_RT_PUBLIC_HEADER_DEVICES ascend) endif() +if(WITH_KUNLUN) + list(APPEND INFINI_RT_PUBLIC_HEADER_DEVICES kunlun) +endif() find_package(Python COMPONENTS Interpreter REQUIRED) execute_process( diff --git a/scripts/generate_public_headers.py b/scripts/generate_public_headers.py index 62deb20..701442e 100644 --- a/scripts/generate_public_headers.py +++ b/scripts/generate_public_headers.py @@ -57,6 +57,11 @@ ("ascend", "device_.h", "native/ascend/device_.h"), ("ascend", "runtime_.h", "native/ascend/runtime_.h"), ), + "kunlun": ( + ("kunlun", "data_type_.h", "native/kunlun/data_type_.h"), + ("kunlun", "device_.h", "native/kunlun/device_.h"), + ("kunlun", "runtime_.h", "native/kunlun/runtime_.h"), + ), } _DEVICE_TYPES = { @@ -70,6 +75,7 @@ "moore": "Device::Type::kMoore", "cambricon": "Device::Type::kCambricon", "ascend": "Device::Type::kAscend", + "kunlun": "Device::Type::kKunlun", } _DEFAULT_DEVICE_PRIORITY = ( @@ -82,6 +88,7 @@ "moore", "cambricon", "ascend", + "kunlun", "cpu", ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1dd146f..8e5bb24 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -105,6 +105,28 @@ if(WITH_CAMBRICON) "${CAMBRICON_RUNTIME_LIB}") endif() +if(WITH_KUNLUN) + # KUNLUN_XRE_ROOT is set by the top-level CMakeLists.txt. + target_compile_definitions(infinirt PUBLIC WITH_KUNLUN=1) + + find_library(KUNLUN_RUNTIME_LIB NAMES xpurt + HINTS "${KUNLUN_XRE_ROOT}/so" "${KUNLUN_XRE_ROOT}/lib" "${KUNLUN_XRE_ROOT}/lib64" + REQUIRED) + # `data_type_.h` pulls XTDK builtin device headers (`xpu/kernel/xtdk_*.h`), + # which live under the XTDK clang resource include dir. + find_path(KUNLUN_XTDK_INC NAMES xpu/kernel/xtdk_bf16.h + HINTS "${KUNLUN_XRE_ROOT}/../xtdk/lib/clang/15.0.7/include" + "${KUNLUN_XRE_ROOT}/../xtdk/include" + NO_DEFAULT_PATH) + # Only the `xpu/kernel` subtree may leak into host compilation: the XTDK + # clang resource root also carries `stddef.h` etc. with clang-only syntax + # that breaks the host compiler. + set(KUNLUN_XTDK_DEVICE_INC "${KUNLUN_XTDK_INC}/xpu/kernel") + _infinirt_use_backend_runtime( + "${KUNLUN_XRE_ROOT}/include;${KUNLUN_XTDK_DEVICE_INC}" + "${KUNLUN_RUNTIME_LIB}") +endif() + if(WITH_ASCEND) # ASCEND_HOME is set by the top-level CMakeLists.txt. target_compile_definitions(infinirt PUBLIC WITH_ASCEND=1) diff --git a/src/device.h b/src/device.h index 3c078ee..4ec071b 100644 --- a/src/device.h +++ b/src/device.h @@ -23,6 +23,7 @@ class Device { kHygon = 7, kThead = 8, kMars = 9, + kKunlun = 10, kCount }; @@ -68,6 +69,7 @@ class Device { {Type::kHygon, "hygon"}, {Type::kThead, "thead"}, {Type::kMars, "mars"}, + {Type::kKunlun, "kunlun"}, }}}; static constexpr ConstexprMap; + Device::Type::kMars, Device::Type::kKunlun>; // Deferred computation of active devices. The `Filter` and `FilterList` // evaluation are nested inside a class template so that `DeviceEnabled` diff --git a/src/native/kunlun/data_type_.h b/src/native/kunlun/data_type_.h new file mode 100644 index 0000000..1e01209 --- /dev/null +++ b/src/native/kunlun/data_type_.h @@ -0,0 +1,55 @@ +#include +#include + +#include "data_type.h" +#include "native/kunlun/device_.h" + +// The generated public header now pulls this file into host compilation +// units (upstream per-device data_type includes). XTDK device headers +// (`xpu/kernel/xtdk_*.h`) only compile under the XTDK device compiler +// (`__xpu__`), so host TUs get plain 16-bit payload views while device +// compilation keeps using the native XTDK types. +#ifndef __xpu__ +namespace infini::rt { + +// Host-side 16-bit payload views (bit-identical layout to the device types). +struct kunlun_half { + std::uint16_t bits; +}; + +struct kunlun_bfloat16 { + std::uint16_t bits; +}; + +template <> +struct TypeMap { + using type = kunlun_half; +}; + +template <> +struct TypeMap { + using type = kunlun_bfloat16; +}; + +} // namespace infini::rt +#else +namespace infini::rt { + +// Kunlun XPU3 (XTDK) provides host+device `half` (`__half`, from +// xtdk_fp16.h) and `bfloat16_t` (`__xpu_bfloat16`, from xtdk_bf16.hpp). +using kunlun_half = half; +using kunlun_bfloat16 = bfloat16_t; + +template <> +struct TypeMap { + using type = kunlun_half; +}; + +template <> +struct TypeMap { + using type = kunlun_bfloat16; +}; + +} // namespace infini::rt +#endif // __xpu__ + diff --git a/src/native/kunlun/device_.h b/src/native/kunlun/device_.h new file mode 100644 index 0000000..598377f --- /dev/null +++ b/src/native/kunlun/device_.h @@ -0,0 +1,13 @@ +#ifndef INFINI_RT_KUNLUN_DEVICE__H_ +#define INFINI_RT_KUNLUN_DEVICE__H_ + +#include "device.h" + +namespace infini::rt { + +template <> +struct DeviceEnabled : std::true_type {}; + +} // namespace infini::rt + +#endif diff --git a/src/native/kunlun/runtime_.h b/src/native/kunlun/runtime_.h new file mode 100644 index 0000000..2d0da51 --- /dev/null +++ b/src/native/kunlun/runtime_.h @@ -0,0 +1,223 @@ +#ifndef INFINI_RT_KUNLUN_RUNTIME__H_ +#define INFINI_RT_KUNLUN_RUNTIME__H_ + +#include +#include +#include +#include +#include + +// clang-format off +#include +#include +// clang-format on + +#include "native/kunlun/device_.h" +#include "runtime.h" + +namespace infini::rt::runtime { + +/// Kunlun XPU runtime (XRE `libxpurt`). +/// +/// Wraps the `xpu_*` C API in the CRTP `Runtime` interface. XRE exposes no +/// graph capture, no event query/elapsed-time, and no memset: memset is +/// emulated with chunked H2D copies from a host staging buffer, and the +/// unsupported graph entry points are simply absent from this specialization +/// so the generated dispatch omits them for this device. +template <> +struct Runtime + : DeviceRuntime> { + using Error = int; + + using Stream = XPUStream; + + using Event = XPUEvent; + + static constexpr Device::Type kDeviceType = Device::Type::kKunlun; + + static constexpr Error kSuccess = XPU_SUCCESS; + + static constexpr auto SetDevice = xpu_set_device; + + static constexpr auto GetDevice = xpu_current_device; + + static constexpr auto GetDeviceCount = xpu_device_count; + + static Error DeviceSynchronize() { + // XRE has no whole-device synchronize; `xpu_wait()` waits on the + // default stream, which matches the legacy kunlun backend behavior. + return xpu_wait(); + } + + static constexpr auto Malloc = [](void** ptr, std::size_t size) -> Error { + return xpu_malloc(ptr, static_cast(size)); + }; + + static constexpr auto MallocHost = [](void** ptr, std::size_t size) -> Error { + return xpu_host_alloc(ptr, static_cast(size), 0); + }; + + // XRE has no stream-ordered allocation; fall back to sync allocation, + // mirroring the legacy backend. + static constexpr auto MallocAsync = [](void** ptr, std::size_t size, + Stream) -> Error { + return xpu_malloc(ptr, static_cast(size)); + }; + + static constexpr auto Free = xpu_free; + + static constexpr auto FreeHost = xpu_host_free; + + // XRE has no stream-ordered free; fall back to sync free. + static constexpr auto FreeAsync = [](void* ptr, Stream) -> Error { + return xpu_free(ptr); + }; + + static Error MemGetInfo(std::size_t* free, std::size_t* total) { + (void)free; + (void)total; + return XPUERR_NOSUPPORT; + } + + static Error Memcpy(void* dst, const void* src, std::size_t count, int kind) { + switch (kind) { + case kMemcpyHostToHost: + std::memcpy(dst, src, count); + return kSuccess; + case kMemcpyHostToDevice: + return xpu_memcpy(dst, src, static_cast(count), + XPU_HOST_TO_DEVICE); + case kMemcpyDeviceToHost: + return xpu_memcpy(dst, src, static_cast(count), + XPU_DEVICE_TO_HOST); + case kMemcpyDeviceToDevice: + return xpu_memcpy(dst, src, static_cast(count), + XPU_DEVICE_TO_DEVICE); + default: + return XPUERR_INVALID_PARAM; + } + } + + static Error MemcpyAsync(void* dst, const void* src, std::size_t count, + int kind, Stream stream) { + switch (kind) { + case kMemcpyHostToHost: + std::memcpy(dst, src, count); + return kSuccess; + case kMemcpyHostToDevice: + return xpu_memcpy_async(dst, src, static_cast(count), + XPU_HOST_TO_DEVICE, stream); + case kMemcpyDeviceToHost: + return xpu_memcpy_async(dst, src, static_cast(count), + XPU_DEVICE_TO_HOST, stream); + case kMemcpyDeviceToDevice: + return xpu_memcpy_async(dst, src, static_cast(count), + XPU_DEVICE_TO_DEVICE, stream); + default: + return XPUERR_INVALID_PARAM; + } + } + + /// XRE exposes no memset API: emulate it with chunked H2D copies from a + /// host staging buffer filled with the requested byte value. The staging + /// buffer stays bounded (16 MiB) regardless of the fill size, matching the + /// legacy backend implementation. + static Error Memset(void* ptr, int value, std::size_t count) { + if (ptr == nullptr) { + return XPUERR_INVALID_PARAM; + } + if (count == 0) { + return kSuccess; + } + + static std::mutex staging_mutex; + static std::vector staging_buffer; + constexpr std::size_t kStagingBytes = 16ULL * 1024 * 1024; + + std::lock_guard lock(staging_mutex); + + const std::size_t chunk = std::min(count, kStagingBytes); + if (staging_buffer.size() < chunk) { + staging_buffer.resize(chunk); + } + std::memset(staging_buffer.data(), value, chunk); + + auto* dst = static_cast(ptr); + for (std::size_t done = 0; done < count;) { + const std::size_t n = std::min(chunk, count - done); + const Error status = + xpu_memcpy(dst + done, staging_buffer.data(), + static_cast(n), XPU_HOST_TO_DEVICE); + if (status != kSuccess) { + return status; + } + done += n; + } + return kSuccess; + } + + // XRE has no async memset; fall back to the (host-blocking) sync fill. + static Error MemsetAsync(void* ptr, int value, std::size_t count, Stream) { + return Memset(ptr, value, count); + } + + static constexpr auto StreamCreate = [](Stream* stream) -> Error { + return xpu_stream_create(stream); + }; + + static constexpr auto StreamDestroy = [](Stream stream) -> Error { + return xpu_stream_destroy(stream); + }; + + static constexpr auto StreamSynchronize = [](Stream stream) -> Error { + return xpu_wait(stream); + }; + + // The generated dispatch bridges `StreamWaitEvent(stream, event, flags)`; + // XRE has no flags parameter, so it is ignored. + static Error StreamWaitEvent(Stream stream, Event event, unsigned int) { + return xpu_stream_wait_event(stream, event); + } + + static constexpr auto EventCreate = [](Event* event) -> Error { + return xpu_event_create(event); + }; + + static Error EventCreateWithFlags(Event*, unsigned int) { + return XPUERR_NOSUPPORT; + } + + static constexpr auto EventRecord = [](Event event, Stream stream) -> Error { + return xpu_event_record(event, stream); + }; + + static Error EventQuery(Event) { return XPUERR_NOSUPPORT; } + + static constexpr auto EventSynchronize = [](Event event) -> Error { + return xpu_event_wait(event); + }; + + static constexpr auto EventDestroy = [](Event event) -> Error { + return xpu_event_destroy(event); + }; + + static Error EventElapsedTime(float*, Event, Event) { + return XPUERR_NOSUPPORT; + } + + // Follow the CPU/backend-neutral convention (0..3); the generated dispatch + // maps the public MemcpyKind enum onto these via RuntimeMemcpyKind. + static constexpr int kMemcpyHostToHost = 0; + + static constexpr int kMemcpyHostToDevice = 1; + + static constexpr int kMemcpyDeviceToHost = 2; + + static constexpr int kMemcpyDeviceToDevice = 3; +}; + +static_assert(Runtime::Validate()); + +} // namespace infini::rt::runtime + +#endif