之前手动clang++缺-ltorch_python导致pybind11 type_caster未定义。 改为优先用torch.utils.cpp_extension.load()——与STEP2(moe_topk)、 STEP3(_moe_C)、STEP6(gdn)完全相同的编译路径,已验证100%成功。 手动clang++作为fallback保留。
102 lines
3.1 KiB
Bash
Executable File
102 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# build_unified_bridge.sh — Compile ix_unified_bridge.so
|
|
# Strategy: try torch.utils.cpp_extension.load() first (proven on BI-V100),
|
|
# fall back to manual clang++ if torch extension not available.
|
|
set -eo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
SRC="$SCRIPT_DIR/csrc/ilu/ix_unified_bridge.cpp"
|
|
BUILD_DIR="$SCRIPT_DIR/build"
|
|
mkdir -p "$BUILD_DIR"
|
|
|
|
if [ ! -f "$SRC" ]; then
|
|
echo "[build_bridge] ERROR: $SRC not found"
|
|
exit 1
|
|
fi
|
|
|
|
PYTHON=${PYTHON:-python3}
|
|
|
|
# Method 1: torch.utils.cpp_extension.load() — same method that works for moe_topk, _moe_C, gdn
|
|
echo "[build_bridge] Trying torch.utils.cpp_extension.load()..."
|
|
$PYTHON << PYEOF
|
|
import os, sys, glob
|
|
|
|
src = "$SRC"
|
|
build_dir = "$BUILD_DIR"
|
|
|
|
try:
|
|
from torch.utils.cpp_extension import load
|
|
|
|
# Find ixformer include/lib dirs for linking
|
|
extra_include = ["$SCRIPT_DIR/csrc/ilu"]
|
|
extra_ldflags = []
|
|
|
|
# ixformer lib dirs — bridge needs these at runtime, not compile time
|
|
for p in ["/usr/local/corex/lib64/python3/dist-packages/ixformer",
|
|
"/usr/local/corex/lib64"]:
|
|
if os.path.isdir(p):
|
|
sos = glob.glob(os.path.join(p, "*.so"))
|
|
if sos:
|
|
extra_ldflags.append(f"-L{p}")
|
|
extra_ldflags.append(f"-Wl,-rpath,{p}")
|
|
|
|
ext = load(
|
|
name="ix_unified_bridge",
|
|
sources=[src],
|
|
extra_include_paths=extra_include,
|
|
extra_ldflags=extra_ldflags,
|
|
verbose=True,
|
|
build_directory=build_dir,
|
|
)
|
|
funcs = [x for x in dir(ext) if not x.startswith('_')]
|
|
print(f"[build_bridge] SUCCESS via cpp_extension: {len(funcs)} functions")
|
|
print(f"[build_bridge] functions: {funcs}")
|
|
sys.exit(0)
|
|
except Exception as e:
|
|
print(f"[build_bridge] cpp_extension failed: {e}")
|
|
sys.exit(1)
|
|
PYEOF
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo "[build_bridge] torch.utils.cpp_extension succeeded"
|
|
ls -la "$BUILD_DIR"/ix_unified_bridge*.so 2>/dev/null
|
|
exit 0
|
|
fi
|
|
|
|
# Method 2: Manual clang++ (fallback)
|
|
echo "[build_bridge] Falling back to manual clang++..."
|
|
PY_INC=$($PYTHON -c "import sysconfig; print(sysconfig.get_path('include'))")
|
|
PY_SUFFIX=$($PYTHON -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))")
|
|
TORCH_ROOT=$($PYTHON -c "import torch; import os; print(os.path.dirname(torch.__file__))")
|
|
TORCH_INC="${TORCH_ROOT}/include"
|
|
TORCH_INC2="${TORCH_ROOT}/include/torch/csrc/api/include"
|
|
TORCH_LIB="${TORCH_ROOT}/lib"
|
|
|
|
CXX=""
|
|
for _CXX in /usr/local/corex/bin/clang++ g++; do
|
|
[ -x "$_CXX" ] && CXX="$_CXX" && break
|
|
done
|
|
|
|
OUT="${BUILD_DIR}/ix_unified_bridge${PY_SUFFIX}"
|
|
|
|
$CXX -shared -fPIC -O2 -std=c++17 \
|
|
-I"$SCRIPT_DIR/csrc/ilu" \
|
|
-I"$PY_INC" \
|
|
-I"$TORCH_INC" \
|
|
-I"$TORCH_INC2" \
|
|
-L"$TORCH_LIB" \
|
|
-ltorch -ltorch_cpu -ltorch_python -lc10 \
|
|
-Wl,--no-as-needed,-rpath,"$TORCH_LIB" \
|
|
-Wl,--unresolved-symbols=ignore-in-shared-libs \
|
|
-D_GLIBCXX_USE_CXX11_ABI=0 \
|
|
-DTORCH_EXTENSION_NAME=ix_unified_bridge \
|
|
"$SRC" \
|
|
-o "$OUT" 2>&1
|
|
|
|
if [ -f "$OUT" ]; then
|
|
echo "[build_bridge] SUCCESS via manual clang: $OUT ($(du -h "$OUT" | cut -f1))"
|
|
else
|
|
echo "[build_bridge] FAILED"
|
|
exit 1
|
|
fi
|