Compare commits

...

2 Commits

Author SHA1 Message Date
claude
7e21571086 test: verify_build.sh for real hardware validation 2026-08-11 07:22:18 +00:00
claude
c17fd30144 feat(CRITICAL): 接入ix_unified到qwen3_5.py MoE prefill路径
- import ix_unified bridge到qwen3_5.py
- MoE prefill: ix_bridge.moe_group_gemm替代per-expert for-loop
- 保留fallback: ix_bridge失败自动回退到PyTorch for-loop
- 新feature flag: BI100_MOE_IX_BRIDGE (default=True when bridge available)
2026-08-11 07:16:19 +00:00
2 changed files with 107 additions and 6 deletions

View File

@@ -143,7 +143,15 @@ from vllm.model_executor.models.interfaces import (HasInnerState, SupportsLoRA,
logger = init_logger(__name__)
_bi100_model_trace("qwen3_5 runtime imports complete")
# --- ix_unified: bridge to ixformer::infer C++ APIs -------------------------
try:
from ix_unified import ix as _ix_bridge
_HAS_IX_BRIDGE = (_ix_bridge._bridge is not None)
except ImportError:
_ix_bridge = None
_HAS_IX_BRIDGE = False
_bi100_model_trace(f"qwen3_5 runtime imports complete (ix_bridge={_HAS_IX_BRIDGE})")
_ALLOW_GDN_NAN_ZERO = env_bool("BI100_GDN_ALLOW_NAN_ZERO", False)
_GDN_FINITE_CHECK = (env_bool("BI100_GDN_FINITE_CHECK", False)
@@ -180,6 +188,7 @@ _USE_COREX_MOE_DIRECT_ROUTED = (
_corex_moe_direct_routed is not None
and env_bool("BI100_MOE_COREX_DIRECT_ROUTED", False))
_USE_FUSED_MOE_ACTIVATION = env_bool("BI100_MOE_FUSED_ACTIVATION", True)
_USE_IX_BRIDGE_MOE = (_HAS_IX_BRIDGE and env_bool("BI100_MOE_IX_BRIDGE", True))
# ---------------------------------------------------------------------------
@@ -1688,18 +1697,32 @@ class Qwen3_5MoeSparseBlock(nn.Module):
out = (expert_out * ws.unsqueeze(-1)).sum(
0, keepdim=True).to(hidden_states.dtype) # (1, H)
else:
# General path (prefill / multi-seq): group assignments once. The
# previous implementation scanned the full (T, top_k) routing
# matrix and ran nonzero() for every active expert.
# General path (prefill / multi-seq)
out = torch.zeros_like(hidden_states)
flat_eids = topk_ids.reshape(-1)
order = torch.argsort(flat_eids, stable=True)
sorted_tok_ids = torch.arange(
T, device=topk_ids.device).repeat_interleave(self.top_k)[order]
sorted_weights = topk_weights.reshape(-1)[order]
expert_counts = torch.bincount(
flat_eids, minlength=w13.shape[0]).tolist()
expert_counts_t = torch.bincount(
flat_eids, minlength=w13.shape[0])
if _USE_IX_BRIDGE_MOE:
# ix_bridge path: batched group_gemm instead of per-expert loop
try:
sorted_hidden = hidden_states[sorted_tok_ids]
gate_up = _ix_bridge.moe_group_gemm(
sorted_hidden, w13, expert_counts_t)
act = self.act_fn(gate_up)
down = _ix_bridge.moe_group_gemm(
act, w2, expert_counts_t)
down_weighted = down * sorted_weights.unsqueeze(-1)
out.index_add_(0, sorted_tok_ids, down_weighted.to(out.dtype))
return out
except Exception as e:
logger.warning("ix_bridge MoE failed (%s), falling back", e)
expert_counts = expert_counts_t.tolist()
start = 0
for eid, count in enumerate(expert_counts):
end = start + count

78
verify_build.sh Executable file
View File

@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# verify_build.sh — 在BI-V100真机上验证完整build链
set -uo pipefail
cd "$(dirname "$0")"
echo "=== 1. patch_ops.sh syntax ==="
bash -n qwen3_6_scripts/patch_ops.sh && echo "✓ OK" || echo "✗ FAIL"
echo ""
echo "=== 2. build ix_unified_bridge.so ==="
bash ex_engine/build_unified_bridge.sh 2>&1 | tail -10
echo ""
echo "=== 3. verify bridge load ==="
python3 << 'PY'
import importlib.util, glob
so = glob.glob("ex_engine/build/ix_unified_bridge*.so")
if not so:
print("✗ .so not built"); exit(1)
spec = importlib.util.spec_from_file_location("ix_unified_bridge", so[0])
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
funcs = [x for x in dir(mod) if not x.startswith('_')]
print(f"✓ {len(funcs)} functions: {funcs}")
PY
echo ""
echo "=== 4. ix_unified dispatch smoke test ==="
python3 << 'PY'
import sys, os
sys.path.insert(0, "ex_engine/build")
sys.path.insert(0, "ex_engine/python")
os.environ["IX_BRIDGE_PATH"] = "ex_engine/build"
from ix_unified import ix
print(f"bridge={ix._bridge is not None}, ixformer={ix._ixf is not None}")
import torch
# silu_and_mul
x = torch.randn(2, 512, device="cuda", dtype=torch.float16)
out = ix.silu_and_mul(x)
print(f"✓ silu_and_mul: {x.shape} → {out.shape}")
# rms_norm
inp = torch.randn(4, 2048, device="cuda", dtype=torch.float16)
outp = torch.empty_like(inp)
w = torch.ones(2048, device="cuda", dtype=torch.float16)
ix.rms_norm(outp, inp, w, 1e-6)
print(f"✓ rms_norm: {inp.shape}")
# moe_topk_softmax
gate = torch.randn(8, 256, device="cuda", dtype=torch.float16)
weights, indices = ix.moe_topk_softmax(gate, 8, True)
print(f"✓ moe_topk_softmax: {gate.shape} → weights={weights.shape} indices={indices.shape}")
print("ALL SMOKE TESTS PASSED")
PY
echo ""
echo "=== 5. prebuilt .so still load ==="
python3 << 'PY'
import importlib.util, os
so_dir = "qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10"
for name in ["corex_moe_direct_routed", "corex_gdn_packed_decode", "corex_gdn_causal_conv"]:
so = os.path.join(so_dir, f"{name}.so")
spec = importlib.util.spec_from_file_location(name, so)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
funcs = [x for x in dir(mod) if not x.startswith('_')]
print(f"✓ {name}: {funcs}")
PY
echo ""
echo "=== 6. py_compile all ==="
cd qwen3_6_scripts
find . -path './wheels' -prune -o -name '*.py' -print0 | xargs -0 python3 -m py_compile 2>&1 && echo "✓ all OK" || echo "✗ errors"
echo ""
echo "=== DONE ==="