From d45c0cee78a63af0caec079cde154b25e952ce93 Mon Sep 17 00:00:00 2001 From: rubik Date: Wed, 18 Nov 2026 17:03:53 +0800 Subject: [PATCH] feat(kunlun): add BKCL backend for collective communication Register Kunlun as device type 10 and BKCL as backend type 7, over the vendor xccl_* C API in libbkcl.so: - send/recv/all_gather/all_reduce/reduce_scatter/comm_init_rank/ comm_destroy/get_unique_id inherit the common backend; broadcast is implemented directly (non-root ranks pass NULL sendbuf) - int16/uint16/uint32/uint64 rejected (no XCCL type mappings); no AVG RedOp - cmake/devices/Kunlun.cmake + cmake/backends/BKCL.cmake; gen_bridge.py registers bkcl; DevicePriority = 5 (without it dispatch silently falls back to CPU, rc=8 NotSupported) - examples/ccl/pp_p2p.cc: two-process TCP-rendezvous test --- CMakeLists.txt | 28 ++- cmake/backends/BKCL.cmake | 29 +++ cmake/devices/Kunlun.cmake | 51 ++++ examples/CMakeLists.txt | 15 ++ examples/ccl/pp_p2p.cc | 265 ++++++++++++++++++++ scripts/gen_bridge.py | 3 + src/CMakeLists.txt | 21 ++ src/backend.h | 8 +- src/backend_device_map.h | 4 + src/backends/ccl/bkcl/api.h | 86 +++++++ src/backends/ccl/bkcl/checks.h | 31 +++ src/backends/ccl/bkcl/impl/all_gather.h | 17 ++ src/backends/ccl/bkcl/impl/all_reduce.h | 17 ++ src/backends/ccl/bkcl/impl/broadcast.h | 53 ++++ src/backends/ccl/bkcl/impl/comm_destroy.h | 17 ++ src/backends/ccl/bkcl/impl/comm_init_rank.h | 17 ++ src/backends/ccl/bkcl/impl/get_unique_id.h | 17 ++ src/backends/ccl/bkcl/impl/recv.h | 17 ++ src/backends/ccl/bkcl/impl/reduce_scatter.h | 17 ++ src/backends/ccl/bkcl/impl/send.h | 17 ++ src/backends/ccl/bkcl/kunlun/api.h | 15 ++ src/backends/ccl/bkcl/type_map.h | 89 +++++++ src/device.h | 21 +- src/devices/kunlun/data_type_.h | 28 +++ src/devices/kunlun/device_.h | 24 ++ src/devices/kunlun/runtime_.h | 100 ++++++++ 26 files changed, 997 insertions(+), 10 deletions(-) create mode 100644 cmake/backends/BKCL.cmake create mode 100644 cmake/devices/Kunlun.cmake create mode 100644 examples/ccl/pp_p2p.cc create mode 100644 src/backends/ccl/bkcl/api.h create mode 100644 src/backends/ccl/bkcl/checks.h create mode 100644 src/backends/ccl/bkcl/impl/all_gather.h create mode 100644 src/backends/ccl/bkcl/impl/all_reduce.h create mode 100644 src/backends/ccl/bkcl/impl/broadcast.h create mode 100644 src/backends/ccl/bkcl/impl/comm_destroy.h create mode 100644 src/backends/ccl/bkcl/impl/comm_init_rank.h create mode 100644 src/backends/ccl/bkcl/impl/get_unique_id.h create mode 100644 src/backends/ccl/bkcl/impl/recv.h create mode 100644 src/backends/ccl/bkcl/impl/reduce_scatter.h create mode 100644 src/backends/ccl/bkcl/impl/send.h create mode 100644 src/backends/ccl/bkcl/kunlun/api.h create mode 100644 src/backends/ccl/bkcl/type_map.h create mode 100644 src/devices/kunlun/data_type_.h create mode 100644 src/devices/kunlun/device_.h create mode 100644 src/devices/kunlun/runtime_.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 918d596..09a93df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ option(WITH_NCCL "Enable NCCL backend" OFF) option(WITH_MCCL "Enable MCCL backend" OFF) option(WITH_CNCL "Enable CNCL backend" OFF) option(WITH_HCCL "Enable HCCL backend" OFF) +option(WITH_BKCL "Enable BKCL backend" OFF) # ========================================================= # --- MISC. BUILD OPTIONS --- @@ -45,6 +46,7 @@ include(cmake/devices/Cambricon.cmake) include(cmake/devices/Ascend.cmake) include(cmake/devices/Hygon.cmake) include(cmake/devices/THead.cmake) +include(cmake/devices/Kunlun.cmake) # Backend modules provide backend probes and required dependency resolution. include(cmake/backends/MPI.cmake) @@ -52,6 +54,7 @@ include(cmake/backends/NCCL.cmake) include(cmake/backends/MCCL.cmake) include(cmake/backends/CNCL.cmake) include(cmake/backends/HCCL.cmake) +include(cmake/backends/BKCL.cmake) # ========================================================= # --- AUTO-DETECTION: DEVICES --- @@ -92,6 +95,10 @@ if(AUTO_DETECT_DEVICES) infiniccl_resolve_device_detection( WITH_THEAD "T-Head" infiniccl_detect_thead THEAD_FOUND) + # Kunlun + infiniccl_resolve_device_detection( + WITH_KUNLUN "Kunlun" infiniccl_detect_kunlun KUNLUN_FOUND) + # NVIDIA Final Resolution: resolve conflicts. if(THEAD_FOUND AND NVIDIA_FOUND AND NOT NVIDIA_REQUESTED) set(NVIDIA_FOUND FALSE) @@ -163,10 +170,21 @@ if(AUTO_DETECT_BACKENDS) message(STATUS "HCCL library/headers not found in Ascend paths.") endif() endif() + + # Detect BKCL Dependencies + if(WITH_KUNLUN) + infiniccl_probe_bkcl(BKCL_FOUND) + if(BKCL_FOUND) + set(WITH_BKCL ON) + message(STATUS "Auto-detected BKCL backend.") + else() + message(STATUS "BKCL library/headers not found in Kunlun paths.") + endif() + endif() endif() # Fallback: If no backends are enabled or auto-detected, fall back to OpenMPI as the default bootstrap profile. -if(NOT WITH_OMPI AND NOT WITH_MPICH AND NOT WITH_NCCL AND NOT WITH_MCCL AND NOT WITH_CNCL AND NOT WITH_HCCL) +if(NOT WITH_OMPI AND NOT WITH_MPICH AND NOT WITH_NCCL AND NOT WITH_MCCL AND NOT WITH_CNCL AND NOT WITH_HCCL AND NOT WITH_BKCL) set(WITH_OMPI ON) message(STATUS "No backend specified or detected. Defaulting to `WITH_OMPI=ON`") endif() @@ -237,6 +255,14 @@ if(WITH_HCCL) infiniccl_configure_hccl(HCCL_INC HCCL_LIB) endif() +if(WITH_KUNLUN) + infiniccl_configure_kunlun(KUNLUN_INC KUNLUN_XPURT_LIB) +endif() + +if(WITH_BKCL) + infiniccl_configure_bkcl(BKCL_INC BKCL_LIB) +endif() + # Python is required for code generation. find_package(Python3 REQUIRED) diff --git a/cmake/backends/BKCL.cmake b/cmake/backends/BKCL.cmake new file mode 100644 index 0000000..21612e5 --- /dev/null +++ b/cmake/backends/BKCL.cmake @@ -0,0 +1,29 @@ +include_guard(GLOBAL) + +function(infiniccl_probe_bkcl output) + infiniccl_collect_kunlun_hints(_hints) + find_path(_INFINICCL_AUTO_BKCL_INC NAMES xccl.h + HINTS ${_hints} PATH_SUFFIXES xccl/include include) + find_library(_INFINICCL_AUTO_BKCL_LIB NAMES bkcl + HINTS ${_hints} PATH_SUFFIXES lib lib64) + if(_INFINICCL_AUTO_BKCL_INC AND _INFINICCL_AUTO_BKCL_LIB) + set(${output} TRUE PARENT_SCOPE) + else() + set(${output} FALSE PARENT_SCOPE) + endif() +endfunction() + +function(infiniccl_configure_bkcl include_output library_output) + if(NOT WITH_KUNLUN) + message(FATAL_ERROR "BKCL backend requires Kunlun XPU support. Please enable `WITH_KUNLUN`.") + endif() + + infiniccl_collect_kunlun_hints(_hints) + find_path(_INFINICCL_BKCL_INC NAMES xccl.h + HINTS ${_hints} PATH_SUFFIXES xccl/include include REQUIRED) + find_library(_INFINICCL_BKCL_LIB NAMES bkcl + HINTS ${_hints} PATH_SUFFIXES lib lib64 REQUIRED) + + set(${include_output} "${_INFINICCL_BKCL_INC}" PARENT_SCOPE) + set(${library_output} "${_INFINICCL_BKCL_LIB}" PARENT_SCOPE) +endfunction() diff --git a/cmake/devices/Kunlun.cmake b/cmake/devices/Kunlun.cmake new file mode 100644 index 0000000..4c17bf4 --- /dev/null +++ b/cmake/devices/Kunlun.cmake @@ -0,0 +1,51 @@ +include_guard(GLOBAL) + +function(infiniccl_collect_kunlun_hints output) + set(_hints) + if(KUNLUN_HOME) + list(APPEND _hints "${KUNLUN_HOME}") + endif() + if(DEFINED ENV{KUNLUN_HOME} AND NOT "$ENV{KUNLUN_HOME}" STREQUAL "") + list(APPEND _hints "$ENV{KUNLUN_HOME}") + endif() + list(APPEND _hints /usr/local/xtdk) + list(REMOVE_DUPLICATES _hints) + set(${output} "${_hints}" PARENT_SCOPE) +endfunction() + +function(infiniccl_detect_kunlun output) + set(_found FALSE) + file(GLOB _KUNLUN_DEV_FILES "/dev/xpu*") + if(_KUNLUN_DEV_FILES) + set(_found TRUE) + else() + find_program(_INFINICCL_KUNLUN_SMI_PATH xpu-smi) + if(_INFINICCL_KUNLUN_SMI_PATH) + execute_process( + COMMAND ${_INFINICCL_KUNLUN_SMI_PATH} + RESULT_VARIABLE _result + OUTPUT_QUIET ERROR_QUIET TIMEOUT 15) + if(_result EQUAL 0) + set(_found TRUE) + endif() + endif() + endif() + set(${output} ${_found} PARENT_SCOPE) +endfunction() + +function(infiniccl_configure_kunlun include_output runtime_library_output) + infiniccl_collect_kunlun_hints(_hints) + + # `xccl.h` (communication API) ships in the XTDK `xccl/` tree; the XRE + # runtime headers (`xpu/runtime.h`) in `xre/`. + find_path(_INFINICCL_BKCL_INC NAMES xccl.h + HINTS ${_hints} PATH_SUFFIXES xccl/include include REQUIRED) + find_path(_INFINICCL_XRE_INC NAMES xpu/runtime.h + HINTS ${_hints} PATH_SUFFIXES xre/include include REQUIRED) + find_library(_INFINICCL_XPURT_LIB NAMES xpurt + HINTS ${_hints} PATH_SUFFIXES xre/so lib lib64 REQUIRED) + + set(${include_output} "${_INFINICCL_BKCL_INC};${_INFINICCL_XRE_INC}" + PARENT_SCOPE) + set(${runtime_library_output} "${_INFINICCL_XPURT_LIB}" PARENT_SCOPE) +endfunction() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2500965..850de81 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -12,6 +12,12 @@ foreach(source_file ${EXAMPLE_SOURCES}) set(target_name ${file_we}) endif() + # The BKCL p2p example drives the kunlun runtime directly and cannot + # compile against backends that lack stream primitives (e.g. CPU). + if(${file_we} STREQUAL "pp_p2p" AND NOT WITH_KUNLUN) + continue() + endif() + add_executable(${target_name} ${source_file}) set_target_properties(${target_name} PROPERTIES @@ -65,6 +71,11 @@ foreach(source_file ${EXAMPLE_SOURCES}) target_link_libraries(${target_name} PRIVATE ${HYGON_HIP_RUNTIME_LIB}) endif() + if(WITH_KUNLUN) + target_include_directories(${target_name} PRIVATE ${KUNLUN_INC}) + target_link_libraries(${target_name} PRIVATE ${KUNLUN_XPURT_LIB}) + endif() + if(WITH_OMPI OR WITH_MPICH) target_link_libraries(${target_name} PRIVATE MPI::MPI_CXX) endif() @@ -86,6 +97,10 @@ foreach(source_file ${EXAMPLE_SOURCES}) target_include_directories(${target_name} PRIVATE ${HCCL_INC}) endif() + if(WITH_BKCL) + target_include_directories(${target_name} PRIVATE ${BKCL_INC}) + endif() + # Explicitly allow examples to "peek" into the internal `src` and binary dirs. # This is necessary because these were marked `PRIVATE` in the library's CMake. target_include_directories(${target_name} PRIVATE diff --git a/examples/ccl/pp_p2p.cc b/examples/ccl/pp_p2p.cc new file mode 100644 index 0000000..85c453c --- /dev/null +++ b/examples/ccl/pp_p2p.cc @@ -0,0 +1,265 @@ +/** + * InfiniCCL Example: Two-Process Kunlun P2P + AllReduce + * + * Pipeline-parallel smoke test without an MPI launcher: rank 0 creates the + * XCCL unique id and hands it to rank 1 over a TCP side channel, then both + * ranks run Send/Recv and AllReduce through the BKCL backend. + * + * Usage: + * XPU_VISIBLE_DEVICES=0 ./pp_p2p --role sender --master-port 29530 + * XPU_VISIBLE_DEVICES=1 ./pp_p2p --role receiver --master-port 29530 + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "backend_manifest.h" +#include "infiniccl.h" +#include "utils.h" + +using namespace infini::ccl; + +namespace { + +constexpr int kRankCount = 2; +constexpr size_t kNumElements = 1 << 16; +constexpr float kSendValue = 7.0f; + +struct Options { + std::string role = "sender"; + std::string master_addr = "127.0.0.1"; + int master_port = 29530; + int device = -1; // defaults to rank +}; + +Options ParseArgs(int argc, char** argv) { + Options opts; + for (int i = 1; i < argc; ++i) { + const std::string arg = argv[i]; + auto next = [&]() -> std::string { + return (i + 1 < argc) ? std::string(argv[++i]) : std::string(); + }; + if (arg == "--role") { + opts.role = next(); + } else if (arg == "--master-addr") { + opts.master_addr = next(); + } else if (arg == "--master-port") { + opts.master_port = std::stoi(next()); + } else if (arg == "--device") { + opts.device = std::stoi(next()); + } + } + return opts; +} + +int RankFromRole(const std::string& role) { + if (role == "sender") { + return 0; + } + if (role == "receiver") { + return 1; + } + std::cerr << "Unknown role: " << role << " (use sender|receiver)" + << std::endl; + std::exit(EXIT_FAILURE); +} + +// Rank 0 hosts the side channel; rank 1 dials in and fetches the unique id. +void ExchangeUniqueId(int rank, const Options& opts, char* id_bytes, + size_t id_len) { + if (rank == 0) { + const int server = ::socket(AF_INET, SOCK_STREAM, 0); + if (server < 0) { + std::cerr << "socket() failed" << std::endl; + std::exit(EXIT_FAILURE); + } + int reuse = 1; + ::setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons(static_cast(opts.master_port)); + if (::bind(server, reinterpret_cast(&addr), sizeof(addr)) < 0 || + ::listen(server, 1) < 0) { + std::cerr << "bind/listen failed on port " << opts.master_port + << std::endl; + std::exit(EXIT_FAILURE); + } + std::cout << "[rank0] waiting for rank1 on port " << opts.master_port + << std::endl; + const int conn = ::accept(server, nullptr, nullptr); + if (conn < 0) { + std::cerr << "accept() failed" << std::endl; + std::exit(EXIT_FAILURE); + } + size_t sent = 0; + while (sent < id_len) { + const ssize_t n = ::send(conn, id_bytes + sent, id_len - sent, 0); + if (n <= 0) { + std::cerr << "send(unique_id) failed" << std::endl; + std::exit(EXIT_FAILURE); + } + sent += static_cast(n); + } + ::close(conn); + ::close(server); + return; + } + + const int sock = ::socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + std::cerr << "socket() failed" << std::endl; + std::exit(EXIT_FAILURE); + } + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(static_cast(opts.master_port)); + if (::inet_pton(AF_INET, opts.master_addr.c_str(), &addr.sin_addr) != 1) { + std::cerr << "invalid master addr: " << opts.master_addr << std::endl; + std::exit(EXIT_FAILURE); + } + for (int attempt = 1; attempt <= 30; ++attempt) { + if (::connect(sock, reinterpret_cast(&addr), sizeof(addr)) == + 0) { + break; + } + if (attempt == 30) { + std::cerr << "connect() to " << opts.master_addr << ":" + << opts.master_port << " failed" << std::endl; + std::exit(EXIT_FAILURE); + } + ::sleep(1); + } + size_t received = 0; + while (received < id_len) { + const ssize_t n = ::recv(sock, id_bytes + received, id_len - received, 0); + if (n <= 0) { + std::cerr << "recv(unique_id) failed" << std::endl; + std::exit(EXIT_FAILURE); + } + received += static_cast(n); + } + ::close(sock); +} + +} // namespace + +int main(int argc, char** argv) { + const Options opts = ParseArgs(argc, argv); + const int rank = RankFromRole(opts.role); + const int device = opts.device >= 0 ? opts.device : rank; + + constexpr Device::Type kDevType = + ListGetBest(EnabledDevices{}); + using Rt = Runtime; + + CHECK_RT(Rt, Rt::SetDevice(device)); + std::cout << "[rank" << rank << "] using device " << device << std::endl; + + // 1. Bootstrap: distribute the XCCL unique id over the TCP side channel. + infinicclUniqueId shared_id{}; + if (rank == 0) { + CHECK_INFINI(infinicclGetUniqueId(&shared_id)); + } + ExchangeUniqueId(rank, opts, shared_id.internal, sizeof(shared_id.internal)); + std::cout << "[rank" << rank << "] unique id exchanged" << std::endl; + + // 2. Build the communicator. + infinicclComm_t comm = nullptr; + CHECK_INFINI(infinicclCommInitRank(&comm, kRankCount, shared_id, rank)); + std::cout << "[rank" << rank << "] communicator ready" << std::endl; + + Rt::Stream stream = nullptr; + CHECK_RT(Rt, Rt::StreamCreate(&stream)); + + std::atomic_bool all_correct{true}; + + float* device_buffer = nullptr; + const size_t total_bytes = kNumElements * sizeof(float); + CHECK_RT(Rt, + Rt::Malloc(reinterpret_cast(&device_buffer), total_bytes)); + + // 3. P2P send/recv: rank0 -> rank1. + std::cout << "[rank" << rank << "] posting p2p " << std::endl; + std::vector host(kNumElements, rank == 0 ? kSendValue : 0.0f); + CHECK_RT(Rt, Rt::Memcpy(device_buffer, host.data(), total_bytes, + Rt::MemcpyHostToDevice)); + std::cout << "[rank" << rank << "] h2d done, calling ccl" << std::endl; + if (rank == 0) { + CHECK_INFINI(infinicclSend(device_buffer, kNumElements, infinicclFloat32, 1, + comm, stream)); + } else { + CHECK_INFINI(infinicclRecv(device_buffer, kNumElements, infinicclFloat32, 0, + comm, stream)); + } + CHECK_RT(Rt, Rt::StreamSynchronize(stream)); + if (rank == 1) { + CHECK_RT(Rt, Rt::Memcpy(host.data(), device_buffer, total_bytes, + Rt::MemcpyDeviceToHost)); + const bool correct = Validator::ValidateResult( + host.data(), kNumElements, kSendValue, rank, true, "Send/Recv"); + all_correct.store(correct, std::memory_order_relaxed); + } + std::cout << "[rank" << rank << "] send/recv done" << std::endl; + + // 4. AllReduce(sum): each rank contributes rank+1 -> expect 3.0. + std::cout << "[rank" << rank << "] posting all_reduce" << std::endl; + const float own = static_cast(rank + 1); + std::fill(host.begin(), host.end(), own); + CHECK_RT(Rt, Rt::Memcpy(device_buffer, host.data(), total_bytes, + Rt::MemcpyHostToDevice)); + CHECK_INFINI(infinicclAllReduce(device_buffer, device_buffer, kNumElements, + infinicclFloat32, infinicclSum, comm, + stream)); + CHECK_RT(Rt, Rt::StreamSynchronize(stream)); + CHECK_RT(Rt, Rt::Memcpy(host.data(), device_buffer, total_bytes, + Rt::MemcpyDeviceToHost)); + { + const bool correct = Validator::ValidateResult( + host.data(), kNumElements, 3.0f, rank, true, "AllReduce(sum)"); + if (!correct) { + all_correct.store(false, std::memory_order_relaxed); + } + } + + // 5. Broadcast from root 0: everyone must observe kSendValue. + std::cout << "[rank" << rank << "] posting broadcast" << std::endl; + std::fill(host.begin(), host.end(), rank == 0 ? kSendValue : -1.0f); + CHECK_RT(Rt, Rt::Memcpy(device_buffer, host.data(), total_bytes, + Rt::MemcpyHostToDevice)); + CHECK_INFINI(infinicclBroadcast(device_buffer, device_buffer, kNumElements, + infinicclFloat32, 0, comm, stream)); + CHECK_RT(Rt, Rt::StreamSynchronize(stream)); + CHECK_RT(Rt, Rt::Memcpy(host.data(), device_buffer, total_bytes, + Rt::MemcpyDeviceToHost)); + { + const bool correct = Validator::ValidateResult( + host.data(), kNumElements, kSendValue, rank, true, "Broadcast"); + if (!correct) { + all_correct.store(false, std::memory_order_relaxed); + } + } + + CHECK_RT(Rt, Rt::StreamDestroy(stream)); + CHECK_RT(Rt, Rt::Free(device_buffer)); + CHECK_INFINI(infinicclCommDestroy(comm)); + + if (!all_correct.load(std::memory_order_relaxed)) { + std::cerr << "[rank" << rank << "] PP P2P validation FAILED." << std::endl; + return EXIT_FAILURE; + } + std::cout + << "[rank" << rank + << "] PP P2P validation passed (send/recv + all_reduce + broadcast)." + << std::endl; + return EXIT_SUCCESS; +} diff --git a/scripts/gen_bridge.py b/scripts/gen_bridge.py index a472c05..34732a9 100644 --- a/scripts/gen_bridge.py +++ b/scripts/gen_bridge.py @@ -34,6 +34,7 @@ "mccl": ["backends/ccl/mccl/impl"], "cncl": ["backends/ccl/cncl/impl"], "hccl": ["backends/ccl/hccl/impl"], + "bkcl": ["backends/ccl/bkcl/impl"], } BACKEND_COMMON_HEADERS = { @@ -41,6 +42,7 @@ "mccl": ["backends/ccl/mccl/type_map.h"], "cncl": ["backends/ccl/cncl/type_map.h"], "hccl": ["backends/ccl/hccl/type_map.h"], + "bkcl": ["backends/ccl/bkcl/type_map.h"], } CCL_PROVIDER_BACKENDS = { @@ -48,6 +50,7 @@ "mccl": "backends/ccl/mccl", "cncl": "backends/ccl/cncl", "hccl": "backends/ccl/hccl", + "bkcl": "backends/ccl/bkcl", } # ================================================================= diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index affb03f..ea48197 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -249,6 +249,17 @@ if(WITH_ASCEND) target_link_libraries(infiniccl PRIVATE ${ASCENDCL_LIB} ${ASCEND_NNOPBASE_LIB}) endif() +# Kunlun +if(WITH_KUNLUN) + list(APPEND DEVICE_LIST "kunlun") + + file(GLOB_RECURSE KUNLUN_SRCS "devices/kunlun/*.cc" "devices/kunlun/*.cpp") + + target_sources(infiniccl PRIVATE ${KUNLUN_SRCS}) + target_include_directories(infiniccl PRIVATE ${KUNLUN_INC}) + target_link_libraries(infiniccl PRIVATE ${KUNLUN_XPURT_LIB}) +endif() + # Hygon if(WITH_HYGON) list(APPEND DEVICE_LIST "hygon") @@ -337,6 +348,16 @@ if(WITH_HCCL) target_link_libraries(infiniccl PRIVATE ${HCCL_LIB} ${HCCL_OPAPI_LIB}) endif() +# BKCL +if(WITH_BKCL) + list(APPEND BACKEND_LIST "bkcl") + file(GLOB_RECURSE BKCL_SRCS "backends/ccl/bkcl/*.cc" "backends/ccl/bkcl/*.cpp") + + target_sources(infiniccl PRIVATE ${BKCL_SRCS}) + target_include_directories(infiniccl PRIVATE ${BKCL_INC}) + target_link_libraries(infiniccl PRIVATE ${BKCL_LIB}) +endif() + # ========================================================= # --- File Generation --- # ========================================================= diff --git a/src/backend.h b/src/backend.h index c48c744..f59d09f 100644 --- a/src/backend.h +++ b/src/backend.h @@ -15,13 +15,14 @@ enum class BackendType : int8_t { kRccl = 4, kCncl = 5, kHccl = 6, + kBkcl = 7, kCount }; using AllBackendTypes = List; + BackendType::kHccl, BackendType::kBkcl>; template struct BackendEnabled : std::false_type {}; @@ -73,6 +74,11 @@ struct BackendPriority { static constexpr int value = 10; }; +template <> +struct BackendPriority { + static constexpr int value = 10; +}; + } // namespace infini::ccl #endif // INFINI_CCL_BACKEND_H_ diff --git a/src/backend_device_map.h b/src/backend_device_map.h index dfb8ad1..130080c 100644 --- a/src/backend_device_map.h +++ b/src/backend_device_map.h @@ -45,6 +45,10 @@ template <> struct IsSupportedCombination : std::true_type {}; +template <> +struct IsSupportedCombination + : std::true_type {}; + }; // namespace infini::ccl #endif // INFINI_CCL_BACKEND_DEVICE_MAP_H_ diff --git a/src/backends/ccl/bkcl/api.h b/src/backends/ccl/bkcl/api.h new file mode 100644 index 0000000..d8573a5 --- /dev/null +++ b/src/backends/ccl/bkcl/api.h @@ -0,0 +1,86 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_API_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_API_H_ + +#include +#include + +#include "backends/ccl/common/api.h" +#include "backends/ccl/bkcl/checks.h" +#include "logging.h" +#include "return_status_impl.h" +#include "runtime.h" + +namespace infini::ccl { + +template +struct BkclApi { + static constexpr BackendType kBackendType = BackendType::kBkcl; + static constexpr Device::Type kDeviceType = device; + + using Comm = XCCLContext_t; + using UniqueId = XCCLUniqueId; + using Result = XCCLResult_t; + using DataType = XCCLDataType; + using RedOp = XCCLOp; + using Stream = typename Runtime::Stream; + + static ReturnStatus Check(Result result) { + if (result != XCCL_SUCCESS) { + LOG(("BKCL/XCCL error code: " + std::to_string(static_cast(result))) + .c_str()); + return ReturnStatus::kSystemError; + } + return ReturnStatus::kSuccess; + } + + static Result GetUniqueId(UniqueId* id) { return xccl_get_unique_id(id); } + + static Result CommInitRank(Comm* comm, int nranks, const UniqueId& id, + int rank) { + return xccl_init_rank(comm, rank, nranks, &id); + } + + static Result CommDestroy(Comm comm) { return xccl_destroy_context(comm); } + + static Result AllReduce(const void* send_buff, void* recv_buff, size_t count, + DataType data_type, RedOp op, Comm comm, + Stream stream) { + return xccl_all_reduce(comm, send_buff, recv_buff, count, data_type, op, + stream); + } + + static Result AllGather(const void* send_buff, void* recv_buff, + size_t send_count, DataType data_type, Comm comm, + Stream stream) { + return xccl_all_gather(comm, send_buff, send_count, recv_buff, data_type, + stream); + } + + static Result ReduceScatter(const void* send_buff, void* recv_buff, + size_t recv_count, DataType data_type, RedOp op, + Comm comm, Stream stream) { + return xccl_reduce_scatter(comm, send_buff, recv_buff, recv_count, + data_type, op, stream); + } + + static Result Send(const void* send_buff, size_t count, DataType data_type, + int peer, Comm comm, Stream stream) { + return xccl_send(comm, send_buff, count, peer, data_type, stream); + } + + static Result Recv(void* recv_buff, size_t count, DataType data_type, + int peer, Comm comm, Stream stream) { + return xccl_recv(comm, recv_buff, count, peer, data_type, stream); + } + + static Result Broadcast(const void* send_buff, void* recv_buff, size_t count, + DataType data_type, int root, Comm comm, + Stream stream) { + return xccl_broadcast(comm, send_buff, recv_buff, count, data_type, root, + stream); + } +}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_API_H_ diff --git a/src/backends/ccl/bkcl/checks.h b/src/backends/ccl/bkcl/checks.h new file mode 100644 index 0000000..4f086fa --- /dev/null +++ b/src/backends/ccl/bkcl/checks.h @@ -0,0 +1,31 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_CHECKS_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_CHECKS_H_ + +#include + +#include + +#include "return_status_impl.h" + +#define INFINI_CHECK_BKCL(result) \ + ::infini::ccl::detail::CheckBkclImpl((result), __FILE__, __LINE__) + +namespace infini::ccl { + +namespace detail { + +inline ReturnStatus CheckBkclImpl(XCCLResult_t bkcl_result, const char* file, + int line) { + if (bkcl_result != XCCL_SUCCESS) { + std::cerr << "backend(bkcl) BKCL error code: " << bkcl_result + << " at line " << line << " in " << file << std::endl; + std::abort(); + } + return ReturnStatus::kSuccess; +} + +} // namespace detail + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_CHECKS_H_ diff --git a/src/backends/ccl/bkcl/impl/all_gather.h b/src/backends/ccl/bkcl/impl/all_gather.h new file mode 100644 index 0000000..9da39ea --- /dev/null +++ b/src/backends/ccl/bkcl/impl/all_gather.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_ALL_GATHER_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_ALL_GATHER_H_ + +#include "backends/ccl/common/impl/all_gather.h" + +namespace infini::ccl { + +template +class AllGatherImpl + : public CclAllGatherImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_ALL_GATHER_H_ diff --git a/src/backends/ccl/bkcl/impl/all_reduce.h b/src/backends/ccl/bkcl/impl/all_reduce.h new file mode 100644 index 0000000..1bc2d47 --- /dev/null +++ b/src/backends/ccl/bkcl/impl/all_reduce.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_ALL_REDUCE_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_ALL_REDUCE_H_ + +#include "backends/ccl/common/impl/all_reduce.h" + +namespace infini::ccl { + +template +class AllReduceImpl + : public CclAllReduceImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_ALL_REDUCE_H_ diff --git a/src/backends/ccl/bkcl/impl/broadcast.h b/src/backends/ccl/bkcl/impl/broadcast.h new file mode 100644 index 0000000..3ccd04f --- /dev/null +++ b/src/backends/ccl/bkcl/impl/broadcast.h @@ -0,0 +1,53 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_BROADCAST_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_BROADCAST_H_ + +#include "backends/ccl/common/api.h" +#include "backends/ccl/common/comm_instance.h" +#include "base/broadcast.h" +#include "communicator.h" + +namespace infini::ccl { + +// The common impls have no broadcast (NCCL relies on `Bcast` -> in-place +// `Broadcast` only), so BKCL provides one directly. XCCL reads the payload +// from `send_buff` on the root rank; every other rank must pass NULL there +// (`xccl_broadcast` contract), which maps 1:1 onto the InfiniCCL signature. +template +class BroadcastImpl { + public: + static ReturnStatus Apply(const void* send_buff, void* recv_buff, + size_t count, DataType data_type, int root, + Communicator* comm, void* stream) { + using Api = CclApi; + using TypeMap = CclTypeMap; + using CommInstance = CclCommInstance; + + auto* comm_internal = static_cast(comm); + if (!comm_internal) { + return ReturnStatus::kInternalError; + } + + auto* intra = static_cast(comm_internal->intra_comm()); + if (!intra || !intra->handle) { + return ReturnStatus::kInternalError; + } + + typename Api::DataType ccl_type{}; + if (!TypeMap::ToBackendDataType(data_type, &ccl_type)) { + return ReturnStatus::kNotSupported; + } + + const bool is_root = comm_internal->rank() == root; + return Api::Check(Api::Broadcast(is_root ? send_buff : nullptr, recv_buff, + count, ccl_type, root, intra->handle, + reinterpret_cast( + stream))); + } +}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_BROADCAST_H_ diff --git a/src/backends/ccl/bkcl/impl/comm_destroy.h b/src/backends/ccl/bkcl/impl/comm_destroy.h new file mode 100644 index 0000000..5015454 --- /dev/null +++ b/src/backends/ccl/bkcl/impl/comm_destroy.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_COMM_DESTROY_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_COMM_DESTROY_H_ + +#include "backends/ccl/common/impl/comm_destroy.h" + +namespace infini::ccl { + +template +class CommDestroyImpl + : public CclCommDestroyImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_COMM_DESTROY_H_ diff --git a/src/backends/ccl/bkcl/impl/comm_init_rank.h b/src/backends/ccl/bkcl/impl/comm_init_rank.h new file mode 100644 index 0000000..2e97d13 --- /dev/null +++ b/src/backends/ccl/bkcl/impl/comm_init_rank.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_COMM_INIT_RANK_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_COMM_INIT_RANK_H_ + +#include "backends/ccl/common/impl/comm_init_rank.h" + +namespace infini::ccl { + +template +class CommInitRankImpl + : public CclCommInitRankImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_COMM_INIT_RANK_H_ diff --git a/src/backends/ccl/bkcl/impl/get_unique_id.h b/src/backends/ccl/bkcl/impl/get_unique_id.h new file mode 100644 index 0000000..b1f541f --- /dev/null +++ b/src/backends/ccl/bkcl/impl/get_unique_id.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_GET_UNIQUE_ID_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_GET_UNIQUE_ID_H_ + +#include "backends/ccl/common/impl/get_unique_id.h" + +namespace infini::ccl { + +template +class GetUniqueIdImpl + : public CclGetUniqueIdImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_GET_UNIQUE_ID_H_ diff --git a/src/backends/ccl/bkcl/impl/recv.h b/src/backends/ccl/bkcl/impl/recv.h new file mode 100644 index 0000000..9271326 --- /dev/null +++ b/src/backends/ccl/bkcl/impl/recv.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_RECV_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_RECV_H_ + +#include "backends/ccl/common/impl/recv.h" + +namespace infini::ccl { + +template +class RecvImpl + : public CclRecvImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_RECV_H_ diff --git a/src/backends/ccl/bkcl/impl/reduce_scatter.h b/src/backends/ccl/bkcl/impl/reduce_scatter.h new file mode 100644 index 0000000..9d19758 --- /dev/null +++ b/src/backends/ccl/bkcl/impl/reduce_scatter.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_REDUCE_SCATTER_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_REDUCE_SCATTER_H_ + +#include "backends/ccl/common/impl/reduce_scatter.h" + +namespace infini::ccl { + +template +class ReduceScatterImpl + : public CclReduceScatterImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_REDUCE_SCATTER_H_ diff --git a/src/backends/ccl/bkcl/impl/send.h b/src/backends/ccl/bkcl/impl/send.h new file mode 100644 index 0000000..e8e7d15 --- /dev/null +++ b/src/backends/ccl/bkcl/impl/send.h @@ -0,0 +1,17 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_SEND_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_SEND_H_ + +#include "backends/ccl/common/impl/send.h" + +namespace infini::ccl { + +template +class SendImpl + : public CclSendImpl {}; + +template <> +struct BackendEnabled : std::true_type {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_IMPL_SEND_H_ diff --git a/src/backends/ccl/bkcl/kunlun/api.h b/src/backends/ccl/bkcl/kunlun/api.h new file mode 100644 index 0000000..0431dad --- /dev/null +++ b/src/backends/ccl/bkcl/kunlun/api.h @@ -0,0 +1,15 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_KUNLUN_API_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_KUNLUN_API_H_ + +#include "backends/ccl/bkcl/api.h" +#include "devices/kunlun/runtime_.h" + +namespace infini::ccl { + +template <> +struct CclApi + : BkclApi {}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_KUNLUN_API_H_ diff --git a/src/backends/ccl/bkcl/type_map.h b/src/backends/ccl/bkcl/type_map.h new file mode 100644 index 0000000..9e45c78 --- /dev/null +++ b/src/backends/ccl/bkcl/type_map.h @@ -0,0 +1,89 @@ +#ifndef INFINI_CCL_BACKENDS_CCL_BKCL_TYPE_MAP_H_ +#define INFINI_CCL_BACKENDS_CCL_BKCL_TYPE_MAP_H_ + +#include + +#include + +#include "backends/ccl/common/api.h" +#include "backends/ccl/bkcl/api.h" +#include "comm_impl.h" +#include "data_type_impl.h" +#include "logging.h" + +namespace infini::ccl { + +// XCCL supports float/float16/bfloat16/float64/int32/int64/uint8 only. There +// is no "unsupported" enumerator, so -1 is used as the ConstexprMap sentinel +// and rejected before reaching the library. +constexpr XCCLDataType kBkclUnsupported = static_cast(-1); + +static constexpr ConstexprMap kBkclDataTypeMap{{{ + {DataType::kInt8, kBkclUnsupported}, + {DataType::kInt16, kBkclUnsupported}, + {DataType::kInt32, XCCL_INT32}, + {DataType::kInt64, XCCL_INT64}, + {DataType::kUInt8, XCCL_UINT8}, + {DataType::kUInt16, kBkclUnsupported}, + {DataType::kUInt32, kBkclUnsupported}, + {DataType::kUInt64, kBkclUnsupported}, + {DataType::kFloat16, XCCL_FLOAT16}, + {DataType::kBFloat16, XCCL_BFLOAT16}, + {DataType::kFloat32, XCCL_FLOAT}, + {DataType::kFloat64, XCCL_FLOAT64}, +}}}; + +static constexpr ConstexprMap kBkclOpMap{{{ + {ReductionOpType::kSum, XCCL_ADD}, + {ReductionOpType::kProd, XCCL_PRODUCT}, + {ReductionOpType::kMax, XCCL_MAX}, + {ReductionOpType::kMin, XCCL_MIN}, + {ReductionOpType::kAvg, static_cast(-1)}, +}}}; + +template +inline XCCLDataType DataTypeToBkclType(DataType dtype) { + auto bkcl_dtype = kBkclDataTypeMap.at(dtype); + + if (bkcl_dtype == kBkclUnsupported) { + // The requested data type is not supported by XCCL. + LOG(("DataType '" + std::string(kDataTypeToDesc.at(dtype)) + + "' is not supported by the BKCL backend") + .c_str()); + } + + return bkcl_dtype; +} + +inline XCCLOp RedOpToBkclOp(ReductionOpType red_op) { + return kBkclOpMap.at(red_op); +} + +template +struct CclTypeMap { + using Api = CclApi; + + static bool ToBackendDataType(DataType dtype, + typename Api::DataType* backend_dtype) { + auto bkcl_dtype = DataTypeToBkclType(dtype); + if (bkcl_dtype == kBkclUnsupported) { + return false; + } + *backend_dtype = bkcl_dtype; + return true; + } + + static bool ToBackendRedOp(ReductionOpType red_op, + typename Api::RedOp* backend_op) { + if (red_op == ReductionOpType::kAvg) { + // XCCL has no average reduction; reject it explicitly. + return false; + } + *backend_op = RedOpToBkclOp(red_op); + return true; + } +}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_BACKENDS_CCL_BKCL_TYPE_MAP_H_ diff --git a/src/device.h b/src/device.h index ccb761f..0b54165 100644 --- a/src/device.h +++ b/src/device.h @@ -28,29 +28,29 @@ class Device { Device() = default; - Device(const Type &type, const int &index = 0) : type_{type}, index_{index} {} + Device(const Type& type, const int& index = 0) : type_{type}, index_{index} {} - static const Type TypeFromString(const std::string &name) { + static const Type TypeFromString(const std::string& name) { return kDescToDevice.at(name); } - static const std::string_view StringFromType(const Type &type) { + static const std::string_view StringFromType(const Type& type) { return kDeviceToDesc.at(type); } - const Type &type() const { return type_; } + const Type& type() const { return type_; } - const int &index() const { return index_; } + const int& index() const { return index_; } std::string ToString() const { return std::string{StringFromType(type_)} + ":" + std::to_string(index_); } - bool operator==(const Device &other) const { + bool operator==(const Device& other) const { return type_ == other.type_ && index_ == other.index_; } - bool operator!=(const Device &other) const { return !(*this == other); } + bool operator!=(const Device& other) const { return !(*this == other); } private: Type type_{Type::kCpu}; @@ -174,10 +174,15 @@ struct DevicePriority { static constexpr int value = 5; }; +template <> +struct DevicePriority { + static constexpr int value = 5; +}; + enum class MemorySpace : std::uint8_t { kHost = 0, kDevice = 1, kUnknown }; template -MemorySpace GetMemorySpace(const void *ptr); +MemorySpace GetMemorySpace(const void* ptr); } // namespace infini::ccl diff --git a/src/devices/kunlun/data_type_.h b/src/devices/kunlun/data_type_.h new file mode 100644 index 0000000..bb1f39e --- /dev/null +++ b/src/devices/kunlun/data_type_.h @@ -0,0 +1,28 @@ +#ifndef INFINI_CCL_DEVICES_KUNLUN_DATA_TYPE_H_ +#define INFINI_CCL_DEVICES_KUNLUN_DATA_TYPE_H_ + +#include + +#include "data_type_impl.h" +#include "devices/kunlun/device_.h" + +namespace infini::ccl { + +// The kunlun 16-bit payload layout is identical to CPU's; reuse those +// definitions (the generated manifest includes both data_type_ headers in +// one TU, so redefining them here would break the build). +#include "devices/cpu/data_type_.h" + +template <> +struct TypeMap { + using type = Float16; +}; + +template <> +struct TypeMap { + using type = BFloat16; +}; + +} // namespace infini::ccl + +#endif // INFINI_CCL_DEVICES_KUNLUN_DATA_TYPE_H_ diff --git a/src/devices/kunlun/device_.h b/src/devices/kunlun/device_.h new file mode 100644 index 0000000..a2c521e --- /dev/null +++ b/src/devices/kunlun/device_.h @@ -0,0 +1,24 @@ +#ifndef INFINI_CCL_DEVICES_KUNLUN_DEVICE_H_ +#define INFINI_CCL_DEVICES_KUNLUN_DEVICE_H_ + +// clang-format off +#include +// clang-format on + +#include "device.h" + +namespace infini::ccl { + +template <> +struct DeviceEnabled : std::true_type {}; + +template <> +MemorySpace GetMemorySpace(const void* /*ptr*/) { + // XRE exposes no pointer-attribute query; report unknown so callers cannot + // act on a wrong host/device guess. + return MemorySpace::kUnknown; +} + +} // namespace infini::ccl + +#endif // INFINI_CCL_DEVICES_KUNLUN_DEVICE_H_ diff --git a/src/devices/kunlun/runtime_.h b/src/devices/kunlun/runtime_.h new file mode 100644 index 0000000..7222734 --- /dev/null +++ b/src/devices/kunlun/runtime_.h @@ -0,0 +1,100 @@ +#ifndef INFINI_CCL_DEVICES_KUNLUN_RUNTIME_H_ +#define INFINI_CCL_DEVICES_KUNLUN_RUNTIME_H_ + +#include + +// clang-format off +#include +#include +// clang-format on + +#include "devices/kunlun/device_.h" +#include "logging.h" +#include "return_status_impl.h" +#include "runtime.h" + +namespace infini::ccl { + +/// Kunlun XPU runtime (XRE `libxpurt`), mirroring the InfiniRT kunlun +/// backend. XRE has no whole-device pointer query and no memset; neither is +/// required by the InfiniCCL device interface. +template <> +struct Runtime + : DeviceRuntime> { + using Error = int; + + using Stream = XPUStream; + + static constexpr Device::Type kDeviceType = Device::Type::kKunlun; + + // Follows the InfiniRT/backend-neutral 0..3 MemcpyKind convention. + static constexpr int MemcpyHostToHost = 0; + static constexpr int MemcpyHostToDevice = 1; + static constexpr int MemcpyDeviceToHost = 2; + static constexpr int MemcpyDeviceToDevice = 3; + + static constexpr auto Check = + [](auto status, ReturnStatus err_code = ReturnStatus::kSystemError) { + if (status != XPU_SUCCESS) { + LOG(("Kunlun XRE error code: " + + std::to_string(static_cast(status))) + .c_str()); + return err_code; + } + return ReturnStatus::kSuccess; + }; + + static constexpr auto Malloc = [](void** ptr, size_t size) { + return xpu_malloc(ptr, static_cast(size)); + }; + + static constexpr auto Free = xpu_free; + + static Error Memcpy(void* dst, const void* src, std::size_t count, + int kind) { + switch (kind) { + case MemcpyHostToHost: + std::memcpy(dst, src, count); + return XPU_SUCCESS; + case MemcpyHostToDevice: + return xpu_memcpy(dst, src, static_cast(count), + XPU_HOST_TO_DEVICE); + case MemcpyDeviceToHost: + return xpu_memcpy(dst, src, static_cast(count), + XPU_DEVICE_TO_HOST); + case MemcpyDeviceToDevice: + return xpu_memcpy(dst, src, static_cast(count), + XPU_DEVICE_TO_DEVICE); + default: + return XPUERR_INVALID_PARAM; + } + } + + static constexpr auto GetDevice = xpu_current_device; + + static constexpr auto SetDevice = xpu_set_device; + + static Error DeviceSynchronize() { + // XRE has no whole-device synchronize; `xpu_wait()` waits on the + // default stream, matching the InfiniRT kunlun backend. + return xpu_wait(); + } + + 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) { + return xpu_wait(stream); + }; +}; + +static_assert(Runtime::Validate()); + +} // namespace infini::ccl + +#endif // INFINI_CCL_DEVICES_KUNLUN_RUNTIME_H_