fix(build): bridge编译不依赖torch.utils.cpp_extension

corex torch精简了cpp_extension模块,改用手动发现torch include/lib路径。
同时确认Tier1 (ixformer.functions) 已在真机验证通过:
- silu_and_mul ✓
- rms_norm ✓
- moe_topk_softmax ✓
即使bridge编不出来,ix_unified也通过Tier1走ixformer硬件加速。
This commit is contained in:
claude
2026-08-11 07:35:24 +00:00
parent 7e21571086
commit e440207977

View File

@@ -1,16 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# build_unified_bridge.sh — Compile ix_unified_bridge.so on BI-V100 real hardware # build_unified_bridge.sh — Compile ix_unified_bridge.so on BI-V100
# # Uses manual compiler flags since torch.utils.cpp_extension is stripped from corex torch.
# This builds a single .so that exposes all 14 ixformer::infer functions
# to Python via pybind11. It links against the base image's existing
# ixformer .so files at runtime (no static linking needed).
#
# Usage:
# cd /tmp/gdn_test/project_6 && bash ex_engine/build_unified_bridge.sh
#
# Output:
# ex_engine/build/ix_unified_bridge.cpython-310-x86_64-linux-gnu.so
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -18,58 +8,49 @@ SRC_DIR="${SCRIPT_DIR}/csrc/ilu"
BUILD_DIR="${SCRIPT_DIR}/build" BUILD_DIR="${SCRIPT_DIR}/build"
mkdir -p "$BUILD_DIR" mkdir -p "$BUILD_DIR"
# Detect Python
PYTHON=${PYTHON:-python3} PYTHON=${PYTHON:-python3}
PY_INC=$($PYTHON -c "import sysconfig; print(sysconfig.get_path('include'))") PY_INC=$($PYTHON -c "import sysconfig; print(sysconfig.get_path('include'))")
PY_SUFFIX=$($PYTHON -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))") PY_SUFFIX=$($PYTHON -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))")
# Detect PyTorch # Torch paths — manual discovery (no cpp_extension)
TORCH_DIR=$($PYTHON -c "import torch; print(torch.utils.cmake_prefix_path)") TORCH_ROOT=$($PYTHON -c "import torch; import os; print(os.path.dirname(torch.__file__))")
TORCH_INC=$($PYTHON -c "import torch; print(torch.utils.cpp_extension.include_paths()[0])") TORCH_INC="${TORCH_ROOT}/include"
TORCH_LIB=$($PYTHON -c "import torch; print(torch.utils.cpp_extension.library_paths()[0])") TORCH_INC2="${TORCH_ROOT}/include/torch/csrc/api/include"
TORCH_LIB="${TORCH_ROOT}/lib"
# Detect corex compiler (prefer) or system g++ # Compiler: corex clang or system g++
if [ -f /usr/local/corex/bin/clang++ ]; then for _CXX in /usr/local/corex/bin/clang++ /usr/local/corex-3.2.3/bin/clang++ g++; do
CXX=/usr/local/corex/bin/clang++ [ -x "$_CXX" ] && CXX="$_CXX" && break
echo "[build] Using CoreX clang++: $CXX" done
elif [ -f /usr/local/corex/lib64/clang/16/bin/clang++ ]; then echo "[build] CXX=$CXX"
CXX=/usr/local/corex/lib64/clang/16/bin/clang++ echo "[build] TORCH_ROOT=$TORCH_ROOT"
echo "[build] Using CoreX clang/16: $CXX" echo "[build] PY_INC=$PY_INC"
else
CXX=g++
echo "[build] Using system g++: $CXX"
fi
echo "[build] Python include: $PY_INC"
echo "[build] Torch include: $TORCH_INC"
echo "[build] Torch lib: $TORCH_LIB"
echo "[build] Output suffix: $PY_SUFFIX"
# Compile
OUT="${BUILD_DIR}/ix_unified_bridge${PY_SUFFIX}" OUT="${BUILD_DIR}/ix_unified_bridge${PY_SUFFIX}"
$CXX -shared -fPIC -O2 -std=c++17 \ $CXX -shared -fPIC -O2 -std=c++17 \
-I"$SRC_DIR" \ -I"$SRC_DIR" \
-I"$PY_INC" \ -I"$PY_INC" \
-I"$TORCH_INC" \ -I"$TORCH_INC" \
-I"$TORCH_INC/torch/csrc/api/include" \ -I"$TORCH_INC2" \
-L"$TORCH_LIB" \ -L"$TORCH_LIB" \
-ltorch -ltorch_cpu -ltorch_cuda -lc10 -lc10_cuda \ -ltorch -ltorch_cpu -ltorch_cuda -lc10 -lc10_cuda \
-Wl,--no-as-needed \ -Wl,--no-as-needed,-rpath,"$TORCH_LIB" \
-D_GLIBCXX_USE_CXX11_ABI=0 \ -D_GLIBCXX_USE_CXX11_ABI=0 \
-DTORCH_EXTENSION_NAME=ix_unified_bridge \ -DTORCH_EXTENSION_NAME=ix_unified_bridge \
"$SRC_DIR/ix_unified_bridge.cpp" \ "$SRC_DIR/ix_unified_bridge.cpp" \
-o "$OUT" -o "$OUT" 2>&1
echo "[build] SUCCESS: $OUT" if [ -f "$OUT" ]; then
ls -lh "$OUT" echo "[build] SUCCESS: $OUT ($(du -h "$OUT" | cut -f1))"
$PYTHON -c "
# Verify import importlib.util
$PYTHON -c "
import importlib.util, sys
spec = importlib.util.spec_from_file_location('ix_unified_bridge', '$OUT') spec = importlib.util.spec_from_file_location('ix_unified_bridge', '$OUT')
mod = importlib.util.module_from_spec(spec) mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod) spec.loader.exec_module(mod)
funcs = [x for x in dir(mod) if not x.startswith('_')] funcs = [x for x in dir(mod) if not x.startswith('_')]
print(f'[verify] {len(funcs)} functions exported: {funcs}') print(f'[verify] {len(funcs)} functions: {funcs}')
" || echo "[verify] Import test requires ixformer runtime (expected on non-BI-V100)" " 2>&1 || echo "[verify] import test needs ixformer runtime symbols"
else
echo "[build] FAILED"
fi