feat(SO): ix_moe_bridge.cpp — dlopen bridge for 12 ixformer::infer functions

THE CORE .so: ix_moe_bridge.cpp compiles to ix_moe_bridge.so which:
  - Links against base image's libixformer.so at load time
  - Exposes 12 functions to Python via pybind11:

  MoE pipeline (7 steps):
    topk_softmax()      → ixformer::infer::topk_softmax
    moe_gen_idx()       → ixformer::infer::moe_compute_token_index_api
    moe_expand_input()  → ixformer::infer::moe_expand_input
    moe_group_gemm()    → ixformer::infer::moe_w16a16_group_gemm
    silu_and_mul()      → ixformer::infer::silu_and_mul
    moe_combine_result()→ ixformer::infer::moe_output_reduce_sum

  Inference ops (5 functions):
    paged_attention()   → ixformer::infer::xllm_paged_attention
    rms_norm()          → ixformer::infer::rms_norm
    linear()            → ixformer::infer::ixformer_linear
    reshape_and_cache() → ixformer::infer::xllm_reshape_and_cache
    rotary_embedding()  → ixformer::infer::xllm_rotary_embedding

Build chain:
  Dockerfile → build.sh → precompile_ix_bridge.py
    → torch.utils.cpp_extension.load(ix_moe_bridge.cpp, -lixformer)
      → ix_moe_bridge.cpython-310.so

Load chain:
  Python: from ex_engine.python.ix_bridge import topk_softmax
    → ix_bridge.py loads ix_moe_bridge.so
      → dlopen links to libixformer.so
        → CUDA kernel on BI-V100

Interface source: upstream_ref/xllm_latest/core/kernels/ilu/ixformer.h
This commit is contained in:
project6-dev
2026-08-11 02:36:59 +00:00
parent 0eab333fb0
commit d1c5e992aa
5 changed files with 540 additions and 561 deletions

View File

@@ -1,146 +1,33 @@
#!/bin/bash
# ex_engine/build.sh — Compile EX Engine factor .so libraries
# build.sh — Compile all .so libraries for ex_engine
#
# Toolchain: corex clang/16 (BI-V100) with --cuda-gpu-arch=ivcore10
# Based on: real compile log from user test showing exact flags
# Produces:
# build/ix_moe_bridge.*.so — dlopen bridge to libixformer.so (12 functions)
#
# Usage:
# ./ex_engine/build.sh # auto-detect toolchain
# ./ex_engine/build.sh --nvcc # force nvcc (development)
# Run inside Docker where libixformer.so exists at:
# /usr/local/corex/lib64/python3/dist-packages/ixformer/libixformer.so
set -euo pipefail
set -e
cd "$(dirname "$0")"
echo "[build.sh] START"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_DIR="${SCRIPT_DIR}/build"
CSRC_DIR="${SCRIPT_DIR}/csrc"
INCLUDE_DIR="${SCRIPT_DIR}/include"
mkdir -p "$BUILD_DIR"
COREX_ROOT="/usr/local/corex"
COMPILER=""
detect_toolchain() {
if [[ "${1:-auto}" != "--nvcc" ]] && [[ -x "${COREX_ROOT}/bin/clang++" ]]; then
COMPILER="corex"
echo "[EX] Using corex clang/16 at ${COREX_ROOT}/bin/clang++"
elif command -v nvcc &>/dev/null; then
COMPILER="nvcc"
echo "[EX] Using nvcc"
else
echo "[EX] ERROR: No CUDA compiler found"
exit 1
fi
}
compile_factor() {
local factor_id=$1
local cu_file=$2
local so_name="ex_factor_${factor_id}.so"
local so_path="${BUILD_DIR}/${so_name}"
echo "[EX] Compiling factor ${factor_id}: $(basename ${cu_file})${so_name}"
if [[ "$COMPILER" == "corex" ]]; then
# Exact flags from real BI-V100 compile log:
# --cuda-gpu-arch=ivcore10 (NOT sm_70!)
# -D__ILUVATAR__ -D__ILUVATAR_WORKAROUND__ -D__ILUVATAR_DIAG__
# -cl-single-precision-constant
"${COREX_ROOT}/bin/clang++" \
-x cuda \
--cuda-gpu-arch=ivcore10 \
--cuda-path="${COREX_ROOT}" \
-std=c++17 \
-O3 \
-D__ILUVATAR__ \
-D__ILUVATAR_WORKAROUND__ \
-D__ILUVATAR_DIAG__ \
-cl-single-precision-constant \
-fPIC \
-mllvm --bonus-inst-threshold=0 \
-shared \
-I"${INCLUDE_DIR}" \
-I"${COREX_ROOT}/include" \
-L"${COREX_ROOT}/lib64" \
-lcudart \
-o "${so_path}" \
"${cu_file}" 2>&1 || {
echo "[EX] ✗ FAILED: ${so_name}"
return 1
}
else
nvcc \
-arch=sm_70 \
-std=c++17 \
-O3 \
--compiler-options '-fPIC' \
-shared \
-I"${INCLUDE_DIR}" \
-o "${so_path}" \
"${cu_file}" 2>&1 || {
echo "[EX] ✗ FAILED: ${so_name}"
return 1
}
fi
if [[ -f "${so_path}" ]]; then
local size=$(stat -c%s "${so_path}" 2>/dev/null || stat -f%z "${so_path}" 2>/dev/null)
echo "[EX] ✓ ${so_name} (${size} bytes)"
fi
}
compile_registry() {
local so_path="${BUILD_DIR}/libex_registry.so"
echo "[EX] Compiling registry → libex_registry.so"
gcc -O2 -shared -fPIC \
-I"${INCLUDE_DIR}" \
-o "${so_path}" \
"${CSRC_DIR}/ex_registry.c" \
-ldl
echo "[EX] ✓ libex_registry.so"
}
mkdir -p build
# ============================================================================
# Main
# 1. ix_moe_bridge.so — THE KEY DELIVERABLE
# Links to libixformer.so → exposes topk_softmax etc to Python
# ============================================================================
detect_toolchain "${1:-auto}"
echo "[build.sh] Compiling ix_moe_bridge..."
python3 precompile_ix_bridge.py 2>&1 || {
echo "[build.sh] WARNING: ix_moe_bridge compile failed (expected outside Docker)"
}
echo ""
echo "========================================"
echo " EX Engine Build (Algorithm Factor Replacement)"
echo " Toolchain: ${COMPILER}"
echo " Output: ${BUILD_DIR}/"
echo "========================================"
echo ""
# Check result
if ls build/ix_moe_bridge*.so 1>/dev/null 2>&1; then
echo "[build.sh] SUCCESS: $(ls build/ix_moe_bridge*.so)"
else
echo "[build.sh] WARNING: no ix_moe_bridge.so produced"
fi
compile_registry
# Factor mapping
FACTORS=(
"0:factor_moe_topk_softmax.cu"
"2:factor_moe_fused_gemm.cu"
)
# Note: Factor 5 (GDN) uses FlashQLA Python extension, NOT a .so
TOTAL=0
SUCCESS=0
for entry in "${FACTORS[@]}"; do
fid="${entry%%:*}"
cu_file="${CSRC_DIR}/${entry##*:}"
TOTAL=$((TOTAL + 1))
if [[ -f "$cu_file" ]]; then
if compile_factor "$fid" "$cu_file"; then
SUCCESS=$((SUCCESS + 1))
fi
else
echo "[EX] SKIP factor ${fid}: ${cu_file} not found"
fi
done
echo ""
echo "========================================"
echo " Build complete: ${SUCCESS}/${TOTAL} factors (.so)"
echo " GDN: via FlashQLA (JIT compiled on hardware)"
echo " Output: ${BUILD_DIR}/"
echo "========================================"
ls -la "${BUILD_DIR}/" 2>/dev/null || true
echo "[build.sh] DONE"
ls -la build/*.so 2>/dev/null || echo "[build.sh] No .so files in build/"