From 03be5f2b152ba222e153f79bb118049c7d3c9b3b Mon Sep 17 00:00:00 2001 From: root Date: Mon, 17 Aug 2026 02:16:58 +0000 Subject: [PATCH] [feat] group gemm --- Dockerfile | 8 +- build_moe_bridge.sh | 156 ++++++++ computility-run.yaml | 4 +- ex_engine/csrc/ix_full_bridge_v2.cpp | 357 ++++++++---------- ex_engine/csrc/moe_ops_impl.cu | 502 +++++++++++++++++++++++++ ex_engine/python/moe_dispatch.py | 172 +++++++++ ex_engine/python/patch_moe_hot_path.py | 109 ++++++ qwen3_6_scripts/patch_ops.sh | 19 +- test_moe_bridge.py | 186 +++++++++ 9 files changed, 1299 insertions(+), 214 deletions(-) create mode 100644 build_moe_bridge.sh create mode 100644 ex_engine/csrc/moe_ops_impl.cu create mode 100644 ex_engine/python/moe_dispatch.py create mode 100644 ex_engine/python/patch_moe_hot_path.py create mode 100644 test_moe_bridge.py diff --git a/Dockerfile b/Dockerfile index faa0a98a..bc2b17e7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,13 @@ WORKDIR /workspace/ # Copy all our engine patches COPY ./qwen3_6_scripts /workspace/qwen3_6_scripts COPY ./computility-run.yaml /workspace/computility-run.yaml +# Copy ex_engine source for MoE bridge compilation +COPY ./ex_engine/csrc/moe_ops_impl.cu /workspace/qwen3_6_scripts/ex_engine_src/csrc/moe_ops_impl.cu +COPY ./ex_engine/csrc/ix_full_bridge_v2.cpp /workspace/qwen3_6_scripts/ex_engine_src/csrc/ix_full_bridge_v2.cpp +COPY ./ex_engine/build_moe_bridge.sh /workspace/qwen3_6_scripts/ex_engine_src/build_moe_bridge.sh +COPY ./ex_engine/python/moe_dispatch.py /workspace/qwen3_6_scripts/ex_engine_src/python/moe_dispatch.py +COPY ./ex_engine/python/patch_moe_hot_path.py /workspace/qwen3_6_scripts/ex_engine_src/python/patch_moe_hot_path.py # Make patch script executable and run it RUN chmod +x /workspace/qwen3_6_scripts/patch_ops.sh && \ bash /workspace/qwen3_6_scripts/patch_ops.sh 2>&1 | tee /workspace/patch_ops.log ; \ - echo "[Dockerfile] patch_ops exit code: $?" + echo "[Dockerfile] patch_ops exit code: $?" \ No newline at end of file diff --git a/build_moe_bridge.sh b/build_moe_bridge.sh new file mode 100644 index 00000000..32ff7ef5 --- /dev/null +++ b/build_moe_bridge.sh @@ -0,0 +1,156 @@ +#!/usr/bin/env bash +# build_moe_bridge.sh — Compile MoE ops + bridge into ix_moe_bridge.so +# +# Links against: +# libcuinfer.so (cuinferCustomGemm, cuinferTopK — confirmed in symbol dump) +# libixformer.so (silu_and_mul, rms_norm, flash_attn, etc — confirmed) +# +# Real device compiler: corex clang/16, NOT nvcc +# Reference: ex_engine/build_ix_bridge.sh + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" +VLLM_ROOT="${1:-}" + +echo "[moe_bridge] Building ix_moe_bridge.so" +echo "[moe_bridge] Script dir: ${SCRIPT_DIR}" + +# --- Locate sources --- +MOE_CU="${SCRIPT_DIR}/ex_engine/csrc/moe_ops_impl.cu" +BRIDGE_CPP="${SCRIPT_DIR}/ex_engine/csrc/ix_full_bridge_v2.cpp" + +if [[ ! -f "$MOE_CU" ]]; then + echo "[moe_bridge] ERROR: $MOE_CU not found" >&2 + exit 1 +fi +if [[ ! -f "$BRIDGE_CPP" ]]; then + echo "[moe_bridge] ERROR: $BRIDGE_CPP not found" >&2 + exit 1 +fi + +# --- Locate libraries --- +COREX_ROOT="${COREX_ROOT:-/usr/local/corex}" + +# Find libcuinfer.so +CUINFER_SO="" +for d in "${COREX_ROOT}/lib64" "${COREX_ROOT}/lib" "/usr/lib64" "/usr/lib"; do + if [[ -f "${d}/libcuinfer.so" ]]; then + CUINFER_SO="${d}/libcuinfer.so" + break + fi +done + +# Find libixformer.so and ixformer Python package +IX_LIB_DIR="" +IX_SO_FILES=() +for d in \ + "${COREX_ROOT}/lib/python3/dist-packages/ixformer" \ + "${COREX_ROOT}/lib64/python3/dist-packages/ixformer" \ + "$(python3 -c 'import ixformer, os; print(os.path.dirname(ixformer.__file__))' 2>/dev/null || echo '')"; do + if [[ -d "$d" ]]; then + IX_LIB_DIR="$d" + while IFS= read -r so; do + IX_SO_FILES+=("$so") + done < <(find "$d" -name "*.so" -type f 2>/dev/null) + break + fi +done + +echo "[moe_bridge] COREX_ROOT: ${COREX_ROOT}" +echo "[moe_bridge] cuinfer: ${CUINFER_SO:-NOT FOUND}" +echo "[moe_bridge] ixformer dir: ${IX_LIB_DIR:-NOT FOUND}" +echo "[moe_bridge] ixformer .so count: ${#IX_SO_FILES[@]}" + +# --- Build via torch.utils.cpp_extension --- +mkdir -p "${SCRIPT_DIR}/prebuilt" + +python3 << 'PYEOF' +import os, sys, glob, shutil + +script_dir = os.environ.get("SCRIPT_DIR", ".") +vllm_root = os.environ.get("VLLM_ROOT", "") + +moe_cu = os.path.join(script_dir, "ex_engine", "csrc", "moe_ops_impl.cu") +bridge_cpp = os.path.join(script_dir, "ex_engine", "csrc", "ix_full_bridge_v2.cpp") + +# Collect linker flags +extra_ldflags = [] +rpath_dirs = set() + +corex_root = os.environ.get("COREX_ROOT", "/usr/local/corex") +for search_dir in [ + os.path.join(corex_root, "lib64"), + os.path.join(corex_root, "lib"), +]: + if os.path.isdir(search_dir): + rpath_dirs.add(search_dir) + for so in glob.glob(os.path.join(search_dir, "libcuinfer*.so*")): + extra_ldflags.append(so) + +# ixformer .so files +try: + import ixformer + ix_dir = os.path.dirname(ixformer.__file__) + rpath_dirs.add(ix_dir) + for so in glob.glob(os.path.join(ix_dir, "*.so")): + extra_ldflags.append(so) + for so in glob.glob(os.path.join(ix_dir, "lib*.so")): + if so not in extra_ldflags: + extra_ldflags.append(so) +except ImportError: + # Search common paths + for d in [ + os.path.join(corex_root, "lib", "python3", "dist-packages", "ixformer"), + os.path.join(corex_root, "lib64", "python3", "dist-packages", "ixformer"), + ]: + if os.path.isdir(d): + rpath_dirs.add(d) + for so in glob.glob(os.path.join(d, "*.so")): + extra_ldflags.append(so) + +for d in rpath_dirs: + extra_ldflags.append(f"-Wl,-rpath,{d}") + +print(f"[moe_bridge] Linking against {len(extra_ldflags)} items") +for f in extra_ldflags[:10]: + print(f" {f}") + +try: + from torch.utils.cpp_extension import load + + mod = load( + name="ix_moe_bridge", + sources=[moe_cu, bridge_cpp], + extra_include_paths=[os.path.join(script_dir, "csrc")], + extra_cflags=["-O2", "-std=c++17"], + extra_cuda_cflags=["-O2", ], + extra_ldflags=extra_ldflags, + verbose=True, + ) + print("[moe_bridge] ✓ Compilation successful") + + # Find and copy the built .so + import importlib + spec = importlib.util.find_spec("ix_moe_bridge") + if spec and spec.origin: + dst = os.path.join(script_dir, "prebuilt", "ix_moe_bridge.so") + shutil.copy2(spec.origin, dst) + print(f"[moe_bridge] ✓ Saved to {dst}") + + if vllm_root: + vllm_dst = os.path.join(vllm_root, "ex_engine", "ix_moe_bridge.so") + os.makedirs(os.path.dirname(vllm_dst), exist_ok=True) + shutil.copy2(spec.origin, vllm_dst) + print(f"[moe_bridge] ✓ Deployed to {vllm_dst}") + else: + print("[moe_bridge] ⚠ Could not locate compiled .so via importlib") + +except Exception as e: + print(f"[moe_bridge] ERROR: {e}", file=sys.stderr) + import traceback; traceback.print_exc() + sys.exit(1) +PYEOF + +echo "[moe_bridge] Done" \ No newline at end of file diff --git a/computility-run.yaml b/computility-run.yaml index 2e09be09..5f94d0a5 100644 --- a/computility-run.yaml +++ b/computility-run.yaml @@ -15,7 +15,7 @@ command: - -tp - '4' - --max-num-seqs - - '1' + - '2' - --disable-log-requests - --disable-frontend-multiprocessing - --max-num-batched-tokens @@ -46,4 +46,4 @@ env: - name: BI100_GDN_RESTORE_MODE value: hybrid64 - name: BI100_MOE_COREX_TOPK_SOFTMAX - value: '1' + value: '1' \ No newline at end of file diff --git a/ex_engine/csrc/ix_full_bridge_v2.cpp b/ex_engine/csrc/ix_full_bridge_v2.cpp index f5396150..d81cef68 100644 --- a/ex_engine/csrc/ix_full_bridge_v2.cpp +++ b/ex_engine/csrc/ix_full_bridge_v2.cpp @@ -1,21 +1,24 @@ -// ix_full_bridge_v2.cpp — Complete bridge to ALL ixformer::infer C++ functions +// ix_full_bridge_v2.cpp — Bridge to ixformer C++ functions + MoE pipeline // -// Base image has ixformer::infer namespace with 14 functions. -// Previous ix_full_bridge.cpp only bridged 4 (silu_and_mul, rms_norm, -// fused_add_rms_norm, linear). This file bridges ALL 14. +// Forward declarations use REAL symbols from nm -D symbol dumps: +// _ixformer_torch.so → namespace ixformer_torch_ext (7 functions) +// moe_ops_impl.cu → namespace ixformer::infer (5 MoE functions, self-compiled) // -// The base image's _ixformer_torch.cpython-310.so and libixformer.so -// export these symbols in the ixformer::infer namespace (confirmed by nm -D). +// Symbol dump verified: +// ixformer_torch_ext::silu_and_mul_forward(at::Tensor&, at::Tensor&) +// ixformer_torch_ext::rms_norm_forward(at::Tensor&, at::Tensor&, at::Tensor&, double) +// ixformer_torch_ext::fused_add_rms_norm_forward(at::Tensor&, at::Tensor&, at::Tensor&, double, double) +// ixformer_torch_ext::ixformer_linear(at::Tensor&, at::Tensor&, c10::optional, c10::optional) +// ixformer_torch_ext::ixformer_linear_ex(at::Tensor&, at::Tensor&, c10::optional) +// ixformer_torch_ext::vllm_rotary_embedding_neox(at::Tensor&, at::Tensor&, at::Tensor&, long, at::Tensor&, long, bool) +// ixformer_torch_ext::vllm_cache_ops_reshape_and_cache(at::Tensor&, at::Tensor&, at::Tensor&, at::Tensor&, at::Tensor&, long, long) +// ixformer_torch_ext::vllm_single_query_cached_kv_attention(13 params — see below) // -// Compile: -// torch.utils.cpp_extension.load( -// name="ix_full_bridge_v2", -// sources=["ix_full_bridge_v2.cpp"], -// extra_ldflags=[, "-Wl,-rpath,..."], -// extra_cflags=["-O2", "-std=c++17"], -// ) -// -// Upstream reference: xllm_latest/core/kernels/ilu/ixformer.h +// NOT available in any .so (confirmed by nm -D on all 4 .so files): +// ixinfer_flash_attn_unpad_with_block_tables — DOES NOT EXIST +// xllm_paged_attention — DOES NOT EXIST +// topk_softmax, moe_w16a16_group_gemm, etc — NOT in libixformer.so +// (provided by moe_ops_impl.cu instead) #include #include @@ -24,103 +27,67 @@ #include // ============================================================================ -// Forward declarations — ixformer::infer namespace from base image .so -// Signatures EXACTLY match upstream_ref/xllm_latest/core/kernels/ilu/ixformer.h +// Forward declarations — ixformer_torch_ext namespace from _ixformer_torch.so +// Signatures EXACTLY match nm -D | c++filt output +// ============================================================================ +namespace ixformer_torch_ext { + +// silu_and_mul_forward(at::Tensor&, at::Tensor&) +void silu_and_mul_forward(at::Tensor& input, at::Tensor& output); + +// rms_norm_forward(at::Tensor&, at::Tensor&, at::Tensor&, double) +void rms_norm_forward(at::Tensor& output, at::Tensor& input, + at::Tensor& weight, double eps); + +// fused_add_rms_norm_forward(at::Tensor&, at::Tensor&, at::Tensor&, double, double) +void fused_add_rms_norm_forward(at::Tensor& input, at::Tensor& residual, + at::Tensor& weight, double eps, double alpha); + +// ixformer_linear(at::Tensor&, at::Tensor&, c10::optional const&, c10::optional const&) +at::Tensor ixformer_linear(at::Tensor& input, at::Tensor& weight, + c10::optional const& bias, + c10::optional const& out); + +// ixformer_linear_ex(at::Tensor&, at::Tensor&, c10::optional const&) +at::Tensor ixformer_linear_ex(at::Tensor& input, at::Tensor& weight, + c10::optional const& bias); + +// vllm_rotary_embedding_neox(at::Tensor&, at::Tensor&, at::Tensor&, long, at::Tensor&, long, bool) +void vllm_rotary_embedding_neox(at::Tensor& positions, at::Tensor& query, + at::Tensor& key, int64_t head_size, + at::Tensor& cos_sin_cache, + int64_t max_position, bool is_neox); + +// vllm_cache_ops_reshape_and_cache(at::Tensor&, at::Tensor&, at::Tensor&, at::Tensor&, at::Tensor&, long, long) +void vllm_cache_ops_reshape_and_cache(at::Tensor& key, at::Tensor& value, + at::Tensor& key_cache, + at::Tensor& value_cache, + at::Tensor& slot_mapping, + int64_t key_token_stride, + int64_t value_token_stride); + +// vllm_single_query_cached_kv_attention(at::Tensor& x13) +// Full signature from nm -D: +// (at::Tensor&, at::Tensor&, at::Tensor&, at::Tensor&, at::Tensor&, +// double, at::Tensor&, at::Tensor&, long, long, long, bool, +// c10::optional const&) +void vllm_single_query_cached_kv_attention( + at::Tensor& output, at::Tensor& query, + at::Tensor& key_cache, at::Tensor& value_cache, + at::Tensor& head_mapping, double scale, + at::Tensor& block_tables, at::Tensor& context_lens, + int64_t block_size, int64_t max_context_len, int64_t num_kv_heads, + bool is_neox, + c10::optional const& alibi_slopes); + +} // namespace ixformer_torch_ext + +// ============================================================================ +// Forward declarations — ixformer::infer namespace from moe_ops_impl.cu +// These 5 MoE functions are compiled from our own CUDA code, NOT from .so // ============================================================================ namespace ixformer { namespace infer { -// --- 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& alibi_slopes, - const std::optional& sinks, - std::optional& lse); - -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& 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& sinks); - -// --- Activation --- -void silu_and_mul(torch::Tensor& input, torch::Tensor& output); - -// --- Linear --- -torch::Tensor ixformer_linear(torch::Tensor& input, - torch::Tensor& weight, - int64_t act_type, - const std::optional& bias, - const std::optional& out, - const std::optional persistent); - -torch::Tensor ixformer_linear_ex(torch::Tensor& input, - torch::Tensor& weight, - const c10::optional& bias, - const c10::optional& out); - -// --- 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); - -// --- 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); - -// --- Norm --- -void residual_rms_norm(torch::Tensor& input, - torch::Tensor& residual, - torch::Tensor& weight, - torch::Tensor& output, - torch::Tensor& residual_output, - const std::optional& fused_bias, - double alpha, - double eps, - bool is_post); - -void rms_norm(torch::Tensor& input, - torch::Tensor& weight, - torch::Tensor& output, - const std::optional& fused_bias, - double eps); - -// --- MoE --- void topk_softmax(torch::Tensor& topk_weights, torch::Tensor& topk_indices, torch::Tensor& token_expert_indices, @@ -132,9 +99,9 @@ void moe_compute_token_index_api( torch::Tensor& src_dst, torch::Tensor& dst_src, torch::Tensor& expert_sizes_gpu, - const c10::optional& expert_mask, - const c10::optional& expert_sizes_cpu, - const c10::optional& expand_tokens_gpu, + const std::optional& expert_mask, + const std::optional& expert_sizes_cpu, + const std::optional& expand_tokens_gpu, int64_t start_expert_id, int64_t end_expert_id, int64_t num_experts); @@ -142,7 +109,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& src_to_dst, + const std::optional& src_to_dst, int64_t dst_tokens, int64_t expand_factor); @@ -150,52 +117,45 @@ void moe_w16a16_group_gemm(torch::Tensor output, torch::Tensor inputs, torch::Tensor weights, torch::Tensor tokens_per_experts, - const c10::optional& dst_to_src, - const c10::optional& bias, + const std::optional& dst_to_src, + const std::optional& 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& mul_weight, - const c10::optional& mask, - const c10::optional& extra_residual, + const std::optional& mul_weight, + const std::optional& mask, + const std::optional& extra_residual, double scaling_factor); }} // namespace ixformer::infer // ============================================================================ -// Python wrappers — thin wrappers that match ix_bridge.py's expected API +// Python wrappers — thin wrappers matching ix_bridge.py's expected API // ============================================================================ // --- 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; } // --- rms_norm --- void ix_rms_norm(torch::Tensor output, torch::Tensor input, torch::Tensor weight, double eps) { - ixformer::infer::rms_norm(input, weight, output, - /*fused_bias=*/std::nullopt, eps); + ixformer_torch_ext::rms_norm_forward(output, input, weight, eps); } // --- fused_add_rms_norm --- -// residual_rms_norm does: output = rms_norm(input + alpha*residual, weight, eps) -// residual_output = input + alpha*residual 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, - /*fused_bias=*/std::nullopt, - /*alpha=*/1.0, eps, - /*is_post=*/false); + torch::Tensor weight, double eps) { + ixformer_torch_ext::fused_add_rms_norm_forward( + input, residual, weight, eps, /*alpha=*/1.0); } // --- linear --- @@ -204,74 +164,56 @@ torch::Tensor ix_linear(torch::Tensor input, torch::Tensor weight, auto input_2d = input.view({-1, input.size(-1)}); int64_t m = input_2d.size(0); if (m <= 1 && !bias.has_value()) { - return ixformer::infer::ixformer_linear_ex( - input, weight, bias, /*out=*/c10::optional()); + return ixformer_torch_ext::ixformer_linear_ex(input, weight, bias); } - return ixformer::infer::ixformer_linear( - input, weight, /*act_type=*/0, bias, - /*out=*/std::nullopt, /*persistent=*/std::nullopt); + return ixformer_torch_ext::ixformer_linear( + input, weight, bias, /*out=*/c10::optional()); } // --- rotary_embedding --- 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); + int64_t max_position = cos_sin_cache.size(0); + ixformer_torch_ext::vllm_rotary_embedding_neox( + positions, query, key, head_size, cos_sin_cache, max_position, is_neox); } // --- reshape_and_cache --- void ix_reshape_and_cache(torch::Tensor key, torch::Tensor value, torch::Tensor key_cache, torch::Tensor value_cache, torch::Tensor slot_mapping) { - // token stride = product of dims after dim 0 for key/value - // key shape: [num_tokens, num_heads, head_dim] int64_t key_token_stride = 1; for (int i = 1; i < key.dim(); i++) key_token_stride *= key.size(i); int64_t value_token_stride = 1; for (int i = 1; i < value.dim(); i++) value_token_stride *= value.size(i); - ixformer::infer::xllm_reshape_and_cache( + ixformer_torch_ext::vllm_cache_ops_reshape_and_cache( key, value, key_cache, value_cache, slot_mapping, key_token_stride, value_token_stride); } -// --- paged_attention (decode) --- -torch::Tensor ix_paged_attention( +// --- paged_attention (decode only — no prefill available in .so) --- +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 head_mapping, double scale, torch::Tensor block_tables, torch::Tensor context_lens, - int64_t block_size, int64_t max_context_len, + int64_t block_size, int64_t max_context_len, int64_t num_kv_heads, const c10::optional& alibi_slopes) { - return ixformer::infer::xllm_paged_attention( + ixformer_torch_ext::vllm_single_query_cached_kv_attention( output, query, key_cache, value_cache, - num_kv_heads, scale, block_tables, context_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=*/std::nullopt); + head_mapping, scale, block_tables, context_lens, + block_size, max_context_len, num_kv_heads, + /*is_neox=*/true, alibi_slopes); } -// --- flash_attn_prefill --- -torch::Tensor ix_flash_attn_prefill( - torch::Tensor query, torch::Tensor key_cache, torch::Tensor value_cache, - 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 lse = std::nullopt; - return ixformer::infer::ixinfer_flash_attn_unpad_with_block_tables( - query, key_cache, value_cache, 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=*/std::nullopt, /*sinks=*/std::nullopt, lse); -} -// --- MoE: topk_softmax --- -// Returns (topk_weights, topk_ids, token_expert_indices) +// ============================================================================ +// MoE wrappers — call moe_ops_impl.cu implementations +// ============================================================================ + +// --- topk_softmax --- std::tuple ix_topk_softmax(torch::Tensor gating_output, int64_t topk, bool renormalize) { int64_t num_tokens = gating_output.size(0); @@ -289,8 +231,7 @@ ix_topk_softmax(torch::Tensor gating_output, int64_t topk, bool renormalize) { return std::make_tuple(topk_weights, topk_ids, token_expert_indices); } -// --- MoE: moe_gen_idx --- -// Equivalent to xllm::kernel::ilu::moe_gen_idx +// --- moe_gen_idx --- std::vector ix_moe_gen_idx(torch::Tensor expert_id, int64_t expert_num) { auto src_dst = expert_id.new_empty({expert_id.numel()}); @@ -299,9 +240,9 @@ ix_moe_gen_idx(torch::Tensor expert_id, int64_t expert_num) { 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=*/std::nullopt, + /*expert_sizes_cpu=*/std::nullopt, + /*expand_tokens_gpu=*/std::nullopt, /*start_expert_id=*/0, /*end_expert_id=*/expert_num, /*num_experts=*/expert_num); @@ -310,7 +251,7 @@ ix_moe_gen_idx(torch::Tensor expert_id, int64_t expert_num) { return {src_dst, dst_src, expert_sizes_gpu, expert_sizes_cumsum}; } -// --- MoE: moe_expand_input --- +// --- moe_expand_input --- torch::Tensor ix_moe_expand_input(torch::Tensor input, torch::Tensor gather_index, torch::Tensor combine_idx, @@ -322,43 +263,41 @@ torch::Tensor ix_moe_expand_input(torch::Tensor input, return output; } -// --- MoE: group_gemm --- +// --- group_gemm --- torch::Tensor ix_group_gemm(torch::Tensor inputs, torch::Tensor weights, torch::Tensor tokens_per_experts, int64_t output_n) { int64_t total_tokens = inputs.size(0); auto output = inputs.new_empty({total_tokens, output_n}); + int64_t gemm_output_n = tokens_per_experts.sum().item(); ixformer::infer::moe_w16a16_group_gemm( output, inputs, weights, tokens_per_experts, - /*dst_to_src=*/c10::nullopt, - /*bias=*/c10::nullopt, - /*format=*/"default", + /*dst_to_src=*/std::nullopt, + /*bias=*/std::nullopt, + /*format=*/"TN", /*persistent=*/0, - output_n); + gemm_output_n); return output; } -// --- MoE: moe_combine_result --- +// --- moe_combine_result --- torch::Tensor ix_moe_combine_result(torch::Tensor input, torch::Tensor weight) { - // input: [T*topk, H], weight: [T, topk] auto input_3d = input.view({-1, weight.size(1), input.size(1)}); auto output = input.new_empty({input_3d.size(0), input_3d.size(2)}); ixformer::infer::moe_output_reduce_sum( output, input_3d, weight, - /*mask=*/c10::nullopt, - /*extra_residual=*/c10::nullopt, + /*mask=*/std::nullopt, + /*extra_residual=*/std::nullopt, /*scaling_factor=*/1.0); return output; } -// --- MoE: fused_moe_forward (7-step pipeline) --- -// This is the full fused MoE forward: topk → gen_idx → expand → gemm(w13) → -// silu_mul → gemm(w2) → combine +// --- fused_moe_forward (7-step pipeline) --- torch::Tensor ix_fused_moe_forward( torch::Tensor hidden_states, torch::Tensor router_logits, - torch::Tensor w13, // [num_experts, 2*intermediate, hidden] - torch::Tensor w2, // [num_experts, hidden, intermediate] + torch::Tensor w13, + torch::Tensor w2, int64_t topk, int64_t num_experts, bool renormalize) { @@ -383,7 +322,7 @@ torch::Tensor ix_fused_moe_forward( // Step 4: group_gemm (w13: gate_up projection) int64_t intermediate_2x = w13.size(1); - auto gate_up = ix_group_gemm(expanded, w13.view({-1, w13.size(2)}), + auto gate_up = ix_group_gemm(expanded, w13, expert_sizes_gpu, intermediate_2x); // Step 5: silu_and_mul @@ -391,7 +330,7 @@ torch::Tensor ix_fused_moe_forward( // Step 6: group_gemm (w2: down projection) int64_t hidden_size = w2.size(1); - auto down = ix_group_gemm(activated, w2.view({-1, w2.size(2)}), + auto down = ix_group_gemm(activated, w2, expert_sizes_gpu, hidden_size); // Step 7: moe_combine_result @@ -402,50 +341,48 @@ torch::Tensor ix_fused_moe_forward( // ============================================================================ -// Module registration — ALL 14 functions + fused pipeline +// Module registration // ============================================================================ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { // Activation m.def("silu_and_mul", &ix_silu_and_mul, - "Fused SiLU+mul activation via ixformer::infer"); + "Fused SiLU+mul via ixformer_torch_ext"); // Norm m.def("rms_norm", &ix_rms_norm, - "RMSNorm via ixformer::infer"); + "RMSNorm via ixformer_torch_ext"); m.def("fused_add_rms_norm", &ix_fused_add_rms_norm, - "Residual + RMSNorm via ixformer::infer"); + "Residual + RMSNorm via ixformer_torch_ext"); // Linear m.def("linear", &ix_linear, - "GEMM via ixformer::infer (linear/linear_ex)"); + "GEMM via ixformer_torch_ext"); // RoPE m.def("rotary_embedding", &ix_rotary_embedding, - "Rotary position embedding via ixformer::infer"); + "Rotary embedding via ixformer_torch_ext"); // Cache m.def("reshape_and_cache", &ix_reshape_and_cache, - "KV cache reshape+store via ixformer::infer"); + "KV cache reshape+store via ixformer_torch_ext"); - // Attention + // Attention (decode only) m.def("paged_attention", &ix_paged_attention, - "Paged attention decode via ixformer::infer"); - m.def("flash_attn_prefill", &ix_flash_attn_prefill, - "Flash attention prefill via ixformer::infer"); + "Paged attention decode via ixformer_torch_ext"); - // MoE (individual steps) + // MoE (individual steps — from moe_ops_impl.cu) m.def("topk_softmax", &ix_topk_softmax, - "MoE topk+softmax routing via ixformer::infer"); + "MoE topk+softmax routing"); m.def("moe_gen_idx", &ix_moe_gen_idx, - "MoE compute token index via ixformer::infer"); + "MoE compute token index"); m.def("moe_expand_input", &ix_moe_expand_input, - "MoE expand input for expert dispatch via ixformer::infer"); + "MoE expand input for expert dispatch"); m.def("group_gemm", &ix_group_gemm, - "MoE grouped GEMM via ixformer::infer"); + "MoE grouped GEMM via cuinferCustomGemm"); m.def("moe_combine_result", &ix_moe_combine_result, - "MoE output reduce sum via ixformer::infer"); + "MoE output reduce sum"); // MoE (fused 7-step pipeline) m.def("fused_moe_forward", &ix_fused_moe_forward, - "Complete fused MoE forward (7-step pipeline) via ixformer::infer"); -} + "Complete fused MoE forward (7-step pipeline)"); +} \ No newline at end of file diff --git a/ex_engine/csrc/moe_ops_impl.cu b/ex_engine/csrc/moe_ops_impl.cu new file mode 100644 index 00000000..c3b7cf3f --- /dev/null +++ b/ex_engine/csrc/moe_ops_impl.cu @@ -0,0 +1,502 @@ +// moe_ops_impl.cu — Implement the 5 missing MoE functions +// +// These functions are declared in ixformer.h (from xllm upstream) +// but NOT present in the base image's libixformer.so. +// +// We implement them using available primitives: +// - cuinferCustomGemm (from libcuinfer.so) for group_gemm +// - Pure CUDA kernels for topk_softmax, moe_compute_index, expand, combine +// - ixformer::functions::cuinfer_gemm (from libixformer.so) as fallback +// +// Reference AST chain: +// xllm/core/kernels/ilu/fused_moe.cpp → calls these 5 functions +// xllm/core/kernels/ilu/group_gemm.cpp → calls moe_w16a16_group_gemm +// xllm/core/kernels/ilu/ixformer.h → declares them in ixformer::infer +// +// We provide them in the SAME namespace so ix_full_bridge_v2.cpp links cleanly. + +#include +#include +#include +#include +#include +#include +#include +#include + +// ============================================================================ +// Forward-declare cuinfer C API (from libcuinfer.so, confirmed in symbol dump) +// ============================================================================ +extern "C" { + +typedef struct cuinferContext* cuinferHandle_t; +typedef enum { CUINFER_STATUS_SUCCESS = 0 } cuinferStatus_t; +typedef enum { + CUINFER_OP_TENSOR_OP_N = 0, + CUINFER_OP_TENSOR_OP_T = 1, +} cuinferOperation_t; +typedef enum { + CUINFER_GEMM_DEFAULT = 0, +} cuinferGEMMCustomOption_t; +typedef enum { + CUINFER_POINTER_MODE_HOST = 0, +} cuinferPointerMode_t; + +cuinferStatus_t cuinferCreate(cuinferHandle_t* handle); +cuinferStatus_t cuinferDestroy(cuinferHandle_t handle); +cuinferStatus_t cuinferSetStream(cuinferHandle_t handle, cudaStream_t stream); + +cuinferStatus_t cuinferCustomGemm( + cuinferHandle_t handle, cudaStream_t stream, + cuinferPointerMode_t ptrMode, + cuinferOperation_t transa, cuinferOperation_t transb, + int m, int n, int k, + const void* alpha, + const void* A, cudaDataType_t Atype, int lda, long long int strideA, + const void* B, cudaDataType_t Btype, int ldb, long long int strideB, + const void* beta, + void* C, cudaDataType_t Ctype, int ldc, long long int strideC, + int batchCount, + cudaDataType_t computeType, cudaDataType_t scaleType, + const void* customHostPtr, const void* customDevicePtr, + cuinferGEMMCustomOption_t customOption); + +} // extern "C" + + +// ============================================================================ +// Kernel 1: topk_softmax +// Adapted from moe_topk_softmax_v3.cu (already working, 64-expert specialized) +// ============================================================================ + +// Qwen3.5-27B: 128 routed experts +// Block size = 128 threads (1 thread per expert for ≤128 experts) +static constexpr int MOE_MAX_EXPERTS = 128; +static constexpr int MOE_BLOCK = 128; + +// All reductions use blockDim.x (dynamic block size, power-of-2) +__device__ float smem_reduce_max(float val, float* smem) { + int tid = threadIdx.x; + smem[tid] = val; + __syncthreads(); + for (int s = blockDim.x / 2; s > 0; s >>= 1) { + if (tid < s) smem[tid] = fmaxf(smem[tid], smem[tid + s]); + __syncthreads(); + } + return smem[0]; +} + +__device__ float smem_reduce_sum(float val, float* smem) { + int tid = threadIdx.x; + smem[tid] = val; + __syncthreads(); + for (int s = blockDim.x / 2; s > 0; s >>= 1) { + if (tid < s) smem[tid] += smem[tid + s]; + __syncthreads(); + } + return smem[0]; +} + +__device__ void smem_argmax(float val, int idx, float* s_val, int* s_idx) { + int tid = threadIdx.x; + s_val[tid] = val; + s_idx[tid] = idx; + __syncthreads(); + for (int s = blockDim.x / 2; s > 0; s >>= 1) { + if (tid < s && s_val[tid + s] > s_val[tid]) { + s_val[tid] = s_val[tid + s]; + s_idx[tid] = s_idx[tid + s]; + } + __syncthreads(); + } +} + +__global__ void topk_softmax_kernel( + const float* __restrict__ input, + float* __restrict__ topk_weights, + int32_t* __restrict__ topk_indices, + int32_t* __restrict__ token_expert_indices, + int num_tokens, int num_experts, int topk, bool renormalize +) { + int row = blockIdx.x; + if (row >= num_tokens) return; + int tid = threadIdx.x; + + extern __shared__ char shared_buf[]; + float* smem = (float*)shared_buf; + int* smem_idx = (int*)(smem + blockDim.x); + + // num_experts passed via gridDim.y (encoded), or read from shared + // We use a separate parameter for clarity + float val = (tid < num_experts) ? input[row * num_experts + tid] : -1e30f; + + // Softmax + float row_max = smem_reduce_max(val, smem); + val = (tid < num_experts) ? expf(val - row_max) : 0.0f; + float row_sum = smem_reduce_sum(val, smem); + val *= (1.0f / row_sum); + + float* out_w = topk_weights + row * topk; + int32_t* out_idx = topk_indices + row * topk; + int32_t* out_src = token_expert_indices + row * topk; + + float my_val = val; + float topk_sum = 0.0f; + + for (int ki = 0; ki < topk; ki++) { + smem_argmax(my_val, tid, smem, smem_idx); + float winner_val = smem[0]; + int winner_idx = smem_idx[0]; + __syncthreads(); + + if (tid == 0) { + out_w[ki] = winner_val; + out_idx[ki] = winner_idx; + out_src[ki] = row; + } + topk_sum += winner_val; + if (tid == winner_idx) my_val = -1.0f; + __syncthreads(); + } + + if (renormalize && tid == 0) { + float inv = 1.0f / (topk_sum + 1e-8f); + for (int ki = 0; ki < topk; ki++) + out_w[ki] *= inv; + } +} + + +// ============================================================================ +// Kernel 2: moe_compute_token_index +// Histogram + prefix sum + scatter — from xllm_kernels/cuda/moe_compute_index.cu +// ============================================================================ + +__global__ void histogram_kernel( + const int32_t* __restrict__ expert_ids, + int32_t* __restrict__ expert_sizes, + int num_elements, int num_experts +) { + int idx = blockIdx.x * blockDim.x + threadIdx.x; + if (idx < num_elements) { + int eid = expert_ids[idx]; + if (eid >= 0 && eid < num_experts) { + atomicAdd(&expert_sizes[eid], 1); + } + } +} + +__global__ void place_indices_kernel( + const int32_t* __restrict__ expert_ids, + int32_t* __restrict__ expert_offsets, // will be atomicAdd'd + int32_t* __restrict__ src_dst, + int32_t* __restrict__ dst_src, + int num_elements +) { + int idx = blockIdx.x * blockDim.x + threadIdx.x; + if (idx < num_elements) { + int eid = expert_ids[idx]; + int pos = atomicAdd(&expert_offsets[eid], 1); + src_dst[idx] = pos; // where token idx goes in sorted order + dst_src[pos] = idx; // reverse mapping + } +} + + +// ============================================================================ +// Kernel 3: moe_expand_input +// Gather-based expand: output[i] = input[gather_index[i]] +// ============================================================================ + +template +__global__ void expand_input_kernel( + scalar_t* __restrict__ output, + const scalar_t* __restrict__ input, + const int32_t* __restrict__ dst_to_src, + int num_output_tokens, int hidden_size +) { + int token = blockIdx.x; + if (token >= num_output_tokens) return; + + int src_token = dst_to_src[token]; + const scalar_t* src = input + (int64_t)src_token * hidden_size; + scalar_t* dst = output + (int64_t)token * hidden_size; + + for (int h = threadIdx.x; h < hidden_size; h += blockDim.x) { + dst[h] = src[h]; + } +} + + +// ============================================================================ +// Kernel 4: moe_combine_result (weighted sum of expert outputs) +// output[t] = sum_k( weight[t][k] * gemm2_output[flat_index(t,k)] ) +// ============================================================================ + +template +__global__ void combine_result_kernel( + scalar_t* __restrict__ output, // [N, H] + const scalar_t* __restrict__ input, // [N*topk, H] + const float* __restrict__ weights, // [N, topk] + int num_tokens, int topk, int hidden_size +) { + int token = blockIdx.x; + if (token >= num_tokens) return; + + for (int h = threadIdx.x; h < hidden_size; h += blockDim.x) { + float acc = 0.0f; + for (int k = 0; k < topk; k++) { + int flat = token * topk + k; + float w = weights[token * topk + k]; + acc += w * __half2float(input[flat * hidden_size + h]); + } + output[token * hidden_size + h] = __float2half(acc); + } +} + +// Float specialization +template <> +__global__ void combine_result_kernel( + float* __restrict__ output, + const float* __restrict__ input, + const float* __restrict__ weights, + int num_tokens, int topk, int hidden_size +) { + int token = blockIdx.x; + if (token >= num_tokens) return; + + for (int h = threadIdx.x; h < hidden_size; h += blockDim.x) { + float acc = 0.0f; + for (int k = 0; k < topk; k++) { + int flat = token * topk + k; + float w = weights[token * topk + k]; + acc += w * input[flat * hidden_size + h]; + } + output[token * hidden_size + h] = acc; + } +} + + +// ============================================================================ +// C++ wrapper functions — ixformer::infer namespace +// These provide the MISSING symbols that ix_full_bridge_v2.cpp needs. +// ============================================================================ + +namespace ixformer { namespace infer { + +void topk_softmax( + torch::Tensor& topk_weights, + torch::Tensor& topk_indices, + torch::Tensor& token_expert_indices, + torch::Tensor& gating_output, + bool renormalize +) { + int num_tokens = gating_output.size(0); + int num_experts = gating_output.size(1); + int topk = topk_weights.size(1); + auto stream = c10::cuda::getCurrentCUDAStream(); + + auto input_f32 = gating_output.to(torch::kFloat32).contiguous(); + + // Block size must be >= num_experts, round up to next power of 2 + int block_size = 1; + while (block_size < num_experts) block_size <<= 1; + TORCH_CHECK(block_size <= 1024, "Too many experts for topk kernel: ", num_experts); + + size_t smem_bytes = block_size * (sizeof(float) + sizeof(int)); + topk_softmax_kernel<<>>( + input_f32.data_ptr(), + topk_weights.data_ptr(), + topk_indices.data_ptr(), + token_expert_indices.data_ptr(), + num_tokens, num_experts, topk, 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 std::optional& expert_mask, + const std::optional& expert_sizes_cpu, + const std::optional& expand_tokens_gpu, + int64_t start_expert_id, + int64_t end_expert_id, + int64_t num_experts +) { + auto stream = c10::cuda::getCurrentCUDAStream(); + int num_elements = topk_ids.numel(); + + // Zero expert_sizes + cudaMemsetAsync(expert_sizes_gpu.data_ptr(), 0, + num_experts * sizeof(int32_t), stream); + + // Phase 1: histogram + int blocks1 = (num_elements + 255) / 256; + histogram_kernel<<>>( + topk_ids.data_ptr(), + expert_sizes_gpu.data_ptr(), + num_elements, num_experts); + + // Phase 2: prefix sum for offsets (exclusive scan on GPU) + // Use a separate buffer for offsets, then reset for place_indices + auto expert_offsets = torch::zeros({num_experts}, topk_ids.options().dtype(torch::kInt32)); + // Copy sizes → do exclusive scan on CPU (small: 64 experts) + auto sizes_cpu = expert_sizes_gpu.to(torch::kCPU); + auto offsets_cpu = torch::zeros({num_experts}, torch::dtype(torch::kInt32)); + int32_t* s = sizes_cpu.data_ptr(); + int32_t* o = offsets_cpu.data_ptr(); + int32_t running = 0; + for (int i = 0; i < num_experts; i++) { + o[i] = running; + running += s[i]; + } + expert_offsets = offsets_cpu.to(topk_ids.device()); + + // Phase 3: place indices + int blocks3 = (num_elements + 255) / 256; + place_indices_kernel<<>>( + topk_ids.data_ptr(), + expert_offsets.data_ptr(), + src_dst.data_ptr(), + dst_src.data_ptr(), + num_elements); +} + +void moe_expand_input( + torch::Tensor outputs, + torch::Tensor inputs, + torch::Tensor dst_to_src, + const std::optional& src_to_dst, + int64_t dst_tokens, + int64_t expand_factor +) { + auto stream = c10::cuda::getCurrentCUDAStream(); + int hidden_size = inputs.size(1); + int block = std::min(hidden_size, 256); + + AT_DISPATCH_FLOATING_TYPES_AND_HALF(inputs.scalar_type(), "expand_input", [&] { + expand_input_kernel<<>>( + outputs.data_ptr(), + inputs.data_ptr(), + dst_to_src.data_ptr(), + dst_tokens, hidden_size); + }); +} + +void moe_w16a16_group_gemm( + torch::Tensor output, + torch::Tensor inputs, + torch::Tensor weights, + torch::Tensor tokens_per_experts, + const std::optional& dst_to_src, + const std::optional& bias, + std::string format, + int64_t persistent, + int64_t output_n +) { + // Implementation: loop over experts, call cuinferCustomGemm for each + // weights: [num_experts, N, K] with format "TN" means transB + // For each expert e with count tokens: + // A = inputs[offset:offset+count, :] (count × K, row-major) + // B = weights[e, :, :] (N × K, needs transB) + // C = output[offset:offset+count, :] (count × N, row-major) + // GEMM: C = A × B^T → (count, K) × (K, N) = (count, N) + + auto stream = c10::cuda::getCurrentCUDAStream(); + int num_experts = weights.size(0); + int N = weights.size(1); // output dim + int K = weights.size(2); // input dim + + // Get token counts on CPU + auto counts_cpu = tokens_per_experts.to(torch::kCPU).to(torch::kInt32); + int32_t* counts = counts_cpu.data_ptr(); + + // Create cuinfer handle + cuinferHandle_t handle; + cuinferCreate(&handle); + cuinferSetStream(handle, stream); + + float alpha = 1.0f, beta = 0.0f; + + int offset = 0; + for (int e = 0; e < num_experts; e++) { + int M = counts[e]; + if (M <= 0) continue; + + // A: inputs[offset : offset+M, :] → M × K + // B: weights[e, :, :] → N × K (transposed: compute A × B^T) + // C: output[offset : offset+M, :] → M × N + const void* A_ptr = (const char*)inputs.data_ptr() + + (int64_t)offset * K * inputs.element_size(); + const void* B_ptr = (const char*)weights.data_ptr() + + (int64_t)e * N * K * weights.element_size(); + void* C_ptr = (char*)output.data_ptr() + + (int64_t)offset * N * output.element_size(); + + cudaDataType_t dtype = (inputs.scalar_type() == torch::kFloat16) + ? CUDA_R_16F : CUDA_R_32F; + + // cuinferCustomGemm: row-major convention + // We want C = A × B^T + // In cuinfer (column-major internally): transa=N, transb=T + // M_gemm = M (rows of C), N_gemm = N (cols of C), K_gemm = K + cuinferCustomGemm( + handle, stream, + CUINFER_POINTER_MODE_HOST, + CUINFER_OP_TENSOR_OP_N, // transa = no transpose + CUINFER_OP_TENSOR_OP_T, // transb = transpose (TN format) + M, N, K, + &alpha, + A_ptr, dtype, K, 0, // lda=K for row-major A + B_ptr, dtype, K, 0, // ldb=K for row-major B (will be transposed) + &beta, + C_ptr, dtype, N, 0, // ldc=N for row-major C + 1, // batchCount=1 + CUDA_R_32F, // computeType + CUDA_R_32F, // scaleType + nullptr, nullptr, // custom pointers + CUINFER_GEMM_DEFAULT); + + offset += M; + } + + cuinferDestroy(handle); +} + +void moe_output_reduce_sum( + torch::Tensor outputs, + torch::Tensor inputs, + const std::optional& mul_weight, + const std::optional& mask, + const std::optional& extra_residual, + double scaling_factor +) { + // inputs: [N, topk, H] — expert outputs per token + // mul_weight: [N, topk] — router weights + // outputs: [N, H] — weighted sum + auto stream = c10::cuda::getCurrentCUDAStream(); + int num_tokens = inputs.size(0); + int topk = inputs.size(1); + int hidden_size = inputs.size(2); + int block = std::min(hidden_size, 256); + + // Reshape inputs to [N*topk, H] for the kernel + auto input_flat = inputs.reshape({num_tokens * topk, hidden_size}); + + if (inputs.scalar_type() == torch::kFloat16) { + combine_result_kernel<__half><<>>( + reinterpret_cast<__half*>(outputs.data_ptr()), + reinterpret_cast(input_flat.data_ptr()), + mul_weight.value().data_ptr(), + num_tokens, topk, hidden_size); + } else { + combine_result_kernel<<>>( + outputs.data_ptr(), + input_flat.data_ptr(), + mul_weight.value().data_ptr(), + num_tokens, topk, hidden_size); + } +} + +}} // namespace ixformer::infer diff --git a/ex_engine/python/moe_dispatch.py b/ex_engine/python/moe_dispatch.py new file mode 100644 index 00000000..411dcc6a --- /dev/null +++ b/ex_engine/python/moe_dispatch.py @@ -0,0 +1,172 @@ +"""moe_dispatch.py — Load ix_moe_bridge.so and dispatch MoE forward. + +3-level fallback: + Tier 0: ix_moe_bridge.fused_moe_forward (C++ fused 7-step pipeline) + Tier 1: ix_moe_bridge individual ops (topk + expand + gemm + silu + gemm + combine) + Tier 2: Pure PyTorch fallback (F.linear loop) + +Used by: patch_moe_hot_path.py → replaces Qwen3_5MoE.forward() + +Reference: ex_engine/python/corex_moe.py (237L) +""" +import os +import sys +import logging +import torch +import torch.nn.functional as F + +logger = logging.getLogger("moe_dispatch") + +# --- Load bridge .so --- +_bridge = None +_tier = 2 # default: PyTorch fallback + + +def _try_load_bridge(): + global _bridge, _tier + + # Try 1: prebuilt .so + search_paths = [ + os.path.join(os.path.dirname(__file__), "ix_moe_bridge.so"), + os.path.join(os.path.dirname(__file__), "..", "prebuilt", "ix_moe_bridge.so"), + os.path.join(os.path.dirname(__file__), "..", "ix_moe_bridge.so"), + ] + for p in search_paths: + if os.path.isfile(p): + try: + import importlib.util + spec = importlib.util.spec_from_file_location("ix_moe_bridge", p) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + _bridge = mod + logger.info(f"[moe_dispatch] ✓ Loaded bridge from {p}") + break + except Exception as e: + logger.warning(f"[moe_dispatch] Failed to load {p}: {e}") + + # Try 2: torch JIT compiled module + if _bridge is None: + try: + import ix_moe_bridge + _bridge = ix_moe_bridge + logger.info("[moe_dispatch] ✓ Loaded bridge via import") + except ImportError: + pass + + if _bridge is None: + logger.warning("[moe_dispatch] Bridge not available, using PyTorch fallback") + _tier = 2 + return + + # Check what functions are available + try: + if hasattr(_bridge, 'fused_moe_forward'): + _tier = 0 + logger.info("[moe_dispatch] Tier 0: fused pipeline available") + elif hasattr(_bridge, 'topk_softmax') and hasattr(_bridge, 'group_gemm'): + _tier = 1 + logger.info("[moe_dispatch] Tier 1: individual ops available") + else: + _tier = 2 + logger.warning("[moe_dispatch] Bridge loaded but missing functions") + except Exception as e: + logger.warning(f"[moe_dispatch] Function check failed: {e}") + _tier = 2 + + +_try_load_bridge() + + +# ============================================================================ +# Tier 2: Pure PyTorch fallback (identical to base vllm behavior) +# ============================================================================ + +def _pytorch_moe_forward(hidden_states, router_logits, w13, w2, + topk, num_experts, renormalize): + """Python fallback: softmax → topk → loop over experts with F.linear.""" + gating = torch.softmax(router_logits.float(), dim=-1) + topk_weights, topk_ids = torch.topk(gating, topk, dim=-1) + if renormalize: + topk_weights = topk_weights / (topk_weights.sum(dim=-1, keepdim=True) + 1e-8) + topk_weights = topk_weights.to(hidden_states.dtype) + + # Per-expert loop + final_output = torch.zeros_like(hidden_states) + for k in range(topk): + expert_ids = topk_ids[:, k] # [T] + weights_k = topk_weights[:, k].unsqueeze(-1) # [T, 1] + for e in range(num_experts): + mask = (expert_ids == e) + if not mask.any(): + continue + expert_input = hidden_states[mask] + # gate_up = expert_input @ w13[e].T → [n, 2*inter] + gate_up = F.linear(expert_input, w13[e]) + inter = gate_up.shape[-1] // 2 + gate = torch.sigmoid(gate_up[:, :inter]) + up = gate_up[:, inter:] + activated = gate * up # SiLU approximated as sigmoid * x (should be silu_and_mul) + # down = activated @ w2[e].T → [n, hidden] + down = F.linear(activated, w2[e]) + final_output[mask] += weights_k[mask] * down + + return final_output + + +# ============================================================================ +# Tier 1: Individual bridge ops +# ============================================================================ + +def _bridge_individual_moe_forward(hidden_states, router_logits, w13, w2, + topk, num_experts, renormalize): + """Use individual bridge ops: topk → gen_idx → expand → gemm → silu → gemm → combine.""" + topk_weights, topk_ids, _ = _bridge.topk_softmax(router_logits, topk, False) + if renormalize: + topk_weights = topk_weights / (topk_weights.sum(dim=-1, keepdim=True) + 1e-8) + + idx_results = _bridge.moe_gen_idx(topk_ids.view(-1).to(torch.int32), num_experts) + src_dst, dst_src, expert_sizes = idx_results[0], idx_results[1], idx_results[2] + + expanded = _bridge.moe_expand_input(hidden_states, src_dst, dst_src, topk) + + gate_up = _bridge.group_gemm(expanded, w13, expert_sizes, w13.size(1)) + activated = _bridge.silu_and_mul(gate_up) + down = _bridge.group_gemm(activated, w2, expert_sizes, w2.size(1)) + output = _bridge.moe_combine_result(down, topk_weights) + + return output + + +# ============================================================================ +# Public API +# ============================================================================ + +def moe_forward(hidden_states, router_logits, w13, w2, + topk, num_experts, renormalize=True): + """Dispatch MoE forward to best available implementation.""" + if _tier == 0: + try: + return _bridge.fused_moe_forward( + hidden_states, router_logits, w13, w2, + topk, num_experts, renormalize) + except Exception as e: + logger.warning(f"[moe_dispatch] Tier 0 failed: {e}, falling to Tier 1") + pass + + if _tier <= 1 and _bridge is not None: + try: + return _bridge_individual_moe_forward( + hidden_states, router_logits, w13, w2, + topk, num_experts, renormalize) + except Exception as e: + logger.warning(f"[moe_dispatch] Tier 1 failed: {e}, falling to Tier 2") + pass + + return _pytorch_moe_forward( + hidden_states, router_logits, w13, w2, + topk, num_experts, renormalize) + + +def get_tier(): + """Return current dispatch tier (0=fused, 1=individual, 2=pytorch).""" + return _tier \ No newline at end of file diff --git a/ex_engine/python/patch_moe_hot_path.py b/ex_engine/python/patch_moe_hot_path.py new file mode 100644 index 00000000..b5e18f7f --- /dev/null +++ b/ex_engine/python/patch_moe_hot_path.py @@ -0,0 +1,109 @@ +"""patch_moe_hot_path.py — Replace Qwen3_5MoE.forward() with bridge dispatch. + +This is the key performance patch: replaces the Python expert-loop MoE +with a single C++ call that does all 7 steps fused. + +Called by: patch_ops.sh during Docker build +Target: vllm.model_executor.models.qwen3_5.Qwen3_5MoE + +Reference: ex_engine/python/patch_vllm_hot_path.py (200L) +""" +import sys +import logging +import torch + +logger = logging.getLogger("patch_moe_hot_path") + + +def apply_moe_patch(): + """Monkey-patch Qwen3_5MoE.forward to use moe_dispatch.""" + try: + from ex_engine.python.moe_dispatch import moe_forward, get_tier + except ImportError: + try: + from moe_dispatch import moe_forward, get_tier + except ImportError: + logger.warning("[moe_patch] moe_dispatch not available, skipping patch") + return False + + tier = get_tier() + logger.info(f"[moe_patch] moe_dispatch tier={tier}") + + # Find the MoE class + moe_cls = None + try: + from vllm.model_executor.models.qwen3_5 import Qwen3_5MoE + moe_cls = Qwen3_5MoE + except ImportError: + pass + + if moe_cls is None: + # Try to find it in sys.modules (may be registered under different name) + for mod_name, mod in sys.modules.items(): + if hasattr(mod, 'Qwen3_5MoE'): + moe_cls = getattr(mod, 'Qwen3_5MoE') + break + + if moe_cls is None: + logger.warning("[moe_patch] Qwen3_5MoE class not found") + return False + + # Save original forward + _original_forward = moe_cls.forward + + def patched_forward(self, hidden_states, *args, **kwargs): + """Patched MoE forward using bridge dispatch.""" + # Get router logits + # In Qwen3_5, the gate + shared_expert_gate are concatenated: + # router_and_shared_gate = self.gate(hidden_states) + # router_logits = router_and_shared_gate[..., :self.num_experts] + # shared_gate = router_and_shared_gate[..., -1] + router_and_shared_gate = self.gate(hidden_states) + router_logits = router_and_shared_gate[..., :self.num_experts] + + # Shared expert (if any) — run in parallel + shared_output = None + if hasattr(self, 'shared_expert') and self.shared_expert is not None: + if hasattr(self, 'shared_expert_gate'): + shared_gate = torch.sigmoid( + router_and_shared_gate[..., -1].unsqueeze(-1)) + else: + shared_gate = None + + # Routed experts via bridge + try: + routed_output = moe_forward( + hidden_states.view(-1, hidden_states.shape[-1]), + router_logits.view(-1, router_logits.shape[-1]), + self.w13_weight if hasattr(self, 'w13_weight') else self.experts.w13_weight, + self.w2_weight if hasattr(self, 'w2_weight') else self.experts.w2_weight, + topk=self.top_k, + num_experts=self.num_experts, + renormalize=True, + ) + routed_output = routed_output.view_as(hidden_states) + except Exception as e: + logger.warning(f"[moe_patch] Bridge failed ({e}), using original forward") + return _original_forward(self, hidden_states, *args, **kwargs) + + # Add shared expert output + if hasattr(self, 'shared_expert') and self.shared_expert is not None: + shared_out = self.shared_expert(hidden_states) + if shared_gate is not None: + shared_out = shared_out * shared_gate + routed_output = routed_output + shared_out + + return routed_output + + # Only patch if we have a real bridge (not pure Python fallback) + if tier < 2: + moe_cls.forward = patched_forward + logger.info(f"[moe_patch] ✓ Patched Qwen3_5MoE.forward (tier={tier})") + return True + else: + logger.info("[moe_patch] Tier 2 (Python only), not patching") + return False + + +if __name__ == "__main__": + apply_moe_patch() \ No newline at end of file diff --git a/qwen3_6_scripts/patch_ops.sh b/qwen3_6_scripts/patch_ops.sh index abac4a57..ac007739 100755 --- a/qwen3_6_scripts/patch_ops.sh +++ b/qwen3_6_scripts/patch_ops.sh @@ -332,6 +332,23 @@ if b"max_completion_tokens" not in installed: raise SystemExit("protocol.py missing max_completion_tokens field") PY +build_stage "building MoE bridge (ix_moe_bridge.so)" +if [[ -f "./ex_engine_src/build_moe_bridge.sh" ]]; then + bash ./ex_engine_src/build_moe_bridge.sh "${VLLM_ROOT}" 2>&1 || { + echo "[WARN] MoE bridge build failed — will use Python fallback" + } +fi + +build_stage "deploying MoE dispatch modules" +EX_DIR="${VLLM_ROOT}/ex_engine/python" +mkdir -p "${EX_DIR}" +for pyfile in moe_dispatch.py patch_moe_hot_path.py; do + if [[ -f "./ex_engine_src/python/${pyfile}" ]]; then + cp "./ex_engine_src/python/${pyfile}" "${EX_DIR}/${pyfile}" + echo " ✓ ${pyfile}" + fi +done + build_stage "compiling submission Python sources" find . -path './wheels' -prune -o -name '*.py' -print0 | xargs -0 python3 -m py_compile @@ -340,4 +357,4 @@ python3 ./verify_dlopen_chain.py --vllm-root "${VLLM_ROOT}" || { echo "[WARN] dlopen chain verification found issues (non-fatal)" } -build_stage "patch script completed" +build_stage "patch script completed" \ No newline at end of file diff --git a/test_moe_bridge.py b/test_moe_bridge.py new file mode 100644 index 00000000..63b2005f --- /dev/null +++ b/test_moe_bridge.py @@ -0,0 +1,186 @@ +"""test_moe_bridge.py — Integration test for ix_moe_bridge on real device. + +Run after build_moe_bridge.sh. No model weights needed — uses random tensors. +Tests each of the 5 MoE functions + the fused pipeline. + +Usage: + python3 test_moe_bridge.py +""" +import sys +import os +import torch +import time + +# Qwen3.5-27B MoE params +NUM_EXPERTS = 128 +TOPK = 8 +HIDDEN_SIZE = 3584 +INTERMEDIATE_SIZE = 18944 # per-partition (full=18944*2 for gate+up, /TP if sharded) +NUM_TOKENS = 4 + +def load_bridge(): + """Try to load ix_moe_bridge.""" + # Try prebuilt + script_dir = os.path.dirname(os.path.abspath(__file__)) + for p in [ + os.path.join(script_dir, "prebuilt", "ix_moe_bridge.so"), + os.path.join(script_dir, "ix_moe_bridge.so"), + ]: + if os.path.isfile(p): + import importlib.util + spec = importlib.util.spec_from_file_location("ix_moe_bridge", p) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + # Try import + import ix_moe_bridge + return ix_moe_bridge + + +def test_topk_softmax(bridge, device): + print("\n--- topk_softmax ---") + gating = torch.randn(NUM_TOKENS, NUM_EXPERTS, device=device, dtype=torch.float32) + topk_w, topk_ids, token_expert_ids = bridge.topk_softmax(gating, TOPK, True) + + assert topk_w.shape == (NUM_TOKENS, TOPK), f"weights shape: {topk_w.shape}" + assert topk_ids.shape == (NUM_TOKENS, TOPK), f"ids shape: {topk_ids.shape}" + assert topk_w.dtype == torch.float32 + assert topk_ids.dtype == torch.int32 + assert (topk_ids >= 0).all() and (topk_ids < NUM_EXPERTS).all(), "ids out of range" + assert torch.allclose(topk_w.sum(-1), torch.ones(NUM_TOKENS, device=device), atol=1e-5), \ + f"weights don't sum to 1: {topk_w.sum(-1)}" + print(f" ✓ shape={topk_w.shape}, sum={topk_w.sum(-1).tolist()}") + print(f" ✓ top expert ids (row 0): {topk_ids[0].tolist()}") + + +def test_moe_gen_idx(bridge, device): + print("\n--- moe_gen_idx ---") + expert_ids = torch.randint(0, NUM_EXPERTS, (NUM_TOKENS * TOPK,), + device=device, dtype=torch.int32) + results = bridge.moe_gen_idx(expert_ids, NUM_EXPERTS) + src_dst, dst_src, expert_sizes, expert_cumsum = results + + assert src_dst.shape == (NUM_TOKENS * TOPK,), f"src_dst shape: {src_dst.shape}" + assert dst_src.shape == (NUM_TOKENS * TOPK,), f"dst_src shape: {dst_src.shape}" + assert expert_sizes.shape[0] == NUM_EXPERTS, f"expert_sizes shape: {expert_sizes.shape}" + assert expert_sizes.sum().item() == NUM_TOKENS * TOPK, \ + f"expert_sizes sum: {expert_sizes.sum().item()} != {NUM_TOKENS * TOPK}" + print(f" ✓ src_dst={src_dst.shape}, expert_sizes sum={expert_sizes.sum().item()}") + + +def test_moe_expand_input(bridge, device): + print("\n--- moe_expand_input ---") + hidden = torch.randn(NUM_TOKENS, HIDDEN_SIZE, device=device, dtype=torch.float16) + # Create simple gather index: [0,1,2,...,NUM_TOKENS*TOPK-1] mod NUM_TOKENS + gather_idx = torch.arange(NUM_TOKENS * TOPK, device=device, dtype=torch.int32) % NUM_TOKENS + combine_idx = torch.arange(NUM_TOKENS * TOPK, device=device, dtype=torch.int32) + + expanded = bridge.moe_expand_input(hidden, gather_idx, combine_idx, TOPK) + assert expanded.shape == (NUM_TOKENS * TOPK, HIDDEN_SIZE), f"shape: {expanded.shape}" + print(f" ✓ shape={expanded.shape}, dtype={expanded.dtype}") + + +def test_group_gemm(bridge, device): + print("\n--- group_gemm ---") + # Simulate: expanded tokens × expert weights + total_tokens = NUM_TOKENS * TOPK # 32 + inputs = torch.randn(total_tokens, HIDDEN_SIZE, device=device, dtype=torch.float16) + # weights: [NUM_EXPERTS, 2*INTERMEDIATE, HIDDEN] — 3D + weights = torch.randn(NUM_EXPERTS, INTERMEDIATE_SIZE * 2, HIDDEN_SIZE, + device=device, dtype=torch.float16) * 0.01 + # tokens_per_expert: distribute evenly + tpe = torch.zeros(NUM_EXPERTS, device=device, dtype=torch.int32) + for i in range(total_tokens): + tpe[i % NUM_EXPERTS] += 1 + + output_n = INTERMEDIATE_SIZE * 2 + result = bridge.group_gemm(inputs, weights, tpe, output_n) + assert result.shape == (total_tokens, output_n), f"shape: {result.shape}" + assert not torch.isnan(result).any(), "NaN in group_gemm output" + print(f" ✓ shape={result.shape}, max={result.abs().max().item():.4f}") + + +def test_silu_and_mul(bridge, device): + print("\n--- silu_and_mul ---") + gate_up = torch.randn(NUM_TOKENS, INTERMEDIATE_SIZE * 2, + device=device, dtype=torch.float16) + activated = bridge.silu_and_mul(gate_up) + assert activated.shape == (NUM_TOKENS, INTERMEDIATE_SIZE), f"shape: {activated.shape}" + print(f" ✓ shape={activated.shape}") + + +def test_moe_combine_result(bridge, device): + print("\n--- moe_combine_result ---") + expert_out = torch.randn(NUM_TOKENS * TOPK, HIDDEN_SIZE, + device=device, dtype=torch.float16) + weights = torch.randn(NUM_TOKENS, TOPK, device=device, dtype=torch.float32) + weights = torch.softmax(weights, dim=-1) + + combined = bridge.moe_combine_result(expert_out, weights) + assert combined.shape == (NUM_TOKENS, HIDDEN_SIZE), f"shape: {combined.shape}" + assert not torch.isnan(combined).any(), "NaN in combine output" + print(f" ✓ shape={combined.shape}") + + +def test_fused_pipeline(bridge, device): + print("\n--- fused_moe_forward (7-step pipeline) ---") + hidden = torch.randn(NUM_TOKENS, HIDDEN_SIZE, device=device, dtype=torch.float16) + router = torch.randn(NUM_TOKENS, NUM_EXPERTS, device=device, dtype=torch.float16) + w13 = torch.randn(NUM_EXPERTS, INTERMEDIATE_SIZE * 2, HIDDEN_SIZE, + device=device, dtype=torch.float16) * 0.01 + w2 = torch.randn(NUM_EXPERTS, HIDDEN_SIZE, INTERMEDIATE_SIZE, + device=device, dtype=torch.float16) * 0.01 + + t0 = time.time() + output = bridge.fused_moe_forward(hidden, router, w13, w2, TOPK, NUM_EXPERTS, True) + torch.cuda.synchronize() + elapsed = time.time() - t0 + + assert output.shape == (NUM_TOKENS, HIDDEN_SIZE), f"shape: {output.shape}" + assert not torch.isnan(output).any(), "NaN in fused output" + print(f" ✓ shape={output.shape}, time={elapsed*1000:.1f}ms") + + +def main(): + if not torch.cuda.is_available(): + print("CUDA not available, skipping GPU tests") + sys.exit(0) + + device = torch.device("cuda:0") + print(f"Device: {torch.cuda.get_device_name(0)}") + print(f"Params: {NUM_EXPERTS} experts, topk={TOPK}, hidden={HIDDEN_SIZE}, " + f"inter={INTERMEDIATE_SIZE}, tokens={NUM_TOKENS}") + + bridge = load_bridge() + funcs = [f for f in dir(bridge) if not f.startswith('_')] + print(f"Bridge loaded: {len(funcs)} functions: {funcs}") + + passed = 0 + failed = 0 + + for test_fn in [ + test_topk_softmax, + test_moe_gen_idx, + test_moe_expand_input, + test_group_gemm, + test_silu_and_mul, + test_moe_combine_result, + test_fused_pipeline, + ]: + try: + test_fn(bridge, device) + passed += 1 + except Exception as e: + print(f" ✗ FAILED: {e}") + import traceback; traceback.print_exc() + failed += 1 + + print(f"\n{'='*40}") + print(f"Results: {passed} passed, {failed} failed") + if failed > 0: + sys.exit(1) + + +if __name__ == "__main__": + main() \ No newline at end of file