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: 31 additions & 3 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_KUNLUN "Enable Kunlun XPU backend" OFF)

option(AUTO_DETECT_DEVICES "Automatically detect available devices" OFF)
option(INFINI_RT_BUILD_TESTING "Build InfiniRT tests" OFF)
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
# (`<root>/include/xpu/runtime.h`, `<root>/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()
Expand Down Expand Up @@ -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(
Expand Down
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"),
),
"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 = {
Expand All @@ -70,6 +75,7 @@
"moore": "Device::Type::kMoore",
"cambricon": "Device::Type::kCambricon",
"ascend": "Device::Type::kAscend",
"kunlun": "Device::Type::kKunlun",
}

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

Expand Down
22 changes: 22 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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,
kKunlun = 10,
kCount
};

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

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},
{"kunlun", Type::kKunlun},
}}};

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::kKunlun>;

// Deferred computation of active devices. The `Filter` and `FilterList`
// evaluation are nested inside a class template so that `DeviceEnabled`
Expand Down
55 changes: 55 additions & 0 deletions src/native/kunlun/data_type_.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cstdint>
#include <cstring>

#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<Device::Type::kKunlun, DataType::kFloat16> {
using type = kunlun_half;
};

template <>
struct TypeMap<Device::Type::kKunlun, DataType::kBFloat16> {
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<Device::Type::kKunlun, DataType::kFloat16> {
using type = kunlun_half;
};

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

} // namespace infini::rt
#endif // __xpu__

13 changes: 13 additions & 0 deletions src/native/kunlun/device_.h
Original file line number Diff line number Diff line change
@@ -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<Device::Type::kKunlun> : std::true_type {};

} // namespace infini::rt

#endif
Loading