fix(critical): fold max_completion_tokens + max_num_seqs=2 + max_model_len=80000 + xllm_latest layer import
Sub 655 root causes (confirmed from log analysis):
1. protocol.py: max_completion_tokens never folded into max_tokens
→ 162/881 replay requests rejected 400 (extra_forbidden)
2. max_num_seqs=1 → t2_n_2 test fails (needs n=2)
3. max_model_len=131072 → OOM crash at 62% replay, opencompass all 0
Fixes:
- protocol.py: model_validator fold_max_completion_tokens
- yaml: max_num_seqs=2, max_model_len=80000, PYTORCH_CUDA_ALLOC_CONF
- topk_softmax stays =0 (corex CUB BlockReduce incompatible on BI-V100)
xllm_latest import to ex_engine/:
- npu_torch layers: GDN(1164L), Qwen3.5 GDN, attention, fused_moe
- cuda/moe kernels: topk_softmax_kernels.cuh, moe_combine, moe_compute_index
- npu kernels: causal_conv1d, recurrent_gated_delta_rule
- model headers: qwen3_5.h, qwen3_next.h
2026-08-13 03:19:39 +00:00
|
|
|
/* Copyright 2025-2026 The xLLM Authors. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
https://github.com/jd-opensource/xllm/blob/main/LICENSE
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==============================================================================*/
|
|
|
|
|
|
|
|
|
|
// Fused MoE combine kernel — reorder + weighted sum in one pass.
|
|
|
|
|
// Replaces: torch::zeros + index_copy_ + view + multiply + sum
|
|
|
|
|
//
|
|
|
|
|
// Algorithm per token (each block handles one token):
|
|
|
|
|
// 1. For each of its topk experts, read gemm2 at flat_idx directly
|
|
|
|
|
// (gemm2 is flat-index-ordered after scatter via index_copy_ with dst_src)
|
|
|
|
|
// 2. Multiply by router weight
|
|
|
|
|
// 3. Accumulate into output[token]
|
|
|
|
|
//
|
|
|
|
|
// Grid: num_tokens (N) blocks
|
|
|
|
|
// Block: HIDDEN_DIM / HIDDEN_TILE threads
|
|
|
|
|
|
|
|
|
|
#include <c10/cuda/CUDAGuard.h>
|
|
|
|
|
|
|
|
|
|
#include "device_utils.cuh"
|
feat: xllm MoE CUDA kernels — fused_topk + compute_index + combine
3 MoE kernel files adapted for corex:
moe_fused_topk.cu: LOG(FATAL)→TORCH_CHECK, +torch/extension.h
moe_compute_index.cu: CHECK_LE→TORCH_CHECK, uses cub::BlockScan (corex CUB)
moe_combine.cu: fixed duplicate include, +torch/extension.h
New pybind binding: xllm_moe_bind.cpp
→ moe_fused_topk(gating, topk, renormalize, bias, scoring_func)
→ moe_compute_index(expert_id, num_experts)
→ moe_combine_result(gemm2, weights, N, topk)
AST verification added for all 3 functions
2026-08-14 11:23:49 +00:00
|
|
|
#include <torch/extension.h>
|
fix(critical): fold max_completion_tokens + max_num_seqs=2 + max_model_len=80000 + xllm_latest layer import
Sub 655 root causes (confirmed from log analysis):
1. protocol.py: max_completion_tokens never folded into max_tokens
→ 162/881 replay requests rejected 400 (extra_forbidden)
2. max_num_seqs=1 → t2_n_2 test fails (needs n=2)
3. max_model_len=131072 → OOM crash at 62% replay, opencompass all 0
Fixes:
- protocol.py: model_validator fold_max_completion_tokens
- yaml: max_num_seqs=2, max_model_len=80000, PYTORCH_CUDA_ALLOC_CONF
- topk_softmax stays =0 (corex CUB BlockReduce incompatible on BI-V100)
xllm_latest import to ex_engine/:
- npu_torch layers: GDN(1164L), Qwen3.5 GDN, attention, fused_moe
- cuda/moe kernels: topk_softmax_kernels.cuh, moe_combine, moe_compute_index
- npu kernels: causal_conv1d, recurrent_gated_delta_rule
- model headers: qwen3_5.h, qwen3_next.h
2026-08-13 03:19:39 +00:00
|
|
|
|
|
|
|
|
namespace xllm::kernel::cuda {
|
|
|
|
|
|
|
|
|
|
constexpr int32_t kCombineBlockSize = 256;
|
|
|
|
|
|
|
|
|
|
template <typename scalar_t>
|
|
|
|
|
__global__ void XLLM_KERNEL_ATTR(kCombineBlockSize) moe_combine_kernel(
|
|
|
|
|
const scalar_t* __restrict__ gemm2, // [N*topk, H] flat-index-ordered
|
|
|
|
|
const float* __restrict__ reduce_weight, // [N, topk]
|
|
|
|
|
scalar_t* __restrict__ output, // [N, H]
|
|
|
|
|
int64_t N,
|
|
|
|
|
int32_t topk,
|
|
|
|
|
int64_t H) {
|
|
|
|
|
int64_t token_id = blockIdx.x; // 0 .. N-1
|
|
|
|
|
if (token_id >= N) return;
|
|
|
|
|
|
|
|
|
|
int32_t tid = threadIdx.x;
|
|
|
|
|
int32_t stride = kCombineBlockSize;
|
|
|
|
|
|
|
|
|
|
// Accumulate over topk experts for this token
|
|
|
|
|
for (int64_t h = tid; h < H; h += stride) {
|
|
|
|
|
float acc = 0.0f;
|
|
|
|
|
for (int32_t k = 0; k < topk; ++k) {
|
|
|
|
|
int64_t flat_idx = token_id * topk + k;
|
|
|
|
|
float w = reduce_weight[flat_idx];
|
|
|
|
|
acc += w * static_cast<float>(gemm2[flat_idx * H + h]);
|
|
|
|
|
}
|
|
|
|
|
output[token_id * H + h] = static_cast<scalar_t>(acc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Host-side orchestrator ----
|
|
|
|
|
torch::Tensor moe_combine_result(
|
|
|
|
|
const torch::Tensor& gemm2, // [N*topk, H] flat-index-ordered
|
|
|
|
|
const torch::Tensor& reduce_weight, // [N, topk] float or same as gemm2
|
|
|
|
|
int64_t N,
|
|
|
|
|
int32_t topk) {
|
|
|
|
|
auto stream = at::cuda::getCurrentCUDAStream();
|
|
|
|
|
int64_t H = gemm2.size(1);
|
|
|
|
|
auto dtype = gemm2.scalar_type();
|
|
|
|
|
|
|
|
|
|
auto output = torch::empty({N, H}, gemm2.options());
|
|
|
|
|
auto rw = reduce_weight.to(gemm2.device(), torch::kFloat32).contiguous();
|
|
|
|
|
|
|
|
|
|
if (dtype == torch::kFloat16) {
|
|
|
|
|
moe_combine_kernel<c10::Half>
|
|
|
|
|
<<<N, kCombineBlockSize, 0, stream>>>(gemm2.data_ptr<c10::Half>(),
|
|
|
|
|
rw.data_ptr<float>(),
|
|
|
|
|
output.data_ptr<c10::Half>(),
|
|
|
|
|
N,
|
|
|
|
|
topk,
|
|
|
|
|
H);
|
|
|
|
|
} else if (dtype == torch::kBFloat16) {
|
|
|
|
|
moe_combine_kernel<c10::BFloat16>
|
|
|
|
|
<<<N, kCombineBlockSize, 0, stream>>>(gemm2.data_ptr<c10::BFloat16>(),
|
|
|
|
|
rw.data_ptr<float>(),
|
|
|
|
|
output.data_ptr<c10::BFloat16>(),
|
|
|
|
|
N,
|
|
|
|
|
topk,
|
|
|
|
|
H);
|
|
|
|
|
} else {
|
|
|
|
|
moe_combine_kernel<float>
|
|
|
|
|
<<<N, kCombineBlockSize, 0, stream>>>(gemm2.data_ptr<float>(),
|
|
|
|
|
rw.data_ptr<float>(),
|
|
|
|
|
output.data_ptr<float>(),
|
|
|
|
|
N,
|
|
|
|
|
topk,
|
|
|
|
|
H);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace xllm::kernel::cuda
|