2026-08-10 03:00:24 +00:00
|
|
|
"""
|
2026-08-12 01:33:24 +00:00
|
|
|
ix_bridge.py — Full ixformer bridge loader.
|
|
|
|
|
|
|
|
|
|
Loads ix_full_bridge.so (all 14 ixformer::infer functions) or falls back
|
|
|
|
|
to ix_moe_bridge.so (MoE-only 6 functions).
|
|
|
|
|
|
|
|
|
|
Functions exposed:
|
|
|
|
|
MoE: topk_softmax, moe_gen_idx, moe_expand_input, group_gemm,
|
|
|
|
|
silu_and_mul, moe_combine_result, fused_moe_forward
|
|
|
|
|
Attention: paged_attention, flash_attn_prefill
|
|
|
|
|
Norm: rms_norm, fused_add_rms_norm
|
|
|
|
|
RoPE: rotary_embedding
|
|
|
|
|
Cache: reshape_and_cache
|
|
|
|
|
Linear: linear
|
2026-08-10 03:00:24 +00:00
|
|
|
"""
|
2026-08-12 01:33:24 +00:00
|
|
|
|
2026-08-10 03:00:24 +00:00
|
|
|
import os
|
|
|
|
|
import logging
|
2026-08-12 01:33:24 +00:00
|
|
|
import torch
|
|
|
|
|
from typing import Tuple, Optional, List
|
2026-08-10 03:00:24 +00:00
|
|
|
|
|
|
|
|
logger = logging.getLogger("ex_engine.ix_bridge")
|
|
|
|
|
|
feat(EX): ix_full_bridge — all 14 ixformer::infer functions bridged
Upstream source: xllm/core/kernels/ilu/ixformer.h (Apache 2.0)
Wrapper patterns: xllm/core/kernels/ilu/{attention,norm,rope,activation,fused_moe,group_gemm}.cpp
Complete bridge (ix_full_bridge.cpp, 331 lines):
MoE: topk_softmax, gen_idx, expand, group_gemm, silu_mul, combine, fused_forward
Attention: paged_attention (decode), flash_attn_prefill (prefill)
Norm: rms_norm, fused_add_rms_norm
RoPE: rotary_embedding
Cache: reshape_and_cache
Linear: ixformer_linear
ix_bridge.py: tries ix_full_bridge first, falls back to ix_moe_bridge
patch_ops.sh: deploys both .cpp files to all JIT search paths
Copied ixformer.h + utils.h headers for reference
2026-08-10 04:01:35 +00:00
|
|
|
_bridge = None
|
|
|
|
|
_loaded = False
|
2026-08-12 01:33:24 +00:00
|
|
|
_available = False
|
|
|
|
|
|
|
|
|
|
# All .cpp sources to try, in priority order
|
|
|
|
|
_CPP_NAMES = ["ix_full_bridge.cpp", "ix_moe_bridge.cpp"]
|
2026-08-10 03:36:39 +00:00
|
|
|
|
|
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
def _find_cpp(name):
|
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
candidates = [
|
|
|
|
|
os.path.join(here, "..", "csrc", name),
|
|
|
|
|
os.path.join(here, name),
|
|
|
|
|
os.path.join("/workspace/ex_engine/csrc", name),
|
|
|
|
|
os.path.join("/workspace/qwen3_6_scripts", name),
|
feat(EX): ix_full_bridge — all 14 ixformer::infer functions bridged
Upstream source: xllm/core/kernels/ilu/ixformer.h (Apache 2.0)
Wrapper patterns: xllm/core/kernels/ilu/{attention,norm,rope,activation,fused_moe,group_gemm}.cpp
Complete bridge (ix_full_bridge.cpp, 331 lines):
MoE: topk_softmax, gen_idx, expand, group_gemm, silu_mul, combine, fused_forward
Attention: paged_attention (decode), flash_attn_prefill (prefill)
Norm: rms_norm, fused_add_rms_norm
RoPE: rotary_embedding
Cache: reshape_and_cache
Linear: ixformer_linear
ix_bridge.py: tries ix_full_bridge first, falls back to ix_moe_bridge
patch_ops.sh: deploys both .cpp files to all JIT search paths
Copied ixformer.h + utils.h headers for reference
2026-08-10 04:01:35 +00:00
|
|
|
]
|
2026-08-12 01:33:24 +00:00
|
|
|
for c in candidates:
|
|
|
|
|
p = os.path.normpath(c)
|
|
|
|
|
if os.path.exists(p):
|
|
|
|
|
return p
|
2026-08-10 03:36:39 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
def _load_bridge():
|
|
|
|
|
global _bridge, _loaded, _available
|
feat(EX): ix_full_bridge — all 14 ixformer::infer functions bridged
Upstream source: xllm/core/kernels/ilu/ixformer.h (Apache 2.0)
Wrapper patterns: xllm/core/kernels/ilu/{attention,norm,rope,activation,fused_moe,group_gemm}.cpp
Complete bridge (ix_full_bridge.cpp, 331 lines):
MoE: topk_softmax, gen_idx, expand, group_gemm, silu_mul, combine, fused_forward
Attention: paged_attention (decode), flash_attn_prefill (prefill)
Norm: rms_norm, fused_add_rms_norm
RoPE: rotary_embedding
Cache: reshape_and_cache
Linear: ixformer_linear
ix_bridge.py: tries ix_full_bridge first, falls back to ix_moe_bridge
patch_ops.sh: deploys both .cpp files to all JIT search paths
Copied ixformer.h + utils.h headers for reference
2026-08-10 04:01:35 +00:00
|
|
|
if _loaded:
|
2026-08-12 01:33:24 +00:00
|
|
|
return _available
|
feat(EX): ix_full_bridge — all 14 ixformer::infer functions bridged
Upstream source: xllm/core/kernels/ilu/ixformer.h (Apache 2.0)
Wrapper patterns: xllm/core/kernels/ilu/{attention,norm,rope,activation,fused_moe,group_gemm}.cpp
Complete bridge (ix_full_bridge.cpp, 331 lines):
MoE: topk_softmax, gen_idx, expand, group_gemm, silu_mul, combine, fused_forward
Attention: paged_attention (decode), flash_attn_prefill (prefill)
Norm: rms_norm, fused_add_rms_norm
RoPE: rotary_embedding
Cache: reshape_and_cache
Linear: ixformer_linear
ix_bridge.py: tries ix_full_bridge first, falls back to ix_moe_bridge
patch_ops.sh: deploys both .cpp files to all JIT search paths
Copied ixformer.h + utils.h headers for reference
2026-08-10 04:01:35 +00:00
|
|
|
_loaded = True
|
2026-08-11 18:07:31 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
from torch.utils.cpp_extension import load
|
|
|
|
|
import glob
|
2026-08-11 18:07:31 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
# Find ixformer .so libraries to link against
|
|
|
|
|
extra_ldflags = []
|
|
|
|
|
ixf_lib_dirs = set()
|
|
|
|
|
try:
|
|
|
|
|
import ixformer
|
|
|
|
|
ixf_dir = os.path.dirname(ixformer.__file__)
|
|
|
|
|
# Link against all .so in the ixformer package
|
|
|
|
|
for so in glob.glob(os.path.join(ixf_dir, "*.so")):
|
|
|
|
|
if "cpython" not in so: # skip the Python extension .so
|
|
|
|
|
extra_ldflags.append(so)
|
|
|
|
|
ixf_lib_dirs.add(os.path.dirname(so))
|
|
|
|
|
# Also try the _C and _ixformer_torch extensions
|
|
|
|
|
for so in glob.glob(os.path.join(ixf_dir, "_ixformer_torch*.so")):
|
|
|
|
|
extra_ldflags.append(so)
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
2026-08-11 18:07:31 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
# Also check /usr/local/corex/lib64 for libixattn etc
|
|
|
|
|
corex_lib = "/usr/local/corex/lib64"
|
|
|
|
|
if os.path.isdir(corex_lib):
|
|
|
|
|
for lib in ["libixattn.so", "libixformer.so", "libcublas.so"]:
|
|
|
|
|
p = os.path.join(corex_lib, lib)
|
|
|
|
|
if os.path.exists(p) and p not in extra_ldflags:
|
|
|
|
|
extra_ldflags.append(p)
|
|
|
|
|
ixf_lib_dirs.add(corex_lib)
|
2026-08-11 18:07:31 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
# Add rpath so the .so can find its dependencies at runtime
|
|
|
|
|
for d in ixf_lib_dirs:
|
|
|
|
|
extra_ldflags.append(f"-Wl,-rpath,{d}")
|
2026-08-11 18:09:22 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
logger.info("ix_bridge extra_ldflags: %s", extra_ldflags)
|
2026-08-11 18:09:22 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
for cpp_name in _CPP_NAMES:
|
|
|
|
|
cpp_path = _find_cpp(cpp_name)
|
|
|
|
|
if cpp_path is None:
|
|
|
|
|
continue
|
|
|
|
|
mod_name = cpp_name.replace(".cpp", "").replace(".", "_")
|
|
|
|
|
try:
|
|
|
|
|
logger.info("JIT-compiling %s from %s ...", cpp_name, cpp_path)
|
|
|
|
|
_bridge = load(
|
|
|
|
|
name=mod_name,
|
|
|
|
|
sources=[cpp_path],
|
|
|
|
|
extra_cflags=["-O2", "-std=c++17"],
|
|
|
|
|
extra_ldflags=extra_ldflags,
|
|
|
|
|
verbose=False,
|
|
|
|
|
)
|
|
|
|
|
_available = True
|
|
|
|
|
fns = [x for x in dir(_bridge) if not x.startswith("_")]
|
|
|
|
|
logger.info("ix_bridge loaded (%s): %s", cpp_name, fns)
|
|
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning("JIT compile %s failed: %s — trying next", cpp_name, e)
|
2026-08-11 18:09:22 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
logger.warning("All ix_bridge sources failed to compile")
|
|
|
|
|
return False
|
2026-08-11 18:09:22 +00:00
|
|
|
|
|
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
def is_available() -> bool:
|
|
|
|
|
if not _loaded:
|
|
|
|
|
_load_bridge()
|
|
|
|
|
return _available
|
2026-08-11 18:09:22 +00:00
|
|
|
|
|
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
def _get():
|
|
|
|
|
if not is_available():
|
|
|
|
|
raise RuntimeError("ix_bridge not available")
|
|
|
|
|
return _bridge
|
2026-08-11 18:09:22 +00:00
|
|
|
|
2026-08-10 03:36:39 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
# =========================================================================
|
|
|
|
|
# MoE
|
|
|
|
|
# =========================================================================
|
|
|
|
|
def topk_softmax(gating_output, topk, renormalize=True):
|
|
|
|
|
return _get().topk_softmax(gating_output, topk, renormalize)
|
2026-08-10 03:36:39 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
def moe_gen_idx(expert_id, expert_num):
|
|
|
|
|
return _get().moe_gen_idx(expert_id, expert_num)
|
|
|
|
|
|
|
|
|
|
def moe_expand_input(input, gather_index, combine_idx, topk):
|
|
|
|
|
return _get().moe_expand_input(input, gather_index, combine_idx, topk)
|
|
|
|
|
|
|
|
|
|
def group_gemm(inputs, weights, token_count, output_n):
|
|
|
|
|
return _get().group_gemm(inputs, weights, token_count, output_n)
|
|
|
|
|
|
|
|
|
|
def silu_and_mul(input):
|
|
|
|
|
return _get().silu_and_mul(input)
|
|
|
|
|
|
|
|
|
|
def moe_combine_result(input, weight):
|
|
|
|
|
return _get().moe_combine_result(input, weight)
|
|
|
|
|
|
|
|
|
|
def fused_moe_forward(hidden_states, router_logits, w13, w2,
|
|
|
|
|
topk, num_experts, renormalize=True):
|
|
|
|
|
return _get().fused_moe_forward(
|
|
|
|
|
hidden_states, router_logits, w13, w2, topk, num_experts, renormalize)
|
|
|
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
|
# Attention
|
|
|
|
|
# =========================================================================
|
|
|
|
|
def paged_attention(output, query, key_cache, value_cache,
|
|
|
|
|
num_kv_heads, scale, block_tables, seq_lens,
|
|
|
|
|
block_size, max_context_len, alibi_slopes=None):
|
|
|
|
|
return _get().paged_attention(
|
|
|
|
|
output, query, key_cache, value_cache,
|
|
|
|
|
num_kv_heads, scale, block_tables, seq_lens,
|
|
|
|
|
block_size, max_context_len, alibi_slopes)
|
|
|
|
|
|
|
|
|
|
def flash_attn_prefill(query, key, value, output, block_tables,
|
|
|
|
|
cu_seq_q, cu_seq_k, max_query_len, max_seq_len,
|
|
|
|
|
scale, is_causal=True, window_left=-1, window_right=-1):
|
|
|
|
|
return _get().flash_attn_prefill(
|
|
|
|
|
query, key, value, output, block_tables,
|
|
|
|
|
cu_seq_q, cu_seq_k, max_query_len, max_seq_len,
|
|
|
|
|
scale, is_causal, window_left, window_right)
|
|
|
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
|
# Norm
|
|
|
|
|
# =========================================================================
|
|
|
|
|
def rms_norm(output, input, weight, eps=1e-6):
|
|
|
|
|
return _get().rms_norm(output, input, weight, eps)
|
|
|
|
|
|
|
|
|
|
def fused_add_rms_norm(input, residual, weight, output, residual_output, eps=1e-6):
|
|
|
|
|
return _get().fused_add_rms_norm(input, residual, weight, output, residual_output, eps)
|
|
|
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
|
# RoPE
|
|
|
|
|
# =========================================================================
|
|
|
|
|
def rotary_embedding(positions, query, key, head_size, cos_sin_cache, is_neox=True):
|
|
|
|
|
return _get().rotary_embedding(positions, query, key, head_size, cos_sin_cache, is_neox)
|
|
|
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
|
# Cache
|
|
|
|
|
# =========================================================================
|
feat(EX): ix_full_bridge — all 14 ixformer::infer functions bridged
Upstream source: xllm/core/kernels/ilu/ixformer.h (Apache 2.0)
Wrapper patterns: xllm/core/kernels/ilu/{attention,norm,rope,activation,fused_moe,group_gemm}.cpp
Complete bridge (ix_full_bridge.cpp, 331 lines):
MoE: topk_softmax, gen_idx, expand, group_gemm, silu_mul, combine, fused_forward
Attention: paged_attention (decode), flash_attn_prefill (prefill)
Norm: rms_norm, fused_add_rms_norm
RoPE: rotary_embedding
Cache: reshape_and_cache
Linear: ixformer_linear
ix_bridge.py: tries ix_full_bridge first, falls back to ix_moe_bridge
patch_ops.sh: deploys both .cpp files to all JIT search paths
Copied ixformer.h + utils.h headers for reference
2026-08-10 04:01:35 +00:00
|
|
|
def reshape_and_cache(key, value, key_cache, value_cache, slot_mapping):
|
2026-08-12 01:33:24 +00:00
|
|
|
return _get().reshape_and_cache(key, value, key_cache, value_cache, slot_mapping)
|
2026-08-10 03:36:39 +00:00
|
|
|
|
2026-08-12 01:33:24 +00:00
|
|
|
# =========================================================================
|
|
|
|
|
# Linear
|
|
|
|
|
# =========================================================================
|
|
|
|
|
def linear(input, weight, bias=None):
|
|
|
|
|
return _get().linear(input, weight, bias)
|