Files
wehub-resource-sync 94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

183 lines
7.1 KiB
Plaintext

// LP prep kernel: build IPM inputs (A_full last column, b, t1) from
// global_counts and the constant per-rank metadata (log_single,
// log_replicated, B1, A_base_row_sum).
//
// Python equivalent (~8 torch ops; this kernel is one launch):
//
// total = global_counts.sum()
// counts_norm = global_counts / total.clamp(min=1.0)
// t1 = counts_norm[log_single] # (NUM_SINGLE,)
// b1 = counts_norm[log_replicated] # (NUM_RED_LOG,)
// b2 = -(B1 @ t1).flatten() # (NUM_GPUS,)
// b = cat(b1, b2) # (NC,)
// A_full[:, -1] = b - A_base_row_sum # last column only
//
// `A_full` is pre-allocated by the caller with shape (NC, NV); its first
// NV-1 columns are pre-filled with A_base.copy_() at solver init and not
// touched by this kernel.
//
// Single-block launch. All intermediate state lives in shared memory.
#include <sgl_kernel/tensor.h>
#include <sgl_kernel/utils.h>
#include <sgl_kernel/utils.cuh>
#include <dlpack/dlpack.h>
#include <tvm/ffi/container/tensor.h>
#include <cstdint>
namespace {
template <int NC, int NV, int NUM_SINGLE, int NUM_RED_LOG, int NUM_GPUS, int BLOCK_DIM>
__global__ void lp_prep_kernel(
float* __restrict__ A_full, // (NC, NV) — last column is written
float* __restrict__ b, // (NC,) — written
float* __restrict__ t1, // (NUM_SINGLE,) — written
const float* __restrict__ global_counts, // (num_logical,)
const int64_t* __restrict__ log_single, // (NUM_SINGLE,)
const int64_t* __restrict__ log_replicated, // (NUM_RED_LOG,)
const float* __restrict__ B1, // (NUM_GPUS, NUM_SINGLE)
const float* __restrict__ A_base_row_sum) { // (NC,)
static_assert(NC == NUM_RED_LOG + NUM_GPUS, "NC must equal NUM_RED_LOG + NUM_GPUS");
constexpr int WARPS_PER_BLOCK = BLOCK_DIM / 32;
extern __shared__ unsigned char raw_smem[];
// Layout: shared_t1 [NUM_SINGLE] | shared_b1 [NUM_RED_LOG] | reduce_buf [WARPS_PER_BLOCK] | total_pad [1]
float* shared_t1 = reinterpret_cast<float*>(raw_smem);
float* shared_b1 = shared_t1 + NUM_SINGLE;
float* reduce_buf = shared_b1 + NUM_RED_LOG;
float* shared_total = reduce_buf + WARPS_PER_BLOCK;
const int tid = threadIdx.x;
const int lane = tid & 31;
const int warp_id = tid >> 5;
// ---- Stage 1: gather raw t1 / b1 + partial sum for total ----
float local_sum = 0.f;
for (int i = tid; i < NUM_SINGLE; i += BLOCK_DIM) {
float v = global_counts[log_single[i]];
shared_t1[i] = v; // raw, scaled below
local_sum += v;
}
for (int i = tid; i < NUM_RED_LOG; i += BLOCK_DIM) {
float v = global_counts[log_replicated[i]];
shared_b1[i] = v;
local_sum += v;
}
// Block-level reduction: warp shuffle -> shared mem -> warp 0 final reduce.
for (int offset = 16; offset > 0; offset >>= 1) {
local_sum += __shfl_xor_sync(0xffffffff, local_sum, offset);
}
if (lane == 0) {
reduce_buf[warp_id] = local_sum;
}
__syncthreads();
if (warp_id == 0) {
float v = (tid < WARPS_PER_BLOCK) ? reduce_buf[tid] : 0.f;
for (int offset = 16; offset > 0; offset >>= 1) {
v += __shfl_xor_sync(0xffffffff, v, offset);
}
if (tid == 0) {
shared_total[0] = fmaxf(v, 1.0f); // clamp(min=1.0)
}
}
__syncthreads();
const float inv_total = 1.0f / shared_total[0];
// ---- Stage 2: scale t1 (keep in shmem for matmul, also write to global)
// scale b[0..NUM_RED_LOG] = scaled b1
for (int i = tid; i < NUM_SINGLE; i += BLOCK_DIM) {
float v = shared_t1[i] * inv_total;
shared_t1[i] = v;
t1[i] = v;
}
for (int i = tid; i < NUM_RED_LOG; i += BLOCK_DIM) {
b[i] = shared_b1[i] * inv_total;
}
__syncthreads();
// ---- Stage 3: b[NUM_RED_LOG + j] = -(B1[j] · t1) for j in [0, NUM_GPUS).
// Sequential GEMV across NUM_GPUS=16 outputs; each is a 240-wide dot
// product reduced across the block. Cheap at this size.
for (int j = 0; j < NUM_GPUS; j++) {
float dot = 0.f;
for (int k = tid; k < NUM_SINGLE; k += BLOCK_DIM) {
dot += B1[j * NUM_SINGLE + k] * shared_t1[k];
}
for (int offset = 16; offset > 0; offset >>= 1) {
dot += __shfl_xor_sync(0xffffffff, dot, offset);
}
if (lane == 0) {
reduce_buf[warp_id] = dot;
}
__syncthreads();
if (warp_id == 0) {
float v = (tid < WARPS_PER_BLOCK) ? reduce_buf[tid] : 0.f;
for (int offset = 16; offset > 0; offset >>= 1) {
v += __shfl_xor_sync(0xffffffff, v, offset);
}
if (tid == 0) {
b[NUM_RED_LOG + j] = -v;
}
}
__syncthreads();
}
// ---- Stage 4: A_full[i][NV-1] = b[i] - A_base_row_sum[i].
// First NV-1 columns of A_full are pre-filled with A_base at solver init,
// so we only write the last column here.
for (int i = tid; i < NC; i += BLOCK_DIM) {
A_full[i * NV + (NV - 1)] = b[i] - A_base_row_sum[i];
}
}
template <int NC, int NV, int NUM_SINGLE, int NUM_RED_LOG, int NUM_GPUS, int BLOCK_DIM>
void lp_prep(
tvm::ffi::TensorView A_full,
tvm::ffi::TensorView b,
tvm::ffi::TensorView t1,
tvm::ffi::TensorView global_counts,
tvm::ffi::TensorView log_single,
tvm::ffi::TensorView log_replicated,
tvm::ffi::TensorView B1,
tvm::ffi::TensorView A_base_row_sum) {
using namespace host;
SymbolicDevice device_;
TensorMatcher({NC, NV}).with_dtype<float>().with_device<kDLCUDA>(device_).verify(A_full);
TensorMatcher({NC}).with_dtype<float>().with_device<kDLCUDA>(device_).verify(b);
TensorMatcher({NUM_SINGLE}).with_dtype<float>().with_device<kDLCUDA>(device_).verify(t1);
TensorMatcher({NUM_SINGLE}).with_dtype<int64_t>().with_device<kDLCUDA>(device_).verify(log_single);
TensorMatcher({NUM_RED_LOG}).with_dtype<int64_t>().with_device<kDLCUDA>(device_).verify(log_replicated);
TensorMatcher({NUM_GPUS, NUM_SINGLE}).with_dtype<float>().with_device<kDLCUDA>(device_).verify(B1);
TensorMatcher({NC}).with_dtype<float>().with_device<kDLCUDA>(device_).verify(A_base_row_sum);
constexpr int WARPS_PER_BLOCK = BLOCK_DIM / 32;
const size_t smem_bytes = (NUM_SINGLE + NUM_RED_LOG + WARPS_PER_BLOCK + 1) * sizeof(float);
using KernelT =
void (*)(float*, float*, float*, const float*, const int64_t*, const int64_t*, const float*, const float*);
KernelT kernel = lp_prep_kernel<NC, NV, NUM_SINGLE, NUM_RED_LOG, NUM_GPUS, BLOCK_DIM>;
if (smem_bytes > 48 * 1024) {
cudaFuncSetAttribute(kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, static_cast<int>(smem_bytes));
}
const DLDevice device = device_.unwrap();
LaunchKernel(/*grid=*/1, /*block=*/BLOCK_DIM, device, smem_bytes)(
kernel,
static_cast<float*>(A_full.data_ptr()),
static_cast<float*>(b.data_ptr()),
static_cast<float*>(t1.data_ptr()),
static_cast<const float*>(global_counts.data_ptr()),
static_cast<const int64_t*>(log_single.data_ptr()),
static_cast<const int64_t*>(log_replicated.data_ptr()),
static_cast<const float*>(B1.data_ptr()),
static_cast<const float*>(A_base_row_sum.data_ptr()));
}
} // namespace