revert: undo 2 premature pushes (c54923a1, 49034d1d) — code needs review first

This commit is contained in:
project_6
2026-08-16 17:48:17 +00:00
parent 49034d1d09
commit 34a8fbf27e
9 changed files with 1 additions and 1251 deletions

View File

@@ -4,12 +4,6 @@ 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 ; \

View File

@@ -15,7 +15,7 @@ command:
- -tp
- '4'
- --max-num-seqs
- '2'
- '1'
- --disable-log-requests
- --disable-frontend-multiprocessing
- --max-num-batched-tokens

View File

@@ -1,156 +0,0 @@
#!/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}/csrc/moe_ops_impl.cu"
BRIDGE_CPP="${SCRIPT_DIR}/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, "csrc", "moe_ops_impl.cu")
bridge_cpp = os.path.join(script_dir, "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", "--extended-lambda"],
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"

View File

@@ -1,502 +0,0 @@
// 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 <torch/extension.h>
#include <c10/cuda/CUDAStream.h>
#include <c10/cuda/CUDAGuard.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <optional>
#include <vector>
#include <numeric>
// ============================================================================
// 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 <typename scalar_t>
__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 <typename scalar_t>
__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>(
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<<<num_tokens, block_size, smem_bytes, stream>>>(
input_f32.data_ptr<float>(),
topk_weights.data_ptr<float>(),
topk_indices.data_ptr<int32_t>(),
token_expert_indices.data_ptr<int32_t>(),
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 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
) {
auto stream = c10::cuda::getCurrentCUDAStream();
int num_elements = topk_ids.numel();
// Zero expert_sizes
cudaMemsetAsync(expert_sizes_gpu.data_ptr<int32_t>(), 0,
num_experts * sizeof(int32_t), stream);
// Phase 1: histogram
int blocks1 = (num_elements + 255) / 256;
histogram_kernel<<<blocks1, 256, 0, stream>>>(
topk_ids.data_ptr<int32_t>(),
expert_sizes_gpu.data_ptr<int32_t>(),
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>();
int32_t* o = offsets_cpu.data_ptr<int32_t>();
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<<<blocks3, 256, 0, stream>>>(
topk_ids.data_ptr<int32_t>(),
expert_offsets.data_ptr<int32_t>(),
src_dst.data_ptr<int32_t>(),
dst_src.data_ptr<int32_t>(),
num_elements);
}
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
) {
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<scalar_t><<<dst_tokens, block, 0, stream>>>(
outputs.data_ptr<scalar_t>(),
inputs.data_ptr<scalar_t>(),
dst_to_src.data_ptr<int32_t>(),
dst_tokens, hidden_size);
});
}
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
) {
// 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<int32_t>();
// 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 c10::optional<torch::Tensor>& mul_weight,
const c10::optional<torch::Tensor>& mask,
const c10::optional<torch::Tensor>& 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><<<num_tokens, block, 0, stream>>>(
reinterpret_cast<__half*>(outputs.data_ptr()),
reinterpret_cast<const __half*>(input_flat.data_ptr()),
mul_weight.value().data_ptr<float>(),
num_tokens, topk, hidden_size);
} else {
combine_result_kernel<float><<<num_tokens, block, 0, stream>>>(
outputs.data_ptr<float>(),
input_flat.data_ptr<float>(),
mul_weight.value().data_ptr<float>(),
num_tokens, topk, hidden_size);
}
}
}} // namespace ixformer::infer

View File

@@ -1,102 +0,0 @@
#!/usr/bin/env bash
# probe_moe_symbols.sh — Verify ix_moe_bridge.so has all 5 MoE symbols
#
# Run on real device after build_moe_bridge.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Find the .so
SO_FILE=""
for p in \
"${SCRIPT_DIR}/prebuilt/ix_moe_bridge.so" \
"${SCRIPT_DIR}/ix_moe_bridge.so" \
"$(python3 -c 'import ix_moe_bridge; print(ix_moe_bridge.__file__)' 2>/dev/null || echo '')"; do
if [[ -f "$p" ]]; then
SO_FILE="$p"
break
fi
done
if [[ -z "$SO_FILE" ]]; then
echo "[probe] ERROR: ix_moe_bridge.so not found"
exit 1
fi
echo "[probe] Checking: $SO_FILE"
echo "[probe] Size: $(du -h "$SO_FILE" | cut -f1)"
echo ""
# Required MoE symbols (must be in ixformer::infer namespace)
REQUIRED=(
"topk_softmax"
"moe_compute_token_index_api"
"moe_expand_input"
"moe_w16a16_group_gemm"
"moe_output_reduce_sum"
)
# Required bridge symbols (pybind11 Python bindings)
BRIDGE_REQUIRED=(
"topk_softmax"
"moe_gen_idx"
"moe_expand_input"
"group_gemm"
"moe_combine_result"
"fused_moe_forward"
"silu_and_mul"
"rms_norm"
"linear"
"paged_attention"
"flash_attn_prefill"
)
echo "=== MoE implementation symbols (ixformer::infer) ==="
PASS=0
FAIL=0
ALL_SYMS=$(nm -D "$SO_FILE" 2>/dev/null || nm "$SO_FILE" 2>/dev/null || echo "")
for sym in "${REQUIRED[@]}"; do
count=$(echo "$ALL_SYMS" | grep -c "$sym" || true)
if [[ $count -gt 0 ]]; then
echo "$sym ($count matches)"
PASS=$((PASS + 1))
else
echo "$sym — MISSING"
FAIL=$((FAIL + 1))
fi
done
echo ""
echo "=== pybind11 bridge symbols ==="
for sym in "${BRIDGE_REQUIRED[@]}"; do
count=$(echo "$ALL_SYMS" | grep -c "$sym" || true)
if [[ $count -gt 0 ]]; then
echo "$sym"
else
echo "$sym — MISSING"
FAIL=$((FAIL + 1))
fi
done
echo ""
echo "=== Python import test ==="
python3 -c "
import sys
sys.path.insert(0, '$(dirname "$SO_FILE")')
try:
import ix_moe_bridge as m
funcs = [f for f in dir(m) if not f.startswith('_')]
print(f' ✓ Import OK, {len(funcs)} functions: {funcs}')
except Exception as e:
print(f' ✗ Import failed: {e}')
" 2>&1
echo ""
if [[ $FAIL -eq 0 ]]; then
echo "[probe] ✓ ALL SYMBOLS PRESENT ($PASS MoE + bridge OK)"
else
echo "[probe] ✗ $FAIL SYMBOLS MISSING"
exit 1
fi

View File

@@ -1,172 +0,0 @@
"""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

View File

@@ -1,109 +0,0 @@
"""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()

View File

@@ -1,186 +0,0 @@
"""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()

View File

@@ -332,23 +332,6 @@ 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