feat: CUTLASS Cu10 grouped GEMM — real device verified
BI-V100 real device results: moe_group_gemm: err=0.000015 PASS moe_decode_cutlass: NaN=False PASS cutlass grouped: 4.77ms vs torch.mm loop: 9.38ms → 1.97x speedup Fix: gemm_grouped.cu ldb=K (not N) for ColumnMajor B view Link: -lcuinfer from /usr/local/corex-3.2.3/lib64/libcuinfer.so.7
This commit is contained in:
161
ex_engine/csrc/cuinfer_gemm_wrapper.cu
Normal file
161
ex_engine/csrc/cuinfer_gemm_wrapper.cu
Normal file
@@ -0,0 +1,161 @@
|
||||
// cuinfer_gemm_wrapper.cu — Wrapper around cuinferCustomGemm
|
||||
//
|
||||
// ixformer::functions::cuinfer_gemm exists in libixformer.so but
|
||||
// takes ixformer::Tensor (not torch::Tensor). We need a torch-compatible
|
||||
// wrapper that calls the C API directly.
|
||||
//
|
||||
// Symbol dump shows cuinferCustomGemm in libcuinfer.so with signature:
|
||||
// cuinferCustomGemm(handle, stream, ptrMode, transa, transb,
|
||||
// m, n, k, alpha, A, Atype, lda, strideA,
|
||||
// B, Btype, ldb, strideB, beta,
|
||||
// C, Ctype, ldc, strideC, batchCount,
|
||||
// computeType, scaleType, customHostPtr, customDevicePtr, customOption)
|
||||
//
|
||||
// Reference:
|
||||
// cat_files/ixinfer.h — cuinferCustomGemm signature
|
||||
// libixformer.so — ixformer::functions::cuinfer_gemm (confirmed in symbol dump)
|
||||
|
||||
#include <torch/extension.h>
|
||||
#include <c10/cuda/CUDAStream.h>
|
||||
#include <cuda_fp16.h>
|
||||
#include "cuinfer_handle.h"
|
||||
|
||||
// cuinferCustomGemm is already declared in cuinfer_handle.h extern "C" block
|
||||
// We add the full signature here
|
||||
extern "C" {
|
||||
int cuinferCustomGemm(
|
||||
cuinferHandle_t handle, cudaStream_t stream,
|
||||
int ptrMode, int transa, int transb,
|
||||
int m, int n, int k,
|
||||
const void* alpha,
|
||||
const void* A, int Atype, int lda, long long int strideA,
|
||||
const void* B, int Btype, int ldb, long long int strideB,
|
||||
const void* beta,
|
||||
void* C, int Ctype, int ldc, long long int strideC,
|
||||
int batchCount, int computeType, int scaleType,
|
||||
const void* customHostPtr, const void* customDevicePtr, int customOption);
|
||||
}
|
||||
|
||||
// CUDA_R_16F = 2, CUDA_R_32F = 0 (from cudaDataType_t)
|
||||
static constexpr int kFP16 = 2;
|
||||
static constexpr int kFP32 = 0;
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// cuinfer_gemm: C = alpha * A @ B + beta * C
|
||||
//
|
||||
// A: (M, K) row-major fp16
|
||||
// B: (K, N) row-major fp16 (or (N, K) if transb)
|
||||
// C: (M, N) row-major fp16
|
||||
// ============================================================================
|
||||
torch::Tensor cuinfer_gemm(
|
||||
torch::Tensor A, // (M, K)
|
||||
torch::Tensor B, // (K, N) or (N, K) if trans_b
|
||||
bool trans_b)
|
||||
{
|
||||
TORCH_CHECK(A.is_cuda() && B.is_cuda(), "inputs must be CUDA");
|
||||
TORCH_CHECK(A.scalar_type() == torch::kHalf, "A must be fp16");
|
||||
TORCH_CHECK(B.scalar_type() == torch::kHalf, "B must be fp16");
|
||||
|
||||
int M = A.size(0);
|
||||
int K = A.size(1);
|
||||
int N = trans_b ? B.size(0) : B.size(1);
|
||||
|
||||
if (!trans_b) {
|
||||
TORCH_CHECK(B.size(0) == K, "B rows must equal K");
|
||||
} else {
|
||||
TORCH_CHECK(B.size(1) == K, "B cols must equal K when transposed");
|
||||
}
|
||||
|
||||
auto C = torch::zeros({M, N}, A.options());
|
||||
auto stream = c10::cuda::getCurrentCUDAStream().stream();
|
||||
auto handle = CuinferHandle::get(stream);
|
||||
|
||||
if (!handle) {
|
||||
// Fallback to torch::mm
|
||||
if (trans_b) {
|
||||
return torch::mm(A.to(torch::kFloat32), B.t().to(torch::kFloat32)).to(torch::kHalf);
|
||||
}
|
||||
return torch::mm(A.to(torch::kFloat32), B.to(torch::kFloat32)).to(torch::kHalf);
|
||||
}
|
||||
|
||||
float alpha = 1.0f, beta = 0.0f;
|
||||
int transa = 0; // N = no transpose
|
||||
int transb_flag = trans_b ? 1 : 0;
|
||||
|
||||
int lda = K;
|
||||
int ldb = trans_b ? K : N;
|
||||
int ldc = N;
|
||||
|
||||
int status = cuinferCustomGemm(
|
||||
handle, stream,
|
||||
0, // CUINFER_POINTER_MODE_HOST
|
||||
transa, transb_flag,
|
||||
M, N, K,
|
||||
&alpha,
|
||||
A.data_ptr(), kFP16, lda, 0,
|
||||
B.data_ptr(), kFP16, ldb, 0,
|
||||
&beta,
|
||||
C.data_ptr(), kFP16, ldc, 0,
|
||||
1, // batchCount
|
||||
kFP32, kFP32, // computeType, scaleType
|
||||
nullptr, nullptr, 0);
|
||||
|
||||
TORCH_CHECK(status == 0, "cuinferCustomGemm failed with status ", status);
|
||||
return C;
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// cuinfer_gemm_batched: batched version
|
||||
// A: (batch, M, K), B: (batch, K, N) or (batch, N, K)
|
||||
// ============================================================================
|
||||
torch::Tensor cuinfer_gemm_batched(
|
||||
torch::Tensor A,
|
||||
torch::Tensor B,
|
||||
bool trans_b)
|
||||
{
|
||||
TORCH_CHECK(A.dim() == 3 && B.dim() == 3, "inputs must be 3D");
|
||||
|
||||
int batch = A.size(0);
|
||||
int M = A.size(1);
|
||||
int K = A.size(2);
|
||||
int N = trans_b ? B.size(1) : B.size(2);
|
||||
|
||||
auto C = torch::zeros({batch, M, N}, A.options());
|
||||
auto stream = c10::cuda::getCurrentCUDAStream().stream();
|
||||
auto handle = CuinferHandle::get(stream);
|
||||
|
||||
float alpha = 1.0f, beta = 0.0f;
|
||||
int lda = K, ldb = trans_b ? K : N, ldc = N;
|
||||
long long strideA = (long long)M * K;
|
||||
long long strideB = trans_b ? (long long)N * K : (long long)K * N;
|
||||
long long strideC = (long long)M * N;
|
||||
|
||||
int status = cuinferCustomGemm(
|
||||
handle, stream,
|
||||
0,
|
||||
0, trans_b ? 1 : 0,
|
||||
M, N, K,
|
||||
&alpha,
|
||||
A.data_ptr(), kFP16, lda, strideA,
|
||||
B.data_ptr(), kFP16, ldb, strideB,
|
||||
&beta,
|
||||
C.data_ptr(), kFP16, ldc, strideC,
|
||||
batch,
|
||||
kFP32, kFP32,
|
||||
nullptr, nullptr, 0);
|
||||
|
||||
TORCH_CHECK(status == 0, "cuinferCustomGemm batched failed: ", status);
|
||||
return C;
|
||||
}
|
||||
|
||||
|
||||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
||||
m.def("cuinfer_gemm", &cuinfer_gemm,
|
||||
"GEMM via cuinferCustomGemm (fp16, Cu10)",
|
||||
py::arg("A"), py::arg("B"), py::arg("trans_b") = false);
|
||||
m.def("cuinfer_gemm_batched", &cuinfer_gemm_batched,
|
||||
"Batched GEMM via cuinferCustomGemm",
|
||||
py::arg("A"), py::arg("B"), py::arg("trans_b") = false);
|
||||
}
|
||||
65
ex_engine/csrc/cuinfer_handle.h
Normal file
65
ex_engine/csrc/cuinfer_handle.h
Normal file
@@ -0,0 +1,65 @@
|
||||
// cuinfer_handle.h — Singleton handle manager for libcuinfer.so
|
||||
//
|
||||
// cuinferCreate/Destroy is expensive. This provides a thread-safe
|
||||
// singleton that creates once and reuses.
|
||||
//
|
||||
// Usage:
|
||||
// #include "cuinfer_handle.h"
|
||||
// cuinferHandle_t h = CuinferHandle::get(stream);
|
||||
//
|
||||
// Reference: ixformer::Context::default_cuinfer_handle (in libixformer.so)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <mutex>
|
||||
#include <cstdio>
|
||||
|
||||
// Forward-declare cuinfer C API
|
||||
extern "C" {
|
||||
|
||||
typedef struct cuinferContext* cuinferHandle_t;
|
||||
|
||||
typedef enum {
|
||||
CUINFER_STATUS_SUCCESS_H = 0,
|
||||
} cuinferStatus_h_t;
|
||||
|
||||
int cuinferCreate(cuinferHandle_t* handle);
|
||||
int cuinferDestroy(cuinferHandle_t handle);
|
||||
int cuinferSetStream(cuinferHandle_t handle, cudaStream_t stream);
|
||||
|
||||
} // extern "C"
|
||||
|
||||
|
||||
class CuinferHandle {
|
||||
public:
|
||||
static cuinferHandle_t get(cudaStream_t stream = nullptr) {
|
||||
static CuinferHandle instance;
|
||||
if (stream && stream != instance.last_stream_) {
|
||||
cuinferSetStream(instance.handle_, stream);
|
||||
instance.last_stream_ = stream;
|
||||
}
|
||||
return instance.handle_;
|
||||
}
|
||||
|
||||
private:
|
||||
cuinferHandle_t handle_ = nullptr;
|
||||
cudaStream_t last_stream_ = nullptr;
|
||||
|
||||
CuinferHandle() {
|
||||
int status = cuinferCreate(&handle_);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, "[cuinfer_handle] WARNING: cuinferCreate failed (%d)\n", status);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
~CuinferHandle() {
|
||||
if (handle_) {
|
||||
cuinferDestroy(handle_);
|
||||
}
|
||||
}
|
||||
|
||||
CuinferHandle(const CuinferHandle&) = delete;
|
||||
CuinferHandle& operator=(const CuinferHandle&) = delete;
|
||||
};
|
||||
175
ex_engine/csrc/cuinfer_types.h
Normal file
175
ex_engine/csrc/cuinfer_types.h
Normal file
@@ -0,0 +1,175 @@
|
||||
// cuinfer_types.h — C API types from libcuinfer.so
|
||||
//
|
||||
// Extracted from: cat_files/ixinfer.h (165952 bytes, from real device)
|
||||
// Only the types/enums needed by our GEMM and MoE code.
|
||||
//
|
||||
// This header replaces the scattered extern "C" blocks across
|
||||
// moe_ops_impl.cu, cuinfer_gemm_wrapper.cu, gemm_grouped.cu.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// --- Handle ---
|
||||
struct cuinferContext;
|
||||
typedef struct cuinferContext* cuinferHandle_t;
|
||||
|
||||
// --- Status ---
|
||||
typedef enum {
|
||||
CUINFER_STATUS_SUCCESS = 0,
|
||||
CUINFER_STATUS_NOT_INITIALIZED = 1,
|
||||
CUINFER_STATUS_ALLOC_FAILED = 2,
|
||||
CUINFER_STATUS_BAD_PARAM = 3,
|
||||
CUINFER_STATUS_INTERNAL_ERROR = 4,
|
||||
CUINFER_STATUS_INVALID_VALUE = 5,
|
||||
CUINFER_STATUS_ARCH_MISMATCH = 6,
|
||||
CUINFER_STATUS_EXECUTION_FAILED = 8,
|
||||
CUINFER_STATUS_NOT_SUPPORTED = 9,
|
||||
} cuinferStatus_t;
|
||||
|
||||
// --- Data types ---
|
||||
typedef enum {
|
||||
CUINFER_DATA_FLOAT = 0,
|
||||
CUINFER_DATA_DOUBLE = 1,
|
||||
CUINFER_DATA_HALF = 2,
|
||||
CUINFER_DATA_INT8 = 3,
|
||||
CUINFER_DATA_INT32 = 4,
|
||||
CUINFER_DATA_INT8x4 = 5,
|
||||
CUINFER_DATA_UINT8 = 6,
|
||||
CUINFER_DATA_UINT8x4 = 7,
|
||||
CUINFER_DATA_INT16 = 8,
|
||||
CUINFER_DATA_BFLOAT16 = 9,
|
||||
} cuinferDataType_t;
|
||||
|
||||
// --- Operations ---
|
||||
typedef enum {
|
||||
CUINFER_OP_N = 0, // no transpose
|
||||
CUINFER_OP_T = 1, // transpose
|
||||
CUINFER_OP_C = 2, // conjugate transpose
|
||||
} cuinferOperation_t;
|
||||
|
||||
// --- Pointer mode ---
|
||||
typedef enum {
|
||||
CUINFER_POINTER_MODE_HOST = 0,
|
||||
CUINFER_POINTER_MODE_DEVICE = 1,
|
||||
} cuinferPointerMode_t;
|
||||
|
||||
// --- GEMM custom option ---
|
||||
typedef enum {
|
||||
CUINFER_GEMM_DEFAULT = 0,
|
||||
} cuinferGEMMCustomOption_t;
|
||||
|
||||
// --- Reduce ops ---
|
||||
typedef enum {
|
||||
CUINFER_REDUCE_TENSOR_ADD = 0,
|
||||
CUINFER_REDUCE_TENSOR_MUL = 1,
|
||||
CUINFER_REDUCE_TENSOR_MIN = 2,
|
||||
CUINFER_REDUCE_TENSOR_MAX = 3,
|
||||
} cuinferReduceTensorOp_t;
|
||||
|
||||
// --- Softmax ---
|
||||
typedef enum {
|
||||
CUINFER_SOFTMAX_FAST = 0,
|
||||
CUINFER_SOFTMAX_ACCURATE = 1,
|
||||
CUINFER_SOFTMAX_LOG = 2,
|
||||
} cuinferSoftmaxAlgorithm_t;
|
||||
|
||||
typedef enum {
|
||||
CUINFER_SOFTMAX_MODE_INSTANCE = 0,
|
||||
CUINFER_SOFTMAX_MODE_CHANNEL = 1,
|
||||
} cuinferSoftmaxMode_t;
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Function declarations (confirmed in libcuinfer.so symbol dump)
|
||||
// ============================================================================
|
||||
|
||||
cuinferStatus_t cuinferCreate(cuinferHandle_t* handle);
|
||||
cuinferStatus_t cuinferDestroy(cuinferHandle_t handle);
|
||||
cuinferStatus_t cuinferSetStream(cuinferHandle_t handle, cudaStream_t stream);
|
||||
cuinferStatus_t cuinferGetStream(cuinferHandle_t handle, cudaStream_t* stream);
|
||||
size_t cuinferGetVersion(void);
|
||||
const char* cuinferGetErrorString(cuinferStatus_t status);
|
||||
|
||||
// GEMM
|
||||
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);
|
||||
|
||||
cuinferStatus_t cuinferCustomGemmEx(
|
||||
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,
|
||||
const void* workspace);
|
||||
|
||||
// TopK
|
||||
cuinferStatus_t cuinferTopK(
|
||||
cuinferHandle_t handle,
|
||||
const void* input, int n, int m, int top_k,
|
||||
int sort_dim, bool largest, bool sorted,
|
||||
void* out_value, int* out_indice,
|
||||
cuinferDataType_t datatype, void* workspace);
|
||||
|
||||
cuinferStatus_t cuinferGetTopKWorkspace(
|
||||
cuinferHandle_t handle,
|
||||
int n, int m, int top_k,
|
||||
cuinferDataType_t datatype, size_t* workspace_size);
|
||||
|
||||
cuinferStatus_t cuinferTopKBatch(
|
||||
cuinferHandle_t handle,
|
||||
const void* input, int top_k, int batch, int n, int m, int k,
|
||||
bool largest, bool sorted, int sort_dim,
|
||||
void* output, int* indice,
|
||||
cuinferDataType_t datatype, void* workspace);
|
||||
|
||||
// Softmax
|
||||
cuinferStatus_t cuinferSoftmaxForward(
|
||||
cuinferHandle_t handle,
|
||||
cuinferSoftmaxAlgorithm_t algo,
|
||||
cuinferSoftmaxMode_t mode,
|
||||
const void* alpha,
|
||||
const void* xDesc, const void* x,
|
||||
const void* beta,
|
||||
const void* yDesc, void* y);
|
||||
|
||||
// Reduce
|
||||
cuinferStatus_t cuinferReduce(
|
||||
cuinferHandle_t handle,
|
||||
const void* in, void* out,
|
||||
cuinferDataType_t in_type,
|
||||
cuinferDataType_t acc_type,
|
||||
cuinferDataType_t out_type,
|
||||
cuinferReduceTensorOp_t reduce_op,
|
||||
int n_dims, const int* dims,
|
||||
int n_reduce_dims, const int* reduce_dim_index,
|
||||
void* workspace);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
188
ex_engine/csrc/gemm_grouped.cu
Normal file
188
ex_engine/csrc/gemm_grouped.cu
Normal file
@@ -0,0 +1,188 @@
|
||||
// gemm_grouped.cu — Per-expert GEMM using CUTLASS Cu10 TensorOp
|
||||
//
|
||||
// Source lineage:
|
||||
// cat_files/batched_gemm.cu — cutlass sample from real device
|
||||
// cat_files/default_gemm_configuration.h — Cu10 half/half/float config
|
||||
// ex_engine/xllm_kernels/cuda/corex_batched_gemm_kernel.cu — existing impl
|
||||
// ex_engine/xllm_kernels/cuda/bindings/hgemm_bind.cpp — moe_expert_gemm pattern
|
||||
//
|
||||
// This file provides:
|
||||
// 1. cutlass_expert_gemm() — one cutlass GEMM per expert (Cu10 TensorOp)
|
||||
// 2. cuinfer_expert_gemm() — one cuinferCustomGemm per expert (fallback)
|
||||
// 3. moe_group_gemm() — unified entry: try cutlass, fall back to cuinfer
|
||||
//
|
||||
// All use RowMajor, FP16 data, FP32 accumulation.
|
||||
// Weight layout: [num_experts, N, K] (TN format = transB in GEMM sense)
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <cuda_fp16.h>
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/numeric_types.h"
|
||||
#include "cutlass/layout/matrix.h"
|
||||
#include "cutlass/gemm/device/gemm_batched.h"
|
||||
|
||||
// ============================================================================
|
||||
// Cu10 TensorOp GEMM type — from default_gemm_configuration.h
|
||||
// ThreadblockShape<128,128,32>, WarpShape<32,32,32>, Instruction<16,16,16>
|
||||
// ============================================================================
|
||||
using GemmCu10 = cutlass::gemm::device::GemmBatched<
|
||||
cutlass::half_t, // ElementA
|
||||
cutlass::layout::RowMajor, // LayoutA
|
||||
cutlass::half_t, // ElementB
|
||||
cutlass::layout::RowMajor, // LayoutB
|
||||
cutlass::half_t, // ElementC
|
||||
cutlass::layout::RowMajor, // LayoutC
|
||||
float, // ElementAccumulator
|
||||
cutlass::arch::OpClassTensorOp, // use TCU
|
||||
cutlass::arch::Cu10 // BI-V100
|
||||
>;
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// cutlass_expert_gemm: per-expert GEMM using CUTLASS
|
||||
//
|
||||
// For each expert e with M_e tokens:
|
||||
// C[offset:offset+M_e, :N] = A[offset:offset+M_e, :K] @ B[e, :N, :K]^T
|
||||
//
|
||||
// B is stored as [num_experts, N, K] (RowMajor), we need A×B^T.
|
||||
// Cutlass RowMajor × RowMajor computes C = A × B, so we transpose:
|
||||
// C(M,N) = A(M,K) × B^T(K,N) = A(M,K) × B_orig(N,K)^T
|
||||
//
|
||||
// In row-major: A lda=K, B lda=K (it's NxK stored row-major), C ldc=N
|
||||
// We use Cutlass's NN mode on (A, B^T) which is implemented as:
|
||||
// Cutlass RowMajor NN: C[i,j] = sum_k A[i,k] * B[k,j]
|
||||
// But B is (N,K) not (K,N), so we pass B as ColumnMajor or handle via stride.
|
||||
//
|
||||
// Simpler: A is (M,K) RowMajor, we want output (M,N).
|
||||
// B_expert is (N,K) RowMajor = same as (K,N) ColumnMajor.
|
||||
// So: A(M,K) RowMajor × B(K,N) ColumnMajor → C(M,N) RowMajor
|
||||
// This is exactly GEMM with transB.
|
||||
// ============================================================================
|
||||
|
||||
using GemmCu10_TN = cutlass::gemm::device::GemmBatched<
|
||||
cutlass::half_t, // ElementA
|
||||
cutlass::layout::RowMajor, // LayoutA — A is (M,K) row-major
|
||||
cutlass::half_t, // ElementB
|
||||
cutlass::layout::ColumnMajor, // LayoutB — B is (N,K) stored row = (K,N) col
|
||||
cutlass::half_t, // ElementC
|
||||
cutlass::layout::RowMajor, // LayoutC
|
||||
float, // ElementAccumulator
|
||||
cutlass::arch::OpClassTensorOp, // TCU
|
||||
cutlass::arch::Cu10 // BI-V100
|
||||
>;
|
||||
|
||||
|
||||
int cutlass_expert_gemm(
|
||||
int num_experts,
|
||||
const int* expert_counts, // host array [num_experts]
|
||||
const int* expert_offsets, // host array [num_experts], exclusive prefix sum
|
||||
int N, int K,
|
||||
const __half* input, // (total_tokens, K) row-major
|
||||
const __half* weights, // (num_experts, N, K) row-major — TN format
|
||||
__half* output, // (total_tokens, N) row-major
|
||||
cudaStream_t stream)
|
||||
{
|
||||
GemmCu10_TN gemm_op;
|
||||
float alpha = 1.0f, beta = 0.0f;
|
||||
int failures = 0;
|
||||
|
||||
for (int e = 0; e < num_experts; e++) {
|
||||
int M_e = expert_counts[e];
|
||||
if (M_e <= 0) continue;
|
||||
|
||||
int off = expert_offsets[e];
|
||||
auto A = reinterpret_cast<cutlass::half_t const*>(input + (long long)off * K);
|
||||
auto B = reinterpret_cast<cutlass::half_t const*>(weights + (long long)e * N * K);
|
||||
auto C = reinterpret_cast<cutlass::half_t*>(output + (long long)off * N);
|
||||
|
||||
// A: (M_e, K) RowMajor, lda = K
|
||||
// B: (N, K) RowMajor → (K, N) ColumnMajor, ldb = N (col-major stride)
|
||||
// C: (M_e, N) RowMajor, ldc = N
|
||||
cutlass::Status status = gemm_op({
|
||||
{M_e, N, K},
|
||||
{A, K}, // A, lda
|
||||
0, // strideA (not batched)
|
||||
{B, K}, // B in col-major view: (N,K) row = (K,N) col, ldb = K
|
||||
0, // strideB
|
||||
{C, N}, // C, ldc
|
||||
0, // strideC
|
||||
{C, N}, // D = C
|
||||
0,
|
||||
{alpha, beta},
|
||||
1 // batch_count = 1 (we loop over experts)
|
||||
});
|
||||
|
||||
if (status != cutlass::Status::kSuccess) {
|
||||
failures++;
|
||||
}
|
||||
}
|
||||
return failures;
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// cuinfer fallback — forward-declare cuinferCustomGemm
|
||||
// ============================================================================
|
||||
extern "C" {
|
||||
typedef struct cuinferContext* cuinferHandle_t;
|
||||
typedef enum { CUINFER_STATUS_SUCCESS_GG = 0 } cuinferStatus_gg_t;
|
||||
cuinferHandle_t cuinferCreate_handle();
|
||||
|
||||
int cuinferCustomGemm(
|
||||
cuinferHandle_t handle, cudaStream_t stream,
|
||||
int ptrMode, int transa, int transb,
|
||||
int m, int n, int k,
|
||||
const void* alpha,
|
||||
const void* A, int Atype, int lda, long long int strideA,
|
||||
const void* B, int Btype, int ldb, long long int strideB,
|
||||
const void* beta,
|
||||
void* C, int Ctype, int ldc, long long int strideC,
|
||||
int batchCount, int computeType, int scaleType,
|
||||
const void* customHostPtr, const void* customDevicePtr, int customOption);
|
||||
}
|
||||
|
||||
|
||||
int cuinfer_expert_gemm(
|
||||
int num_experts,
|
||||
const int* expert_counts,
|
||||
const int* expert_offsets,
|
||||
int N, int K,
|
||||
const __half* input,
|
||||
const __half* weights,
|
||||
__half* output,
|
||||
cudaStream_t stream,
|
||||
cuinferHandle_t handle)
|
||||
{
|
||||
float alpha = 1.0f, beta = 0.0f;
|
||||
int failures = 0;
|
||||
|
||||
for (int e = 0; e < num_experts; e++) {
|
||||
int M_e = expert_counts[e];
|
||||
if (M_e <= 0) continue;
|
||||
|
||||
int off = expert_offsets[e];
|
||||
const void* A = input + (long long)off * K;
|
||||
const void* B = weights + (long long)e * N * K;
|
||||
void* C = output + (long long)off * N;
|
||||
|
||||
// cuinferCustomGemm: transa=0 (N), transb=1 (T)
|
||||
// CUDA_R_16F = 2
|
||||
int status = cuinferCustomGemm(
|
||||
handle, stream,
|
||||
0, // CUINFER_POINTER_MODE_HOST
|
||||
0, 1, // transa=N, transb=T
|
||||
M_e, N, K,
|
||||
&alpha,
|
||||
A, 2, K, 0, // A: fp16, lda=K
|
||||
B, 2, K, 0, // B: fp16, ldb=K (row-major N×K, transposed)
|
||||
&beta,
|
||||
C, 2, N, 0, // C: fp16, ldc=N
|
||||
1, // batchCount=1
|
||||
0, 0, // computeType=fp32, scaleType=fp32
|
||||
nullptr, nullptr, 0);
|
||||
|
||||
if (status != 0) failures++;
|
||||
}
|
||||
return failures;
|
||||
}
|
||||
182
ex_engine/csrc/gemm_grouped_bind.cpp
Normal file
182
ex_engine/csrc/gemm_grouped_bind.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
// gemm_grouped_bind.cpp — Python bindings for grouped GEMM
|
||||
//
|
||||
// Source lineage:
|
||||
// ex_engine/xllm_kernels/cuda/bindings/hgemm_bind.cpp — moe_expert_gemm pattern
|
||||
// ex_engine/xllm_kernels/cuda/bindings/corex_batched_gemm_bind.cpp — batched pattern
|
||||
//
|
||||
// Exports:
|
||||
// moe_group_gemm(input, weights, expert_counts) → output
|
||||
// moe_group_gemm_cutlass(input, weights, expert_counts) → output
|
||||
// moe_decode_cutlass(hidden, w13, w2, topk_weights) → output
|
||||
|
||||
#include <torch/extension.h>
|
||||
#include <cuda_runtime.h>
|
||||
#include <cuda_fp16.h>
|
||||
#include <c10/cuda/CUDAStream.h>
|
||||
#include <vector>
|
||||
|
||||
// From gemm_grouped.cu
|
||||
int cutlass_expert_gemm(
|
||||
int num_experts,
|
||||
const int* expert_counts, const int* expert_offsets,
|
||||
int N, int K,
|
||||
const __half* input, const __half* weights, __half* output,
|
||||
cudaStream_t stream);
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// moe_group_gemm: per-expert GEMM using CUTLASS Cu10 TensorOp
|
||||
//
|
||||
// input: (total_tokens, K) fp16
|
||||
// weights: (num_experts, N, K) fp16, TN layout
|
||||
// expert_counts: (num_experts,) int32
|
||||
// Returns: (total_tokens, N) fp16
|
||||
// ============================================================================
|
||||
torch::Tensor moe_group_gemm(
|
||||
torch::Tensor input,
|
||||
torch::Tensor weights,
|
||||
torch::Tensor expert_counts)
|
||||
{
|
||||
TORCH_CHECK(input.is_cuda() && weights.is_cuda(), "inputs must be CUDA");
|
||||
TORCH_CHECK(input.scalar_type() == torch::kHalf, "input must be fp16");
|
||||
TORCH_CHECK(weights.scalar_type() == torch::kHalf, "weights must be fp16");
|
||||
|
||||
int total_tokens = input.size(0);
|
||||
int K = input.size(1);
|
||||
int num_experts = weights.size(0);
|
||||
int N = weights.size(1);
|
||||
TORCH_CHECK(weights.size(2) == K, "weights K dim must match input K");
|
||||
|
||||
auto output = torch::zeros({total_tokens, N}, input.options());
|
||||
|
||||
// Build host arrays
|
||||
auto counts_cpu = expert_counts.to(torch::kCPU).to(torch::kInt32).contiguous();
|
||||
int32_t* c = counts_cpu.data_ptr<int32_t>();
|
||||
std::vector<int> counts(num_experts), offsets(num_experts);
|
||||
int cumsum = 0;
|
||||
for (int i = 0; i < num_experts; i++) {
|
||||
counts[i] = c[i];
|
||||
offsets[i] = cumsum;
|
||||
cumsum += c[i];
|
||||
}
|
||||
|
||||
cudaStream_t stream = c10::cuda::getCurrentCUDAStream().stream();
|
||||
|
||||
int fails = cutlass_expert_gemm(
|
||||
num_experts, counts.data(), offsets.data(),
|
||||
N, K,
|
||||
reinterpret_cast<const __half*>(input.data_ptr<at::Half>()),
|
||||
reinterpret_cast<const __half*>(weights.data_ptr<at::Half>()),
|
||||
reinterpret_cast<__half*>(output.data_ptr<at::Half>()),
|
||||
stream);
|
||||
|
||||
if (fails > 0) {
|
||||
// Fallback to PyTorch F.linear per expert
|
||||
auto input_a = input.to(torch::kFloat32);
|
||||
auto output_f = torch::zeros({total_tokens, N},
|
||||
input.options().dtype(torch::kFloat32));
|
||||
for (int e = 0; e < num_experts; e++) {
|
||||
if (counts[e] <= 0) continue;
|
||||
int off = offsets[e];
|
||||
auto x = input_a.narrow(0, off, counts[e]);
|
||||
auto w = weights[e].to(torch::kFloat32); // (N, K)
|
||||
output_f.narrow(0, off, counts[e]) = torch::mm(x, w.t());
|
||||
}
|
||||
output = output_f.to(torch::kHalf);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// moe_decode_cutlass: fused MoE decode for single-token (batch=1)
|
||||
//
|
||||
// Uses CUTLASS batched GEMM for the topk experts simultaneously.
|
||||
//
|
||||
// hidden: (1, H) fp16
|
||||
// w13_sel: (topk, 2*I, H) fp16 — already-gathered expert weights
|
||||
// w2_sel: (topk, H, I) fp16
|
||||
// topk_weights: (topk,) float32
|
||||
// Returns: (1, H) fp16
|
||||
// ============================================================================
|
||||
|
||||
// From corex_batched_gemm_kernel.cu
|
||||
cudaError_t cutlass_batched_hgemm(
|
||||
int m, int n, int k,
|
||||
__half const *A, int lda, long long int batch_stride_A,
|
||||
__half const *B, int ldb, long long int batch_stride_B,
|
||||
__half *C, int ldc, long long int batch_stride_C,
|
||||
int batch_count);
|
||||
|
||||
|
||||
torch::Tensor moe_decode_cutlass(
|
||||
torch::Tensor hidden, // (1, H)
|
||||
torch::Tensor w13_sel, // (topk, 2*I, H)
|
||||
torch::Tensor w2_sel, // (topk, H, I)
|
||||
torch::Tensor topk_weights) // (topk,)
|
||||
{
|
||||
int topk = w13_sel.size(0);
|
||||
int two_I = w13_sel.size(1);
|
||||
int H = w13_sel.size(2);
|
||||
int I = two_I / 2;
|
||||
|
||||
// x: (1,H) → expand to (topk, 1, H)
|
||||
auto x = hidden.expand({topk, 1, H}).contiguous();
|
||||
|
||||
// w13^T: (topk, 2I, H) → transpose → (topk, H, 2I)
|
||||
auto w13_t = w13_sel.transpose(1, 2).contiguous();
|
||||
|
||||
// Step 1: gate_up = x @ w13^T → (topk, 1, 2I)
|
||||
auto gate_up_3d = torch::empty({topk, 1, two_I}, x.options());
|
||||
auto status1 = cutlass_batched_hgemm(
|
||||
1, two_I, H,
|
||||
reinterpret_cast<const __half*>(x.data_ptr<at::Half>()),
|
||||
H, H,
|
||||
reinterpret_cast<const __half*>(w13_t.data_ptr<at::Half>()),
|
||||
two_I, H * two_I,
|
||||
reinterpret_cast<__half*>(gate_up_3d.data_ptr<at::Half>()),
|
||||
two_I, two_I,
|
||||
topk);
|
||||
TORCH_CHECK(status1 == cudaSuccess, "batched GEMM 1 failed");
|
||||
|
||||
auto gate_up = gate_up_3d.squeeze(1); // (topk, 2I)
|
||||
|
||||
// Step 2: SiLU activation
|
||||
auto chunks = gate_up.chunk(2, 1);
|
||||
auto act = torch::silu(chunks[0]) * chunks[1]; // (topk, I)
|
||||
act = act.unsqueeze(1).contiguous(); // (topk, 1, I)
|
||||
|
||||
// w2^T: (topk, H, I) → transpose → (topk, I, H)
|
||||
auto w2_t = w2_sel.transpose(1, 2).contiguous();
|
||||
|
||||
// Step 3: down = act @ w2^T → (topk, 1, H)
|
||||
auto down_3d = torch::empty({topk, 1, H}, x.options());
|
||||
auto status2 = cutlass_batched_hgemm(
|
||||
1, H, I,
|
||||
reinterpret_cast<const __half*>(act.data_ptr<at::Half>()),
|
||||
I, I,
|
||||
reinterpret_cast<const __half*>(w2_t.data_ptr<at::Half>()),
|
||||
H, I * H,
|
||||
reinterpret_cast<__half*>(down_3d.data_ptr<at::Half>()),
|
||||
H, H,
|
||||
topk);
|
||||
TORCH_CHECK(status2 == cudaSuccess, "batched GEMM 2 failed");
|
||||
|
||||
auto down = down_3d.squeeze(1); // (topk, H)
|
||||
|
||||
// Step 4: weighted sum
|
||||
auto out = (down * topk_weights.unsqueeze(1).to(down.dtype())).sum(0, true);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
||||
m.def("moe_group_gemm", &moe_group_gemm,
|
||||
"Per-expert GEMM via CUTLASS Cu10 TensorOp",
|
||||
py::arg("input"), py::arg("weights"), py::arg("expert_counts"));
|
||||
m.def("moe_decode_cutlass", &moe_decode_cutlass,
|
||||
"Fused MoE decode via CUTLASS batched GEMM",
|
||||
py::arg("hidden"), py::arg("w13_sel"),
|
||||
py::arg("w2_sel"), py::arg("topk_weights"));
|
||||
}
|
||||
Reference in New Issue
Block a user