From c60cca4799e5c336bb93b8f6863630dd3768afd7 Mon Sep 17 00:00:00 2001 From: Ceng23333 <441651826@qq.com> Date: Mon, 24 Aug 2026 10:01:36 +0800 Subject: [PATCH 1/2] feat(mars): add WITH_MARS platform and htcc wrapper Enable HPCC/Mars CMake detection, htcc compile wrapper, and device registration without Mars operator specializations. --- CMakeLists.txt | 40 +++++++++++++++++++++---- examples/runtime_api.h | 5 ++++ scripts/generate_wrappers.py | 4 ++- scripts/htcc_wrapper.sh | 57 ++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 47 +++++++++++++++++++++++++++-- src/device.h | 2 +- src/pybind11_utils.h | 1 + 7 files changed, 145 insertions(+), 11 deletions(-) create mode 100755 scripts/htcc_wrapper.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index e9a0cac20..636748aea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ option(WITH_NVIDIA "Enable CUDA backend" OFF) option(WITH_ILUVATAR "Enable Iluvatar GPU backend" OFF) option(WITH_HYGON "Enable Hygon GPU backend" OFF) option(WITH_METAX "Enable MetaX backend" OFF) +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) @@ -114,7 +115,10 @@ if(AUTO_DETECT_DEVICES) message(STATUS "Auto-detected Hygon environment.") endif() - if(DEFINED ENV{MACA_PATH}) + if(DEFINED ENV{HPCC_PATH}) + set(WITH_MARS ON) + message(STATUS "Auto-detected Mars environment from HPCC_PATH") + elseif(DEFINED ENV{MACA_PATH}) set(WITH_METAX ON) message(STATUS "Auto-detected MetaX environment from MACA_PATH") else() @@ -156,7 +160,7 @@ if(AUTO_DETECT_DEVICES) if(WITH_NVIDIA) set(_non_nvidia_gpu_detected FALSE) - foreach(_gpu_backend WITH_ILUVATAR WITH_HYGON WITH_METAX WITH_CAMBRICON WITH_MOORE WITH_ASCEND) + foreach(_gpu_backend WITH_ILUVATAR WITH_HYGON WITH_METAX WITH_MARS WITH_CAMBRICON WITH_MOORE WITH_ASCEND) if(${_gpu_backend}) set(_non_nvidia_gpu_detected TRUE) endif() @@ -328,14 +332,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_METAX WITH_MOORE WITH_ASCEND) +foreach(_gpu_backend WITH_NVIDIA WITH_ILUVATAR WITH_HYGON WITH_METAX WITH_MARS WITH_MOORE WITH_ASCEND) 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_METAX`, `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_METAX`, `WITH_MARS`, `WITH_MOORE`, and `WITH_ASCEND` are mutually exclusive. Build one GPU backend at a time.") endif() if(WITH_NINETOOTHED AND NOT WITH_NVIDIA) @@ -472,6 +476,30 @@ if(WITH_METAX) find_library(MACA_BLAS_LIB NAMES mcblas HINTS "${MACA_PATH}/lib" REQUIRED) endif() +if(WITH_MARS) + add_compile_definitions(WITH_MARS=1) + + # Normally can be found at: `/opt/hpcc/`. + set(HPCC_PATH $ENV{HPCC_PATH}) + if(NOT HPCC_PATH) + set(HPCC_PATH "/opt/hpcc") + endif() + set(CMAKE_C_COMPILER ${CMAKE_CURRENT_SOURCE_DIR}/scripts/htcc_wrapper.sh) + set(CMAKE_CXX_COMPILER ${CMAKE_CURRENT_SOURCE_DIR}/scripts/htcc_wrapper.sh) + + include_directories("${HPCC_PATH}/include") + include_directories("${HPCC_PATH}/tools/cu-bridge/include") + link_directories("${HPCC_PATH}/lib") + link_directories("${HPCC_PATH}/htgpu_llvm/lib") + + # Libraries: hcruntime / hcdnn / hcblas. HPCC is the toolkit; Mars is the device. + find_library(HPCC_RUNTIME_LIB NAMES hcruntime HINTS "${HPCC_PATH}/lib" REQUIRED) + find_library(HPCC_DNN_LIB NAMES hcdnn HINTS "${HPCC_PATH}/lib" REQUIRED) + find_library(HPCC_BLAS_LIB NAMES hcblas HINTS "${HPCC_PATH}/lib" REQUIRED) + # htcc emits LLVM OpenMP (__kmpc_*), not GNU libgomp. + find_library(HPCC_OMP_LIB NAMES omp iomp5 HINTS "${HPCC_PATH}/htgpu_llvm/lib" REQUIRED) +endif() + if(WITH_MOORE) add_compile_definitions(WITH_MOORE=1) @@ -539,11 +567,11 @@ if(WITH_ASCEND) 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_METAX 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_METAX AND NOT WITH_MARS AND NOT WITH_MOORE AND NOT WITH_CAMBRICON AND NOT WITH_ASCEND) add_compile_definitions(WITH_CPU=1) endif() -if(WITH_TORCH OR WITH_METAX OR WITH_MOORE) +if(WITH_TORCH OR WITH_METAX OR WITH_MARS OR WITH_MOORE) set(PYBIND11_ENABLE_EXTRAS OFF) endif() diff --git a/examples/runtime_api.h b/examples/runtime_api.h index 1bbfdc1f3..7f754026d 100644 --- a/examples/runtime_api.h +++ b/examples/runtime_api.h @@ -14,6 +14,9 @@ #elif WITH_METAX #include "native/cuda/metax/ops/gemm/mcblas.h" #include "native/cuda/metax/runtime_.h" +#elif WITH_MARS +#include "native/cuda/mars/ops/gemm/hcblas.h" +#include "native/cuda/mars/runtime_.h" #elif WITH_CAMBRICON #include "native/cambricon/ops/gemm/cnblas.h" #include "native/cambricon/runtime_.h" @@ -39,6 +42,8 @@ using DefaultRuntimeUtils = Runtime; using DefaultRuntimeUtils = Runtime; #elif WITH_METAX using DefaultRuntimeUtils = Runtime; +#elif WITH_MARS +using DefaultRuntimeUtils = Runtime; #elif WITH_CAMBRICON using DefaultRuntimeUtils = Runtime; #elif WITH_MOORE diff --git a/scripts/generate_wrappers.py b/scripts/generate_wrappers.py index 0a188b2a0..6cf1a8188 100644 --- a/scripts/generate_wrappers.py +++ b/scripts/generate_wrappers.py @@ -1721,8 +1721,10 @@ def _device_marker_headers(devices): "cambricon": "infini/rt/cambricon/device_.h", "ascend": "infini/rt/ascend/device_.h", "metax": "infini/rt/metax/device_.h", + "mars": "infini/rt/mars/device_.h", "moore": "infini/rt/moore/device_.h", "iluvatar": "infini/rt/iluvatar/device_.h", + "hygon": "infini/rt/hygon/device_.h", } return [paths[device] for device in devices if device in paths] @@ -2108,7 +2110,7 @@ def _dispatch_gen_batch_size(): nargs="+", default="cpu", type=str, - help="Devices to use. Please pick from `cpu`, `nvidia`, `cambricon`, `ascend`, `metax`, `moore`, `iluvatar`, and `hygon`. (default: `cpu`)", + help="Devices to use. Please pick from `cpu`, `nvidia`, `cambricon`, `ascend`, `metax`, `mars`, `moore`, `iluvatar`, and `hygon`. (default: `cpu`)", ) parser.add_argument( diff --git a/scripts/htcc_wrapper.sh b/scripts/htcc_wrapper.sh new file mode 100755 index 000000000..69ab3a431 --- /dev/null +++ b/scripts/htcc_wrapper.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# HPCC analog of mxcc_wrapper.sh. +# htcc only enables device compilation for `.cu`. Rewrite compile inputs. +# Place the `.cu` symlink next to the source so same-directory `#include` +# resolution (e.g. bindings `mul.h` vs C-API `generated/include/mul.h`) still +# works — unlike rewriting into `/tmp`. +ARGS=() +skip_next=0 +has_compile=0 +for arg in "$@"; do + if [ $skip_next -eq 1 ]; then + skip_next=0 + ARGS+=("$arg") + continue + fi + case "$arg" in + -pthread) + ;; + -B) + skip_next=1 + ;; + -B*) + ;; + -c) + has_compile=1 + ARGS+=("$arg") + ;; + *) + ARGS+=("$arg") + ;; + esac +done + +if [ "$has_compile" -eq 1 ]; then + rewritten=() + for arg in "${ARGS[@]}"; do + case "$arg" in + *.cc|*.cpp|*.cxx) + if [ -f "$arg" ]; then + abs=$(readlink -f "$arg") + cu="${abs}.cu" + ln -sfn "$abs" "$cu" + rewritten+=("$cu") + else + rewritten+=("$arg") + fi + ;; + *) + rewritten+=("$arg") + ;; + esac + done + ARGS=("${rewritten[@]}") +fi + +HPCC_PATH="${HPCC_PATH:-/opt/hpcc}" +exec "${HPCC_PATH}/htgpu_llvm/bin/htcc" "${ARGS[@]}" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 36f946d8d..c39ad6bef 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -291,6 +291,38 @@ if(WITH_METAX) list(APPEND DEVICE_LIST "metax") endif() +if(WITH_MARS) + set(MARS_PATTERNS + "native/cuda/*.cc" + "native/cuda/*.cpp" + "native/cuda/mars/*.cc" + "native/cuda/mars/*.cu" + ) + + file(GLOB_RECURSE MARS_SOURCES CONFIGURE_DEPENDS ${MARS_PATTERNS}) + + set_source_files_properties(${MARS_SOURCES} PROPERTIES LANGUAGE CXX) + + target_compile_definitions(infiniops PRIVATE WITH_MARS=1) + target_sources(infiniops PRIVATE ${MARS_SOURCES}) + + target_include_directories(infiniops PUBLIC + "${HPCC_PATH}/include" + "${HPCC_PATH}/tools/cu-bridge/include") + target_link_libraries(infiniops PUBLIC + ${HPCC_RUNTIME_LIB} + ${HPCC_DNN_LIB} + ${HPCC_BLAS_LIB} + ${HPCC_OMP_LIB} + ) + set_property(TARGET infiniops APPEND PROPERTY + INSTALL_RPATH "${HPCC_PATH}/htgpu_llvm/lib") + set_property(TARGET infiniops APPEND PROPERTY + BUILD_RPATH "${HPCC_PATH}/htgpu_llvm/lib") + + list(APPEND DEVICE_LIST "mars") +endif() + if(WITH_MOORE) set(MOORE_PATTERNS "native/cuda/*.cc" @@ -857,9 +889,10 @@ if(TORCH_SOURCES) torch_compile=${INFINI_OPS_TORCH_COMPILE_JOBS}) endif() - if(WITH_METAX OR WITH_MOORE) - # Vendor compilers (`mxcc`/`mcc`) cannot compile vendor-forked `torch` - # headers. Compile `torch` sources with the system C++ compiler instead. + if(WITH_METAX OR WITH_MOORE OR WITH_MARS) + # Vendor compilers (`mxcc`/`mcc`/`htcc`) cannot compile vendor-forked + # `torch` headers. Compile `torch` sources with the system C++ compiler + # instead. find_program(SYSTEM_CXX NAMES g++ c++) if(NOT SYSTEM_CXX) @@ -878,6 +911,11 @@ if(TORCH_SOURCES) "-I${MACA_PATH}/include/mcr" "-I${MACA_PATH}/tools/cu-bridge/include") endif() + if(WITH_MARS) + list(APPEND _torch_vendor_include_flags + "-I${HPCC_PATH}/include" + "-I${HPCC_PATH}/tools/cu-bridge/include") + endif() if(WITH_MOORE) list(APPEND _torch_vendor_include_flags "-I${MUSA_ROOT}/include") execute_process( @@ -913,6 +951,9 @@ if(TORCH_SOURCES) if(WITH_METAX) list(APPEND _torch_extra_flags "-DUSE_MACA=1" "-DWITH_METAX=1") endif() + if(WITH_MARS) + list(APPEND _torch_extra_flags "-DUSE_HPCC=1" "-DWITH_MARS=1") + endif() if(WITH_MOORE) list(APPEND _torch_extra_flags "-DWITH_MOORE=1") endif() diff --git a/src/device.h b/src/device.h index 59bbeb9c3..e049d0f5b 100644 --- a/src/device.h +++ b/src/device.h @@ -15,7 +15,7 @@ using DeviceEnabled = infini::rt::DeviceEnabled; using AllDeviceTypes = List; + Device::Type::kIluvatar, Device::Type::kHygon, Device::Type::kMars>; template struct ActiveDevicesImpl { diff --git a/src/pybind11_utils.h b/src/pybind11_utils.h index b1bfe344f..42d2b219e 100644 --- a/src/pybind11_utils.h +++ b/src/pybind11_utils.h @@ -203,6 +203,7 @@ inline std::optional TryDeviceTypeFromString( {"cambricon", Device::Type::kCambricon}, {"ascend", Device::Type::kAscend}, {"metax", Device::Type::kMetax}, + {"mars", Device::Type::kMars}, {"moore", Device::Type::kMoore}, {"iluvatar", Device::Type::kIluvatar}, {"hygon", Device::Type::kHygon}, From c3feb6726da7259e2e28b872f024d6faf88ac1a6 Mon Sep 17 00:00:00 2001 From: Ceng23333 <441651826@qq.com> Date: Mon, 24 Aug 2026 10:02:08 +0800 Subject: [PATCH 2/2] fix(mars): keep system-g++ torch path for MetaX/Moore only Defer WITH_MARS torch host compilation to the smoke follow-up PR. Co-authored-by: Cursor --- src/CMakeLists.txt | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c39ad6bef..8f33d1dee 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -889,10 +889,9 @@ if(TORCH_SOURCES) torch_compile=${INFINI_OPS_TORCH_COMPILE_JOBS}) endif() - if(WITH_METAX OR WITH_MOORE OR WITH_MARS) - # Vendor compilers (`mxcc`/`mcc`/`htcc`) cannot compile vendor-forked - # `torch` headers. Compile `torch` sources with the system C++ compiler - # instead. + if(WITH_METAX OR WITH_MOORE) + # Vendor compilers (`mxcc`/`mcc`) cannot compile vendor-forked `torch` + # headers. Compile `torch` sources with the system C++ compiler instead. find_program(SYSTEM_CXX NAMES g++ c++) if(NOT SYSTEM_CXX) @@ -911,11 +910,6 @@ if(TORCH_SOURCES) "-I${MACA_PATH}/include/mcr" "-I${MACA_PATH}/tools/cu-bridge/include") endif() - if(WITH_MARS) - list(APPEND _torch_vendor_include_flags - "-I${HPCC_PATH}/include" - "-I${HPCC_PATH}/tools/cu-bridge/include") - endif() if(WITH_MOORE) list(APPEND _torch_vendor_include_flags "-I${MUSA_ROOT}/include") execute_process( @@ -951,9 +945,6 @@ if(TORCH_SOURCES) if(WITH_METAX) list(APPEND _torch_extra_flags "-DUSE_MACA=1" "-DWITH_METAX=1") endif() - if(WITH_MARS) - list(APPEND _torch_extra_flags "-DUSE_HPCC=1" "-DWITH_MARS=1") - endif() if(WITH_MOORE) list(APPEND _torch_extra_flags "-DWITH_MOORE=1") endif()