Compare commits

...

8 Commits

Author SHA1 Message Date
project6-dev
905bf4db2c feat(moe): wire silu_and_mul through C++ bridge in corex_moe.py
Now MoE activation uses:
  Tier 0: ix_bridge.silu_and_mul (C++ ixformer_torch_ext, verified on BI-V100)
  Tier 1: ixformer.functions.silu_and_mul (Python)
  Tier 2: F.silu(gate) * up (pure PyTorch)

Verified 7/8 on single BI-V100:
  ✓ compile, silu_and_mul, rms_norm, fused_add_rms_norm, linear, paged_attn, corex_moe
  ✗ flash_attn import path (not needed, vllm xformers backend handles it)
2026-08-10 06:36:40 +00:00
project6-dev
7127d18491 refactor(bridge): rewrite ix_full_bridge.cpp for actual base image symbols
Symbol probe revealed ixformer::infer namespace does NOT exist in base image.
That namespace is xllm's own compiled wrapper layer.

Actual available symbols in base image:
  _ixformer_torch.so: silu_and_mul_forward, rms_norm_forward,
    fused_add_rms_norm_forward, ixformer_linear, ixformer_linear_ex
  libixformer.so: ixinfer_flash_attn_unpad_fwd (different signature)

MoE functions (topk_softmax, group_gemm, moe_expand_input, etc.)
are NOT in any base image .so — MoE must use Python path.

Bridge now only wraps: silu_and_mul, rms_norm, fused_add_rms_norm, linear
These accelerate the per-layer ops that run 200x per token.
2026-08-10 06:34:54 +00:00
project6-dev
b9fd2755d9 debug: probe all ixformer symbol locations 2026-08-10 06:33:14 +00:00
project6-dev
3e7fc565ff debug: probe script to find silu_and_mul symbol location 2026-08-10 06:32:04 +00:00
project6-dev
a54dbda3bb fix(bridge): link against libixformer.so for silu_and_mul symbol
- ix_bridge.py: auto-discover ixformer .so files, pass as extra_ldflags
- ix_moe_bridge.cpp: fix mangled header from bad sed, add #include <optional>
- verify_single_gpu.py: also pass extra_ldflags during JIT compile

The undefined symbol _ZN8ixformer5infer12silu_and_mulERN2at6TensorES3_
lives in libixformer.so — need to explicitly link it.
2026-08-10 06:29:11 +00:00
project6-dev
ac3c8e28eb fix(bridge): c10::nullopt → typed std::optional{} for CoreX torch compat
CoreX torch's c10::nullopt cannot implicitly convert to const std::optional<T>&.
Solution: use static typed empty optionals (kNoneTensor, kNoneBool).
Also unified all c10::optional forward decls to std::optional.
Applied same fix to ix_moe_bridge.cpp.
2026-08-10 06:25:15 +00:00
project6-dev
ff0bf8c1d6 fix: total_mem → total_memory (torch API) 2026-08-10 06:21:40 +00:00
project6-dev
c579c75039 test: single-GPU verification script for ix_full_bridge compile + MoE 7-step dispatch chain 2026-08-10 06:21:04 +00:00
7 changed files with 487 additions and 319 deletions

View File

@@ -1,331 +1,90 @@
// ix_full_bridge.cpp — Complete ixformer::infer bridge for BI-V100
// ix_full_bridge.cpp — Bridge to ixformer C++ functions available in base image
//
// Exposes ALL 14 ixformer C++ functions to Python via pybind11.
// Header source: upstream_ref/xllm/xllm/core/kernels/ilu/ixformer.h
// Based on symbol probe of the actual BI-V100 base image:
// _ixformer_torch.so has: silu_and_mul_forward, rms_norm_forward,
// fused_add_rms_norm_forward, ixformer_linear, ixformer_linear_ex
// libixformer.so has: ixinfer_flash_attn_unpad_fwd
//
// This replaces the partial ix_moe_bridge.cpp with the full set:
// MoE pipeline: topk_softmax, moe_compute_token_index_api, moe_expand_input,
// moe_w16a16_group_gemm, silu_and_mul, moe_output_reduce_sum
// Attention: ixinfer_flash_attn_unpad_with_block_tables, xllm_paged_attention
// Norm: rms_norm, residual_rms_norm
// RoPE: xllm_rotary_embedding
// Linear: ixformer_linear, ixformer_linear_ex
// Cache: xllm_reshape_and_cache
// MoE functions (topk_softmax, group_gemm, etc.) are NOT in base image.
// They exist only in xllm's compiled library. MoE must use Python fallback.
#include <torch/extension.h>
#include <optional>
#include <tuple>
#include <vector>
// ============================================================================
// Forward-declare ixformer::infer namespace — matches ixformer.h exactly
// We forward-declare instead of #include to avoid build-time dependency
// on internal headers (ixinfer.h etc) that may not be on include path.
// The symbols resolve at link time against the base image's libixattn.so etc.
// Forward declarations — ACTUAL symbols from base image .so files
// Namespace: ixformer_torch_ext (in _ixformer_torch.cpython-310.so)
// ============================================================================
namespace ixformer {
namespace infer {
namespace ixformer_torch_ext {
// --- Attention ---
torch::Tensor ixinfer_flash_attn_unpad_with_block_tables(
torch::Tensor& query, torch::Tensor& key_cache, torch::Tensor& value_cache,
torch::Tensor& out, torch::Tensor& block_tables,
torch::Tensor& cu_seq_q, torch::Tensor& cu_seq_k,
int64_t max_seq_q, int64_t max_seq_k, bool is_causal,
int64_t window_left, int64_t window_right,
double scale, double softcap, bool sqrt_alibi,
const std::optional<torch::Tensor>& alibi_slopes,
const std::optional<torch::Tensor>& sinks,
std::optional<torch::Tensor>& lse);
// silu_and_mul: _ZN18ixformer_torch_ext20silu_and_mul_forwardERN2at6TensorES2_
void silu_and_mul_forward(at::Tensor& input, at::Tensor& output);
torch::Tensor xllm_paged_attention(
torch::Tensor& out, torch::Tensor& query,
torch::Tensor& key_cache, torch::Tensor& value_cache,
int64_t num_kv_heads, double scale,
torch::Tensor& block_tables, torch::Tensor& context_lens,
int64_t block_size, int64_t max_context_len,
const std::optional<torch::Tensor>& alibi_slopes,
bool causal, int32_t window_left, int32_t window_right,
double softcap, bool enable_cuda_graph, bool use_sqrt_alibi,
const std::optional<torch::Tensor>& sinks);
// rms_norm: _ZN18ixformer_torch_ext16rms_norm_forwardERN2at6TensorES2_S2_d
void rms_norm_forward(at::Tensor& input, at::Tensor& weight, at::Tensor& output, double eps);
// --- Norm ---
void residual_rms_norm(
torch::Tensor& input, torch::Tensor& residual, torch::Tensor& weight,
torch::Tensor& output, torch::Tensor& residual_output,
const std::optional<torch::Tensor>& fused_bias,
double alpha, double eps, bool is_post);
// fused_add_rms_norm: _ZN18ixformer_torch_ext26fused_add_rms_norm_forwardERN2at6TensorES2_S2_dd
void fused_add_rms_norm_forward(at::Tensor& input, at::Tensor& residual,
at::Tensor& weight, double eps, double alpha);
void rms_norm(
torch::Tensor& input, torch::Tensor& weight, torch::Tensor& output,
const std::optional<torch::Tensor>& fused_bias, double eps);
// ixformer_linear: _ZN18ixformer_torch_ext15ixformer_linearERN2at6TensorES2_RKN3c108optionalIS1_EES7_
at::Tensor ixformer_linear(at::Tensor& input, at::Tensor& weight,
const c10::optional<at::Tensor>& bias,
const c10::optional<at::Tensor>& out);
// --- Activation ---
void silu_and_mul(torch::Tensor& input, torch::Tensor& output);
// ixformer_linear_ex: _ZN18ixformer_torch_ext18ixformer_linear_exERN2at6TensorES2_RKN3c108optionalIS1_EE
at::Tensor ixformer_linear_ex(at::Tensor& input, at::Tensor& weight,
const c10::optional<at::Tensor>& bias);
// --- RoPE ---
void xllm_rotary_embedding(
torch::Tensor& positions, torch::Tensor& query, torch::Tensor& key,
int64_t head_size, torch::Tensor& cos_sin_cache, bool is_neox);
} // namespace ixformer_torch_ext
// --- KV Cache ---
void xllm_reshape_and_cache(
torch::Tensor& key, torch::Tensor& value,
torch::Tensor& key_cache, torch::Tensor& value_cache,
torch::Tensor& slot_mapping,
int64_t key_token_stride, int64_t value_token_stride);
// --- Linear ---
torch::Tensor ixformer_linear(
torch::Tensor& input, torch::Tensor& weight, int64_t act_type,
const std::optional<torch::Tensor>& bias,
const std::optional<torch::Tensor>& out,
const std::optional<bool> persistent);
torch::Tensor ixformer_linear_ex(
torch::Tensor& input, torch::Tensor& weight,
const c10::optional<torch::Tensor>& bias,
const c10::optional<torch::Tensor>& out);
// --- MoE ---
void topk_softmax(
torch::Tensor& topk_weights, torch::Tensor& topk_indices,
torch::Tensor& token_expert_indices, torch::Tensor& gating_output,
bool renormalize);
void moe_compute_token_index_api(
torch::Tensor& topk_ids, torch::Tensor& src_dst, torch::Tensor& dst_src,
torch::Tensor& expert_sizes_gpu,
const c10::optional<torch::Tensor>& expert_mask,
const c10::optional<torch::Tensor>& expert_sizes_cpu,
const c10::optional<torch::Tensor>& expand_tokens_gpu,
int64_t start_expert_id, int64_t end_expert_id, int64_t num_experts);
void moe_expand_input(
torch::Tensor outputs, torch::Tensor inputs, torch::Tensor dst_to_src,
const c10::optional<torch::Tensor>& src_to_dst,
int64_t dst_tokens, int64_t expand_factor);
void moe_w16a16_group_gemm(
torch::Tensor output, torch::Tensor inputs, torch::Tensor weights,
torch::Tensor tokens_per_experts,
const c10::optional<torch::Tensor>& dst_to_src,
const c10::optional<torch::Tensor>& bias,
std::string format, int64_t persistent, int64_t output_n);
void moe_output_reduce_sum(
torch::Tensor outputs, torch::Tensor inputs,
const c10::optional<torch::Tensor>& mul_weight,
const c10::optional<torch::Tensor>& mask,
const c10::optional<torch::Tensor>& extra_residual,
double scaling_factor);
} // namespace infer
} // namespace ixformer
// ============================================================================
// Python wrappers — thin wrappers matching upstream xllm ILU kernel layer
// Source: upstream_ref/xllm/xllm/core/kernels/ilu/*.cpp
// Python wrappers
// ============================================================================
// --- MoE: topk_softmax (from ilu/fused_moe.cpp moe_active_topk) ---
std::tuple<torch::Tensor, torch::Tensor> ix_topk_softmax(
torch::Tensor gating_output, int64_t topk, bool renormalize) {
auto input = gating_output.to(torch::kFloat32).contiguous();
int64_t num_tokens = input.size(0);
auto topk_weights = torch::empty({num_tokens, topk},
torch::dtype(torch::kFloat32).device(input.device()));
auto topk_indices = torch::empty({num_tokens, topk},
torch::dtype(torch::kInt32).device(input.device()));
auto token_expert_indices = torch::empty({num_tokens, topk},
torch::dtype(torch::kInt32).device(input.device()));
ixformer::infer::topk_softmax(
topk_weights, topk_indices, token_expert_indices, input, false);
if (renormalize) {
topk_weights = topk_weights / topk_weights.sum(-1, /*keepdim=*/true);
}
return std::make_tuple(topk_weights, topk_indices);
}
// --- MoE: gen_idx (from ilu/fused_moe.cpp moe_gen_idx) ---
std::vector<torch::Tensor> ix_moe_gen_idx(
torch::Tensor expert_id, int64_t expert_num) {
auto src_dst = expert_id.new_empty({expert_id.numel()});
auto dst_src = torch::empty_like(src_dst);
auto expert_sizes_gpu = expert_id.new_empty({expert_num});
ixformer::infer::moe_compute_token_index_api(
expert_id, src_dst, dst_src, expert_sizes_gpu,
c10::nullopt, c10::nullopt, c10::nullopt, 0, expert_num, expert_num);
auto expert_sizes_gpu_cumsum = expert_sizes_gpu.cumsum(-1);
return {src_dst, dst_src, expert_sizes_gpu, expert_sizes_gpu_cumsum};
}
// --- MoE: expand_input ---
torch::Tensor ix_moe_expand_input(
torch::Tensor input, torch::Tensor gather_index,
torch::Tensor combine_idx, int64_t topk) {
int64_t dst_tokens = input.size(0) * topk;
auto output = input.new_empty({dst_tokens, input.size(1)});
ixformer::infer::moe_expand_input(
output, input, combine_idx, gather_index, dst_tokens, topk);
return output;
}
// --- MoE: group_gemm ---
torch::Tensor ix_group_gemm(
torch::Tensor inputs, torch::Tensor weights,
torch::Tensor token_count, int64_t output_n) {
int64_t total_tokens = inputs.size(0);
auto output = inputs.new_empty({total_tokens, output_n});
ixformer::infer::moe_w16a16_group_gemm(
output, inputs, weights, token_count,
c10::nullopt, c10::nullopt, "NT", 0, output_n);
return output;
}
// --- MoE: silu_and_mul ---
// --- silu_and_mul ---
torch::Tensor ix_silu_and_mul(torch::Tensor input) {
int64_t half_dim = input.size(-1) / 2;
auto output = input.new_empty({input.size(0), half_dim});
ixformer::infer::silu_and_mul(input, output);
ixformer_torch_ext::silu_and_mul_forward(input, output);
return output;
}
// --- MoE: combine_result ---
torch::Tensor ix_moe_combine_result(torch::Tensor input, torch::Tensor weight) {
input = input.view({-1, weight.size(1), input.size(1)});
auto output = input.new_empty({input.size(0), input.size(2)});
ixformer::infer::moe_output_reduce_sum(
output, input, weight, c10::nullopt, c10::nullopt, 1.0);
return output;
// --- rms_norm ---
void ix_rms_norm(torch::Tensor output, torch::Tensor input,
torch::Tensor weight, double eps) {
ixformer_torch_ext::rms_norm_forward(input, weight, output, eps);
}
// --- MoE: full fused forward (from ilu/layers/fused_moe.cpp) ---
torch::Tensor ix_fused_moe_forward(
torch::Tensor hidden_states, torch::Tensor router_logits,
torch::Tensor w13, torch::Tensor w2,
int64_t topk, int64_t num_experts, bool renormalize) {
auto [topk_weights, topk_ids] = ix_topk_softmax(router_logits, topk, renormalize);
auto idx = ix_moe_gen_idx(topk_ids.view({-1}), num_experts);
auto expanded = ix_moe_expand_input(hidden_states, idx[0], idx[1], topk);
int64_t gate_up_dim = w13.size(1);
auto gemm1_out = ix_group_gemm(expanded, w13, idx[2], gate_up_dim);
auto act_out = ix_silu_and_mul(gemm1_out);
int64_t hidden_dim = w2.size(1);
auto gemm2_out = ix_group_gemm(act_out, w2, idx[2], hidden_dim);
return ix_moe_combine_result(gemm2_out, topk_weights);
// --- fused_add_rms_norm ---
void ix_fused_add_rms_norm(torch::Tensor input, torch::Tensor residual,
torch::Tensor weight, double eps) {
ixformer_torch_ext::fused_add_rms_norm_forward(input, residual, weight, eps, 1.0);
}
// --- Attention: paged decode (from ilu/attention.cpp batch_decode) ---
void ix_paged_attention(
torch::Tensor output, torch::Tensor query,
torch::Tensor key_cache, torch::Tensor value_cache,
int64_t num_kv_heads, double scale,
torch::Tensor block_tables, torch::Tensor seq_lens,
int64_t block_size, int64_t max_context_len,
const std::optional<torch::Tensor>& alibi_slopes) {
if (query.dim() == 4) {
query = query.view({query.size(0)*query.size(1), query.size(2), query.size(3)}).contiguous();
// --- linear ---
torch::Tensor ix_linear(torch::Tensor input, torch::Tensor weight,
const c10::optional<torch::Tensor>& bias) {
// Use linear_ex for decode (m<=1), linear for prefill
auto input_2d = input.view({-1, input.size(-1)});
int64_t m = input_2d.size(0);
if (m <= 1 && !bias.has_value()) {
return ixformer_torch_ext::ixformer_linear_ex(input, weight, bias);
}
if (output.dim() == 4) {
output = output.view({output.size(0)*output.size(1), output.size(2), output.size(3)}).contiguous();
}
ixformer::infer::xllm_paged_attention(
output, query, key_cache, value_cache, num_kv_heads, scale,
block_tables, seq_lens, block_size, max_context_len,
alibi_slopes, /*causal=*/true, /*window_left=*/-1, /*window_right=*/-1,
/*softcap=*/0.0, /*enable_cuda_graph=*/false, /*use_sqrt_alibi=*/false,
/*sinks=*/c10::nullopt);
return ixformer_torch_ext::ixformer_linear(input, weight, bias,
c10::optional<at::Tensor>());
}
// --- Attention: prefill flash (from ilu/attention.cpp batch_prefill) ---
void ix_flash_attn_prefill(
torch::Tensor query, torch::Tensor key, torch::Tensor value,
torch::Tensor output, torch::Tensor block_tables,
torch::Tensor cu_seq_q, torch::Tensor cu_seq_k,
int64_t max_query_len, int64_t max_seq_len,
double scale, bool is_causal,
int64_t window_left, int64_t window_right) {
std::optional<torch::Tensor> lse = c10::nullopt;
ixformer::infer::ixinfer_flash_attn_unpad_with_block_tables(
query, key, value, output, block_tables,
cu_seq_q, cu_seq_k, max_query_len, max_seq_len,
is_causal, window_left, window_right,
scale, /*softcap=*/0.0, /*sqrt_alibi=*/false,
/*alibi_slopes=*/c10::nullopt, /*sinks=*/c10::nullopt, lse);
}
// --- Norm: rms_norm (from ilu/norm.cpp) ---
void ix_rms_norm(
torch::Tensor output, torch::Tensor input,
torch::Tensor weight, double eps) {
ixformer::infer::rms_norm(input, weight, output, c10::nullopt, eps);
}
// --- Norm: fused residual + rms_norm (from ilu/norm.cpp) ---
void ix_fused_add_rms_norm(
torch::Tensor input, torch::Tensor residual,
torch::Tensor weight, torch::Tensor output,
torch::Tensor residual_output, double eps) {
ixformer::infer::residual_rms_norm(
input, residual, weight, output, residual_output,
c10::nullopt, 1.0, eps, false);
}
// --- RoPE (from ilu/rope.cpp) ---
void ix_rotary_embedding(
torch::Tensor positions, torch::Tensor query, torch::Tensor key,
int64_t head_size, torch::Tensor cos_sin_cache, bool is_neox) {
ixformer::infer::xllm_rotary_embedding(
positions, query, key, head_size, cos_sin_cache, is_neox);
}
// --- KV Cache reshape (from ilu/attention.cpp reshape_paged_cache) ---
void ix_reshape_and_cache(
torch::Tensor key, torch::Tensor value,
torch::Tensor key_cache, torch::Tensor value_cache,
torch::Tensor slot_mapping) {
slot_mapping = slot_mapping.to(torch::kLong);
int64_t key_stride = key.stride(0);
int64_t val_stride = value.stride(0);
ixformer::infer::xllm_reshape_and_cache(
key, value, key_cache, value_cache, slot_mapping,
key_stride, val_stride);
}
// --- Linear ---
torch::Tensor ix_linear(
torch::Tensor input, torch::Tensor weight,
const std::optional<torch::Tensor>& bias) {
return ixformer::infer::ixformer_linear(
input, weight, /*act_type=*/0, bias, c10::nullopt, c10::nullopt);
}
// ============================================================================
// Module registration
// ============================================================================
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
// MoE
m.def("topk_softmax", &ix_topk_softmax, "Fused topk+softmax",
py::arg("gating_output"), py::arg("topk"), py::arg("renormalize")=true);
m.def("moe_gen_idx", &ix_moe_gen_idx);
m.def("moe_expand_input", &ix_moe_expand_input);
m.def("group_gemm", &ix_group_gemm);
m.def("silu_and_mul", &ix_silu_and_mul);
m.def("moe_combine_result", &ix_moe_combine_result);
m.def("fused_moe_forward", &ix_fused_moe_forward,
py::arg("hidden_states"), py::arg("router_logits"),
py::arg("w13"), py::arg("w2"),
py::arg("topk"), py::arg("num_experts"), py::arg("renormalize")=true);
// Attention
m.def("paged_attention", &ix_paged_attention);
m.def("flash_attn_prefill", &ix_flash_attn_prefill);
// Norm
m.def("rms_norm", &ix_rms_norm);
m.def("fused_add_rms_norm", &ix_fused_add_rms_norm);
// RoPE
m.def("rotary_embedding", &ix_rotary_embedding);
// Cache
m.def("reshape_and_cache", &ix_reshape_and_cache);
// Linear
m.def("linear", &ix_linear);
m.def("silu_and_mul", &ix_silu_and_mul, "Fused SiLU+mul activation");
m.def("rms_norm", &ix_rms_norm, "RMSNorm");
m.def("fused_add_rms_norm", &ix_fused_add_rms_norm, "Fused residual + RMSNorm");
m.def("linear", &ix_linear, "ixformer GEMM (linear/linear_ex)");
}

View File

@@ -15,6 +15,9 @@
#include <torch/extension.h>
#include <tuple>
#include <vector>
#include <optional>
static const std::optional<torch::Tensor> kNoneTensor = {};
// Forward-declare ixformer C++ API (from base image SDK)
namespace ixformer {
@@ -31,9 +34,9 @@ void moe_compute_token_index_api(
torch::Tensor& src_dst,
torch::Tensor& dst_src,
torch::Tensor& expert_sizes_gpu,
const c10::optional<torch::Tensor>& expert_mask,
const c10::optional<torch::Tensor>& expert_sizes_cpu,
const c10::optional<torch::Tensor>& expand_tokens_gpu,
const std::optional<torch::Tensor>& expert_mask,
const std::optional<torch::Tensor>& expert_sizes_cpu,
const std::optional<torch::Tensor>& expand_tokens_gpu,
int64_t start_expert_id,
int64_t end_expert_id,
int64_t num_experts);
@@ -41,7 +44,7 @@ void moe_compute_token_index_api(
void moe_expand_input(torch::Tensor outputs,
torch::Tensor inputs,
torch::Tensor dst_to_src,
const c10::optional<torch::Tensor>& src_to_dst,
const std::optional<torch::Tensor>& src_to_dst,
int64_t dst_tokens,
int64_t expand_factor);
@@ -49,17 +52,17 @@ void moe_w16a16_group_gemm(torch::Tensor output,
torch::Tensor inputs,
torch::Tensor weights,
torch::Tensor tokens_per_experts,
const c10::optional<torch::Tensor>& dst_to_src,
const c10::optional<torch::Tensor>& bias,
const std::optional<torch::Tensor>& dst_to_src,
const std::optional<torch::Tensor>& bias,
std::string format,
int64_t persistent,
int64_t output_n);
void moe_output_reduce_sum(torch::Tensor outputs,
torch::Tensor inputs,
const c10::optional<torch::Tensor>& mul_weight,
const c10::optional<torch::Tensor>& mask,
const c10::optional<torch::Tensor>& extra_residual,
const std::optional<torch::Tensor>& mul_weight,
const std::optional<torch::Tensor>& mask,
const std::optional<torch::Tensor>& extra_residual,
double scaling_factor);
void silu_and_mul(torch::Tensor& input, torch::Tensor& output);
@@ -110,9 +113,9 @@ std::vector<torch::Tensor> ix_moe_gen_idx(
ixformer::infer::moe_compute_token_index_api(
expert_id, src_dst, dst_src, expert_sizes_gpu,
/*expert_mask=*/c10::nullopt,
/*expert_sizes_cpu=*/c10::nullopt,
/*expand_tokens_gpu=*/c10::nullopt,
/*expert_mask=*/kNoneTensor,
/*expert_sizes_cpu=*/kNoneTensor,
/*expand_tokens_gpu=*/kNoneTensor,
0, expert_num, expert_num);
expert_sizes_gpu_cumsum = expert_sizes_gpu.cumsum(-1);
@@ -144,8 +147,8 @@ torch::Tensor ix_group_gemm(
ixformer::infer::moe_w16a16_group_gemm(
output, inputs, weights, token_count,
/*dst_to_src=*/c10::nullopt,
/*bias=*/c10::nullopt,
/*dst_to_src=*/kNoneTensor,
/*bias=*/kNoneTensor,
/*format=*/"NT",
/*persistent=*/0,
/*output_n=*/output_n);
@@ -169,8 +172,8 @@ torch::Tensor ix_moe_combine_result(
ixformer::infer::moe_output_reduce_sum(
output, input, weight,
/*mask=*/c10::nullopt,
/*extra_residual=*/c10::nullopt,
/*mask=*/kNoneTensor,
/*extra_residual=*/kNoneTensor,
/*scaling_factor=*/1.0);
return output;
}

View File

@@ -71,14 +71,25 @@ def _python_topk_softmax(gating_output, topk, renormalize=True):
# -----------------------------------------------------------------------
# ixformer.functions Python-level SiLU
# silu_and_mul acceleration: prefer C++ bridge, fallback to ixformer Python
# -----------------------------------------------------------------------
_ixf_silu = None
try:
import ixformer.functions as _ixf_F
_ixf_silu = _ixf_F.silu_and_mul
except (ImportError, AttributeError):
pass
_silu_fn = None
def _get_silu_fn():
global _silu_fn
if _silu_fn is not None:
return _silu_fn
# Tier 0: C++ bridge (ixformer_torch_ext::silu_and_mul_forward)
if _ensure_bridge() and hasattr(_bridge, 'silu_and_mul'):
_silu_fn = _bridge.silu_and_mul
return _silu_fn
# Tier 1: ixformer Python
try:
import ixformer.functions as _ixf_F
_silu_fn = _ixf_F.silu_and_mul
except (ImportError, AttributeError):
pass
return _silu_fn
# -----------------------------------------------------------------------
@@ -182,11 +193,10 @@ def _python_moe_forward(hidden_states, gate_output, w13, w2,
gate_up = tokens @ w13[eidx].t()
# SiLU activation
if _ixf_silu is not None:
act = torch.empty(tokens.shape[0], half_inter,
dtype=dtype, device=tokens.device)
silu_fn = _get_silu_fn()
if silu_fn is not None:
try:
_ixf_silu(gate_up, act)
act = silu_fn(gate_up)
except Exception:
gate_out = gate_up[:, :half_inter]
up_out = gate_up[:, half_inter:]

View File

@@ -51,6 +51,39 @@ def _load_bridge():
_loaded = True
from torch.utils.cpp_extension import load
import glob
# 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
# 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)
# Add rpath so the .so can find its dependencies at runtime
for d in ixf_lib_dirs:
extra_ldflags.append(f"-Wl,-rpath,{d}")
logger.info("ix_bridge extra_ldflags: %s", extra_ldflags)
for cpp_name in _CPP_NAMES:
cpp_path = _find_cpp(cpp_name)
@@ -63,6 +96,7 @@ def _load_bridge():
name=mod_name,
sources=[cpp_path],
extra_cflags=["-O2", "-std=c++17"],
extra_ldflags=extra_ldflags,
verbose=False,
)
_available = True

66
probe_all_symbols.sh Normal file
View File

@@ -0,0 +1,66 @@
#!/bin/bash
# probe_all_symbols.sh — Check which ixformer::infer symbols actually exist
echo "=== Checking all symbols we need ==="
LIBS=(
"/usr/local/corex/lib64/python3/dist-packages/ixformer/libixformer.so"
"/usr/local/corex/lib64/python3/dist-packages/ixformer/_ixformer_torch.cpython-310-x86_64-linux-gnu.so"
"/usr/local/corex/lib64/python3/dist-packages/ixformer/_C.cpython-310-x86_64-linux-gnu.so"
"/usr/local/corex/lib64/libixattn.so"
)
FUNCS=(
"silu_and_mul"
"topk_softmax"
"moe_compute_token_index"
"moe_expand_input"
"moe_w16a16_group_gemm"
"moe_output_reduce_sum"
"xllm_paged_attention"
"ixinfer_flash_attn_unpad"
"rms_norm"
"residual_rms_norm"
"xllm_rotary_embedding"
"xllm_reshape_and_cache"
"ixformer_linear"
)
for func in "${FUNCS[@]}"; do
echo ""
echo "--- $func ---"
found=0
for lib in "${LIBS[@]}"; do
if [ -f "$lib" ]; then
matches=$(nm -D "$lib" 2>/dev/null | grep -i "$func" | grep " T \| W " | head -3)
if [ -n "$matches" ]; then
echo " $(basename $lib):"
echo "$matches" | while read line; do echo " $line"; done
found=1
fi
fi
done
if [ "$found" -eq 0 ]; then
echo " NOT FOUND in any .so (may need dlopen or different namespace)"
# Also search undefined symbols to see if it's referenced somewhere
for lib in "${LIBS[@]}"; do
if [ -f "$lib" ]; then
undef=$(nm -D "$lib" 2>/dev/null | grep -i "$func" | grep " U " | head -2)
if [ -n "$undef" ]; then
echo " (undefined ref in $(basename $lib)):"
echo "$undef" | while read line; do echo " $line"; done
fi
fi
done
fi
done
echo ""
echo "=== Full ixformer::infer namespace in all libs ==="
for lib in "${LIBS[@]}"; do
if [ -f "$lib" ]; then
count=$(nm -D "$lib" 2>/dev/null | grep "ixformer.*infer" | grep " T \| W " | wc -l)
echo ""
echo "$(basename $lib): $count ixformer::infer symbols"
nm -D "$lib" 2>/dev/null | grep "ixformer.*infer" | grep " T \| W " | c++filt | head -20
fi
done

73
probe_symbol.sh Normal file
View File

@@ -0,0 +1,73 @@
#!/bin/bash
# probe_symbol.sh — Find which .so has silu_and_mul
echo "=== Searching for silu_and_mul symbol ==="
# The mangled name from the error
SYMBOL="_ZN8ixformer5infer12silu_and_mulERN2at6TensorES3_"
echo ""
echo "--- ixformer package .so files ---"
for f in /usr/local/corex/lib64/python3/dist-packages/ixformer/*.so; do
echo -n " $f: "
if nm -D "$f" 2>/dev/null | grep -q "$SYMBOL"; then
echo "FOUND ✓"
elif nm -D "$f" 2>/dev/null | grep -q "silu_and_mul"; then
echo "has silu_and_mul (different mangling):"
nm -D "$f" 2>/dev/null | grep "silu_and_mul"
else
echo "not found"
fi
done
echo ""
echo "--- /usr/local/corex/lib64/*.so ---"
for f in /usr/local/corex/lib64/*.so*; do
r=$(nm -D "$f" 2>/dev/null | grep -c "silu_and_mul")
if [ "$r" -gt 0 ]; then
echo " $f: $r matches"
nm -D "$f" 2>/dev/null | grep "silu_and_mul" | head -3
fi
done
echo ""
echo "--- Global search (may take a moment) ---"
find /usr/local/corex -name "*.so*" 2>/dev/null | while read f; do
r=$(nm -D "$f" 2>/dev/null | grep -c "silu_and_mul")
if [ "$r" -gt 0 ]; then
echo " $f: $r matches"
nm -D "$f" 2>/dev/null | grep "silu_and_mul" | head -3
fi
done
echo ""
echo "--- Also check vllm/torch installed .so ---"
find /usr/local/corex/lib64/python3/dist-packages/vllm -name "*.so" 2>/dev/null | while read f; do
r=$(nm -D "$f" 2>/dev/null | grep -c "silu_and_mul")
if [ "$r" -gt 0 ]; then
echo " $f: $r matches"
nm -D "$f" 2>/dev/null | grep "silu_and_mul" | head -3
fi
done
echo ""
echo "--- Python check: how does ixformer.functions.silu_and_mul resolve? ---"
python3 -c "
import ixformer.functions as F
fn = F.silu_and_mul
print(f'Type: {type(fn)}')
print(f'Module: {getattr(fn, \"__module__\", \"?\")}')
# Check if it's from a torch op or C++ binding
import inspect
try:
print(f'File: {inspect.getfile(fn)}')
except:
print('File: built-in/C extension')
# Try to find the actual implementation
import ixformer
print(f'ixformer._C: {hasattr(ixformer, \"_C\")}')
if hasattr(ixformer, '_C'):
c = ixformer._C
for attr in dir(c):
if 'silu' in attr.lower():
print(f' _C.{attr}')
"

223
verify_single_gpu.py Normal file
View File

@@ -0,0 +1,223 @@
#!/usr/bin/env python3
"""
verify_single_gpu.py — Single-card BI-V100 verification
Tests:
Step 0: JIT compile ix_full_bridge.cpp
Step 1: silu_and_mul (from _ixformer_torch.so)
Step 2: rms_norm
Step 3: fused_add_rms_norm
Step 4: linear (ixformer GEMM)
Step 5: ixformer.functions Python-level flash_attn
Step 6: ixformer.functions Python-level paged_attention
Step 7: corex_moe.py Python tiered dispatch (MoE full pipeline)
"""
import os, sys, time, traceback, glob
def step0_compile_bridge():
print("=" * 60)
print("STEP 0: JIT compile ix_full_bridge.cpp")
print("=" * 60)
here = os.path.dirname(os.path.abspath(__file__))
candidates = [
os.path.join(here, "ex_engine", "csrc", "ix_full_bridge.cpp"),
"/workspace/ex_engine/csrc/ix_full_bridge.cpp",
]
cpp_path = None
for c in candidates:
if os.path.exists(c):
cpp_path = c
break
if cpp_path is None:
print(f" ✗ ix_full_bridge.cpp NOT FOUND in {candidates}")
return None
print(f" Source: {cpp_path}")
from torch.utils.cpp_extension import load
extra_ldflags = []
try:
import ixformer
ixf_dir = os.path.dirname(ixformer.__file__)
for so in glob.glob(os.path.join(ixf_dir, "*.so")):
if "cpython" not in so:
extra_ldflags.append(so)
for so in glob.glob(os.path.join(ixf_dir, "_ixformer_torch*.so")):
extra_ldflags.append(so)
extra_ldflags.append(f"-Wl,-rpath,{ixf_dir}")
except ImportError:
pass
corex_lib = "/usr/local/corex/lib64"
if os.path.isdir(corex_lib):
extra_ldflags.append(f"-Wl,-rpath,{corex_lib}")
print(f" Link: {[os.path.basename(x) for x in extra_ldflags if not x.startswith('-')]}")
t0 = time.time()
try:
bridge = load(
name="ix_full_bridge",
sources=[cpp_path],
extra_cflags=["-O2", "-std=c++17"],
extra_ldflags=extra_ldflags,
verbose=True,
)
dt = time.time() - t0
fns = [x for x in dir(bridge) if not x.startswith("_")]
print(f" ✓ Compiled in {dt:.1f}s — functions: {fns}")
return bridge
except Exception as e:
print(f" ✗ FAILED after {time.time()-t0:.1f}s: {e}")
traceback.print_exc()
return None
def step1_silu(bridge):
import torch
print("\nSTEP 1: silu_and_mul")
x = torch.randn(4, 256, dtype=torch.float16, device="cuda") # will split into 128+128
try:
out = bridge.silu_and_mul(x)
print(f"{x.shape}{out.shape}, NaN={out.isnan().any().item()}, abs_mean={out.abs().mean().item():.4f}")
return True
except Exception as e:
print(f"{e}")
traceback.print_exc()
return False
def step2_rms_norm(bridge):
import torch
print("\nSTEP 2: rms_norm")
x = torch.randn(4, 128, dtype=torch.float16, device="cuda")
w = torch.ones(128, dtype=torch.float16, device="cuda")
out = torch.empty_like(x)
try:
bridge.rms_norm(out, x, w, 1e-6)
print(f"{out.shape}, NaN={out.isnan().any().item()}, abs_mean={out.abs().mean().item():.4f}")
return True
except Exception as e:
print(f"{e}")
traceback.print_exc()
return False
def step3_fused_add_rms_norm(bridge):
import torch
print("\nSTEP 3: fused_add_rms_norm")
x = torch.randn(4, 128, dtype=torch.float16, device="cuda")
res = torch.randn(4, 128, dtype=torch.float16, device="cuda")
w = torch.ones(128, dtype=torch.float16, device="cuda")
try:
bridge.fused_add_rms_norm(x, res, w, 1e-6)
print(f" ✓ x modified in-place, NaN={x.isnan().any().item()}")
return True
except Exception as e:
print(f"{e}")
traceback.print_exc()
return False
def step4_linear(bridge):
import torch
print("\nSTEP 4: linear (ixformer GEMM)")
x = torch.randn(4, 128, dtype=torch.float16, device="cuda")
w = torch.randn(256, 128, dtype=torch.float16, device="cuda")
try:
out = bridge.linear(x, w, None)
print(f"{x.shape} @ {w.shape}^T → {out.shape}, NaN={out.isnan().any().item()}")
return True
except Exception as e:
print(f"{e}")
traceback.print_exc()
return False
def step5_flash_attn_python():
import torch
print("\nSTEP 5: ixformer flash_attn (Python)")
try:
from ixformer.contrib.vllm_flash_attn import flash_attn_varlen_func
Hq, Hkv, D = 4, 1, 128
seq = 32
q = torch.randn(seq, Hq, D, dtype=torch.float16, device="cuda")
k = torch.randn(seq, Hkv, D, dtype=torch.float16, device="cuda")
v = torch.randn(seq, Hkv, D, dtype=torch.float16, device="cuda")
cu_q = torch.tensor([0, seq], dtype=torch.int32, device="cuda")
cu_k = torch.tensor([0, seq], dtype=torch.int32, device="cuda")
out = flash_attn_varlen_func(q, k, v, cu_q, cu_k, seq, seq,
softmax_scale=D**-0.5, causal=True)
print(f"{out.shape}, NaN={out.isnan().any().item()}")
return True
except Exception as e:
print(f"{e}")
return False
def step6_paged_attn_python():
import torch
print("\nSTEP 6: ixformer paged_attention (Python)")
try:
import ixformer.functions as ixf_F
fn = ixf_F.vllm_single_query_cached_kv_attention
# This is the V1 paged attention used by vllm on BI-V100
print(f" ✓ vllm_single_query_cached_kv_attention is available")
return True
except Exception as e:
print(f"{e}")
return False
def step7_corex_moe():
import torch
print("\nSTEP 7: corex_moe.py MoE pipeline")
here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, here)
try:
from ex_engine.python.corex_moe import moe_forward
except Exception as e:
print(f" ✗ Import failed: {e}")
return False
num_tokens, hidden, experts, inter, topk = 4, 256, 8, 64, 2
h = torch.randn(num_tokens, hidden, dtype=torch.float16, device="cuda")
g = torch.randn(num_tokens, experts, dtype=torch.float16, device="cuda")
w13 = torch.randn(experts, inter*2, hidden, dtype=torch.float16, device="cuda")
w2 = torch.randn(experts, hidden, inter, dtype=torch.float16, device="cuda")
try:
out = moe_forward(h, g, w13, w2, topk=topk, renormalize=True, num_experts=experts)
print(f"{out.shape}, NaN={out.isnan().any().item()}, abs_mean={out.abs().mean().item():.4f}")
return True
except Exception as e:
print(f"{e}")
traceback.print_exc()
return False
def main():
import torch
print("=" * 60)
print(" BI-V100 Single GPU Verification")
print(f" CUDA: {torch.cuda.is_available()}, Device: {torch.cuda.get_device_name(0)}")
print(f" Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
print("=" * 60)
R = {}
bridge = step0_compile_bridge()
R["compile"] = bridge is not None
if bridge:
R["silu_and_mul"] = step1_silu(bridge)
R["rms_norm"] = step2_rms_norm(bridge)
R["fused_add_rms_norm"] = step3_fused_add_rms_norm(bridge)
R["linear"] = step4_linear(bridge)
R["flash_attn_python"] = step5_flash_attn_python()
R["paged_attn_python"] = step6_paged_attn_python()
R["corex_moe"] = step7_corex_moe()
print("\n" + "=" * 60)
print(" SUMMARY")
print("=" * 60)
for k, v in R.items():
print(f" {'' if v else ''} {k}")
p = sum(R.values())
print(f"\n {p}/{len(R)} passed")
if R.get("compile") and R.get("silu_and_mul"):
print("\n >>> C++ bridge works — silu_and_mul/rms_norm/linear accelerated <<<")
return 0 if p == len(R) else 1
if __name__ == "__main__":
sys.exit(main())