Compare commits
10 Commits
phase13
...
9bb5c5c328
| Author | SHA1 | Date | |
|---|---|---|---|
| 9bb5c5c328 | |||
| 986a289616 | |||
| a67e724119 | |||
| d5532ef209 | |||
| e207523e21 | |||
| 876d3f5d6a | |||
| 9783fcf410 | |||
| 6cc1c9332d | |||
| d67dda404e | |||
| ee68d3565d |
1186
Cargo.lock
generated
Normal file
1186
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -24,3 +24,5 @@ regex = "1"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
axum = "0.8"
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
tokio-stream = "0.1"
|
||||
rand = "0.8"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::error::Result;
|
||||
use crate::ffi;
|
||||
use crate::memory::GpuBuffer;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Caching allocator that reuses freed GPU buffers instead of calling
|
||||
@@ -84,6 +85,33 @@ impl Drop for CachingAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static ALLOCATOR: RefCell<CachingAllocator> = RefCell::new(CachingAllocator::new());
|
||||
}
|
||||
|
||||
/// Allocate a GPU buffer through the caching allocator.
|
||||
/// The returned buffer has `pooled = true` so it will be returned
|
||||
/// to the pool on drop instead of calling cudaFree.
|
||||
pub fn cached_alloc(size: usize) -> Result<GpuBuffer> {
|
||||
ALLOCATOR.with(|cell| {
|
||||
let mut buf = cell.borrow_mut().alloc(size)?;
|
||||
buf.set_pooled(true);
|
||||
Ok(buf)
|
||||
})
|
||||
}
|
||||
|
||||
/// Return a raw GPU pointer to the caching allocator's free list.
|
||||
/// Called from `GpuBuffer::Drop` for pooled buffers. Takes raw pointer
|
||||
/// and size to avoid re-triggering Drop.
|
||||
pub fn return_to_pool(ptr: *mut u8, len: usize) {
|
||||
ALLOCATOR.with(|cell| {
|
||||
let mut alloc = cell.borrow_mut();
|
||||
let bucket = bucket_size(len);
|
||||
alloc.stats.current_allocated = alloc.stats.current_allocated.saturating_sub(len);
|
||||
alloc.free_lists.entry(bucket).or_default().push((ptr, len));
|
||||
});
|
||||
}
|
||||
|
||||
/// Round up to next power-of-2, minimum 512 bytes.
|
||||
fn bucket_size(size: usize) -> usize {
|
||||
let min = 512;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::error::{self, Result};
|
||||
use crate::ffi;
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DeviceInfo {
|
||||
@@ -44,10 +45,14 @@ pub fn current_device() -> Result<u32> {
|
||||
}
|
||||
|
||||
pub fn device_info(device: u32) -> Result<DeviceInfo> {
|
||||
// Get device name from cudaGetDeviceProperties (only use the name field).
|
||||
let mut prop = unsafe { std::mem::zeroed::<ffi::CudaDeviceProp>() };
|
||||
error::check(unsafe { ffi::cudaGetDeviceProperties(&mut prop, device as i32) })?;
|
||||
let name = unsafe { CStr::from_ptr(prop.name.as_ptr()) }
|
||||
// Heap-allocate oversized buffer for cudaDeviceProp (layout varies by CUDA version).
|
||||
// CUDA 12.x struct is ~5-6 KB; use 32 KB to guard against future growth.
|
||||
let mut prop_buf = vec![0u8; 32768];
|
||||
error::check(unsafe {
|
||||
ffi::cudaGetDeviceProperties(prop_buf.as_mut_ptr(), device as i32)
|
||||
})?;
|
||||
// Name is always the first field: char[256].
|
||||
let name = unsafe { CStr::from_ptr(prop_buf.as_ptr() as *const c_char) }
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ use std::os::raw::c_char;
|
||||
|
||||
pub type CudaStream = *mut c_void;
|
||||
pub type CudaEvent = *mut c_void;
|
||||
pub type CudaGraph = *mut c_void;
|
||||
pub type CudaGraphExec = *mut c_void;
|
||||
|
||||
pub const CUDA_MEMCPY_H2D: i32 = 1;
|
||||
pub const CUDA_MEMCPY_D2H: i32 = 2;
|
||||
@@ -11,31 +13,16 @@ pub const CUDA_MEMCPY_D2D: i32 = 3;
|
||||
pub const CUDA_SUCCESS: i32 = 0;
|
||||
pub const CUDA_ERROR_OUT_OF_MEMORY: i32 = 2;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct CudaDeviceProp {
|
||||
pub name: [c_char; 256],
|
||||
pub total_global_mem: usize,
|
||||
pub shared_mem_per_block: usize,
|
||||
pub regs_per_block: i32,
|
||||
pub warp_size: i32,
|
||||
pub max_threads_per_block: i32,
|
||||
pub max_threads_dim: [i32; 3],
|
||||
pub max_grid_size: [i32; 3],
|
||||
pub clock_rate: i32,
|
||||
pub total_const_mem: usize,
|
||||
pub major: i32,
|
||||
pub minor: i32,
|
||||
// There are many more fields; we only read up to what we need.
|
||||
// cudaDeviceProp is a large struct (~1KB). We pad the rest.
|
||||
_pad: [u8; 4096],
|
||||
}
|
||||
/// cudaStreamCaptureMode::cudaStreamCaptureModeGlobal
|
||||
pub const CUDA_STREAM_CAPTURE_MODE_GLOBAL: i32 = 0;
|
||||
|
||||
unsafe extern "C" {
|
||||
// --- Device ---
|
||||
pub fn cudaGetDeviceCount(count: *mut i32) -> i32;
|
||||
pub fn cudaSetDevice(device: i32) -> i32;
|
||||
pub fn cudaGetDevice(device: *mut i32) -> i32;
|
||||
pub fn cudaGetDeviceProperties(prop: *mut CudaDeviceProp, device: i32) -> i32;
|
||||
/// Takes a raw pointer; caller provides a heap buffer large enough for any CUDA version.
|
||||
pub fn cudaGetDeviceProperties(prop: *mut u8, device: i32) -> i32;
|
||||
pub fn cudaDeviceSynchronize() -> i32;
|
||||
|
||||
// --- Memory ---
|
||||
@@ -62,6 +49,18 @@ unsafe extern "C" {
|
||||
pub fn cudaGetLastError() -> i32;
|
||||
pub fn cudaGetErrorString(error: i32) -> *const c_char;
|
||||
|
||||
// --- CUDA Graphs ---
|
||||
pub fn cudaStreamBeginCapture(stream: CudaStream, mode: i32) -> i32;
|
||||
pub fn cudaStreamEndCapture(stream: CudaStream, graph: *mut CudaGraph) -> i32;
|
||||
pub fn cudaGraphInstantiate(
|
||||
graph_exec: *mut CudaGraphExec,
|
||||
graph: CudaGraph,
|
||||
flags: u64,
|
||||
) -> i32;
|
||||
pub fn cudaGraphLaunch(graph_exec: CudaGraphExec, stream: CudaStream) -> i32;
|
||||
pub fn cudaGraphDestroy(graph: CudaGraph) -> i32;
|
||||
pub fn cudaGraphExecDestroy(graph_exec: CudaGraphExec) -> i32;
|
||||
|
||||
// --- Our test kernel ---
|
||||
pub fn launch_vecadd_f32(
|
||||
a: *const f32,
|
||||
|
||||
98
crates/xserv-cuda/src/graph.rs
Normal file
98
crates/xserv-cuda/src/graph.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
//! CUDA Graphs: capture a sequence of kernel launches and replay them with
|
||||
//! near-zero host-side overhead (~3-5 us per launch eliminated).
|
||||
//!
|
||||
//! Usage:
|
||||
//! ```ignore
|
||||
//! let stream = CudaStream::new()?;
|
||||
//! let mut graph = CudaGraph::new();
|
||||
//!
|
||||
//! // First call: capture
|
||||
//! graph.begin_capture(&stream)?;
|
||||
//! // ... launch kernels on `stream` ...
|
||||
//! graph.end_capture(&stream)?;
|
||||
//!
|
||||
//! // Subsequent calls: replay
|
||||
//! graph.launch(&stream)?;
|
||||
//! ```
|
||||
//!
|
||||
//! Requirements for captured kernels:
|
||||
//! - All tensor shapes must be identical between capture and replay.
|
||||
//! - No host-side branching during the captured section.
|
||||
//! - Memory addresses used during capture must remain valid during replay.
|
||||
|
||||
use crate::error::{self, Result};
|
||||
use crate::ffi;
|
||||
use crate::stream::CudaStream;
|
||||
|
||||
/// RAII wrapper around a captured CUDA graph and its executable instance.
|
||||
pub struct CudaGraph {
|
||||
graph: ffi::CudaGraph,
|
||||
exec: ffi::CudaGraphExec,
|
||||
}
|
||||
|
||||
impl CudaGraph {
|
||||
/// Create an empty graph handle (not yet captured).
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
graph: std::ptr::null_mut(),
|
||||
exec: std::ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if a graph has been captured and instantiated.
|
||||
pub fn is_ready(&self) -> bool {
|
||||
!self.exec.is_null()
|
||||
}
|
||||
|
||||
/// Begin capturing kernel launches on `stream`.
|
||||
/// All subsequent kernel launches on this stream are recorded into the
|
||||
/// graph instead of being executed.
|
||||
pub fn begin_capture(&mut self, stream: &CudaStream) -> Result<()> {
|
||||
// If we have an old graph, destroy it first
|
||||
self.destroy_inner();
|
||||
error::check(unsafe {
|
||||
ffi::cudaStreamBeginCapture(
|
||||
stream.as_raw(),
|
||||
ffi::CUDA_STREAM_CAPTURE_MODE_GLOBAL,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// End capture and instantiate the executable graph.
|
||||
pub fn end_capture(&mut self, stream: &CudaStream) -> Result<()> {
|
||||
error::check(unsafe {
|
||||
ffi::cudaStreamEndCapture(stream.as_raw(), &mut self.graph)
|
||||
})?;
|
||||
error::check(unsafe {
|
||||
ffi::cudaGraphInstantiate(&mut self.exec, self.graph, 0)
|
||||
})
|
||||
}
|
||||
|
||||
/// Replay the captured graph on `stream`.
|
||||
/// Panics if no graph has been captured yet.
|
||||
pub fn launch(&self, stream: &CudaStream) -> Result<()> {
|
||||
assert!(self.is_ready(), "CudaGraph::launch called before capture");
|
||||
error::check(unsafe {
|
||||
ffi::cudaGraphLaunch(self.exec, stream.as_raw())
|
||||
})
|
||||
}
|
||||
|
||||
fn destroy_inner(&mut self) {
|
||||
if !self.exec.is_null() {
|
||||
unsafe { ffi::cudaGraphExecDestroy(self.exec) };
|
||||
self.exec = std::ptr::null_mut();
|
||||
}
|
||||
if !self.graph.is_null() {
|
||||
unsafe { ffi::cudaGraphDestroy(self.graph) };
|
||||
self.graph = std::ptr::null_mut();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for CudaGraph {
|
||||
fn drop(&mut self) {
|
||||
self.destroy_inner();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for CudaGraph {}
|
||||
@@ -2,11 +2,13 @@ pub mod allocator;
|
||||
pub mod device;
|
||||
pub mod error;
|
||||
pub mod ffi;
|
||||
pub mod graph;
|
||||
pub mod memory;
|
||||
pub mod stream;
|
||||
|
||||
pub use allocator::CachingAllocator;
|
||||
pub use device::DeviceInfo;
|
||||
pub use error::{CudaError, Result};
|
||||
pub use graph::CudaGraph;
|
||||
pub use memory::{GpuBuffer, PinnedBuffer};
|
||||
pub use stream::CudaStream;
|
||||
|
||||
@@ -3,9 +3,18 @@ use crate::ffi;
|
||||
use crate::stream::CudaStream;
|
||||
|
||||
/// RAII wrapper around a GPU memory allocation.
|
||||
///
|
||||
/// When `owned` is true (the default), dropping frees the GPU memory.
|
||||
/// A borrowed buffer (`owned = false`) does NOT free on drop — the
|
||||
/// caller must ensure the backing allocation outlives all borrows.
|
||||
///
|
||||
/// When `pooled` is true, dropping returns the buffer to the caching
|
||||
/// allocator's free list instead of calling cudaFree.
|
||||
pub struct GpuBuffer {
|
||||
ptr: *mut u8,
|
||||
len: usize,
|
||||
owned: bool,
|
||||
pooled: bool,
|
||||
}
|
||||
|
||||
impl GpuBuffer {
|
||||
@@ -13,7 +22,13 @@ impl GpuBuffer {
|
||||
assert!(len > 0, "cannot allocate 0 bytes on GPU");
|
||||
let mut ptr = std::ptr::null_mut();
|
||||
error::check(unsafe { ffi::cudaMalloc(&mut ptr, len) })?;
|
||||
Ok(Self { ptr, len })
|
||||
Ok(Self { ptr, len, owned: true, pooled: false })
|
||||
}
|
||||
|
||||
/// Mark this buffer as pooled (returned to caching allocator on drop)
|
||||
/// or not. Called by `cached_alloc` after obtaining a buffer.
|
||||
pub fn set_pooled(&mut self, pooled: bool) {
|
||||
self.pooled = pooled;
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
@@ -113,14 +128,29 @@ impl GpuBuffer {
|
||||
/// Reconstruct a GpuBuffer from a raw pointer + length.
|
||||
/// Safety: ptr must have been allocated with cudaMalloc, len must be correct.
|
||||
pub unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
|
||||
Self { ptr, len }
|
||||
Self { ptr, len, owned: true, pooled: false }
|
||||
}
|
||||
|
||||
/// Create a non-owning view of GPU memory. Dropping this buffer does NOT
|
||||
/// call `cudaFree`. The caller must ensure the underlying allocation
|
||||
/// outlives this borrow.
|
||||
///
|
||||
/// # Safety
|
||||
/// `ptr` must point to a valid GPU allocation of at least `len` bytes that
|
||||
/// will remain live for the lifetime of the returned `GpuBuffer`.
|
||||
pub unsafe fn borrow_raw(ptr: *mut u8, len: usize) -> Self {
|
||||
Self { ptr, len, owned: false, pooled: false }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for GpuBuffer {
|
||||
fn drop(&mut self) {
|
||||
if !self.ptr.is_null() {
|
||||
unsafe { ffi::cudaFree(self.ptr) };
|
||||
if self.owned && !self.ptr.is_null() {
|
||||
if self.pooled {
|
||||
crate::allocator::return_to_pool(self.ptr, self.len);
|
||||
} else {
|
||||
unsafe { ffi::cudaFree(self.ptr) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ fn main() {
|
||||
.include("../../csrc")
|
||||
.file("../../csrc/gemm/naive.cu")
|
||||
.file("../../csrc/gemm/tiled.cu")
|
||||
.file("../../csrc/gemm/gemv.cu")
|
||||
.file("../../csrc/normalization/rmsnorm.cu")
|
||||
.file("../../csrc/normalization/layernorm.cu")
|
||||
.file("../../csrc/activation/activations.cu")
|
||||
@@ -24,6 +25,7 @@ fn main() {
|
||||
.file("../../csrc/embedding/rope.cu")
|
||||
.file("../../csrc/attention/causal_mask.cu")
|
||||
.file("../../csrc/embedding/transpose.cu")
|
||||
.file("../../csrc/attention/flash_attention.cu")
|
||||
.compile("xserv_kernels");
|
||||
|
||||
println!("cargo:rerun-if-changed=../../csrc/");
|
||||
|
||||
@@ -12,12 +12,13 @@ unsafe extern "C" {
|
||||
fn launch_add_bf16(a: *const c_void, b: *const c_void, out: *mut c_void, n: i32, stream: *mut c_void);
|
||||
fn launch_mul_f32(a: *const c_void, b: *const c_void, out: *mut c_void, n: i32, stream: *mut c_void);
|
||||
fn launch_mul_bf16(a: *const c_void, b: *const c_void, out: *mut c_void, n: i32, stream: *mut c_void);
|
||||
fn launch_silu_mul_bf16(gate: *const c_void, up: *const c_void, out: *mut c_void, n: i32, stream: *mut c_void);
|
||||
}
|
||||
|
||||
fn dispatch_unary(x: &Tensor, f32_fn: unsafe extern "C" fn(*const c_void, *mut c_void, i32, *mut c_void),
|
||||
bf16_fn: unsafe extern "C" fn(*const c_void, *mut c_void, i32, *mut c_void)) -> Tensor {
|
||||
assert!(x.is_contiguous() && matches!(x.device(), Device::Cuda(_)));
|
||||
let out = Tensor::zeros(x.shape(), x.dtype(), x.device());
|
||||
let out = Tensor::empty(x.shape(), x.dtype(), x.device());
|
||||
let n = x.numel() as i32;
|
||||
unsafe {
|
||||
match x.dtype() {
|
||||
@@ -26,7 +27,6 @@ fn dispatch_unary(x: &Tensor, f32_fn: unsafe extern "C" fn(*const c_void, *mut c
|
||||
_ => panic!("unsupported dtype"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ fn dispatch_binary(a: &Tensor, b: &Tensor,
|
||||
assert!(a.is_contiguous() && b.is_contiguous());
|
||||
assert!(matches!(a.device(), Device::Cuda(_)));
|
||||
assert_eq!(a.dtype(), b.dtype());
|
||||
let out = Tensor::zeros(a.shape(), a.dtype(), a.device());
|
||||
let out = Tensor::empty(a.shape(), a.dtype(), a.device());
|
||||
let n = a.numel() as i32;
|
||||
unsafe {
|
||||
match a.dtype() {
|
||||
@@ -46,7 +46,6 @@ fn dispatch_binary(a: &Tensor, b: &Tensor,
|
||||
_ => panic!("unsupported dtype"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -55,7 +54,7 @@ pub fn silu(x: &Tensor) -> Tensor { dispatch_unary(x, launch_silu_f32, launch_si
|
||||
|
||||
pub fn scale(x: &Tensor, scale_val: f32) -> Tensor {
|
||||
assert!(x.is_contiguous() && matches!(x.device(), Device::Cuda(_)));
|
||||
let out = Tensor::zeros(x.shape(), x.dtype(), x.device());
|
||||
let out = Tensor::empty(x.shape(), x.dtype(), x.device());
|
||||
let n = x.numel() as i32;
|
||||
unsafe {
|
||||
match x.dtype() {
|
||||
@@ -64,9 +63,29 @@ pub fn scale(x: &Tensor, scale_val: f32) -> Tensor {
|
||||
_ => panic!("unsupported dtype for scale"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
pub fn add(a: &Tensor, b: &Tensor) -> Tensor { dispatch_binary(a, b, launch_add_f32, launch_add_bf16) }
|
||||
pub fn mul(a: &Tensor, b: &Tensor) -> Tensor { dispatch_binary(a, b, launch_mul_f32, launch_mul_bf16) }
|
||||
|
||||
/// Fused SiLU×Mul: out = silu(gate) * up (BF16 only)
|
||||
/// Saves one HBM read + one HBM write compared to separate silu + mul.
|
||||
pub fn silu_mul(gate: &Tensor, up: &Tensor) -> Tensor {
|
||||
assert_eq!(gate.shape(), up.shape());
|
||||
assert!(gate.is_contiguous() && up.is_contiguous());
|
||||
assert!(matches!(gate.device(), Device::Cuda(_)));
|
||||
assert_eq!(gate.dtype(), DType::BF16, "silu_mul requires BF16");
|
||||
let out = Tensor::empty(gate.shape(), gate.dtype(), gate.device());
|
||||
let n = gate.numel() as i32;
|
||||
unsafe {
|
||||
launch_silu_mul_bf16(
|
||||
gate.data_ptr() as *const c_void,
|
||||
up.data_ptr() as *const c_void,
|
||||
out.data_ptr() as *mut c_void,
|
||||
n,
|
||||
std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
@@ -10,6 +10,18 @@ unsafe extern "C" {
|
||||
offset: i32, stream: *mut c_void);
|
||||
fn launch_causal_mask_bf16(scores: *mut c_void, batch: i32, rows: i32, cols: i32,
|
||||
offset: i32, stream: *mut c_void);
|
||||
fn launch_flash_attention_bf16(
|
||||
q: *const c_void, k: *const c_void, v: *const c_void, o: *mut c_void,
|
||||
batch: i32, num_q_heads: i32, num_kv_heads: i32,
|
||||
q_len: i32, kv_len: i32, head_dim: i32,
|
||||
scale: f32, causal: i32, stream: *mut c_void,
|
||||
);
|
||||
fn launch_decode_attention_bf16(
|
||||
q: *const c_void, k: *const c_void, v: *const c_void, o: *mut c_void,
|
||||
batch: i32, num_q_heads: i32, num_kv_heads: i32,
|
||||
kv_len: i32, head_dim: i32,
|
||||
scale: f32, causal: i32, stream: *mut c_void,
|
||||
);
|
||||
}
|
||||
|
||||
fn apply_causal_mask(scores: &Tensor, offset: usize) {
|
||||
@@ -33,7 +45,6 @@ fn apply_causal_mask(scores: &Tensor, offset: usize) {
|
||||
_ => panic!("unsupported dtype for causal mask"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
|
||||
/// Multi-head attention (naive, materializes S×S score matrix).
|
||||
@@ -75,3 +86,109 @@ pub fn attention(q: &Tensor, k: &Tensor, v: &Tensor, causal: bool) -> Tensor {
|
||||
// output = weights @ V → [B, H, q_len, head_dim]
|
||||
batched_matmul(&weights, v)
|
||||
}
|
||||
|
||||
/// Decode Attention — optimized for single-token decode (q_len=1).
|
||||
///
|
||||
/// q: [batch, num_q_heads, 1, head_dim] BF16, contiguous, GPU
|
||||
/// k: [batch, num_kv_heads, kv_len, head_dim] BF16, contiguous, GPU
|
||||
/// v: [batch, num_kv_heads, kv_len, head_dim] BF16, contiguous, GPU
|
||||
///
|
||||
/// Returns: [batch, num_q_heads, 1, head_dim] BF16
|
||||
pub fn decode_attention(q: &Tensor, k: &Tensor, v: &Tensor) -> Tensor {
|
||||
assert_eq!(q.ndim(), 4);
|
||||
assert_eq!(q.shape()[2], 1, "decode_attention requires q_len == 1");
|
||||
|
||||
let batch = q.shape()[0];
|
||||
let num_q_heads = q.shape()[1];
|
||||
let head_dim = q.shape()[3];
|
||||
let num_kv_heads = k.shape()[1];
|
||||
let kv_len = k.shape()[2];
|
||||
|
||||
let scale = 1.0 / (head_dim as f32).sqrt();
|
||||
let output = Tensor::empty(
|
||||
&[batch, num_q_heads, 1, head_dim],
|
||||
DType::BF16,
|
||||
q.device(),
|
||||
);
|
||||
|
||||
unsafe {
|
||||
launch_decode_attention_bf16(
|
||||
q.data_ptr() as *const c_void,
|
||||
k.data_ptr() as *const c_void,
|
||||
v.data_ptr() as *const c_void,
|
||||
output.data_ptr() as *mut c_void,
|
||||
batch as i32,
|
||||
num_q_heads as i32,
|
||||
num_kv_heads as i32,
|
||||
kv_len as i32,
|
||||
head_dim as i32,
|
||||
scale,
|
||||
1, // causal (always 1 for decode)
|
||||
std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
/// Flash Attention 2 — O(1) extra memory, supports GQA natively.
|
||||
/// Auto-dispatches to decode_attention when q_len == 1.
|
||||
///
|
||||
/// q: [batch, num_q_heads, q_len, head_dim] BF16, contiguous, GPU
|
||||
/// k: [batch, num_kv_heads, kv_len, head_dim] BF16, contiguous, GPU
|
||||
/// v: [batch, num_kv_heads, kv_len, head_dim] BF16, contiguous, GPU
|
||||
///
|
||||
/// Returns: [batch, num_q_heads, q_len, head_dim] BF16
|
||||
pub fn flash_attention(q: &Tensor, k: &Tensor, v: &Tensor, causal: bool) -> Tensor {
|
||||
assert_eq!(q.ndim(), 4);
|
||||
assert_eq!(k.ndim(), 4);
|
||||
assert_eq!(v.ndim(), 4);
|
||||
assert!(q.is_contiguous() && k.is_contiguous() && v.is_contiguous());
|
||||
assert_eq!(q.dtype(), DType::BF16, "flash_attention requires BF16");
|
||||
assert_eq!(k.dtype(), DType::BF16);
|
||||
assert_eq!(v.dtype(), DType::BF16);
|
||||
|
||||
let batch = q.shape()[0];
|
||||
let num_q_heads = q.shape()[1];
|
||||
let q_len = q.shape()[2];
|
||||
let head_dim = q.shape()[3];
|
||||
let num_kv_heads = k.shape()[1];
|
||||
let kv_len = k.shape()[2];
|
||||
|
||||
assert_eq!(k.shape(), &[batch, num_kv_heads, kv_len, head_dim]);
|
||||
assert_eq!(v.shape(), &[batch, num_kv_heads, kv_len, head_dim]);
|
||||
assert!(num_q_heads % num_kv_heads == 0, "num_q_heads must be divisible by num_kv_heads");
|
||||
assert!(head_dim <= 128, "flash_attention supports head_dim up to 128");
|
||||
|
||||
// Dispatch to specialized decode kernel for single-token generation
|
||||
if q_len == 1 {
|
||||
return decode_attention(q, k, v);
|
||||
}
|
||||
|
||||
let scale = 1.0 / (head_dim as f32).sqrt();
|
||||
let output = Tensor::empty(
|
||||
&[batch, num_q_heads, q_len, head_dim],
|
||||
DType::BF16,
|
||||
q.device(),
|
||||
);
|
||||
|
||||
unsafe {
|
||||
launch_flash_attention_bf16(
|
||||
q.data_ptr() as *const c_void,
|
||||
k.data_ptr() as *const c_void,
|
||||
v.data_ptr() as *const c_void,
|
||||
output.data_ptr() as *mut c_void,
|
||||
batch as i32,
|
||||
num_q_heads as i32,
|
||||
num_kv_heads as i32,
|
||||
q_len as i32,
|
||||
kv_len as i32,
|
||||
head_dim as i32,
|
||||
scale,
|
||||
if causal { 1 } else { 0 },
|
||||
std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
@@ -26,10 +26,10 @@ pub fn embedding(table: &Tensor, token_ids: &[u32]) -> Tensor {
|
||||
num_tokens * std::mem::size_of::<u32>(),
|
||||
)
|
||||
};
|
||||
let mut ids_gpu = GpuBuffer::alloc(ids_bytes.len()).expect("alloc token_ids");
|
||||
let mut ids_gpu = xserv_cuda::allocator::cached_alloc(ids_bytes.len()).expect("alloc token_ids");
|
||||
ids_gpu.copy_from_host(ids_bytes).unwrap();
|
||||
|
||||
let out = Tensor::zeros(&[num_tokens, hidden_size], table.dtype(), table.device());
|
||||
let out = Tensor::empty(&[num_tokens, hidden_size], table.dtype(), table.device());
|
||||
|
||||
unsafe {
|
||||
match table.dtype() {
|
||||
@@ -46,6 +46,5 @@ pub fn embedding(table: &Tensor, token_ids: &[u32]) -> Tensor {
|
||||
_ => panic!("unsupported dtype for embedding"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::c_void;
|
||||
use xserv_cuda::error::{self, Result};
|
||||
use xserv_tensor::{DType, Device, Tensor};
|
||||
@@ -15,6 +16,7 @@ unsafe extern "C" {
|
||||
fn launch_gemm_naive_bf16(a: *const c_void, b: *const c_void, c: *mut c_void, m: i32, n: i32, k: i32, stream: *mut c_void);
|
||||
fn launch_gemm_tiled_f32(a: *const c_void, b: *const c_void, c: *mut c_void, m: i32, n: i32, k: i32, stream: *mut c_void);
|
||||
fn launch_gemm_tiled_bf16(a: *const c_void, b: *const c_void, c: *mut c_void, m: i32, n: i32, k: i32, stream: *mut c_void);
|
||||
fn launch_gemv_bf16(x: *const c_void, w: *const c_void, y_bf16: *mut c_void, y_fp32_buf: *mut c_void, k: i32, n: i32, stream: *mut c_void);
|
||||
}
|
||||
|
||||
// --- FFI: cuBLAS ---
|
||||
@@ -81,6 +83,23 @@ impl Drop for CublasContext {
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static CUBLAS_CTX: RefCell<CublasContext> = RefCell::new(
|
||||
CublasContext::new().expect("failed to create thread-local cuBLAS handle")
|
||||
);
|
||||
}
|
||||
|
||||
/// Borrow the thread-local cuBLAS handle for the duration of a closure.
|
||||
fn with_cublas<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(CublasHandle) -> R,
|
||||
{
|
||||
CUBLAS_CTX.with(|cell| {
|
||||
let ctx = cell.borrow();
|
||||
f(ctx.handle)
|
||||
})
|
||||
}
|
||||
|
||||
/// Matrix multiplication: C = A @ B
|
||||
/// A: [M, K], B: [K, N], C: [M, N]
|
||||
/// All tensors must be contiguous and on the same GPU.
|
||||
@@ -97,7 +116,9 @@ pub fn matmul(a: &Tensor, b: &Tensor, backend: GemmBackend) -> Tensor {
|
||||
let n = b.shape()[1];
|
||||
let dtype = a.dtype();
|
||||
|
||||
let c = Tensor::zeros(&[m, n], dtype, a.device());
|
||||
// All backends (naive, tiled, cuBLAS with beta=0, custom GEMV) fully
|
||||
// overwrite every element of C, so we skip the cudaMemset.
|
||||
let c = Tensor::empty(&[m, n], dtype, a.device());
|
||||
|
||||
let a_ptr = a.data_ptr() as *const c_void;
|
||||
let b_ptr = b.data_ptr() as *const c_void;
|
||||
@@ -113,7 +134,6 @@ pub fn matmul(a: &Tensor, b: &Tensor, backend: GemmBackend) -> Tensor {
|
||||
_ => panic!("unsupported dtype for naive GEMM"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
GemmBackend::Tiled => {
|
||||
unsafe {
|
||||
@@ -123,40 +143,51 @@ pub fn matmul(a: &Tensor, b: &Tensor, backend: GemmBackend) -> Tensor {
|
||||
_ => panic!("unsupported dtype for tiled GEMM"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
GemmBackend::CuBlas => {
|
||||
// cuBLAS uses column-major, but we have row-major tensors.
|
||||
// Trick: compute C^T = B^T @ A^T, which gives us C in row-major.
|
||||
// cuBLAS sees our row-major data as column-major transposed.
|
||||
let ctx = CublasContext::new().unwrap();
|
||||
let alpha = 1.0f32;
|
||||
let beta = 0.0f32;
|
||||
// Fast path: custom GEMV for M=1 BF16 (bandwidth-optimal decode)
|
||||
if m == 1 && dtype == DType::BF16 {
|
||||
let mut fp32_buf = xserv_cuda::allocator::cached_alloc(n * 4).unwrap();
|
||||
unsafe {
|
||||
launch_gemv_bf16(
|
||||
a_ptr, b_ptr, c_ptr,
|
||||
fp32_buf.as_mut_ptr() as *mut c_void,
|
||||
k as i32, n as i32,
|
||||
null_stream,
|
||||
);
|
||||
}
|
||||
// fp32_buf returned to caching allocator pool on drop
|
||||
} else {
|
||||
// cuBLAS uses column-major, but we have row-major tensors.
|
||||
// Trick: compute C^T = B^T @ A^T, which gives us C in row-major.
|
||||
// cuBLAS sees our row-major data as column-major transposed.
|
||||
let alpha = 1.0f32;
|
||||
let beta = 0.0f32;
|
||||
|
||||
let (a_type, b_type, c_type) = match dtype {
|
||||
DType::F32 => (CUDA_R_32F, CUDA_R_32F, CUDA_R_32F),
|
||||
DType::BF16 => (CUDA_R_16BF, CUDA_R_16BF, CUDA_R_16BF),
|
||||
_ => panic!("unsupported dtype for cuBLAS GEMM"),
|
||||
};
|
||||
let (a_type, b_type, c_type) = match dtype {
|
||||
DType::F32 => (CUDA_R_32F, CUDA_R_32F, CUDA_R_32F),
|
||||
DType::BF16 => (CUDA_R_16BF, CUDA_R_16BF, CUDA_R_16BF),
|
||||
_ => panic!("unsupported dtype for cuBLAS GEMM"),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
cublasSetStream_v2(ctx.handle, null_stream);
|
||||
// Row-major trick: swap A/B and transpose flags
|
||||
// C(row-major) = A @ B <=> C^T(col-major) = B^T @ A^T
|
||||
error::check(cublasGemmEx(
|
||||
ctx.handle,
|
||||
CUBLAS_OP_N, CUBLAS_OP_N,
|
||||
n as i32, m as i32, k as i32,
|
||||
&alpha as *const f32 as *const c_void,
|
||||
b_ptr, b_type, n as i32, // B as col-major = B^T
|
||||
a_ptr, a_type, k as i32, // A as col-major = A^T
|
||||
&beta as *const f32 as *const c_void,
|
||||
c_ptr, c_type, n as i32, // C as col-major = C^T
|
||||
CUBLAS_COMPUTE_32F,
|
||||
-1, // default algo
|
||||
)).expect("cuBLAS GEMM failed");
|
||||
with_cublas(|handle| unsafe {
|
||||
cublasSetStream_v2(handle, null_stream);
|
||||
// Row-major trick: swap A/B and transpose flags
|
||||
// C(row-major) = A @ B <=> C^T(col-major) = B^T @ A^T
|
||||
error::check(cublasGemmEx(
|
||||
handle,
|
||||
CUBLAS_OP_N, CUBLAS_OP_N,
|
||||
n as i32, m as i32, k as i32,
|
||||
&alpha as *const f32 as *const c_void,
|
||||
b_ptr, b_type, n as i32, // B as col-major = B^T
|
||||
a_ptr, a_type, k as i32, // A as col-major = A^T
|
||||
&beta as *const f32 as *const c_void,
|
||||
c_ptr, c_type, n as i32, // C as col-major = C^T
|
||||
CUBLAS_COMPUTE_32F,
|
||||
-1, // default algo
|
||||
)).expect("cuBLAS GEMM failed");
|
||||
});
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +221,8 @@ pub fn batched_matmul(a: &Tensor, b: &Tensor) -> Tensor {
|
||||
let mut out_shape: Vec<usize> = a.shape()[..ndim - 2].to_vec();
|
||||
out_shape.push(m);
|
||||
out_shape.push(n);
|
||||
let c = Tensor::zeros(&out_shape, a.dtype(), a.device());
|
||||
// cuBLAS with beta=0 fully overwrites every element of C.
|
||||
let c = Tensor::empty(&out_shape, a.dtype(), a.device());
|
||||
|
||||
let dtype = a.dtype();
|
||||
let (a_type, b_type, c_type) = match dtype {
|
||||
@@ -206,12 +238,11 @@ pub fn batched_matmul(a: &Tensor, b: &Tensor) -> Tensor {
|
||||
let stride_b = (k * n) as i64;
|
||||
let stride_c = (m * n) as i64;
|
||||
|
||||
let ctx = CublasContext::new().unwrap();
|
||||
unsafe {
|
||||
cublasSetStream_v2(ctx.handle, std::ptr::null_mut());
|
||||
with_cublas(|handle| unsafe {
|
||||
cublasSetStream_v2(handle, std::ptr::null_mut());
|
||||
// Row-major trick: C = A @ B ⟺ C^T = B^T @ A^T (col-major)
|
||||
error::check(cublasGemmStridedBatchedEx(
|
||||
ctx.handle,
|
||||
handle,
|
||||
CUBLAS_OP_N, CUBLAS_OP_N,
|
||||
n as i32, m as i32, k as i32,
|
||||
&alpha as *const f32 as *const c_void,
|
||||
@@ -223,7 +254,6 @@ pub fn batched_matmul(a: &Tensor, b: &Tensor) -> Tensor {
|
||||
CUBLAS_COMPUTE_32F,
|
||||
-1,
|
||||
)).expect("cuBLAS batched GEMM failed");
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
});
|
||||
c
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ pub fn layernorm(x: &Tensor, gamma: &Tensor, beta: &Tensor, eps: f32) -> Tensor
|
||||
assert_eq!(beta.shape(), &[hidden_size]);
|
||||
|
||||
let rows = x.numel() / hidden_size;
|
||||
let out = Tensor::zeros(x.shape(), x.dtype(), x.device());
|
||||
let out = Tensor::empty(x.shape(), x.dtype(), x.device());
|
||||
|
||||
unsafe {
|
||||
match x.dtype() {
|
||||
@@ -34,6 +34,5 @@ pub fn layernorm(x: &Tensor, gamma: &Tensor, beta: &Tensor, eps: f32) -> Tensor
|
||||
_ => panic!("unsupported dtype for layernorm"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -8,12 +8,17 @@ pub mod rope;
|
||||
pub mod softmax;
|
||||
pub mod transpose;
|
||||
|
||||
pub use activation::{add, gelu, mul, scale, silu};
|
||||
pub use transpose::{merge_heads_gpu, repeat_kv_gpu, reshape_heads_gpu, transpose_for_rope_gpu, transpose_from_rope_gpu};
|
||||
pub use attention::attention;
|
||||
pub use activation::{add, gelu, mul, scale, silu, silu_mul};
|
||||
pub use transpose::{merge_heads_gpu, repeat_kv_gpu, reshape_heads_gpu, strided_to_contiguous_gpu, transpose_for_rope_gpu, transpose_from_rope_gpu};
|
||||
pub use attention::{attention, decode_attention, flash_attention};
|
||||
pub use embedding::embedding;
|
||||
pub use gemm::{batched_matmul, matmul, GemmBackend};
|
||||
pub use layernorm::layernorm;
|
||||
pub use rmsnorm::rmsnorm;
|
||||
pub use rmsnorm::{add_rmsnorm, rmsnorm};
|
||||
pub use rope::{rope_inplace, RopeCache};
|
||||
pub use softmax::softmax;
|
||||
|
||||
/// Register GPU kernels with the tensor crate. Call once at startup.
|
||||
pub fn init() {
|
||||
xserv_tensor::register_gpu_contiguous(strided_to_contiguous_gpu);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ unsafe extern "C" {
|
||||
rows: i32, hidden_size: i32, eps: f32, stream: *mut c_void);
|
||||
fn launch_rmsnorm_bf16(x: *const c_void, gamma: *const c_void, out: *mut c_void,
|
||||
rows: i32, hidden_size: i32, eps: f32, stream: *mut c_void);
|
||||
fn launch_add_rmsnorm_bf16(x: *const c_void, residual: *const c_void, gamma: *const c_void,
|
||||
normed_out: *mut c_void, sum_out: *mut c_void,
|
||||
rows: i32, hidden_size: i32, eps: f32, stream: *mut c_void);
|
||||
}
|
||||
|
||||
pub fn rmsnorm(x: &Tensor, gamma: &Tensor, eps: f32) -> Tensor {
|
||||
@@ -17,7 +20,7 @@ pub fn rmsnorm(x: &Tensor, gamma: &Tensor, eps: f32) -> Tensor {
|
||||
assert_eq!(x.dtype(), gamma.dtype());
|
||||
|
||||
let rows = x.numel() / hidden_size;
|
||||
let out = Tensor::zeros(x.shape(), x.dtype(), x.device());
|
||||
let out = Tensor::empty(x.shape(), x.dtype(), x.device());
|
||||
|
||||
unsafe {
|
||||
match x.dtype() {
|
||||
@@ -32,6 +35,41 @@ pub fn rmsnorm(x: &Tensor, gamma: &Tensor, eps: f32) -> Tensor {
|
||||
_ => panic!("unsupported dtype for rmsnorm"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
/// Fused Add + RMSNorm: computes sum = x + residual, then normed = rmsnorm(sum, gamma, eps).
|
||||
/// Returns (normed, sum). BF16 only.
|
||||
/// Saves one kernel launch and one full HBM round-trip per layer.
|
||||
pub fn add_rmsnorm(x: &Tensor, residual: &Tensor, gamma: &Tensor, eps: f32) -> (Tensor, Tensor) {
|
||||
assert!(x.ndim() >= 1);
|
||||
assert_eq!(x.shape(), residual.shape());
|
||||
assert!(x.is_contiguous() && residual.is_contiguous() && gamma.is_contiguous());
|
||||
assert!(matches!(x.device(), Device::Cuda(_)));
|
||||
assert_eq!(x.dtype(), DType::BF16, "add_rmsnorm requires BF16");
|
||||
assert_eq!(residual.dtype(), DType::BF16);
|
||||
assert_eq!(gamma.dtype(), DType::BF16);
|
||||
|
||||
let hidden_size = *x.shape().last().unwrap();
|
||||
assert_eq!(gamma.shape(), &[hidden_size]);
|
||||
|
||||
let rows = x.numel() / hidden_size;
|
||||
let normed_out = Tensor::empty(x.shape(), DType::BF16, x.device());
|
||||
let sum_out = Tensor::empty(x.shape(), DType::BF16, x.device());
|
||||
|
||||
unsafe {
|
||||
launch_add_rmsnorm_bf16(
|
||||
x.data_ptr() as *const c_void,
|
||||
residual.data_ptr() as *const c_void,
|
||||
gamma.data_ptr() as *const c_void,
|
||||
normed_out.data_ptr() as *mut c_void,
|
||||
sum_out.data_ptr() as *mut c_void,
|
||||
rows as i32,
|
||||
hidden_size as i32,
|
||||
eps,
|
||||
std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
|
||||
(normed_out, sum_out)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ impl RopeCache {
|
||||
max_seq_len as i32, half_dim as i32, theta, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
|
||||
Self { cos, sin, max_seq_len, half_dim }
|
||||
}
|
||||
@@ -59,7 +58,7 @@ pub fn rope_inplace(x: &Tensor, cache: &RopeCache, positions: &[u32]) {
|
||||
num_tokens * std::mem::size_of::<u32>(),
|
||||
)
|
||||
};
|
||||
let mut pos_gpu = GpuBuffer::alloc(pos_bytes.len()).expect("alloc positions");
|
||||
let mut pos_gpu = xserv_cuda::allocator::cached_alloc(pos_bytes.len()).expect("alloc positions");
|
||||
pos_gpu.copy_from_host(pos_bytes).unwrap();
|
||||
|
||||
unsafe {
|
||||
@@ -81,5 +80,4 @@ pub fn rope_inplace(x: &Tensor, cache: &RopeCache, positions: &[u32]) {
|
||||
_ => panic!("unsupported dtype for rope"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn softmax(x: &Tensor) -> Tensor {
|
||||
|
||||
let cols = *x.shape().last().unwrap();
|
||||
let rows = x.numel() / cols;
|
||||
let out = Tensor::zeros(x.shape(), x.dtype(), x.device());
|
||||
let out = Tensor::empty(x.shape(), x.dtype(), x.device());
|
||||
|
||||
unsafe {
|
||||
match x.dtype() {
|
||||
@@ -29,6 +29,5 @@ pub fn softmax(x: &Tensor) -> Tensor {
|
||||
_ => panic!("unsupported dtype for softmax"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -7,20 +7,27 @@ unsafe extern "C" {
|
||||
fn launch_transpose_hsd_to_shd_bf16(inp: *const c_void, out: *mut c_void, seq_len: i32, num_heads: i32, head_dim: i32, stream: *mut c_void);
|
||||
fn launch_transpose_shd_to_hsd_bf16(inp: *const c_void, out: *mut c_void, seq_len: i32, num_heads: i32, head_dim: i32, stream: *mut c_void);
|
||||
fn launch_repeat_kv_bf16(inp: *const c_void, out: *mut c_void, kv_heads: i32, n_rep: i32, seq_len: i32, head_dim: i32, stream: *mut c_void);
|
||||
fn launch_strided_copy_bf16(inp: *const c_void, out: *mut c_void, numel: i32, ndim: i32,
|
||||
shape0: i32, shape1: i32, shape2: i32, shape3: i32,
|
||||
in_stride0: i32, in_stride1: i32, in_stride2: i32, in_stride3: i32,
|
||||
in_offset: i32, stream: *mut c_void);
|
||||
fn launch_strided_copy_f32(inp: *const c_void, out: *mut c_void, numel: i32, ndim: i32,
|
||||
shape0: i32, shape1: i32, shape2: i32, shape3: i32,
|
||||
in_stride0: i32, in_stride1: i32, in_stride2: i32, in_stride3: i32,
|
||||
in_offset: i32, stream: *mut c_void);
|
||||
}
|
||||
|
||||
/// [S, H*D] → [1, H, S, D] on GPU (BF16)
|
||||
pub fn reshape_heads_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim: usize) -> Tensor {
|
||||
assert_eq!(x.dtype(), DType::BF16);
|
||||
assert!(x.is_contiguous() && matches!(x.device(), Device::Cuda(_)));
|
||||
let out = Tensor::zeros(&[1, num_heads, seq_len, head_dim], DType::BF16, x.device());
|
||||
let out = Tensor::empty(&[1, num_heads, seq_len, head_dim], DType::BF16, x.device());
|
||||
unsafe {
|
||||
launch_reshape_heads_bf16(
|
||||
x.data_ptr() as _, out.data_ptr() as *mut c_void,
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -29,14 +36,13 @@ pub fn merge_heads_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim: u
|
||||
assert_eq!(x.dtype(), DType::BF16);
|
||||
assert!(x.is_contiguous() && matches!(x.device(), Device::Cuda(_)));
|
||||
let hidden = num_heads * head_dim;
|
||||
let out = Tensor::zeros(&[seq_len, hidden], DType::BF16, x.device());
|
||||
let out = Tensor::empty(&[seq_len, hidden], DType::BF16, x.device());
|
||||
unsafe {
|
||||
launch_merge_heads_bf16(
|
||||
x.data_ptr() as _, out.data_ptr() as *mut c_void,
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -44,14 +50,13 @@ pub fn merge_heads_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim: u
|
||||
pub fn transpose_for_rope_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim: usize) -> Tensor {
|
||||
assert_eq!(x.dtype(), DType::BF16);
|
||||
assert!(x.is_contiguous() && matches!(x.device(), Device::Cuda(_)));
|
||||
let out = Tensor::zeros(&[seq_len, num_heads, head_dim], DType::BF16, x.device());
|
||||
let out = Tensor::empty(&[seq_len, num_heads, head_dim], DType::BF16, x.device());
|
||||
unsafe {
|
||||
launch_transpose_hsd_to_shd_bf16(
|
||||
x.data_ptr() as _, out.data_ptr() as *mut c_void,
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -59,14 +64,13 @@ pub fn transpose_for_rope_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head
|
||||
pub fn transpose_from_rope_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim: usize) -> Tensor {
|
||||
assert_eq!(x.dtype(), DType::BF16);
|
||||
assert!(x.is_contiguous() && matches!(x.device(), Device::Cuda(_)));
|
||||
let out = Tensor::zeros(&[1, num_heads, seq_len, head_dim], DType::BF16, x.device());
|
||||
let out = Tensor::empty(&[1, num_heads, seq_len, head_dim], DType::BF16, x.device());
|
||||
unsafe {
|
||||
launch_transpose_shd_to_hsd_bf16(
|
||||
x.data_ptr() as _, out.data_ptr() as *mut c_void,
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -79,13 +83,60 @@ pub fn repeat_kv_gpu(x: &Tensor, n_rep: usize) -> Tensor {
|
||||
let seq_len = x.shape()[2];
|
||||
let head_dim = x.shape()[3];
|
||||
let new_heads = kv_heads * n_rep;
|
||||
let out = Tensor::zeros(&[1, new_heads, seq_len, head_dim], DType::BF16, x.device());
|
||||
let out = Tensor::empty(&[1, new_heads, seq_len, head_dim], DType::BF16, x.device());
|
||||
unsafe {
|
||||
launch_repeat_kv_bf16(
|
||||
x.data_ptr() as _, out.data_ptr() as *mut c_void,
|
||||
kv_heads as i32, n_rep as i32, seq_len as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
/// Make a non-contiguous GPU tensor contiguous via a strided copy kernel.
|
||||
/// Supports BF16 and F32, up to 4D tensors (padded to 4D internally).
|
||||
pub fn strided_to_contiguous_gpu(x: &Tensor) -> Tensor {
|
||||
assert!(matches!(x.device(), Device::Cuda(_)), "expected GPU tensor");
|
||||
assert!(!x.is_contiguous(), "tensor is already contiguous");
|
||||
assert!(x.ndim() <= 4, "strided_to_contiguous_gpu supports up to 4D");
|
||||
|
||||
let ndim = x.ndim();
|
||||
let numel = x.numel();
|
||||
|
||||
// Pad shape and strides to 4D (prepend 1s for shape, 0s for strides)
|
||||
let mut shape4 = [1i32; 4];
|
||||
let mut strides4 = [0i32; 4];
|
||||
let pad = 4 - ndim;
|
||||
for i in 0..ndim {
|
||||
shape4[pad + i] = x.shape()[i] as i32;
|
||||
strides4[pad + i] = x.strides()[i] as i32;
|
||||
}
|
||||
|
||||
let out = Tensor::empty(x.shape(), x.dtype(), x.device());
|
||||
|
||||
// Use storage base pointer + element offset, because strides are relative to
|
||||
// element 0 of the storage, not the data_ptr() (which already adds byte offset).
|
||||
let storage_ptr = x.storage().gpu_buffer().as_ptr();
|
||||
let in_offset = x.offset() as i32;
|
||||
|
||||
unsafe {
|
||||
match x.dtype() {
|
||||
DType::BF16 => launch_strided_copy_bf16(
|
||||
storage_ptr as _, out.data_ptr() as *mut c_void,
|
||||
numel as i32, ndim as i32,
|
||||
shape4[0], shape4[1], shape4[2], shape4[3],
|
||||
strides4[0], strides4[1], strides4[2], strides4[3],
|
||||
in_offset, std::ptr::null_mut(),
|
||||
),
|
||||
DType::F32 => launch_strided_copy_f32(
|
||||
storage_ptr as _, out.data_ptr() as *mut c_void,
|
||||
numel as i32, ndim as i32,
|
||||
shape4[0], shape4[1], shape4[2], shape4[3],
|
||||
strides4[0], strides4[1], strides4[2], strides4[3],
|
||||
in_offset, std::ptr::null_mut(),
|
||||
),
|
||||
_ => panic!("strided_to_contiguous_gpu: unsupported dtype {:?}", x.dtype()),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
@@ -121,6 +121,20 @@ fn test_gemm_cublas_bf16_small() { run_gemm_test_bf16(GemmBackend::CuBlas, 4, 4,
|
||||
#[test]
|
||||
fn test_gemm_cublas_bf16_medium() { run_gemm_test_bf16(GemmBackend::CuBlas, 256, 256, 256); }
|
||||
|
||||
// --- Custom GEMV tests (M=1, BF16 fast path) ---
|
||||
|
||||
#[test]
|
||||
fn test_gemv_bf16_small() { run_gemm_test_bf16(GemmBackend::CuBlas, 1, 64, 64); }
|
||||
|
||||
#[test]
|
||||
fn test_gemv_bf16_medium() { run_gemm_test_bf16(GemmBackend::CuBlas, 1, 256, 256); }
|
||||
|
||||
#[test]
|
||||
fn test_gemv_bf16_4096() { run_gemm_test_bf16(GemmBackend::CuBlas, 1, 4096, 4096); }
|
||||
|
||||
#[test]
|
||||
fn test_gemv_bf16_rect() { run_gemm_test_bf16(GemmBackend::CuBlas, 1, 512, 4096); }
|
||||
|
||||
// --- Larger benchmark-style tests ---
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -13,3 +13,4 @@ smallvec.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
safetensors.workspace = true
|
||||
rand.workspace = true
|
||||
|
||||
@@ -31,7 +31,7 @@ fn main() {
|
||||
// Warmup
|
||||
{
|
||||
let ids = tokenizer.encode("warmup");
|
||||
let mut cache = GpuKVCache::new(&config, 256, DType::BF16);
|
||||
let mut cache = GpuKVCache::new(&config, 256, DType::BF16, 0);
|
||||
let _ = model.forward_gpu_cache(&ids, &mut cache);
|
||||
}
|
||||
eprintln!("Warmup done. Running benchmark...");
|
||||
@@ -94,7 +94,7 @@ fn main() {
|
||||
let input_ids = tokenizer.encode(prompt);
|
||||
let input_len = input_ids.len();
|
||||
|
||||
let mut cache = GpuKVCache::new(&config, 256, DType::BF16);
|
||||
let mut cache = GpuKVCache::new(&config, 256, DType::BF16, 0);
|
||||
|
||||
// Prefill
|
||||
let t0 = Instant::now();
|
||||
|
||||
@@ -116,6 +116,7 @@ fn tensor_from_raw_bytes(bytes: &[u8], shape: &[usize], dtype: DType) -> Tensor
|
||||
|
||||
impl GPT2 {
|
||||
pub fn from_weights(config: ModelConfig, mut w: HashMap<String, Tensor>) -> Self {
|
||||
crate::init_kernels();
|
||||
let take = |w: &mut HashMap<String, Tensor>, name: &str| -> Tensor {
|
||||
w.remove(name).unwrap_or_else(|| panic!("missing weight: {name}"))
|
||||
};
|
||||
|
||||
@@ -9,16 +9,22 @@ pub struct GpuKVCache {
|
||||
// Layout: [num_kv_heads, max_seq_len, head_dim] — contiguous per head
|
||||
k_bufs: Vec<GpuBuffer>,
|
||||
v_bufs: Vec<GpuBuffer>,
|
||||
// Per layer: pre-allocated staging buffers for get_kv_len output.
|
||||
// Size: num_kv_heads * max_seq_len * head_dim * elem_size (max possible output).
|
||||
// Avoids cudaMalloc/cudaFree on every get_kv_len call.
|
||||
k_staging: Vec<GpuBuffer>,
|
||||
v_staging: Vec<GpuBuffer>,
|
||||
seq_len: usize,
|
||||
max_seq_len: usize,
|
||||
num_kv_heads: usize,
|
||||
head_dim: usize,
|
||||
elem_size: usize,
|
||||
dtype: DType,
|
||||
device: u32,
|
||||
}
|
||||
|
||||
impl GpuKVCache {
|
||||
pub fn new(config: &ModelConfig, max_seq_len: usize, dtype: DType) -> Self {
|
||||
pub fn new(config: &ModelConfig, max_seq_len: usize, dtype: DType, device: u32) -> Self {
|
||||
let num_layers = config.num_layers();
|
||||
let num_kv_heads = config.num_kv_heads();
|
||||
let head_dim = config.head_dim();
|
||||
@@ -27,6 +33,8 @@ impl GpuKVCache {
|
||||
|
||||
let mut k_bufs = Vec::with_capacity(num_layers);
|
||||
let mut v_bufs = Vec::with_capacity(num_layers);
|
||||
let mut k_staging = Vec::with_capacity(num_layers);
|
||||
let mut v_staging = Vec::with_capacity(num_layers);
|
||||
for _ in 0..num_layers {
|
||||
let mut k = GpuBuffer::alloc(buf_size).expect("alloc KV cache K");
|
||||
let mut v = GpuBuffer::alloc(buf_size).expect("alloc KV cache V");
|
||||
@@ -34,9 +42,11 @@ impl GpuKVCache {
|
||||
v.zero().unwrap();
|
||||
k_bufs.push(k);
|
||||
v_bufs.push(v);
|
||||
k_staging.push(GpuBuffer::alloc(buf_size).expect("alloc KV staging K"));
|
||||
v_staging.push(GpuBuffer::alloc(buf_size).expect("alloc KV staging V"));
|
||||
}
|
||||
|
||||
Self { k_bufs, v_bufs, seq_len: 0, max_seq_len, num_kv_heads, head_dim, elem_size, dtype }
|
||||
Self { k_bufs, v_bufs, k_staging, v_staging, seq_len: 0, max_seq_len, num_kv_heads, head_dim, elem_size, dtype, device }
|
||||
}
|
||||
|
||||
pub fn seq_len(&self) -> usize { self.seq_len }
|
||||
@@ -69,45 +79,58 @@ impl GpuKVCache {
|
||||
}
|
||||
|
||||
/// Get K/V cache tensors for a layer up to `seq_len` tokens: [1, num_kv_heads, seq_len, head_dim]
|
||||
pub fn get_kv(&self, layer: usize) -> (Tensor, Tensor) {
|
||||
pub fn get_kv(&mut self, layer: usize) -> (Tensor, Tensor) {
|
||||
let sl = self.seq_len;
|
||||
self.get_kv_len(layer, sl)
|
||||
}
|
||||
|
||||
pub fn get_kv_len(&self, layer: usize, sl: usize) -> (Tensor, Tensor) {
|
||||
pub fn get_kv_len(&mut self, layer: usize, sl: usize) -> (Tensor, Tensor) {
|
||||
let hd = self.head_dim;
|
||||
let nh = self.num_kv_heads;
|
||||
let es = self.elem_size;
|
||||
let max_s = self.max_seq_len;
|
||||
|
||||
// Allocate output tensors [1, nh, sl, hd]
|
||||
// Copy each head's valid portion into pre-allocated staging buffers.
|
||||
// Split borrows: staging (mut) vs cache (shared) are separate struct fields,
|
||||
// so the borrow checker allows simultaneous &mut staging + &cache.
|
||||
let out_size = nh * sl * hd * es;
|
||||
let mut k_out = GpuBuffer::alloc(out_size).expect("alloc k_out");
|
||||
let mut v_out = GpuBuffer::alloc(out_size).expect("alloc v_out");
|
||||
|
||||
// Copy each head's valid portion
|
||||
let k_stg = &mut self.k_staging[layer];
|
||||
let k_buf = &self.k_bufs[layer];
|
||||
let v_stg = &mut self.v_staging[layer];
|
||||
let v_buf = &self.v_bufs[layer];
|
||||
for h in 0..nh {
|
||||
let src_off = (h * max_s) * hd * es;
|
||||
let dst_off = (h * sl) * hd * es;
|
||||
let count = sl * hd * es;
|
||||
k_out.copy_from_device_at(&self.k_bufs[layer], src_off, dst_off, count).unwrap();
|
||||
v_out.copy_from_device_at(&self.v_bufs[layer], src_off, dst_off, count).unwrap();
|
||||
k_stg.copy_from_device_at(k_buf, src_off, dst_off, count).unwrap();
|
||||
v_stg.copy_from_device_at(v_buf, src_off, dst_off, count).unwrap();
|
||||
}
|
||||
// Grab raw pointers before dropping the mutable borrows
|
||||
let k_ptr = k_stg.as_mut_ptr();
|
||||
let v_ptr = v_stg.as_mut_ptr();
|
||||
|
||||
// Create Tensors that borrow from the staging buffers (no cudaMalloc/cudaFree).
|
||||
// Safety: staging buffers are owned by GpuKVCache and outlive the returned Tensors
|
||||
// in practice (Tensors are consumed within the same forward pass before the next
|
||||
// get_kv_len call overwrites the staging buffer).
|
||||
let shape = &[1usize, nh, sl, hd];
|
||||
let k = unsafe { tensor_from_gpu_buffer(k_out, shape, self.dtype) };
|
||||
let v = unsafe { tensor_from_gpu_buffer(v_out, shape, self.dtype) };
|
||||
let k = unsafe {
|
||||
tensor_from_gpu_buffer(GpuBuffer::borrow_raw(k_ptr, out_size), shape, self.dtype, self.device)
|
||||
};
|
||||
let v = unsafe {
|
||||
tensor_from_gpu_buffer(GpuBuffer::borrow_raw(v_ptr, out_size), shape, self.dtype, self.device)
|
||||
};
|
||||
(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a Tensor from a GpuBuffer (takes ownership).
|
||||
unsafe fn tensor_from_gpu_buffer(buf: GpuBuffer, shape: &[usize], dtype: DType) -> Tensor {
|
||||
unsafe fn tensor_from_gpu_buffer(buf: GpuBuffer, shape: &[usize], dtype: DType, device: u32) -> Tensor {
|
||||
use xserv_tensor::storage::Storage;
|
||||
use xserv_tensor::shape::contiguous_strides;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
let storage = Storage::cuda(buf);
|
||||
let storage = Storage::cuda(buf, device);
|
||||
Tensor::from_storage(
|
||||
storage,
|
||||
SmallVec::from_slice(shape),
|
||||
@@ -116,3 +139,11 @@ unsafe fn tensor_from_gpu_buffer(buf: GpuBuffer, shape: &[usize], dtype: DType)
|
||||
dtype,
|
||||
)
|
||||
}
|
||||
|
||||
/// Public version for use by other modules (e.g., batched decode concat).
|
||||
///
|
||||
/// # Safety
|
||||
/// `buf` must be a valid GPU allocation with at least `product(shape) * dtype.size_bytes()` bytes.
|
||||
pub unsafe fn tensor_from_gpu_buffer_pub(buf: GpuBuffer, shape: &[usize], dtype: DType, device: u32) -> Tensor {
|
||||
tensor_from_gpu_buffer(buf, shape, dtype, device)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,16 @@ pub mod gpt2;
|
||||
pub mod kv_cache;
|
||||
pub mod loader;
|
||||
pub mod qwen3;
|
||||
pub mod sampling;
|
||||
|
||||
pub use config::ModelConfig;
|
||||
pub use gpt2::{GPT2, KVCache};
|
||||
pub use kv_cache::GpuKVCache;
|
||||
pub use qwen3::Qwen3;
|
||||
pub use sampling::{SamplingParams, sample};
|
||||
|
||||
/// Initialize GPU kernel hooks. Called automatically by model constructors,
|
||||
/// but safe to call multiple times (idempotent via OnceLock).
|
||||
pub fn init_kernels() {
|
||||
xserv_kernels::init();
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ struct Qwen3Block {
|
||||
|
||||
impl Qwen3 {
|
||||
pub fn from_weights(config: ModelConfig, mut w: HashMap<String, Tensor>) -> Self {
|
||||
crate::init_kernels();
|
||||
let take = |w: &mut HashMap<String, Tensor>, name: &str| -> Tensor {
|
||||
w.remove(name).unwrap_or_else(|| panic!("missing weight: {name}"))
|
||||
};
|
||||
@@ -41,7 +42,7 @@ impl Qwen3 {
|
||||
let lm_head_raw = take(&mut w, "lm_head.weight");
|
||||
|
||||
let rope_cache = RopeCache::new(
|
||||
config.max_seq_len().min(8192), // limit for memory
|
||||
config.max_seq_len(),
|
||||
config.head_dim(),
|
||||
config.rope_theta.unwrap_or(1_000_000.0) as f32,
|
||||
);
|
||||
@@ -147,6 +148,113 @@ impl Qwen3 {
|
||||
matmul_2d(&x, &self.lm_head_t)
|
||||
}
|
||||
|
||||
/// Batched decode: process one token per sequence simultaneously.
|
||||
/// All compute-heavy ops (projections, FFN) operate on [B, hidden] tensors.
|
||||
/// Per-sequence ops (RoPE, KV cache, attention) are handled individually.
|
||||
///
|
||||
/// tokens: one token per sequence (len = batch_size)
|
||||
/// positions: position offset for each sequence (len = batch_size)
|
||||
/// caches: one mutable KV cache per sequence (len = batch_size)
|
||||
///
|
||||
/// Returns logits: [batch_size, vocab_size]
|
||||
pub fn forward_decode_batch(
|
||||
&self,
|
||||
tokens: &[u32],
|
||||
positions: &[usize],
|
||||
caches: &mut [&mut GpuKVCache],
|
||||
) -> Tensor {
|
||||
let batch = tokens.len();
|
||||
assert_eq!(positions.len(), batch);
|
||||
assert_eq!(caches.len(), batch);
|
||||
assert!(batch > 0);
|
||||
|
||||
let num_heads = self.config.num_heads();
|
||||
let num_kv_heads = self.config.num_kv_heads();
|
||||
let head_dim = self.config.head_dim();
|
||||
let eps = self.config.rms_norm_eps.unwrap_or(1e-6) as f32;
|
||||
|
||||
// Batched embedding: [B, hidden]
|
||||
let mut x = embedding(&self.embed_tokens, tokens);
|
||||
|
||||
for (layer_idx, layer) in self.layers.iter().enumerate() {
|
||||
let residual = x.clone();
|
||||
let normed = rmsnorm(&x, &layer.input_norm, eps); // [B, hidden]
|
||||
|
||||
// Batched projections: [B, hidden] × [hidden, X] = [B, X]
|
||||
let q_all = matmul_2d(&normed, &layer.q_proj_wt); // [B, num_heads*head_dim]
|
||||
let k_all = matmul_2d(&normed, &layer.k_proj_wt); // [B, num_kv_heads*head_dim]
|
||||
let v_all = matmul_2d(&normed, &layer.v_proj_wt); // [B, num_kv_heads*head_dim]
|
||||
|
||||
// Per-sequence: reshape, qk-norm, RoPE, KV cache, attention, merge
|
||||
let mut attn_outputs: Vec<Tensor> = Vec::with_capacity(batch);
|
||||
for b in 0..batch {
|
||||
// Extract row b: [1, X] — view into contiguous [B, X]
|
||||
let q_row = row_view(&q_all, b); // [1, num_heads*head_dim]
|
||||
let k_row = row_view(&k_all, b); // [1, num_kv_heads*head_dim]
|
||||
let v_row = row_view(&v_all, b); // [1, num_kv_heads*head_dim]
|
||||
|
||||
// GPU reshape: [1, H*D] → [1, H, 1, D]
|
||||
let q = xserv_kernels::reshape_heads_gpu(&q_row, 1, num_heads, head_dim);
|
||||
let k = xserv_kernels::reshape_heads_gpu(&k_row, 1, num_kv_heads, head_dim);
|
||||
let v = xserv_kernels::reshape_heads_gpu(&v_row, 1, num_kv_heads, head_dim);
|
||||
|
||||
// QK norm
|
||||
let q = head_rmsnorm(&q, &layer.q_norm, eps);
|
||||
let k = head_rmsnorm(&k, &layer.k_norm, eps);
|
||||
|
||||
// GPU transpose for RoPE: [1, H, 1, D] → [1, H, D]
|
||||
let q = xserv_kernels::transpose_for_rope_gpu(&q, 1, num_heads, head_dim);
|
||||
let k = xserv_kernels::transpose_for_rope_gpu(&k, 1, num_kv_heads, head_dim);
|
||||
|
||||
// RoPE with per-sequence position
|
||||
let pos = [positions[b] as u32];
|
||||
rope_inplace(&q, &self.rope_cache, &pos);
|
||||
rope_inplace(&k, &self.rope_cache, &pos);
|
||||
|
||||
// Transpose back: [1, H, D] → [1, H, 1, D]
|
||||
let q = xserv_kernels::transpose_from_rope_gpu(&q, 1, num_heads, head_dim);
|
||||
let k = xserv_kernels::transpose_from_rope_gpu(&k, 1, num_kv_heads, head_dim);
|
||||
|
||||
// KV cache: append and get full cache
|
||||
let pos_b = positions[b];
|
||||
caches[b].append(layer_idx, &k, &v, 1, pos_b);
|
||||
let (k_full, v_full) = caches[b].get_kv_len(layer_idx, pos_b + 1);
|
||||
|
||||
// Decode attention (uses native GQA, no repeat_kv needed)
|
||||
let attn_out = flash_attention(&q, &k_full, &v_full, true);
|
||||
|
||||
// Merge heads: [1, H, 1, D] → [1, hidden]
|
||||
let merged = xserv_kernels::merge_heads_gpu(&attn_out, 1, num_heads, head_dim);
|
||||
attn_outputs.push(merged);
|
||||
}
|
||||
|
||||
// Concat attention outputs: [B, hidden]
|
||||
let attn_merged = concat_rows(&attn_outputs);
|
||||
|
||||
// Batched O projection: [B, hidden] × [hidden, hidden] = [B, hidden]
|
||||
let attn_proj = matmul_2d(&attn_merged, &layer.o_proj_wt);
|
||||
|
||||
// Fused add + rmsnorm
|
||||
let (normed, x_new) = xserv_kernels::add_rmsnorm(&attn_proj, &residual, &layer.post_norm, eps);
|
||||
let residual = x_new.clone();
|
||||
|
||||
// Batched FFN: all projections on [B, hidden]
|
||||
let gate = matmul_2d(&normed, &layer.gate_proj_wt);
|
||||
let up = matmul_2d(&normed, &layer.up_proj_wt);
|
||||
let hidden_states = xserv_kernels::silu_mul(&gate, &up);
|
||||
let down = matmul_2d(&hidden_states, &layer.down_proj_wt);
|
||||
x = add_any(&residual, &down);
|
||||
}
|
||||
|
||||
// Advance KV cache seq_len for each sequence
|
||||
for b in 0..batch {
|
||||
caches[b].advance_seq_len(1);
|
||||
}
|
||||
|
||||
let x = rmsnorm(&x, &self.norm, eps);
|
||||
matmul_2d(&x, &self.lm_head_t) // [B, vocab_size]
|
||||
}
|
||||
|
||||
/// Forward with GPU-resident KV cache and GPU transpose/reshape kernels.
|
||||
pub fn forward_gpu_cache(&self, token_ids: &[u32], cache: &mut GpuKVCache) -> Tensor {
|
||||
let new_tokens = token_ids.len();
|
||||
@@ -190,23 +298,20 @@ impl Qwen3 {
|
||||
cache.append(layer_idx, &k, &v, new_tokens, pos_offset);
|
||||
let (k_full, v_full) = cache.get_kv_len(layer_idx, pos_offset + new_tokens);
|
||||
|
||||
// GPU repeat KV for GQA
|
||||
let n_rep = num_heads / num_kv_heads;
|
||||
let k_full = xserv_kernels::repeat_kv_gpu(&k_full, n_rep);
|
||||
let v_full = xserv_kernels::repeat_kv_gpu(&v_full, n_rep);
|
||||
|
||||
let attn_out = attention(&q, &k_full, &v_full, true);
|
||||
// Flash Attention with native GQA (no repeat_kv needed)
|
||||
let attn_out = flash_attention(&q, &k_full, &v_full, true);
|
||||
// GPU merge_heads: [1, H, S, D] → [S, H*D]
|
||||
let attn_merged = xserv_kernels::merge_heads_gpu(&attn_out, new_tokens, num_heads, head_dim);
|
||||
let attn_proj = matmul_2d(&attn_merged, &layer.o_proj_wt);
|
||||
x = add_any(&residual, &attn_proj);
|
||||
|
||||
let residual = x.clone();
|
||||
let normed = rmsnorm(&x, &layer.post_norm, eps);
|
||||
// Fused add + rmsnorm: (normed, x) where x = residual + attn_proj
|
||||
let (normed, x_new) = xserv_kernels::add_rmsnorm(&attn_proj, &residual, &layer.post_norm, eps);
|
||||
let residual = x_new.clone();
|
||||
|
||||
// Fused SiLU×Mul
|
||||
let gate = matmul_2d(&normed, &layer.gate_proj_wt);
|
||||
let up = matmul_2d(&normed, &layer.up_proj_wt);
|
||||
let gate_activated = silu(&gate);
|
||||
let hidden_states = mul_any(&gate_activated, &up);
|
||||
let hidden_states = xserv_kernels::silu_mul(&gate, &up);
|
||||
let down = matmul_2d(&hidden_states, &layer.down_proj_wt);
|
||||
x = add_any(&residual, &down);
|
||||
}
|
||||
@@ -319,6 +424,53 @@ fn repeat_kv(x: &Tensor, n_rep: usize) -> Tensor {
|
||||
Tensor::from_slice(&out, &[1, new_heads, seq_len, head_dim]).to_device(x.device())
|
||||
}
|
||||
|
||||
/// Extract row `b` from a contiguous 2D tensor [B, cols] as a [1, cols] view.
|
||||
/// Zero-copy: shares storage with the original tensor.
|
||||
fn row_view(t: &Tensor, row: usize) -> Tensor {
|
||||
assert_eq!(t.ndim(), 2);
|
||||
assert!(t.is_contiguous());
|
||||
let cols = t.shape()[1];
|
||||
assert!(row < t.shape()[0]);
|
||||
let new_offset = t.offset() + row * cols;
|
||||
Tensor::from_storage(
|
||||
t.storage().clone(),
|
||||
smallvec::SmallVec::from_slice(&[1, cols]),
|
||||
xserv_tensor::shape::contiguous_strides(&[1, cols]),
|
||||
new_offset,
|
||||
t.dtype(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Concatenate row tensors [1, cols] into a single [B, cols] tensor via D2D memcpy.
|
||||
fn concat_rows(rows: &[Tensor]) -> Tensor {
|
||||
assert!(!rows.is_empty());
|
||||
let batch = rows.len();
|
||||
let cols = rows[0].shape()[1];
|
||||
let dtype = rows[0].dtype();
|
||||
let device = rows[0].device();
|
||||
let elem_size = dtype.size_bytes();
|
||||
let row_bytes = cols * elem_size;
|
||||
|
||||
// Allocate output [B, cols] and copy each row into it
|
||||
let total_bytes = batch * row_bytes;
|
||||
let mut out_buf = xserv_cuda::allocator::cached_alloc(total_bytes).expect("alloc concat_rows");
|
||||
|
||||
for (b, row) in rows.iter().enumerate() {
|
||||
assert_eq!(row.shape(), &[1, cols]);
|
||||
assert!(row.is_contiguous());
|
||||
let src_buf = row.storage().gpu_buffer();
|
||||
let src_offset = row.offset() * elem_size;
|
||||
let dst_offset = b * row_bytes;
|
||||
out_buf.copy_from_device_at(src_buf, src_offset, dst_offset, row_bytes).unwrap();
|
||||
}
|
||||
|
||||
// Wrap in a Tensor
|
||||
let device_id = match device { Device::Cuda(id) => id, _ => panic!("expected CUDA device") };
|
||||
unsafe {
|
||||
crate::kv_cache::tensor_from_gpu_buffer_pub(out_buf, &[batch, cols], dtype, device_id)
|
||||
}
|
||||
}
|
||||
|
||||
fn add_any(a: &Tensor, b: &Tensor) -> Tensor {
|
||||
xserv_kernels::add(a, b)
|
||||
}
|
||||
|
||||
120
crates/xserv-model/src/sampling.rs
Normal file
120
crates/xserv-model/src/sampling.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
use half::bf16;
|
||||
use rand::Rng;
|
||||
use xserv_tensor::{DType, Device, Tensor};
|
||||
|
||||
pub struct SamplingParams {
|
||||
pub temperature: f32,
|
||||
pub top_k: usize,
|
||||
pub top_p: f32,
|
||||
}
|
||||
|
||||
impl Default for SamplingParams {
|
||||
fn default() -> Self {
|
||||
Self { temperature: 0.0, top_k: 0, top_p: 1.0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Sample a token from logits with shape [seq_len, vocab_size].
|
||||
/// Uses the last position's logits. Handles both F32 and BF16 dtypes.
|
||||
pub fn sample(logits: &Tensor, params: &SamplingParams) -> u32 {
|
||||
assert_eq!(logits.ndim(), 2);
|
||||
let vocab_size = logits.shape()[1];
|
||||
let seq_len = logits.shape()[0];
|
||||
let logits_cpu = logits.to_device(Device::Cpu);
|
||||
|
||||
// Extract last row as f32
|
||||
let last_row: Vec<f32> = match logits.dtype() {
|
||||
DType::F32 => {
|
||||
let data = logits_cpu.as_slice::<f32>();
|
||||
data[(seq_len - 1) * vocab_size..seq_len * vocab_size].to_vec()
|
||||
}
|
||||
DType::BF16 => {
|
||||
let data = logits_cpu.as_slice::<bf16>();
|
||||
data[(seq_len - 1) * vocab_size..seq_len * vocab_size]
|
||||
.iter()
|
||||
.map(|v| v.to_f32())
|
||||
.collect()
|
||||
}
|
||||
_ => panic!("unsupported dtype for sampling: {:?}", logits.dtype()),
|
||||
};
|
||||
|
||||
// Greedy
|
||||
if params.temperature == 0.0 {
|
||||
return argmax(&last_row);
|
||||
}
|
||||
|
||||
// Apply temperature
|
||||
let mut logits_f32: Vec<f32> = last_row.iter().map(|v| v / params.temperature).collect();
|
||||
|
||||
// Top-k filtering
|
||||
if params.top_k > 0 && params.top_k < vocab_size {
|
||||
let mut indices: Vec<usize> = (0..vocab_size).collect();
|
||||
indices.select_nth_unstable_by(params.top_k, |&a, &b| {
|
||||
logits_f32[b].partial_cmp(&logits_f32[a]).unwrap()
|
||||
});
|
||||
// Everything after top_k should be masked
|
||||
for &i in &indices[params.top_k..] {
|
||||
logits_f32[i] = f32::NEG_INFINITY;
|
||||
}
|
||||
}
|
||||
|
||||
// Top-p (nucleus) filtering
|
||||
if params.top_p < 1.0 {
|
||||
// Sort indices by descending logit value
|
||||
let mut indices: Vec<usize> = (0..vocab_size).collect();
|
||||
indices.sort_unstable_by(|&a, &b| logits_f32[b].partial_cmp(&logits_f32[a]).unwrap());
|
||||
|
||||
// Compute softmax probabilities for the sorted order
|
||||
let max_val = logits_f32[indices[0]];
|
||||
let sorted_probs: Vec<f32> = indices
|
||||
.iter()
|
||||
.map(|&i| (logits_f32[i] - max_val).exp())
|
||||
.collect();
|
||||
let sum: f32 = sorted_probs.iter().sum();
|
||||
let sorted_probs: Vec<f32> = sorted_probs.iter().map(|v| v / sum).collect();
|
||||
|
||||
// Cumulative sum, find cutoff
|
||||
let mut cumsum = 0.0f32;
|
||||
let mut cutoff = indices.len();
|
||||
for (rank, &prob) in sorted_probs.iter().enumerate() {
|
||||
cumsum += prob;
|
||||
if cumsum > params.top_p {
|
||||
cutoff = rank + 1; // keep at least this many
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mask everything beyond cutoff
|
||||
for &i in &indices[cutoff..] {
|
||||
logits_f32[i] = f32::NEG_INFINITY;
|
||||
}
|
||||
}
|
||||
|
||||
// Softmax
|
||||
let max_val = logits_f32.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
|
||||
let exps: Vec<f32> = logits_f32.iter().map(|v| (v - max_val).exp()).collect();
|
||||
let sum: f32 = exps.iter().sum();
|
||||
let probs: Vec<f32> = exps.iter().map(|v| v / sum).collect();
|
||||
|
||||
// Weighted random sampling
|
||||
let mut rng = rand::thread_rng();
|
||||
let r: f32 = rng.r#gen();
|
||||
let mut cumsum = 0.0f32;
|
||||
for (i, &p) in probs.iter().enumerate() {
|
||||
cumsum += p;
|
||||
if cumsum > r {
|
||||
return i as u32;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback (rounding edge case)
|
||||
(vocab_size - 1) as u32
|
||||
}
|
||||
|
||||
fn argmax(data: &[f32]) -> u32 {
|
||||
data.iter()
|
||||
.enumerate()
|
||||
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
|
||||
.map(|(i, _)| i as u32)
|
||||
.unwrap()
|
||||
}
|
||||
@@ -19,3 +19,4 @@ serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
axum.workspace = true
|
||||
uuid.workspace = true
|
||||
tokio-stream.workspace = true
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
use axum::Extension;
|
||||
use axum::Json;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::sse::{Event, KeepAlive, Sse};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::convert::Infallible;
|
||||
use std::sync::Arc;
|
||||
use tokio_stream::StreamExt;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::engine::{GenerateEvent, GenerateRequest};
|
||||
use crate::AppState;
|
||||
use crate::engine::{GenerateEvent, GenerateRequest};
|
||||
use xserv_model::SamplingParams;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ChatRequest {
|
||||
@@ -14,6 +21,14 @@ pub struct ChatRequest {
|
||||
pub messages: Vec<Message>,
|
||||
#[serde(default = "default_max_tokens")]
|
||||
pub max_tokens: usize,
|
||||
#[serde(default)]
|
||||
pub stream: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub temperature: Option<f32>,
|
||||
#[serde(default)]
|
||||
pub top_k: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub top_p: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -22,7 +37,9 @@ pub struct Message {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
fn default_max_tokens() -> usize { 256 }
|
||||
fn default_max_tokens() -> usize {
|
||||
256
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ModelsResponse {
|
||||
@@ -37,7 +54,9 @@ pub struct ModelInfo {
|
||||
owned_by: &'static str,
|
||||
}
|
||||
|
||||
pub async fn health() -> &'static str { "ok" }
|
||||
pub async fn health() -> &'static str {
|
||||
"ok"
|
||||
}
|
||||
|
||||
pub async fn list_models(Extension(state): Extension<Arc<AppState>>) -> Json<ModelsResponse> {
|
||||
Json(ModelsResponse {
|
||||
@@ -53,34 +72,61 @@ pub async fn list_models(Extension(state): Extension<Arc<AppState>>) -> Json<Mod
|
||||
pub async fn chat_completions(
|
||||
Extension(state): Extension<Arc<AppState>>,
|
||||
Json(req): Json<ChatRequest>,
|
||||
) -> Json<serde_json::Value> {
|
||||
) -> Response {
|
||||
if req.stream == Some(true) {
|
||||
chat_stream(state, req)
|
||||
} else {
|
||||
chat_non_stream(state, req).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn chat_non_stream(state: Arc<AppState>, req: ChatRequest) -> Response {
|
||||
let id = format!("chatcmpl-{}", Uuid::new_v4());
|
||||
let model_name = state.model_name.clone();
|
||||
let created = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let created = unix_timestamp();
|
||||
|
||||
// Prepare prompt tokens (MutexGuard scoped)
|
||||
let prompt = build_prompt(&req.messages);
|
||||
let prompt_tokens = state.engine_tokenizer.lock().unwrap().encode(&prompt);
|
||||
let prompt_token_count = prompt_tokens.len();
|
||||
|
||||
let max_seq_len = state.max_seq_len;
|
||||
if prompt_token_count >= max_seq_len {
|
||||
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
|
||||
"error": {
|
||||
"message": format!("prompt is {} tokens, exceeds max_seq_len {}", prompt_token_count, max_seq_len),
|
||||
"type": "invalid_request_error"
|
||||
}
|
||||
}))).into_response();
|
||||
}
|
||||
let max_tokens = req.max_tokens.min(max_seq_len - prompt_token_count);
|
||||
|
||||
// Create channel and submit request (MutexGuard scoped)
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<GenerateEvent>(64);
|
||||
let gen_req = GenerateRequest {
|
||||
prompt_tokens,
|
||||
max_tokens: req.max_tokens,
|
||||
max_tokens,
|
||||
sampling: sampling_params(&req),
|
||||
sender: tx,
|
||||
};
|
||||
state.engine_sender.lock().unwrap().send(gen_req).expect("engine channel closed");
|
||||
state
|
||||
.engine_sender
|
||||
.lock()
|
||||
.unwrap()
|
||||
.send(gen_req)
|
||||
.expect("engine channel closed");
|
||||
|
||||
// Now await — no MutexGuards held here
|
||||
let mut content = String::new();
|
||||
let mut completion_token_count: usize = 0;
|
||||
let mut finish_reason = "length".to_string();
|
||||
while let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
GenerateEvent::Token { text, .. } => content.push_str(&text),
|
||||
GenerateEvent::Done { finish_reason: fr } => { finish_reason = fr; break; }
|
||||
GenerateEvent::Token { text, .. } => {
|
||||
completion_token_count += 1;
|
||||
content.push_str(&text);
|
||||
}
|
||||
GenerateEvent::Done { finish_reason: fr } => {
|
||||
finish_reason = fr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,21 +141,159 @@ pub async fn chat_completions(
|
||||
"finish_reason": finish_reason,
|
||||
}],
|
||||
"usage": {
|
||||
"prompt_tokens": 0,
|
||||
"completion_tokens": 0,
|
||||
"total_tokens": 0
|
||||
"prompt_tokens": prompt_token_count,
|
||||
"completion_tokens": completion_token_count,
|
||||
"total_tokens": prompt_token_count + completion_token_count
|
||||
}
|
||||
}))
|
||||
})).into_response()
|
||||
}
|
||||
|
||||
fn chat_stream(
|
||||
state: Arc<AppState>,
|
||||
req: ChatRequest,
|
||||
) -> Response {
|
||||
let id = format!("chatcmpl-{}", Uuid::new_v4());
|
||||
let model_name = state.model_name.clone();
|
||||
let created = unix_timestamp();
|
||||
|
||||
let prompt = build_prompt(&req.messages);
|
||||
let prompt_tokens = state.engine_tokenizer.lock().unwrap().encode(&prompt);
|
||||
|
||||
let max_seq_len = state.max_seq_len;
|
||||
if prompt_tokens.len() >= max_seq_len {
|
||||
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
|
||||
"error": {
|
||||
"message": format!("prompt is {} tokens, exceeds max_seq_len {}", prompt_tokens.len(), max_seq_len),
|
||||
"type": "invalid_request_error"
|
||||
}
|
||||
}))).into_response();
|
||||
}
|
||||
let max_tokens = req.max_tokens.min(max_seq_len - prompt_tokens.len());
|
||||
|
||||
let (engine_tx, engine_rx) = tokio::sync::mpsc::channel::<GenerateEvent>(64);
|
||||
let gen_req = GenerateRequest {
|
||||
prompt_tokens,
|
||||
max_tokens,
|
||||
sampling: sampling_params(&req),
|
||||
sender: engine_tx,
|
||||
};
|
||||
state
|
||||
.engine_sender
|
||||
.lock()
|
||||
.unwrap()
|
||||
.send(gen_req)
|
||||
.expect("engine channel closed");
|
||||
|
||||
// SSE event channel: engine events -> SSE events
|
||||
let (sse_tx, sse_rx) = tokio::sync::mpsc::channel::<Result<Event, Infallible>>(64);
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut engine_stream = ReceiverStream::new(engine_rx);
|
||||
let mut first = true;
|
||||
|
||||
while let Some(event) = engine_stream.next().await {
|
||||
match event {
|
||||
GenerateEvent::Token { text, .. } => {
|
||||
if first {
|
||||
// First chunk: role announcement
|
||||
let chunk =
|
||||
make_chunk(&id, &model_name, created, None, Some("assistant"), None);
|
||||
let _ = sse_tx.send(Ok(Event::default().data(chunk))).await;
|
||||
first = false;
|
||||
}
|
||||
let chunk = make_chunk(&id, &model_name, created, Some(&text), None, None);
|
||||
if sse_tx.send(Ok(Event::default().data(chunk))).await.is_err() {
|
||||
return; // client disconnected
|
||||
}
|
||||
}
|
||||
GenerateEvent::Done { finish_reason } => {
|
||||
if first {
|
||||
// Edge case: Done arrived with no tokens
|
||||
let chunk =
|
||||
make_chunk(&id, &model_name, created, None, Some("assistant"), None);
|
||||
let _ = sse_tx.send(Ok(Event::default().data(chunk))).await;
|
||||
}
|
||||
let chunk =
|
||||
make_chunk(&id, &model_name, created, None, None, Some(&finish_reason));
|
||||
let _ = sse_tx.send(Ok(Event::default().data(chunk))).await;
|
||||
let _ = sse_tx
|
||||
.send(Ok(Event::default().data("[DONE]".to_string())))
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Sse::new(ReceiverStream::new(sse_rx)).keep_alive(KeepAlive::default()).into_response()
|
||||
}
|
||||
|
||||
fn make_chunk(
|
||||
id: &str,
|
||||
model: &str,
|
||||
created: u64,
|
||||
content: Option<&str>,
|
||||
role: Option<&str>,
|
||||
finish_reason: Option<&str>,
|
||||
) -> String {
|
||||
let mut delta = serde_json::Map::new();
|
||||
if let Some(r) = role {
|
||||
delta.insert("role".into(), serde_json::Value::String(r.into()));
|
||||
// Role chunk also includes empty content per OpenAI spec
|
||||
delta.insert("content".into(), serde_json::Value::String(String::new()));
|
||||
}
|
||||
if let Some(c) = content {
|
||||
delta.insert("content".into(), serde_json::Value::String(c.into()));
|
||||
}
|
||||
|
||||
let fr = match finish_reason {
|
||||
Some(r) => serde_json::Value::String(r.into()),
|
||||
None => serde_json::Value::Null,
|
||||
};
|
||||
|
||||
serde_json::json!({
|
||||
"id": id,
|
||||
"object": "chat.completion.chunk",
|
||||
"created": created,
|
||||
"model": model,
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"delta": delta,
|
||||
"finish_reason": fr,
|
||||
}]
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn unix_timestamp() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
fn sampling_params(req: &ChatRequest) -> SamplingParams {
|
||||
SamplingParams {
|
||||
temperature: req.temperature.unwrap_or(0.0),
|
||||
top_k: req.top_k.unwrap_or(0),
|
||||
top_p: req.top_p.unwrap_or(1.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_prompt(messages: &[Message]) -> String {
|
||||
let mut prompt = String::new();
|
||||
for msg in messages {
|
||||
match msg.role.as_str() {
|
||||
"system" => { prompt.push_str(&msg.content); prompt.push('\n'); }
|
||||
"user" | "assistant" => { prompt.push_str(&msg.content); }
|
||||
"system" | "user" | "assistant" => {
|
||||
prompt.push_str("<|im_start|>");
|
||||
prompt.push_str(&msg.role);
|
||||
prompt.push('\n');
|
||||
prompt.push_str(&msg.content);
|
||||
prompt.push_str("<|im_end|>\n");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
prompt.push_str("<|im_start|>assistant\n");
|
||||
prompt
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::path::Path;
|
||||
use std::sync::mpsc;
|
||||
use xserv_model::{GpuKVCache, ModelConfig, Qwen3};
|
||||
use std::sync::Once;
|
||||
use std::time::Instant;
|
||||
use xserv_model::{GpuKVCache, ModelConfig, Qwen3, SamplingParams, sample};
|
||||
use xserv_model::loader;
|
||||
use xserv_model::qwen3::sample_greedy;
|
||||
use xserv_tensor::{DType, Device};
|
||||
use xserv_tokenizer::Tokenizer;
|
||||
|
||||
@@ -18,6 +19,7 @@ pub struct Engine {
|
||||
pub struct GenerateRequest {
|
||||
pub prompt_tokens: Vec<u32>,
|
||||
pub max_tokens: usize,
|
||||
pub sampling: SamplingParams,
|
||||
pub sender: tokio::sync::mpsc::Sender<GenerateEvent>,
|
||||
}
|
||||
|
||||
@@ -31,13 +33,16 @@ struct Sequence {
|
||||
prompt_tokens: Vec<u32>,
|
||||
generated_tokens: Vec<u32>,
|
||||
max_tokens: usize,
|
||||
kv_cache: GpuKVCache,
|
||||
sampling: SamplingParams,
|
||||
kv_cache: Option<GpuKVCache>,
|
||||
sender: tokio::sync::mpsc::Sender<GenerateEvent>,
|
||||
prefilled: bool,
|
||||
eos_token_id: Option<u32>,
|
||||
created_at: Instant,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn load(model_dir: &Path, max_batch_size: usize) -> Self {
|
||||
pub fn load(model_dir: &Path, max_batch_size: usize, max_seq_len: usize) -> Self {
|
||||
xserv_cuda::device::set_device(0).unwrap();
|
||||
let config = ModelConfig::from_file(&model_dir.join("config.json"));
|
||||
eprintln!("[engine] Loading weights...");
|
||||
@@ -45,13 +50,14 @@ impl Engine {
|
||||
eprintln!("[engine] Loaded {} tensors", weights.len());
|
||||
let model = Qwen3::from_weights(config.clone(), weights);
|
||||
let tokenizer = Tokenizer::from_file(&model_dir.join("tokenizer.json"));
|
||||
let max_seq_len = 256;
|
||||
eprintln!("[engine] Ready (max_batch_size={max_batch_size}, max_seq_len={max_seq_len})");
|
||||
Self { model, config, tokenizer, max_batch_size, max_seq_len }
|
||||
}
|
||||
|
||||
pub fn tokenizer(&self) -> &Tokenizer { &self.tokenizer }
|
||||
|
||||
pub fn max_seq_len(&self) -> usize { self.max_seq_len }
|
||||
|
||||
/// Main scheduler loop. Receives requests from channel, manages concurrent sequences.
|
||||
pub fn run(&self, rx: mpsc::Receiver<GenerateRequest>) {
|
||||
let mut waiting: VecDeque<Sequence> = VecDeque::new();
|
||||
@@ -84,22 +90,86 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
// Step 4: Process one iteration for all running sequences
|
||||
// Step 4a: Process prefills (one at a time — different prompt lengths)
|
||||
// Prefill sequences must be processed individually because they have
|
||||
// different prompt lengths and each needs a full forward pass.
|
||||
let mut newly_prefilled = Vec::new();
|
||||
for seq in running.iter_mut() {
|
||||
if !seq.prefilled {
|
||||
// Prefill
|
||||
let logits = self.model.forward_gpu_cache(&seq.prompt_tokens, &mut seq.kv_cache);
|
||||
let next = sample_greedy(&logits);
|
||||
let logits = self.model.forward_gpu_cache(&seq.prompt_tokens, seq.kv_cache.as_mut().unwrap());
|
||||
let next = sample(&logits, &seq.sampling);
|
||||
seq.generated_tokens.push(next);
|
||||
seq.prefilled = true;
|
||||
self.emit_token(seq, next);
|
||||
newly_prefilled.push(seq.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 4b: Batched decode — batch all decode-ready sequences into one forward pass.
|
||||
// Projections and FFN run as [B, hidden] matmuls; attention remains per-seq.
|
||||
let decode_indices: Vec<usize> = running.iter().enumerate()
|
||||
.filter(|(_, s)| s.prefilled && !newly_prefilled.contains(&s.id))
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
|
||||
if !decode_indices.is_empty() {
|
||||
static LOG_ONCE: Once = Once::new();
|
||||
LOG_ONCE.call_once(|| {
|
||||
eprintln!("[scheduler] batched decode active");
|
||||
});
|
||||
eprintln!("[scheduler] decode batch_size={}", decode_indices.len());
|
||||
|
||||
if decode_indices.len() == 1 {
|
||||
// Single sequence: use per-seq path (no batching overhead)
|
||||
let i = decode_indices[0];
|
||||
let last = *running[i].generated_tokens.last().unwrap();
|
||||
let logits = self.model.forward_gpu_cache(&[last], running[i].kv_cache.as_mut().unwrap());
|
||||
let next = sample(&logits, &running[i].sampling);
|
||||
running[i].generated_tokens.push(next);
|
||||
self.emit_token(&running[i], next);
|
||||
} else {
|
||||
// Decode one token
|
||||
let last = *seq.generated_tokens.last().unwrap();
|
||||
let logits = self.model.forward_gpu_cache(&[last], &mut seq.kv_cache);
|
||||
let next = sample_greedy(&logits);
|
||||
seq.generated_tokens.push(next);
|
||||
self.emit_token(seq, next);
|
||||
// Batched decode: extract tokens and positions
|
||||
let tokens: Vec<u32> = decode_indices.iter()
|
||||
.map(|&i| *running[i].generated_tokens.last().unwrap())
|
||||
.collect();
|
||||
let positions: Vec<usize> = decode_indices.iter()
|
||||
.map(|&i| running[i].kv_cache.as_ref().unwrap().seq_len())
|
||||
.collect();
|
||||
|
||||
// Take caches out of sequences via Option::take (no dummy allocation).
|
||||
let mut caches: Vec<GpuKVCache> = decode_indices.iter()
|
||||
.map(|&i| running[i].kv_cache.take().unwrap())
|
||||
.collect();
|
||||
let mut cache_refs: Vec<&mut GpuKVCache> = caches.iter_mut().collect();
|
||||
|
||||
let logits = self.model.forward_decode_batch(&tokens, &positions, &mut cache_refs);
|
||||
|
||||
// Put caches back: pop from end while iterating in reverse
|
||||
drop(cache_refs);
|
||||
for &i in decode_indices.iter().rev() {
|
||||
running[i].kv_cache = Some(caches.pop().unwrap());
|
||||
}
|
||||
|
||||
// Sample per-sequence from batched logits [B, vocab_size]
|
||||
let vocab_size = logits.shape()[1];
|
||||
let logits_cpu = logits.to_device(xserv_tensor::Device::Cpu);
|
||||
let data = logits_cpu.as_slice::<half::bf16>();
|
||||
for (j, &i) in decode_indices.iter().enumerate() {
|
||||
let row_start = j * vocab_size;
|
||||
let row_logits = &data[row_start..row_start + vocab_size];
|
||||
let next = if running[i].sampling.temperature == 0.0 {
|
||||
// Greedy: argmax
|
||||
row_logits.iter().enumerate()
|
||||
.max_by(|a, b| a.1.to_f32().partial_cmp(&b.1.to_f32()).unwrap())
|
||||
.map(|(idx, _)| idx as u32).unwrap()
|
||||
} else {
|
||||
// Use the row as a single-row tensor for full sampling
|
||||
let row_tensor = xserv_tensor::Tensor::from_slice(row_logits, &[1, vocab_size]);
|
||||
sample(&row_tensor, &running[i].sampling)
|
||||
};
|
||||
running[i].generated_tokens.push(next);
|
||||
self.emit_token(&running[i], next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,15 +190,18 @@ impl Engine {
|
||||
fn make_sequence(&self, req: GenerateRequest, next_id: &mut u64) -> Sequence {
|
||||
let id = *next_id;
|
||||
*next_id += 1;
|
||||
let kv_cache = GpuKVCache::new(&self.config, self.max_seq_len, DType::BF16);
|
||||
let kv_cache = GpuKVCache::new(&self.config, self.max_seq_len, DType::BF16, 0);
|
||||
Sequence {
|
||||
id,
|
||||
prompt_tokens: req.prompt_tokens,
|
||||
generated_tokens: Vec::new(),
|
||||
max_tokens: req.max_tokens,
|
||||
kv_cache,
|
||||
sampling: req.sampling,
|
||||
kv_cache: Some(kv_cache),
|
||||
sender: req.sender,
|
||||
prefilled: false,
|
||||
eos_token_id: self.tokenizer.eos_token_id(),
|
||||
created_at: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +209,6 @@ impl Engine {
|
||||
let text = self.tokenizer.decode(&[token_id]);
|
||||
|
||||
if self.tokenizer.eos_token_id() == Some(token_id) {
|
||||
let _ = seq.sender.blocking_send(GenerateEvent::Token { id: token_id, text });
|
||||
let _ = seq.sender.blocking_send(GenerateEvent::Done {
|
||||
finish_reason: "stop".to_string(),
|
||||
});
|
||||
@@ -157,5 +229,5 @@ fn is_finished(seq: &Sequence) -> bool {
|
||||
if seq.generated_tokens.len() >= seq.max_tokens { return true; }
|
||||
// Check EOS — need tokenizer info. Use a simple heuristic:
|
||||
// If sender is closed (receiver dropped), also consider finished.
|
||||
seq.sender.is_closed() || last == 151645 // Qwen3 EOS token ID (hardcoded for now)
|
||||
seq.sender.is_closed() || seq.eos_token_id == Some(last)
|
||||
}
|
||||
|
||||
@@ -10,13 +10,14 @@ pub struct AppState {
|
||||
pub model_name: String,
|
||||
pub engine_sender: Mutex<mpsc::Sender<GenerateRequest>>,
|
||||
pub engine_tokenizer: Mutex<xserv_tokenizer::Tokenizer>,
|
||||
pub max_seq_len: usize,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: xserv-server <model-dir> [--port PORT] [--max-batch N]");
|
||||
eprintln!("Usage: xserv-server <model-dir> [--port PORT] [--max-batch N] [--max-seq-len N]");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
@@ -31,6 +32,11 @@ async fn main() {
|
||||
.and_then(|i| args.get(i + 1))
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(4);
|
||||
let max_seq_len: usize = args.iter()
|
||||
.position(|a| a == "--max-seq-len")
|
||||
.and_then(|i| args.get(i + 1))
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(2048);
|
||||
|
||||
let model_name = model_dir.file_name()
|
||||
.map(|n| n.to_string_lossy().to_string())
|
||||
@@ -43,7 +49,7 @@ async fn main() {
|
||||
|
||||
let model_dir_clone = model_dir.clone();
|
||||
std::thread::spawn(move || {
|
||||
let engine = engine::Engine::load(&model_dir_clone, max_batch);
|
||||
let engine = engine::Engine::load(&model_dir_clone, max_batch, max_seq_len);
|
||||
engine.run(rx);
|
||||
});
|
||||
|
||||
@@ -51,6 +57,7 @@ async fn main() {
|
||||
model_name,
|
||||
engine_sender: Mutex::new(tx),
|
||||
engine_tokenizer: Mutex::new(tokenizer),
|
||||
max_seq_len,
|
||||
});
|
||||
|
||||
let app = Router::new()
|
||||
@@ -60,7 +67,7 @@ async fn main() {
|
||||
.layer(Extension(state));
|
||||
|
||||
let addr = format!("0.0.0.0:{port}");
|
||||
eprintln!("[server] Listening on {addr} (max_batch={max_batch})");
|
||||
eprintln!("[server] Listening on {addr} (max_batch={max_batch}, max_seq_len={max_seq_len})");
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -6,4 +6,4 @@ pub mod tensor;
|
||||
pub use dtype::{DType, TensorDType};
|
||||
pub use shape::Dims;
|
||||
pub use storage::{Device, Storage};
|
||||
pub use tensor::Tensor;
|
||||
pub use tensor::{register_gpu_contiguous, Tensor};
|
||||
|
||||
@@ -3,7 +3,7 @@ use xserv_cuda::{GpuBuffer, Result as CudaResult};
|
||||
|
||||
enum StorageInner {
|
||||
Cpu { data: Vec<u8> },
|
||||
Cuda { buffer: GpuBuffer },
|
||||
Cuda { buffer: GpuBuffer, device: u32 },
|
||||
}
|
||||
|
||||
/// Reference-counted storage for tensor data. Multiple tensors can share
|
||||
@@ -31,21 +31,21 @@ impl Storage {
|
||||
Self(Arc::new(StorageInner::Cpu { data }))
|
||||
}
|
||||
|
||||
pub fn cuda(buffer: GpuBuffer) -> Self {
|
||||
Self(Arc::new(StorageInner::Cuda { buffer }))
|
||||
pub fn cuda(buffer: GpuBuffer, device: u32) -> Self {
|
||||
Self(Arc::new(StorageInner::Cuda { buffer, device }))
|
||||
}
|
||||
|
||||
pub fn device(&self) -> Device {
|
||||
match self.0.as_ref() {
|
||||
StorageInner::Cpu { .. } => Device::Cpu,
|
||||
StorageInner::Cuda { .. } => Device::Cuda(0),
|
||||
StorageInner::Cuda { device, .. } => Device::Cuda(*device),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len_bytes(&self) -> usize {
|
||||
match self.0.as_ref() {
|
||||
StorageInner::Cpu { data } => data.len(),
|
||||
StorageInner::Cuda { buffer } => buffer.len(),
|
||||
StorageInner::Cuda { buffer, .. } => buffer.len(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ impl Storage {
|
||||
|
||||
pub fn gpu_buffer(&self) -> &GpuBuffer {
|
||||
match self.0.as_ref() {
|
||||
StorageInner::Cuda { buffer } => buffer,
|
||||
StorageInner::Cuda { buffer, .. } => buffer,
|
||||
StorageInner::Cpu { .. } => panic!("cannot access CPU storage as GPU buffer"),
|
||||
}
|
||||
}
|
||||
@@ -71,11 +71,11 @@ impl Storage {
|
||||
return Ok(self.clone());
|
||||
}
|
||||
match (current, target) {
|
||||
(Device::Cpu, Device::Cuda(_dev)) => {
|
||||
(Device::Cpu, Device::Cuda(dev)) => {
|
||||
let cpu_data = self.as_cpu_bytes();
|
||||
let mut buf = GpuBuffer::alloc(cpu_data.len())?;
|
||||
let mut buf = xserv_cuda::allocator::cached_alloc(cpu_data.len())?;
|
||||
buf.copy_from_host(cpu_data)?;
|
||||
Ok(Storage::cuda(buf))
|
||||
Ok(Storage::cuda(buf, dev))
|
||||
}
|
||||
(Device::Cuda(_), Device::Cpu) => {
|
||||
let gpu_buf = self.gpu_buffer();
|
||||
@@ -83,11 +83,11 @@ impl Storage {
|
||||
gpu_buf.copy_to_host(&mut data)?;
|
||||
Ok(Storage::cpu(data))
|
||||
}
|
||||
(Device::Cuda(_), Device::Cuda(_)) => {
|
||||
(Device::Cuda(_), Device::Cuda(dev)) => {
|
||||
let src = self.gpu_buffer();
|
||||
let mut dst = GpuBuffer::alloc(src.len())?;
|
||||
let mut dst = xserv_cuda::allocator::cached_alloc(src.len())?;
|
||||
dst.copy_from_device(src)?;
|
||||
Ok(Storage::cuda(dst))
|
||||
Ok(Storage::cuda(dst, dev))
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -97,10 +97,10 @@ impl Storage {
|
||||
pub fn deep_copy(&self) -> CudaResult<Self> {
|
||||
match self.0.as_ref() {
|
||||
StorageInner::Cpu { data } => Ok(Storage::cpu(data.clone())),
|
||||
StorageInner::Cuda { buffer } => {
|
||||
let mut dst = GpuBuffer::alloc(buffer.len())?;
|
||||
StorageInner::Cuda { buffer, device } => {
|
||||
let mut dst = xserv_cuda::allocator::cached_alloc(buffer.len())?;
|
||||
dst.copy_from_device(buffer)?;
|
||||
Ok(Storage::cuda(dst))
|
||||
Ok(Storage::cuda(dst, *device))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,10 +109,24 @@ impl Storage {
|
||||
pub fn zeros(len_bytes: usize, device: Device) -> CudaResult<Self> {
|
||||
match device {
|
||||
Device::Cpu => Ok(Storage::cpu(vec![0u8; len_bytes])),
|
||||
Device::Cuda(_) => {
|
||||
let mut buf = GpuBuffer::alloc(len_bytes)?;
|
||||
Device::Cuda(dev) => {
|
||||
let mut buf = xserv_cuda::allocator::cached_alloc(len_bytes)?;
|
||||
buf.zero()?;
|
||||
Ok(Storage::cuda(buf))
|
||||
Ok(Storage::cuda(buf, dev))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocate storage **without zeroing** on the given device.
|
||||
/// The buffer may contain stale data from the caching allocator's pool.
|
||||
/// Only use when the caller guarantees the kernel will fully overwrite
|
||||
/// every element before any read.
|
||||
pub fn empty(len_bytes: usize, device: Device) -> CudaResult<Self> {
|
||||
match device {
|
||||
Device::Cpu => Ok(Storage::cpu(vec![0u8; len_bytes])), // CPU still zeros (cheap)
|
||||
Device::Cuda(dev) => {
|
||||
let buf = xserv_cuda::allocator::cached_alloc(len_bytes)?;
|
||||
Ok(Storage::cuda(buf, dev))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::dtype::{DType, TensorDType};
|
||||
use crate::shape::{self, Dims};
|
||||
use crate::storage::{Device, Storage};
|
||||
|
||||
/// Global hook for GPU strided-to-contiguous copy.
|
||||
/// Set by `xserv-kernels` (or any crate that provides a GPU kernel) via
|
||||
/// `register_gpu_contiguous`. When set, `contiguous()` on a non-contiguous
|
||||
/// GPU tensor calls this instead of doing a CPU round-trip.
|
||||
static GPU_CONTIGUOUS_FN: OnceLock<fn(&Tensor) -> Tensor> = OnceLock::new();
|
||||
|
||||
/// Register a function that makes a non-contiguous GPU tensor contiguous.
|
||||
/// Intended to be called once by the kernel crate at startup.
|
||||
pub fn register_gpu_contiguous(f: fn(&Tensor) -> Tensor) {
|
||||
let _ = GPU_CONTIGUOUS_FN.set(f);
|
||||
}
|
||||
|
||||
/// Multi-dimensional array with CPU or GPU storage.
|
||||
///
|
||||
/// Tensors support view semantics: transpose, slice, etc. share
|
||||
@@ -51,6 +65,22 @@ impl Tensor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocate a tensor **without zeroing** the backing memory.
|
||||
/// The buffer may contain stale data. Only use when the calling kernel
|
||||
/// will fully overwrite every element before any read.
|
||||
pub fn empty(shape: &[usize], dtype: DType, device: Device) -> Self {
|
||||
let numel = shape::num_elements(shape);
|
||||
let len_bytes = numel * dtype.size_bytes();
|
||||
let storage = Storage::empty(len_bytes, device).expect("alloc failed");
|
||||
Self {
|
||||
storage,
|
||||
shape: Dims::from_slice(shape),
|
||||
strides: shape::contiguous_strides(shape),
|
||||
offset: 0,
|
||||
dtype,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ones(shape: &[usize], dtype: DType) -> Self {
|
||||
let numel = shape::num_elements(shape);
|
||||
match dtype {
|
||||
@@ -123,10 +153,15 @@ impl Tensor {
|
||||
pub fn unsqueeze(&self, dim: usize) -> Self {
|
||||
assert!(dim <= self.ndim());
|
||||
let mut new_shape = self.shape.clone();
|
||||
let mut new_strides = self.strides.clone();
|
||||
new_shape.insert(dim, 1);
|
||||
let stride_val = if dim < self.strides.len() { self.strides[dim] } else { 1 };
|
||||
new_strides.insert(dim, stride_val);
|
||||
let new_strides = if self.is_contiguous() {
|
||||
shape::contiguous_strides(&new_shape)
|
||||
} else {
|
||||
let mut s = self.strides.clone();
|
||||
let stride_val = if dim < self.strides.len() { self.strides[dim] } else { 1 };
|
||||
s.insert(dim, stride_val);
|
||||
s
|
||||
};
|
||||
Self {
|
||||
storage: self.storage.clone(),
|
||||
shape: new_shape,
|
||||
@@ -142,9 +177,12 @@ impl Tensor {
|
||||
if self.is_contiguous() {
|
||||
return self.clone();
|
||||
}
|
||||
// For GPU tensors: round-trip through CPU (correct but slow).
|
||||
// TODO: write a GPU contiguous-copy kernel for performance.
|
||||
// For GPU tensors: use the registered GPU kernel if available,
|
||||
// otherwise fall back to CPU round-trip.
|
||||
if matches!(self.device(), Device::Cuda(_)) {
|
||||
if let Some(gpu_fn) = GPU_CONTIGUOUS_FN.get() {
|
||||
return gpu_fn(self);
|
||||
}
|
||||
let cpu = self.to_device(Device::Cpu);
|
||||
let contig = cpu.contiguous();
|
||||
return contig.to_device(self.device());
|
||||
@@ -237,3 +275,58 @@ impl std::fmt::Debug for Tensor {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn contiguous_2d() -> Tensor {
|
||||
Tensor::from_slice(&[1.0f32; 12], &[3, 4])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsqueeze_dim0_contiguous() {
|
||||
let t = contiguous_2d();
|
||||
let u = t.unsqueeze(0);
|
||||
assert_eq!(u.shape(), &[1, 3, 4]);
|
||||
assert!(u.is_contiguous());
|
||||
assert_eq!(u.strides(), &[12, 4, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsqueeze_dim1_contiguous() {
|
||||
let t = contiguous_2d();
|
||||
let u = t.unsqueeze(1);
|
||||
assert_eq!(u.shape(), &[3, 1, 4]);
|
||||
assert!(u.is_contiguous());
|
||||
assert_eq!(u.strides(), &[4, 4, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsqueeze_dim2_contiguous() {
|
||||
let t = contiguous_2d();
|
||||
let u = t.unsqueeze(2);
|
||||
assert_eq!(u.shape(), &[3, 4, 1]);
|
||||
assert!(u.is_contiguous());
|
||||
assert_eq!(u.strides(), &[4, 1, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsqueeze_noncontiguous() {
|
||||
// Transpose makes [3,4] into [4,3] with strides [1,4] (non-contiguous)
|
||||
let t = contiguous_2d().transpose(0, 1);
|
||||
assert!(!t.is_contiguous());
|
||||
let u = t.unsqueeze(0);
|
||||
assert_eq!(u.shape(), &[1, 4, 3]);
|
||||
// Non-contiguous path: stride_val copied from strides[0]=1
|
||||
assert_eq!(u.strides(), &[1, 1, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsqueeze_squeeze_roundtrip() {
|
||||
let t = contiguous_2d();
|
||||
let u = t.unsqueeze(1).squeeze(1);
|
||||
assert_eq!(u.shape(), t.shape());
|
||||
assert!(u.is_contiguous());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,8 +51,6 @@ impl Tokenizer {
|
||||
let tj: TokenizerJson = serde_json::from_str(&data)
|
||||
.unwrap_or_else(|e| panic!("failed to parse tokenizer.json: {e}"));
|
||||
|
||||
let byte_fallback = tj.model.byte_fallback;
|
||||
|
||||
// Build encoder: token bytes → ID
|
||||
// All HF tokenizers use GPT-2 byte-to-unicode mapping for vocab keys.
|
||||
let mut encoder = HashMap::new();
|
||||
@@ -170,10 +168,26 @@ impl Tokenizer {
|
||||
}
|
||||
// Fall back to per-byte encoding
|
||||
let word_bytes: Vec<u8> = word.bytes().collect();
|
||||
let mut token_ids: Vec<u32> = word_bytes.iter().map(|&b| {
|
||||
*self.encoder.get(&vec![b]).unwrap_or_else(|| {
|
||||
panic!("byte {b} (0x{b:02X}) not in vocab")
|
||||
})
|
||||
let mut token_ids: Vec<u32> = word_bytes.iter().filter_map(|&b| {
|
||||
if let Some(&id) = self.encoder.get(&vec![b]) {
|
||||
Some(id)
|
||||
} else if self.byte_fallback {
|
||||
let hex_token = format!("<0x{:02X}>", b);
|
||||
if let Some(&id) = self.special_tokens.get(&hex_token) {
|
||||
Some(id)
|
||||
} else if let Some(&id) = self.encoder.get(hex_token.as_bytes()) {
|
||||
Some(id)
|
||||
} else if let Some(&unk_id) = self.special_tokens.get("<unk>") {
|
||||
eprintln!("warning: byte 0x{b:02X} not in vocab, using <unk> token");
|
||||
Some(unk_id)
|
||||
} else {
|
||||
eprintln!("warning: byte 0x{b:02X} not in vocab and no fallback token, using token 0");
|
||||
Some(0)
|
||||
}
|
||||
} else {
|
||||
eprintln!("warning: byte {b} (0x{b:02X}) not in vocab, skipping");
|
||||
None
|
||||
}
|
||||
}).collect();
|
||||
|
||||
// BPE merges
|
||||
|
||||
@@ -45,6 +45,18 @@ __global__ void scale_bf16_kernel(const __nv_bfloat16* x, __nv_bfloat16* out, fl
|
||||
if (idx < n) out[idx] = __float2bfloat16(__bfloat162float(x[idx]) * scale);
|
||||
}
|
||||
|
||||
// Fused SiLU×Mul: out = silu(gate) * up
|
||||
__global__ void silu_mul_bf16_kernel(const __nv_bfloat16* gate, const __nv_bfloat16* up,
|
||||
__nv_bfloat16* out, int n) {
|
||||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (idx < n) {
|
||||
float g = __bfloat162float(gate[idx]);
|
||||
float u = __bfloat162float(up[idx]);
|
||||
float silu_g = g / (1.0f + expf(-g));
|
||||
out[idx] = __float2bfloat16(silu_g * u);
|
||||
}
|
||||
}
|
||||
|
||||
// Element-wise add: out = a + b
|
||||
__global__ void add_f32_kernel(const float* a, const float* b, float* out, int n) {
|
||||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
@@ -132,4 +144,11 @@ void launch_mul_bf16(const void* a, const void* b, void* out, int n, void* strea
|
||||
(const __nv_bfloat16*)a, (const __nv_bfloat16*)b, (__nv_bfloat16*)out, n);
|
||||
}
|
||||
|
||||
void launch_silu_mul_bf16(const void* gate, const void* up, void* out, int n, void* stream) {
|
||||
int block = 256;
|
||||
int grid = (n + block - 1) / block;
|
||||
silu_mul_bf16_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)gate, (const __nv_bfloat16*)up, (__nv_bfloat16*)out, n);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,8 +27,7 @@ __global__ void causal_mask_bf16(
|
||||
int col = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
|
||||
if (col < cols && col > row + offset) {
|
||||
// BF16 doesn't have proper -inf literal, use a very large negative
|
||||
scores[batch_idx * rows * cols + row * cols + col] = __float2bfloat16(-1e9f);
|
||||
scores[batch_idx * rows * cols + row * cols + col] = __float2bfloat16(-INFINITY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
416
csrc/attention/flash_attention.cu
Normal file
416
csrc/attention/flash_attention.cu
Normal file
@@ -0,0 +1,416 @@
|
||||
#include <cuda_bf16.h>
|
||||
#include <float.h>
|
||||
|
||||
// Flash Attention 2 forward kernel for BF16 with FP32 accumulation.
|
||||
//
|
||||
// Algorithm: outer loop over Q tiles (BR rows), inner loop over K/V tiles (BC rows).
|
||||
// Uses online softmax — no O(S^2) memory.
|
||||
//
|
||||
// Layout: Q [batch, num_q_heads, q_len, head_dim]
|
||||
// K [batch, num_kv_heads, kv_len, head_dim]
|
||||
// V [batch, num_kv_heads, kv_len, head_dim]
|
||||
// O [batch, num_q_heads, q_len, head_dim]
|
||||
//
|
||||
// Shared memory (BF16):
|
||||
// smem_q[BR][head_dim] — 64 * 128 * 2 = 16 KB (loaded once per Q tile)
|
||||
// smem_kv[BC][head_dim] — 64 * 128 * 2 = 16 KB (alternates K and V)
|
||||
// Total: 32 KB (fits in default 48 KB shared memory)
|
||||
|
||||
#define BR 64
|
||||
#define BC 64
|
||||
#define THREADS_PER_BLOCK 128
|
||||
|
||||
__global__ void flash_attention_bf16_kernel(
|
||||
const __nv_bfloat16* __restrict__ Q,
|
||||
const __nv_bfloat16* __restrict__ K,
|
||||
const __nv_bfloat16* __restrict__ V,
|
||||
__nv_bfloat16* __restrict__ O,
|
||||
int num_q_heads, int num_kv_heads,
|
||||
int q_len, int kv_len, int head_dim,
|
||||
float scale, int causal
|
||||
) {
|
||||
// Grid: (ceil(q_len / BR), batch * num_q_heads)
|
||||
int q_tile_idx = blockIdx.x;
|
||||
int bh = blockIdx.y;
|
||||
int batch_idx = bh / num_q_heads;
|
||||
int q_head = bh % num_q_heads;
|
||||
|
||||
// GQA: map Q head to KV head
|
||||
int heads_per_group = num_q_heads / num_kv_heads;
|
||||
int kv_head = q_head / heads_per_group;
|
||||
|
||||
int q_tile_start = q_tile_idx * BR;
|
||||
if (q_tile_start >= q_len) return;
|
||||
int q_tile_rows = min(BR, q_len - q_tile_start);
|
||||
|
||||
// Pointers to this batch/head's data
|
||||
const __nv_bfloat16* Q_head = Q + ((long long)batch_idx * num_q_heads + q_head) * q_len * head_dim;
|
||||
const __nv_bfloat16* K_head = K + ((long long)batch_idx * num_kv_heads + kv_head) * kv_len * head_dim;
|
||||
const __nv_bfloat16* V_head = V + ((long long)batch_idx * num_kv_heads + kv_head) * kv_len * head_dim;
|
||||
__nv_bfloat16* O_head = O + ((long long)batch_idx * num_q_heads + q_head) * q_len * head_dim;
|
||||
|
||||
int tid = threadIdx.x;
|
||||
|
||||
// Dynamic shared memory
|
||||
extern __shared__ __nv_bfloat16 smem[];
|
||||
__nv_bfloat16* smem_q = smem; // BR * head_dim elements
|
||||
__nv_bfloat16* smem_kv = smem + BR * head_dim; // BC * head_dim elements
|
||||
|
||||
// ---- Load Q tile into shared memory (cooperative) ----
|
||||
int q_elems = q_tile_rows * head_dim;
|
||||
for (int i = tid; i < q_elems; i += THREADS_PER_BLOCK) {
|
||||
int row = i / head_dim;
|
||||
int col = i % head_dim;
|
||||
smem_q[row * head_dim + col] = Q_head[(q_tile_start + row) * head_dim + col];
|
||||
}
|
||||
// Zero-pad if q_tile_rows < BR
|
||||
for (int i = q_elems + tid; i < BR * head_dim; i += THREADS_PER_BLOCK) {
|
||||
smem_q[i] = __float2bfloat16(0.0f);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Thread t (0 <= t < q_tile_rows) owns Q row t
|
||||
bool owns_row = (tid < q_tile_rows);
|
||||
|
||||
// Per-thread FP32 accumulators (head_dim up to 128)
|
||||
float O_acc[128];
|
||||
float m_val = -INFINITY;
|
||||
float l_val = 0.0f;
|
||||
if (owns_row) {
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
O_acc[d] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// kv_offset handles cached KV longer than Q (decode step)
|
||||
int kv_offset = kv_len - q_len;
|
||||
int num_kv_tiles = (kv_len + BC - 1) / BC;
|
||||
|
||||
// ---- Inner loop over K/V tiles ----
|
||||
for (int j = 0; j < num_kv_tiles; j++) {
|
||||
int kv_tile_start = j * BC;
|
||||
int kv_tile_cols = min(BC, kv_len - kv_tile_start);
|
||||
|
||||
// Causal: skip entire tile if all K positions are in the future
|
||||
if (causal) {
|
||||
int max_allowed_kv = (q_tile_start + q_tile_rows - 1) + kv_offset;
|
||||
if (kv_tile_start > max_allowed_kv) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Load K tile into smem_kv ----
|
||||
int kv_elems = kv_tile_cols * head_dim;
|
||||
for (int i = tid; i < kv_elems; i += THREADS_PER_BLOCK) {
|
||||
int row = i / head_dim;
|
||||
int col = i % head_dim;
|
||||
smem_kv[row * head_dim + col] = K_head[(kv_tile_start + row) * head_dim + col];
|
||||
}
|
||||
for (int i = kv_elems + tid; i < BC * head_dim; i += THREADS_PER_BLOCK) {
|
||||
smem_kv[i] = __float2bfloat16(0.0f);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// ---- Compute S = Q @ K^T * scale, causal mask, online softmax ----
|
||||
float P[BC];
|
||||
|
||||
if (owns_row) {
|
||||
float row_max = -INFINITY;
|
||||
for (int c = 0; c < kv_tile_cols; c++) {
|
||||
float dot = 0.0f;
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
dot += __bfloat162float(smem_q[tid * head_dim + d])
|
||||
* __bfloat162float(smem_kv[c * head_dim + d]);
|
||||
}
|
||||
float s = dot * scale;
|
||||
|
||||
if (causal) {
|
||||
int q_pos = q_tile_start + tid;
|
||||
int kv_pos = kv_tile_start + c;
|
||||
if (kv_pos > q_pos + kv_offset) {
|
||||
s = -INFINITY;
|
||||
}
|
||||
}
|
||||
|
||||
P[c] = s; // store score temporarily in P
|
||||
row_max = fmaxf(row_max, s);
|
||||
}
|
||||
|
||||
// Online softmax: m_new, P = exp(S - m_new), l_new
|
||||
float m_new = fmaxf(m_val, row_max);
|
||||
|
||||
float psum = 0.0f;
|
||||
for (int c = 0; c < kv_tile_cols; c++) {
|
||||
P[c] = expf(P[c] - m_new);
|
||||
psum += P[c];
|
||||
}
|
||||
|
||||
// Rescale previous accumulator
|
||||
float correction = expf(m_val - m_new);
|
||||
l_val = correction * l_val + psum;
|
||||
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
O_acc[d] *= correction;
|
||||
}
|
||||
|
||||
m_val = m_new;
|
||||
}
|
||||
|
||||
// Sync before overwriting smem_kv with V tile
|
||||
__syncthreads();
|
||||
|
||||
// ---- Load V tile (reuse smem_kv) ----
|
||||
int v_elems = kv_tile_cols * head_dim;
|
||||
for (int i = tid; i < v_elems; i += THREADS_PER_BLOCK) {
|
||||
int row = i / head_dim;
|
||||
int col = i % head_dim;
|
||||
smem_kv[row * head_dim + col] = V_head[(kv_tile_start + row) * head_dim + col];
|
||||
}
|
||||
for (int i = v_elems + tid; i < BC * head_dim; i += THREADS_PER_BLOCK) {
|
||||
smem_kv[i] = __float2bfloat16(0.0f);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// ---- Accumulate O += P @ V_tile ----
|
||||
if (owns_row) {
|
||||
for (int c = 0; c < kv_tile_cols; c++) {
|
||||
float p = P[c];
|
||||
if (p != 0.0f) {
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
O_acc[d] += p * __bfloat162float(smem_kv[c * head_dim + d]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
}
|
||||
|
||||
// ---- Final normalize and write output (convert FP32 → BF16) ----
|
||||
if (owns_row) {
|
||||
float inv_l = (l_val > 0.0f) ? (1.0f / l_val) : 0.0f;
|
||||
int global_row = q_tile_start + tid;
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
O_head[global_row * head_dim + d] = __float2bfloat16(O_acc[d] * inv_l);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Decode Attention kernel: optimized for Q_len=1 (single-token decode).
|
||||
// Parallelizes across KV sequence dimension instead of Q rows.
|
||||
//
|
||||
// Grid: (batch * num_q_heads, 1) — one block per Q head
|
||||
// Block: 256 threads — each thread handles ceil(kv_len / 256) KV positions
|
||||
// Uses online softmax reduction across threads.
|
||||
// ============================================================
|
||||
|
||||
#define DECODE_THREADS 256
|
||||
#define HEAD_DIM_MAX 128
|
||||
|
||||
__global__ void decode_attention_bf16_kernel(
|
||||
const __nv_bfloat16* __restrict__ Q,
|
||||
const __nv_bfloat16* __restrict__ K,
|
||||
const __nv_bfloat16* __restrict__ V,
|
||||
__nv_bfloat16* __restrict__ O,
|
||||
int num_q_heads, int num_kv_heads,
|
||||
int kv_len, int head_dim,
|
||||
float scale
|
||||
) {
|
||||
int bh = blockIdx.x;
|
||||
int batch_idx = bh / num_q_heads;
|
||||
int q_head = bh % num_q_heads;
|
||||
|
||||
// GQA mapping
|
||||
int heads_per_group = num_q_heads / num_kv_heads;
|
||||
int kv_head = q_head / heads_per_group;
|
||||
|
||||
int tid = threadIdx.x;
|
||||
|
||||
// Pointers to this batch/head's data
|
||||
// Q: [batch, num_q_heads, 1, head_dim]
|
||||
const __nv_bfloat16* Q_ptr = Q + ((long long)batch_idx * num_q_heads + q_head) * head_dim;
|
||||
// K/V: [batch, num_kv_heads, kv_len, head_dim]
|
||||
const __nv_bfloat16* K_base = K + ((long long)batch_idx * num_kv_heads + kv_head) * kv_len * head_dim;
|
||||
const __nv_bfloat16* V_base = V + ((long long)batch_idx * num_kv_heads + kv_head) * kv_len * head_dim;
|
||||
__nv_bfloat16* O_ptr = O + ((long long)batch_idx * num_q_heads + q_head) * head_dim;
|
||||
|
||||
// Load Q vector into registers (head_dim <= 128)
|
||||
float q_reg[HEAD_DIM_MAX];
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
q_reg[d] = __bfloat162float(Q_ptr[d]);
|
||||
}
|
||||
|
||||
// Each thread processes a chunk of KV positions
|
||||
// Thread tid handles positions: tid, tid+DECODE_THREADS, tid+2*DECODE_THREADS, ...
|
||||
float local_max = -INFINITY;
|
||||
float local_sum = 0.0f;
|
||||
float local_O[HEAD_DIM_MAX];
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
local_O[d] = 0.0f;
|
||||
}
|
||||
|
||||
for (int pos = tid; pos < kv_len; pos += DECODE_THREADS) {
|
||||
// Compute dot(Q, K[pos]) * scale
|
||||
const __nv_bfloat16* K_pos = K_base + pos * head_dim;
|
||||
float dot = 0.0f;
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
dot += q_reg[d] * __bfloat162float(K_pos[d]);
|
||||
}
|
||||
float s = dot * scale;
|
||||
|
||||
// Online softmax update
|
||||
float new_max = fmaxf(local_max, s);
|
||||
float correction = expf(local_max - new_max);
|
||||
float p = expf(s - new_max);
|
||||
|
||||
// Rescale running sum and O
|
||||
local_sum = local_sum * correction + p;
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
local_O[d] = local_O[d] * correction;
|
||||
}
|
||||
|
||||
// Accumulate V[pos] weighted by p
|
||||
const __nv_bfloat16* V_pos = V_base + pos * head_dim;
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
local_O[d] += p * __bfloat162float(V_pos[d]);
|
||||
}
|
||||
|
||||
local_max = new_max;
|
||||
}
|
||||
|
||||
// --- Block-level online softmax reduction ---
|
||||
// We need to combine (local_max, local_sum, local_O) across all threads.
|
||||
// Strategy: reduce max, then each thread rescales, then reduce sum and O.
|
||||
|
||||
// Shared memory for reduction
|
||||
__shared__ float smem_max[32]; // one per warp
|
||||
__shared__ float smem_sum[32];
|
||||
__shared__ float smem_O[HEAD_DIM_MAX]; // final output accumulator
|
||||
|
||||
// Step 1: Block-wide max reduction
|
||||
int lane = tid & 31;
|
||||
int warp_id = tid >> 5;
|
||||
int num_warps = DECODE_THREADS >> 5; // 8 warps
|
||||
|
||||
float warp_max = local_max;
|
||||
#pragma unroll
|
||||
for (int offset = 16; offset > 0; offset >>= 1)
|
||||
warp_max = fmaxf(warp_max, __shfl_down_sync(0xffffffff, warp_max, offset));
|
||||
if (lane == 0) smem_max[warp_id] = warp_max;
|
||||
__syncthreads();
|
||||
|
||||
float global_max;
|
||||
if (tid == 0) {
|
||||
global_max = smem_max[0];
|
||||
for (int i = 1; i < num_warps; i++)
|
||||
global_max = fmaxf(global_max, smem_max[i]);
|
||||
smem_max[0] = global_max;
|
||||
}
|
||||
__syncthreads();
|
||||
global_max = smem_max[0];
|
||||
|
||||
// Step 2: Each thread rescales its local_sum and local_O with global_max
|
||||
float rescale = (local_max == -INFINITY) ? 0.0f : expf(local_max - global_max);
|
||||
local_sum *= rescale;
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
local_O[d] *= rescale;
|
||||
}
|
||||
|
||||
// Step 3: Reduce sum across block
|
||||
float warp_sum = local_sum;
|
||||
#pragma unroll
|
||||
for (int offset = 16; offset > 0; offset >>= 1)
|
||||
warp_sum += __shfl_down_sync(0xffffffff, warp_sum, offset);
|
||||
if (lane == 0) smem_sum[warp_id] = warp_sum;
|
||||
__syncthreads();
|
||||
|
||||
float global_sum;
|
||||
if (tid == 0) {
|
||||
global_sum = 0.0f;
|
||||
for (int i = 0; i < num_warps; i++)
|
||||
global_sum += smem_sum[i];
|
||||
smem_sum[0] = global_sum;
|
||||
}
|
||||
__syncthreads();
|
||||
global_sum = smem_sum[0];
|
||||
|
||||
// Step 4: Reduce O across block (dimension by dimension using shared mem)
|
||||
float inv_sum = (global_sum > 0.0f) ? (1.0f / global_sum) : 0.0f;
|
||||
|
||||
// Process head_dim in chunks: each iteration reduces one dimension
|
||||
// Use shared memory accumulator: each warp contributes via warp reduction + atomic
|
||||
// Actually simpler: iterate over dimensions, warp reduce each, then lane0 atomicAdd to smem_O
|
||||
|
||||
// Initialize smem_O
|
||||
for (int d = tid; d < head_dim; d += DECODE_THREADS) {
|
||||
smem_O[d] = 0.0f;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Each thread adds its local_O contributions via warp reduction + atomicAdd
|
||||
for (int d = 0; d < head_dim; d++) {
|
||||
float val = local_O[d];
|
||||
// Warp-level reduction
|
||||
#pragma unroll
|
||||
for (int offset = 16; offset > 0; offset >>= 1)
|
||||
val += __shfl_down_sync(0xffffffff, val, offset);
|
||||
if (lane == 0) {
|
||||
atomicAdd(&smem_O[d], val);
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Thread 0..head_dim-1 write final output
|
||||
for (int d = tid; d < head_dim; d += DECODE_THREADS) {
|
||||
O_ptr[d] = __float2bfloat16(smem_O[d] * inv_sum);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void launch_flash_attention_bf16(
|
||||
const void* Q, const void* K, const void* V, void* O,
|
||||
int batch, int num_q_heads, int num_kv_heads,
|
||||
int q_len, int kv_len, int head_dim,
|
||||
float scale, int causal, void* stream
|
||||
) {
|
||||
int q_tiles = (q_len + BR - 1) / BR;
|
||||
dim3 grid(q_tiles, batch * num_q_heads);
|
||||
int block = THREADS_PER_BLOCK;
|
||||
|
||||
// Shared memory: smem_q[BR * head_dim] + smem_kv[BC * head_dim], all BF16
|
||||
int smem_bytes = (BR + BC) * head_dim * (int)sizeof(__nv_bfloat16);
|
||||
|
||||
flash_attention_bf16_kernel<<<grid, block, smem_bytes, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)Q,
|
||||
(const __nv_bfloat16*)K,
|
||||
(const __nv_bfloat16*)V,
|
||||
(__nv_bfloat16*)O,
|
||||
num_q_heads, num_kv_heads,
|
||||
q_len, kv_len, head_dim,
|
||||
scale, causal
|
||||
);
|
||||
}
|
||||
|
||||
void launch_decode_attention_bf16(
|
||||
const void* Q, const void* K, const void* V, void* O,
|
||||
int batch, int num_q_heads, int num_kv_heads,
|
||||
int kv_len, int head_dim,
|
||||
float scale, int causal, void* stream
|
||||
) {
|
||||
int grid = batch * num_q_heads;
|
||||
int block = DECODE_THREADS;
|
||||
|
||||
decode_attention_bf16_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)Q,
|
||||
(const __nv_bfloat16*)K,
|
||||
(const __nv_bfloat16*)V,
|
||||
(__nv_bfloat16*)O,
|
||||
num_q_heads, num_kv_heads,
|
||||
kv_len, head_dim,
|
||||
scale
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -111,6 +111,55 @@ __global__ void repeat_kv_bf16(
|
||||
out[idx] = in[in_idx];
|
||||
}
|
||||
|
||||
// ---- Generic strided copy (up to 4D) ----
|
||||
// Each thread copies one element. Maps flat contiguous output index to strided input index.
|
||||
// Unused dimensions are padded with shape=1, stride=0.
|
||||
|
||||
__global__ void strided_copy_bf16(
|
||||
const __nv_bfloat16* __restrict__ in,
|
||||
__nv_bfloat16* __restrict__ out,
|
||||
int numel,
|
||||
int ndim,
|
||||
int shape0, int shape1, int shape2, int shape3,
|
||||
int in_stride0, int in_stride1, int in_stride2, int in_stride3,
|
||||
int in_offset
|
||||
) {
|
||||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (idx >= numel) return;
|
||||
|
||||
// Decompose flat output index into multi-dim indices (rightmost = fastest)
|
||||
int remaining = idx;
|
||||
int i3 = remaining % shape3; remaining /= shape3;
|
||||
int i2 = remaining % shape2; remaining /= shape2;
|
||||
int i1 = remaining % shape1; remaining /= shape1;
|
||||
int i0 = remaining;
|
||||
|
||||
int in_idx = in_offset + i0 * in_stride0 + i1 * in_stride1 + i2 * in_stride2 + i3 * in_stride3;
|
||||
out[idx] = in[in_idx];
|
||||
}
|
||||
|
||||
__global__ void strided_copy_f32(
|
||||
const float* __restrict__ in,
|
||||
float* __restrict__ out,
|
||||
int numel,
|
||||
int ndim,
|
||||
int shape0, int shape1, int shape2, int shape3,
|
||||
int in_stride0, int in_stride1, int in_stride2, int in_stride3,
|
||||
int in_offset
|
||||
) {
|
||||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (idx >= numel) return;
|
||||
|
||||
int remaining = idx;
|
||||
int i3 = remaining % shape3; remaining /= shape3;
|
||||
int i2 = remaining % shape2; remaining /= shape2;
|
||||
int i1 = remaining % shape1; remaining /= shape1;
|
||||
int i0 = remaining;
|
||||
|
||||
int in_idx = in_offset + i0 * in_stride0 + i1 * in_stride1 + i2 * in_stride2 + i3 * in_stride3;
|
||||
out[idx] = in[in_idx];
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void launch_reshape_heads_bf16(const void* in, void* out,
|
||||
@@ -158,4 +207,28 @@ void launch_repeat_kv_bf16(const void* in, void* out,
|
||||
(const __nv_bfloat16*)in, (__nv_bfloat16*)out, kv_heads, n_rep, seq_len, head_dim);
|
||||
}
|
||||
|
||||
void launch_strided_copy_bf16(const void* in, void* out, int numel, int ndim,
|
||||
int shape0, int shape1, int shape2, int shape3,
|
||||
int in_stride0, int in_stride1, int in_stride2, int in_stride3,
|
||||
int in_offset, void* stream) {
|
||||
int block = 256;
|
||||
int grid = (numel + block - 1) / block;
|
||||
strided_copy_bf16<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)in, (__nv_bfloat16*)out, numel, ndim,
|
||||
shape0, shape1, shape2, shape3,
|
||||
in_stride0, in_stride1, in_stride2, in_stride3, in_offset);
|
||||
}
|
||||
|
||||
void launch_strided_copy_f32(const void* in, void* out, int numel, int ndim,
|
||||
int shape0, int shape1, int shape2, int shape3,
|
||||
int in_stride0, int in_stride1, int in_stride2, int in_stride3,
|
||||
int in_offset, void* stream) {
|
||||
int block = 256;
|
||||
int grid = (numel + block - 1) / block;
|
||||
strided_copy_f32<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||||
(const float*)in, (float*)out, numel, ndim,
|
||||
shape0, shape1, shape2, shape3,
|
||||
in_stride0, in_stride1, in_stride2, in_stride3, in_offset);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
102
csrc/gemm/gemv.cu
Normal file
102
csrc/gemm/gemv.cu
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <cuda_bf16.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// Custom GEMV kernel for M=1 decode step (BF16):
|
||||
// y[n] = sum_k x[k] * W[k * N + n]
|
||||
// where x: [K] (BF16), W: [K, N] (BF16, row-major), y: [N] (BF16).
|
||||
//
|
||||
// Design: K-split for high occupancy on large GPU (170 SMs).
|
||||
// Grid: (N / TILE_N, K / TILE_K) — each block computes a partial sum
|
||||
// for TILE_N output columns over a TILE_K slice of K.
|
||||
// Partial results are atomicAdd'd to an FP32 accumulator, then a
|
||||
// second kernel converts FP32 -> BF16.
|
||||
//
|
||||
// Memory access: adjacent threads read adjacent columns of the same row
|
||||
// of W, giving perfectly coalesced 128-byte transactions.
|
||||
|
||||
#define GEMV_TILE_N 128
|
||||
#define GEMV_TILE_K 256
|
||||
#define GEMV_BLOCK 128 // = TILE_N, one thread per output column
|
||||
|
||||
__global__ void gemv_bf16_kernel(
|
||||
const __nv_bfloat16* __restrict__ x, // [K]
|
||||
const __nv_bfloat16* __restrict__ W, // [K, N] row-major
|
||||
float* __restrict__ y_fp32, // [N] accumulator
|
||||
int K, int N
|
||||
) {
|
||||
const int block_n = blockIdx.x;
|
||||
const int block_k = blockIdx.y;
|
||||
const int t = threadIdx.x;
|
||||
const int col = block_n * GEMV_TILE_N + t;
|
||||
|
||||
if (col >= N) return;
|
||||
|
||||
const int k_start = block_k * GEMV_TILE_K;
|
||||
const int k_end = min(k_start + GEMV_TILE_K, K);
|
||||
const int k_len = k_end - k_start;
|
||||
|
||||
// Load x[k_start..k_end] into shared memory as FP32
|
||||
__shared__ float x_shared[GEMV_TILE_K];
|
||||
for (int i = t; i < k_len; i += GEMV_BLOCK) {
|
||||
x_shared[i] = __bfloat162float(x[k_start + i]);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Compute partial dot product for this column
|
||||
float sum = 0.0f;
|
||||
for (int ki = 0; ki < k_len; ki++) {
|
||||
sum += x_shared[ki] * __bfloat162float(W[(k_start + ki) * N + col]);
|
||||
}
|
||||
|
||||
// Atomic accumulate (handles K-split reduction)
|
||||
atomicAdd(&y_fp32[col], sum);
|
||||
}
|
||||
|
||||
// Conversion kernel: FP32 accumulator -> BF16 output
|
||||
__global__ void gemv_fp32_to_bf16_kernel(
|
||||
const float* __restrict__ src,
|
||||
__nv_bfloat16* __restrict__ dst,
|
||||
int n
|
||||
) {
|
||||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (idx < n) {
|
||||
dst[idx] = __float2bfloat16(src[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void launch_gemv_bf16(
|
||||
const void* x, // [K] BF16
|
||||
const void* W, // [K, N] BF16 row-major
|
||||
void* y_bf16, // [N] BF16 output
|
||||
void* y_fp32_buf, // [N] FP32 temporary (caller-provided)
|
||||
int K, int N,
|
||||
void* stream
|
||||
) {
|
||||
cudaStream_t s = (cudaStream_t)stream;
|
||||
|
||||
// Zero the FP32 accumulator
|
||||
cudaMemsetAsync((float*)y_fp32_buf, 0, N * sizeof(float), s);
|
||||
|
||||
// Launch GEMV kernel
|
||||
dim3 grid((N + GEMV_TILE_N - 1) / GEMV_TILE_N,
|
||||
(K + GEMV_TILE_K - 1) / GEMV_TILE_K);
|
||||
gemv_bf16_kernel<<<grid, GEMV_BLOCK, 0, s>>>(
|
||||
(const __nv_bfloat16*)x,
|
||||
(const __nv_bfloat16*)W,
|
||||
(float*)y_fp32_buf,
|
||||
K, N
|
||||
);
|
||||
|
||||
// Convert FP32 -> BF16
|
||||
int conv_block = 256;
|
||||
int conv_grid = (N + conv_block - 1) / conv_block;
|
||||
gemv_fp32_to_bf16_kernel<<<conv_grid, conv_block, 0, s>>>(
|
||||
(const float*)y_fp32_buf,
|
||||
(__nv_bfloat16*)y_bf16,
|
||||
N
|
||||
);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -14,27 +14,34 @@ __global__ void layernorm_f32(
|
||||
const float* x_row = x + row * hidden_size;
|
||||
float* out_row = out + row * hidden_size;
|
||||
|
||||
// Welford online: compute mean and variance in one pass
|
||||
// Pass 1: compute mean
|
||||
float local_sum = 0.0f;
|
||||
float local_sum_sq = 0.0f;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float v = x_row[i];
|
||||
local_sum += v;
|
||||
local_sum_sq += v * v;
|
||||
local_sum += x_row[i];
|
||||
}
|
||||
local_sum = block_reduce_sum(local_sum);
|
||||
local_sum_sq = block_reduce_sum(local_sum_sq);
|
||||
|
||||
__shared__ float s_mean, s_inv_std;
|
||||
if (threadIdx.x == 0) {
|
||||
float mean = local_sum / hidden_size;
|
||||
float var = local_sum_sq / hidden_size - mean * mean;
|
||||
s_mean = mean;
|
||||
s_inv_std = rsqrtf(var + eps);
|
||||
s_mean = local_sum / hidden_size;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
float mean = s_mean;
|
||||
|
||||
// Pass 2: compute variance = sum((x - mean)^2) / N
|
||||
float local_var = 0.0f;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float d = x_row[i] - mean;
|
||||
local_var += d * d;
|
||||
}
|
||||
local_var = block_reduce_sum(local_var);
|
||||
|
||||
if (threadIdx.x == 0) {
|
||||
s_inv_std = rsqrtf(local_var / hidden_size + eps);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
float inv_std = s_inv_std;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
out_row[i] = gamma[i] * (x_row[i] - mean) * inv_std + beta[i];
|
||||
@@ -52,26 +59,34 @@ __global__ void layernorm_bf16(
|
||||
const __nv_bfloat16* x_row = x + row * hidden_size;
|
||||
__nv_bfloat16* out_row = out + row * hidden_size;
|
||||
|
||||
// Pass 1: compute mean
|
||||
float local_sum = 0.0f;
|
||||
float local_sum_sq = 0.0f;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float v = __bfloat162float(x_row[i]);
|
||||
local_sum += v;
|
||||
local_sum_sq += v * v;
|
||||
local_sum += __bfloat162float(x_row[i]);
|
||||
}
|
||||
local_sum = block_reduce_sum(local_sum);
|
||||
local_sum_sq = block_reduce_sum(local_sum_sq);
|
||||
|
||||
__shared__ float s_mean, s_inv_std;
|
||||
if (threadIdx.x == 0) {
|
||||
float mean = local_sum / hidden_size;
|
||||
float var = local_sum_sq / hidden_size - mean * mean;
|
||||
s_mean = mean;
|
||||
s_inv_std = rsqrtf(var + eps);
|
||||
s_mean = local_sum / hidden_size;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
float mean = s_mean;
|
||||
|
||||
// Pass 2: compute variance = sum((x - mean)^2) / N
|
||||
float local_var = 0.0f;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float d = __bfloat162float(x_row[i]) - mean;
|
||||
local_var += d * d;
|
||||
}
|
||||
local_var = block_reduce_sum(local_var);
|
||||
|
||||
if (threadIdx.x == 0) {
|
||||
s_inv_std = rsqrtf(local_var / hidden_size + eps);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
float inv_std = s_inv_std;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float v = __bfloat162float(x_row[i]);
|
||||
@@ -86,6 +101,7 @@ extern "C" {
|
||||
void launch_layernorm_f32(const void* x, const void* gamma, const void* beta,
|
||||
void* out, int rows, int hidden_size, float eps, void* stream) {
|
||||
int block = (hidden_size < 1024) ? hidden_size : 1024;
|
||||
if (block < 32) block = 32;
|
||||
layernorm_f32<<<rows, block, 0, (cudaStream_t)stream>>>(
|
||||
(const float*)x, (const float*)gamma, (const float*)beta,
|
||||
(float*)out, hidden_size, eps);
|
||||
@@ -94,6 +110,7 @@ void launch_layernorm_f32(const void* x, const void* gamma, const void* beta,
|
||||
void launch_layernorm_bf16(const void* x, const void* gamma, const void* beta,
|
||||
void* out, int rows, int hidden_size, float eps, void* stream) {
|
||||
int block = (hidden_size < 1024) ? hidden_size : 1024;
|
||||
if (block < 32) block = 32;
|
||||
layernorm_bf16<<<rows, block, 0, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)x, (const __nv_bfloat16*)gamma, (const __nv_bfloat16*)beta,
|
||||
(__nv_bfloat16*)out, hidden_size, eps);
|
||||
|
||||
@@ -63,11 +63,52 @@ __global__ void rmsnorm_bf16(
|
||||
}
|
||||
}
|
||||
|
||||
// Fused Add + RMSNorm: sum_out = x + residual, normed_out = rmsnorm(sum_out, gamma, eps)
|
||||
// Each block handles one row of [hidden_size].
|
||||
__global__ void add_rmsnorm_bf16(
|
||||
const __nv_bfloat16* __restrict__ x,
|
||||
const __nv_bfloat16* __restrict__ residual,
|
||||
const __nv_bfloat16* __restrict__ gamma,
|
||||
__nv_bfloat16* __restrict__ normed_out,
|
||||
__nv_bfloat16* __restrict__ sum_out,
|
||||
int hidden_size, float eps
|
||||
) {
|
||||
int row = blockIdx.x;
|
||||
const __nv_bfloat16* x_row = x + row * hidden_size;
|
||||
const __nv_bfloat16* res_row = residual + row * hidden_size;
|
||||
__nv_bfloat16* sum_row = sum_out + row * hidden_size;
|
||||
__nv_bfloat16* norm_row = normed_out + row * hidden_size;
|
||||
|
||||
// Pass 1: compute sum = x + residual, and accumulate sum_sq
|
||||
float sum_sq = 0.0f;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float s = __bfloat162float(x_row[i]) + __bfloat162float(res_row[i]);
|
||||
sum_row[i] = __float2bfloat16(s);
|
||||
sum_sq += s * s;
|
||||
}
|
||||
sum_sq = block_reduce_sum(sum_sq);
|
||||
|
||||
__shared__ float s_rms_inv;
|
||||
if (threadIdx.x == 0) {
|
||||
s_rms_inv = rsqrtf(sum_sq / hidden_size + eps);
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Pass 2: normed_out = sum * rms_inv * gamma
|
||||
float rms_inv = s_rms_inv;
|
||||
for (int i = threadIdx.x; i < hidden_size; i += blockDim.x) {
|
||||
float s = __bfloat162float(sum_row[i]);
|
||||
float g = __bfloat162float(gamma[i]);
|
||||
norm_row[i] = __float2bfloat16(s * rms_inv * g);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void launch_rmsnorm_f32(const void* x, const void* gamma, void* out,
|
||||
int rows, int hidden_size, float eps, void* stream) {
|
||||
int block = (hidden_size < 1024) ? hidden_size : 1024;
|
||||
if (block < 32) block = 32;
|
||||
rmsnorm_f32<<<rows, block, 0, (cudaStream_t)stream>>>(
|
||||
(const float*)x, (const float*)gamma, (float*)out, hidden_size, eps);
|
||||
}
|
||||
@@ -75,9 +116,22 @@ void launch_rmsnorm_f32(const void* x, const void* gamma, void* out,
|
||||
void launch_rmsnorm_bf16(const void* x, const void* gamma, void* out,
|
||||
int rows, int hidden_size, float eps, void* stream) {
|
||||
int block = (hidden_size < 1024) ? hidden_size : 1024;
|
||||
if (block < 32) block = 32;
|
||||
rmsnorm_bf16<<<rows, block, 0, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)x, (const __nv_bfloat16*)gamma,
|
||||
(__nv_bfloat16*)out, hidden_size, eps);
|
||||
}
|
||||
|
||||
void launch_add_rmsnorm_bf16(const void* x, const void* residual, const void* gamma,
|
||||
void* normed_out, void* sum_out,
|
||||
int rows, int hidden_size, float eps, void* stream) {
|
||||
int block = (hidden_size < 1024) ? hidden_size : 1024;
|
||||
if (block < 32) block = 32;
|
||||
add_rmsnorm_bf16<<<rows, block, 0, (cudaStream_t)stream>>>(
|
||||
(const __nv_bfloat16*)x, (const __nv_bfloat16*)residual,
|
||||
(const __nv_bfloat16*)gamma,
|
||||
(__nv_bfloat16*)normed_out, (__nv_bfloat16*)sum_out,
|
||||
hidden_size, eps);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
| 抽象层级 | Level 0.5 | 自写 CUDA kernel + cuBLAS 可切换,便于 benchmark 对比 |
|
||||
| 硬件 | 8×RTX 5090 (Blackwell, CC 12.0, 32GB GDDR7) | 纯 PCIe Gen5 x16 互联,无 NVLink (详见下方硬件拓扑) |
|
||||
| 语言 | Rust + CUDA (C/C++) | Rust FFI 调用 CUDA |
|
||||
| 起步模型 | GPT-2 124M → Qwen3-7B | 从简单到实用 |
|
||||
| 起步模型 | GPT-2 124M → Qwen3-8B | 从简单到实用 |
|
||||
| 精度 | BF16/FP16 | 后期扩展 FP8 |
|
||||
| Tensor | 自己实现 | 完整学习 tensor 抽象设计 |
|
||||
| Tokenizer | 自己实现 BPE | 学习分词机制 |
|
||||
@@ -101,7 +101,7 @@ Phase 8: GPT-2 完整推理 ◄──────────── 里程碑
|
||||
│
|
||||
Phase 9: KV Cache + Autoregressive Generation
|
||||
│
|
||||
Phase 10: Qwen3-7B 支持 ◄─────────── 里程碑 ② 7B 模型推理
|
||||
Phase 10: Qwen3-8B 支持 ◄─────────── 里程碑 ② 8B 模型推理
|
||||
│
|
||||
Phase 11: Paged Attention + KV Cache Manager
|
||||
│
|
||||
@@ -109,7 +109,7 @@ Phase 12: Continuous Batching + Request Scheduler
|
||||
│
|
||||
Phase 13: HTTP API + SSE Streaming ◄── 里程碑 ③ 端到端 API 可用
|
||||
│
|
||||
Phase 14: Flash Attention v2
|
||||
Phase 14: Flash Attention (FA2 for SM120)
|
||||
│
|
||||
Phase 15: 性能优化 ◄──────────────── 里程碑 ④ 50% vLLM throughput
|
||||
│
|
||||
@@ -625,8 +625,8 @@ safetensors file (disk)
|
||||
|
||||
- [ ] 加载 GPT-2 124M (`openai-community/gpt2`),打印所有 tensor name, shape, dtype
|
||||
- [ ] 抽查几个 tensor 的前 10 个值,与 PyTorch `from_pretrained` 对比
|
||||
- [ ] 加载 Qwen3-7B sharded 权重,验证所有 tensor 都成功加载
|
||||
- [ ] 性能: 测量 7B 模型权重加载时间 (mmap → GPU 全流程)
|
||||
- [ ] 加载 Qwen3-8B sharded 权重,验证所有 tensor 都成功加载
|
||||
- [ ] 性能: 测量 8B 模型权重加载时间 (mmap → GPU 全流程)
|
||||
- [ ] 错误处理: 缺少 tensor、dtype 不匹配、文件不存在等情况
|
||||
|
||||
---
|
||||
@@ -869,15 +869,15 @@ weights × V_cache [B, H, S, D] → output [B, H, 1, D]
|
||||
|
||||
---
|
||||
|
||||
## Phase 10: Qwen3-7B 支持 — 里程碑 ②
|
||||
## Phase 10: Qwen3-8B 支持 — 里程碑 ②
|
||||
|
||||
**Crate**: `xserv-model`
|
||||
|
||||
**目标**: 扩展模型定义以支持 Qwen3-7B,验证输出正确性。
|
||||
**目标**: 扩展模型定义以支持 Qwen3-8B,验证输出正确性。
|
||||
|
||||
### 架构对比
|
||||
|
||||
| 特性 | GPT-2 (124M) | Qwen3-7B |
|
||||
| 特性 | GPT-2 (124M) | Qwen3-8B |
|
||||
|------|-------------|----------|
|
||||
| Normalization | LayerNorm (pre-LN) | RMSNorm (pre-LN) |
|
||||
| Position Encoding | Learned absolute (wpe) | RoPE (无单独参数) |
|
||||
@@ -885,8 +885,8 @@ weights × V_cache [B, H, S, D] → output [B, H, 1, D]
|
||||
| Activation | GELU | SwiGLU (SiLU gate) |
|
||||
| FFN | Linear(H→4H) → GELU → Linear(4H→H) | gate_proj + up_proj → SiLU gate → down_proj |
|
||||
| Vocab Size | 50,257 | ~152,000 |
|
||||
| Hidden Size | 768 | 3,584 (7B) |
|
||||
| Layers | 12 | 28 |
|
||||
| Hidden Size | 768 | 4,096 (8B) |
|
||||
| Layers | 12 | 36 |
|
||||
| Tied Embeddings | Yes | No |
|
||||
|
||||
### 需要新增/修改的组件
|
||||
@@ -948,16 +948,16 @@ pub struct Qwen3DecoderLayer {
|
||||
### 显存预算 (BF16, 单卡 5090 32GB)
|
||||
|
||||
```
|
||||
模型权重: 7B × 2B = ~14 GB
|
||||
KV cache: 28 layers × 2(KV) × 8 heads × 4096 tokens × 128 dim × 2B ≈ 4.5 GB
|
||||
模型权重: 8B × 2B = ~16 GB
|
||||
KV cache: 36 layers × 2(KV) × 8 heads × 4096 tokens × 128 dim × 2B ≈ 5.6 GB
|
||||
Activation (单请求): ~1 GB
|
||||
────────────────────────
|
||||
总计: ~19.5 GB (单请求),剩余 ~12 GB 可用于更多并发
|
||||
总计: ~22.6 GB (单请求),剩余 ~10 GB 可用于更多并发
|
||||
```
|
||||
|
||||
### 测试验收
|
||||
|
||||
- [ ] 加载 Qwen3-7B 权重到单张 5090,打印模型结构和参数量
|
||||
- [ ] 加载 Qwen3-8B 权重到单张 5090,打印模型结构和参数量
|
||||
- [ ] Prefill logits 与 HF transformers 对比: 输入 "你好" → top-5 logits 一致
|
||||
- [ ] 英文生成: "What is the capital of France?" → 生成合理回答
|
||||
- [ ] 中文生成: "请介绍一下量子计算" → 生成通顺中文
|
||||
@@ -1196,7 +1196,7 @@ GET /health # 健康检查
|
||||
**Chat Completion Request**:
|
||||
```json
|
||||
{
|
||||
"model": "qwen3-7b",
|
||||
"model": "qwen3-8b",
|
||||
"messages": [
|
||||
{"role": "system", "content": "You are a helpful assistant."},
|
||||
{"role": "user", "content": "What is 1+1?"}
|
||||
@@ -1211,13 +1211,13 @@ GET /health # 健康检查
|
||||
|
||||
**SSE Streaming Response**:
|
||||
```
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-7b","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-8b","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
|
||||
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-7b","choices":[{"index":0,"delta":{"content":"The"},"finish_reason":null}]}
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-8b","choices":[{"index":0,"delta":{"content":"The"},"finish_reason":null}]}
|
||||
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-7b","choices":[{"index":0,"delta":{"content":" answer"},"finish_reason":null}]}
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-8b","choices":[{"index":0,"delta":{"content":" answer"},"finish_reason":null}]}
|
||||
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-7b","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
|
||||
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen3-8b","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
|
||||
|
||||
data: [DONE]
|
||||
```
|
||||
@@ -1228,7 +1228,7 @@ data: [DONE]
|
||||
"id": "chatcmpl-xxx",
|
||||
"object": "chat.completion",
|
||||
"created": 1234567890,
|
||||
"model": "qwen3-7b",
|
||||
"model": "qwen3-8b",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"message": {"role": "assistant", "content": "The answer is 2."},
|
||||
@@ -1278,7 +1278,7 @@ Client (curl / Python OpenAI SDK)
|
||||
```bash
|
||||
curl http://localhost:8080/v1/chat/completions \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model":"qwen3-7b","messages":[{"role":"user","content":"Hello"}],"stream":true}'
|
||||
-d '{"model":"qwen3-8b","messages":[{"role":"user","content":"Hello"}],"stream":true}'
|
||||
```
|
||||
看到 SSE 逐 token 输出
|
||||
|
||||
@@ -1287,7 +1287,7 @@ Client (curl / Python OpenAI SDK)
|
||||
from openai import OpenAI
|
||||
client = OpenAI(base_url="http://localhost:8080/v1", api_key="unused")
|
||||
for chunk in client.chat.completions.create(
|
||||
model="qwen3-7b",
|
||||
model="qwen3-8b",
|
||||
messages=[{"role": "user", "content": "What is 1+1?"}],
|
||||
stream=True
|
||||
):
|
||||
@@ -1302,12 +1302,26 @@ Client (curl / Python OpenAI SDK)
|
||||
|
||||
---
|
||||
|
||||
## Phase 14: Flash Attention v2
|
||||
## Phase 14: Flash Attention (FA2 for SM120)
|
||||
|
||||
**Crate**: `xserv-kernels`
|
||||
**CUDA 源码**: `csrc/attention/flash_attention.cu`
|
||||
|
||||
**目标**: 实现 Flash Attention v2 的 CUDA kernel,大幅降低 attention 的显存占用并提升速度。
|
||||
**目标**: 实现 Flash Attention 的 CUDA kernel,大幅降低 attention 的显存占用并提升速度。
|
||||
|
||||
### 硬件适配说明
|
||||
|
||||
Flash Attention 已发展到第 4 代 (FA4, arxiv 2603.05451),但各版本有明确的硬件依赖:
|
||||
|
||||
| 版本 | 目标架构 | 关键硬件特性 | RTX 5090 兼容 |
|
||||
|------|---------|------------|--------------|
|
||||
| FA2 | 通用 CUDA (SM75+) | 标准 shared memory + HMMA | **是** ✅ |
|
||||
| FA3 | Hopper SM90 (H100) | TMA + WGMMA + warp specialization | 否 |
|
||||
| FA4 | Blackwell SM100 (B200/B300) | TMEM + async MMA + 2-CTA mode | 否 |
|
||||
|
||||
**RTX 5090 (SM120, CC 12.0) 使用的是消费级 Blackwell 架构 (GB202),与数据中心 Blackwell (B200, SM100) 是不同的硅片设计。SM120 物理上没有 TMEM (Tensor Memory) 子系统,因此 FA4 的 kernel 无法在 5090 上运行。这不是软件限制,是硬件级差异。**
|
||||
|
||||
因此本项目实现 **FA2 算法**,使用标准 CUDA (shared memory + HMMA)。FA2 的核心优化——online softmax tiling、O(1) 显存占用——在任何架构上都有效。
|
||||
|
||||
### 核心思想
|
||||
|
||||
@@ -1323,16 +1337,18 @@ Flash Attention 的解法:
|
||||
- 将 Q, K, V 分成 tiles,在 SRAM (shared memory) 中计算
|
||||
- 使用 **online softmax trick**: 边算边更新 running max 和 running sum
|
||||
|
||||
### 算法 (Forward Pass)
|
||||
### 算法 (Forward Pass, FA2)
|
||||
|
||||
FA2 相比 FA1 的改进: 外层循环遍历 Q tiles (而非 K/V),减少 HBM 读写次数。
|
||||
|
||||
```
|
||||
Br, Bc = tile sizes for Q and K/V respectively
|
||||
|
||||
for each Q tile (q_start..q_start+Br):
|
||||
for each Q tile (q_start..q_start+Br): ← 外层: Q tiles
|
||||
load Q_tile [Br, D] to shared memory
|
||||
initialize: O_tile = 0, l = 0, m = -inf // running sum and max
|
||||
initialize: O_tile = 0, l = 0, m = -inf // running sum and max
|
||||
|
||||
for each K,V tile (kv_start..kv_start+Bc):
|
||||
for each K,V tile (kv_start..kv_start+Bc): ← 内层: K/V tiles
|
||||
load K_tile [Bc, D], V_tile [Bc, D] to shared memory
|
||||
|
||||
// Compute attention scores for this tile pair
|
||||
@@ -1345,6 +1361,8 @@ for each Q tile (q_start..q_start+Br):
|
||||
m_new = max(m, rowmax(S_tile)) // new running max
|
||||
P_tile = exp(S_tile - m_new) // safe exp
|
||||
l_new = exp(m - m_new) * l + rowsum(P_tile) // update running sum
|
||||
|
||||
// Rescale and accumulate output
|
||||
O_tile = diag(exp(m - m_new)) * O_tile + P_tile @ V_tile
|
||||
m = m_new
|
||||
l = l_new
|
||||
@@ -1356,9 +1374,12 @@ for each Q tile (q_start..q_start+Br):
|
||||
### 实现要点
|
||||
|
||||
1. **Tile 大小选择**:
|
||||
- 受限于 shared memory (5090 Blackwell CC 12.0: 需要实测确认 per-SM shared memory 上限)
|
||||
- 需要同时存 Q_tile, K_tile, V_tile, S_tile
|
||||
- 典型值: Br=Bc=128 for D=128, BF16
|
||||
- 5090 SM120: shared memory per SM = 100 KB (需实测确认)
|
||||
- 需同时存 Q_tile, K_tile, V_tile, S_tile
|
||||
- BF16: Q_tile [Br, D] = Br × 128 × 2B; K_tile [Bc, D] = Bc × 128 × 2B
|
||||
- S_tile [Br, Bc] 保持 FP32 = Br × Bc × 4B
|
||||
- 推荐起步: Br=Bc=64, head_dim=128 → 共需 ~100KB shared memory
|
||||
- 优化版: Br=Bc=128 需要更多 shared memory, 可能需要拆分
|
||||
|
||||
2. **Causal mask 优化**:
|
||||
- 如果 K/V tile 完全在 Q tile 的"未来"(kv_start > q_end)→ 跳过整个 tile
|
||||
@@ -1369,10 +1390,14 @@ for each Q tile (q_start..q_start+Br):
|
||||
- Q, K, V 的加载用 BF16(节省 bandwidth)
|
||||
- 最终 O 转回 BF16 写出
|
||||
|
||||
4. **与 Paged Attention 的结合**:
|
||||
- Flash Attention 的 K/V tile 遍历逻辑需要适配间接寻址
|
||||
- 每个 tile 查 block_table 得到物理地址
|
||||
- 这是 "Flash-Decoding" / "FlashInfer" 的核心
|
||||
4. **GQA 支持**:
|
||||
- K/V heads 数量 < Q heads 时,kernel 中做 `kv_head = q_head / num_groups` 索引
|
||||
- 不需要 repeat_kv 操作,直接在 kernel 内部解决
|
||||
|
||||
5. **Decode attention 特化**:
|
||||
- Decode 时 Q 只有 1 行 (Br=1),退化为 vector-matrix attention
|
||||
- 可以写一个专门的 decode attention kernel (类似 FlashDecoding)
|
||||
- 沿 KV sequence 维度做 parallel reduction
|
||||
|
||||
### 测试验收
|
||||
|
||||
@@ -1386,8 +1411,9 @@ for each Q tile (q_start..q_start+Br):
|
||||
| 8192 | OOM? | MB | OOM? | ms |
|
||||
| 32768 | OOM | MB | OOM | ms |
|
||||
|
||||
- [ ] 集成到 Qwen3-7B,端到端 decode latency 对比
|
||||
- [ ] 集成到 Qwen3-8B,端到端 decode latency 对比
|
||||
- [ ] Profile: `ncu` 分析 compute utilization, memory throughput
|
||||
- [ ] GQA 支持: 无 repeat_kv 开销
|
||||
|
||||
---
|
||||
|
||||
@@ -1441,7 +1467,7 @@ ncu --target-processes all --set full ./target/release/xserv-server
|
||||
|
||||
### 测试验收
|
||||
|
||||
- [ ] 安装 vLLM,同一台机器跑 Qwen3-7B
|
||||
- [ ] 安装 vLLM,同一台机器跑 Qwen3-8B
|
||||
- [ ] Benchmark 对比:
|
||||
|
||||
| Metric | vLLM | xserv | Ratio |
|
||||
@@ -1488,7 +1514,7 @@ ncu --target-processes all --set full ./target/release/xserv-server
|
||||
|
||||
- **无损**: rejection sampling 保证输出分布与纯 target model 一致
|
||||
- **加速条件**: draft model 足够快且与 target 分布接近
|
||||
- **Draft model 选择**: Qwen3-0.5B / Qwen3-1.5B 作为 Qwen3-7B 的 draft
|
||||
- **Draft model 选择**: Qwen3-0.5B / Qwen3-1.5B 作为 Qwen3-8B 的 draft
|
||||
|
||||
### KV Cache 处理
|
||||
|
||||
@@ -1578,7 +1604,7 @@ Row Parallel: down_proj 按行切分
|
||||
|
||||
### 测试验收
|
||||
|
||||
- [ ] TP=2: Qwen3-7B 输出与单卡 (TP=1) 完全一致
|
||||
- [ ] TP=2: Qwen3-8B 输出与单卡 (TP=1) 完全一致
|
||||
- [ ] TP=4: 每卡权重显存占用约 1/4
|
||||
- [ ] Scaling benchmark (同组 GPU 0-3):
|
||||
|
||||
@@ -1646,7 +1672,7 @@ tensor_fp8 = cast_to_fp8(tensor / scale)
|
||||
| FP8 E4M3 | X.XX | +0.XX |
|
||||
| INT8 weight-only | X.XX | +0.XX |
|
||||
|
||||
- [ ] 显存: FP8 权重占用约 BF16 的一半 (~7 GB for 7B model)
|
||||
- [ ] 显存: FP8 权重占用约 BF16 的一半 (~8 GB for 8B model)
|
||||
- [ ] 性能: FP8 GEMM throughput vs BF16 GEMM
|
||||
|
||||
---
|
||||
@@ -1727,7 +1753,7 @@ Text → Tokenizer → Text Tokens ────────────→
|
||||
| 里程碑 | Phase | 验收标准 |
|
||||
|--------|-------|---------|
|
||||
| ① GPT-2 推理 | 8 | CLI 输入 prompt, GPT-2 生成连贯文本, logits 与 PyTorch 一致 |
|
||||
| ② Qwen3-7B 推理 | 10 | 7B 模型中英文对话, 多轮 chat template 正确 |
|
||||
| ② Qwen3-8B 推理 | 10 | 8B 模型中英文对话, 多轮 chat template 正确 |
|
||||
| ③ E2E API | 13 | HTTP streaming API, Python OpenAI SDK 可调用, 10 并发正确 |
|
||||
| ④ 性能达标 | 15 | throughput >= 50% vLLM, profiling 报告完成 |
|
||||
| ⑤ 多卡推理 | 17 | TP=2/4 同组 GPU 推理正确, scaling benchmark 完成 |
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# Phase 10: Qwen3-7B Support — Design Document (Milestone ②)
|
||||
# Phase 10: Qwen3-8B Support — Design Document (Milestone ②)
|
||||
|
||||
## Goal
|
||||
|
||||
扩展模型定义支持 Qwen3-7B 架构,验证输出正确性。与 GPT-2 的关键差异:RMSNorm、RoPE、GQA、SwiGLU、不共享 embedding。
|
||||
扩展模型定义支持 Qwen3-8B 架构,验证输出正确性。与 GPT-2 的关键差异:RMSNorm、RoPE、GQA、SwiGLU、不共享 embedding。
|
||||
|
||||
## 架构差异 (GPT-2 → Qwen3)
|
||||
|
||||
| 特性 | GPT-2 | Qwen3-7B |
|
||||
| 特性 | GPT-2 | Qwen3-8B |
|
||||
|------|-------|----------|
|
||||
| Norm | LayerNorm(gamma, beta) | RMSNorm(gamma only) |
|
||||
| Position | Learned absolute (wpe) | RoPE (no params) |
|
||||
@@ -15,8 +15,8 @@
|
||||
| FFN | 2 Linear (fc, proj) + GELU | 3 Linear (gate, up, down) + SwiGLU |
|
||||
| Weight layout | [in, out] (Conv1D style) | [out, in] (standard Linear) |
|
||||
| Tied embeddings | Yes | No (separate lm_head) |
|
||||
| hidden_size | 768 | 3584 |
|
||||
| num_layers | 12 | 28 |
|
||||
| hidden_size | 768 | 4096 |
|
||||
| num_layers | 12 | 36 |
|
||||
| head_dim | 64 | 128 |
|
||||
|
||||
## Weight Names (HuggingFace)
|
||||
@@ -67,17 +67,17 @@ out = down_proj(out) # [S, 18944] @ [18944, 3584]^T → [S, 3584]
|
||||
## 显存预算 (BF16, 单卡 5090)
|
||||
|
||||
```
|
||||
权重: 7B × 2B = ~14 GB (BF16)
|
||||
7B × 4B = ~28 GB (FP32) — 不够! 必须用 BF16
|
||||
权重: 8B × 2B = ~16 GB (BF16)
|
||||
8B × 4B = ~32 GB (FP32) — 不够! 必须用 BF16
|
||||
KV cache (S=256, B=1): ~0.1 GB
|
||||
总计: ~14 GB (BF16), 单卡可运行
|
||||
总计: ~16 GB (BF16), 单卡可运行
|
||||
```
|
||||
|
||||
**关键**: Qwen3-7B 必须用 BF16 才能在单张 5090 (32GB) 上运行。当前 GPT-2 用 FP32,需要支持 BF16 forward pass。
|
||||
**关键**: Qwen3-8B 必须用 BF16 才能在单张 5090 (32GB) 上运行。当前 GPT-2 用 FP32,需要支持 BF16 forward pass。
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
1. 下载 Qwen3-7B 模型 (BF16, ~14GB)
|
||||
1. 下载 Qwen3-8B 模型 (BF16, ~14GB)
|
||||
2. 实现 Qwen3 模型结构 (qwen3.rs)
|
||||
3. 支持 BF16 forward pass (linear_transpose for [out, in] weights)
|
||||
4. 实现 GQA (K/V repeat in split)
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
# Phase 11: Paged Attention + KV Cache Manager — Design Document
|
||||
# Phase 11: GPU-Resident KV Cache — Design Document
|
||||
|
||||
> **注意**: 原计划为 "Paged Attention + KV Cache Manager",实际实现为 GPU 连续预分配 KV cache(非 paged)。Paged allocation 留待后续优化。
|
||||
|
||||
## Goal
|
||||
|
||||
将 KV cache 从 CPU Vec 迁移到 GPU,使用 block-based paging 管理显存。消除每步 decode 的 CPU round-trip(当前 KV cache 最大性能瓶颈之一)。
|
||||
将 KV cache 从 CPU Vec 迁移到 GPU,消除每步 decode 的 CPU round-trip(当前 KV cache 最大性能瓶颈之一)。
|
||||
|
||||
## 当前问题
|
||||
|
||||
|
||||
@@ -150,4 +150,8 @@ HTTP Handler Engine Thread
|
||||
|
||||
## 当前状态
|
||||
|
||||
**未实现**。当前是 FIFO 串行,一次只处理一个请求。本文档是实现的设计规格。
|
||||
**已实现: iteration-level scheduling**。多请求可以并发进入 batch (max_batch_size),新请求在 mid-generation 动态加入。Prefill 和 decode 阶段在每轮迭代内分离处理。
|
||||
|
||||
**未实现: batched GPU forward**。每个 seq 的 model forward 仍是串行调用 (per-seq forward_gpu_cache)。真正的 batched decode (多 seq 的 token 合并为一次 GPU forward) 需要 Flash Attention 的 variable-length attention 支持。Phase 14 实现了 FA2 kernel,为后续 batched forward 提供了基础。
|
||||
|
||||
**验证**: 8 个并发请求 (max_batch=4) 总 wall clock 22.5s,各请求延迟之和 135.0s,调度加速 6.0x。Server log 确认 `decode batch_size=4`。
|
||||
|
||||
167
docs/14-flash-attention.md
Normal file
167
docs/14-flash-attention.md
Normal file
@@ -0,0 +1,167 @@
|
||||
# Phase 14: Flash Attention 2 for SM120 — Design Document
|
||||
|
||||
## Goal
|
||||
|
||||
用自写的 Flash Attention 2 CUDA kernel 替换 naive attention (Phase 5)。消除 O(S²) 显存分配,支持 GQA kernel 内部索引(消除 repeat_kv 开销)。
|
||||
|
||||
## 硬件约束: FA4 不适用于 RTX 5090
|
||||
|
||||
Flash Attention 已发展到第 4 代 (FA4, arxiv 2603.05451),但各版本有明确硬件依赖:
|
||||
|
||||
| 版本 | 目标架构 | 关键硬件特性 | RTX 5090 (SM120) |
|
||||
|------|---------|------------|-----------------|
|
||||
| FA2 | 通用 CUDA (SM75+) | shared memory + HMMA | **兼容** |
|
||||
| FA3 | Hopper SM90 (H100) | TMA + WGMMA + warp specialization | 不兼容 |
|
||||
| FA4 | Blackwell SM100 (B200/B300) | TMEM + async MMA + 2-CTA mode | 不兼容 |
|
||||
|
||||
RTX 5090 使用消费级 Blackwell (GB202, SM120),与数据中心 Blackwell (B200, SM100) 是不同硅片。SM120 **没有 TMEM (Tensor Memory)**,这是 FA4 kernel 设计的核心硬件依赖。这不是软件限制,是硬件级差异。
|
||||
|
||||
因此本项目实现 **FA2 算法**,使用标准 CUDA (shared memory + 标准 HMMA)。
|
||||
|
||||
## Naive Attention 的问题
|
||||
|
||||
Phase 5 的 naive attention 流程:
|
||||
```
|
||||
k_t = K.transpose(2,3).contiguous() ← 分配 K^T 显存
|
||||
scores = batched_matmul(Q, k_t) ← 分配 [B,H,S,S] score 矩阵 (O(S²) 显存)
|
||||
scores = scale(scores, 1/sqrt(d)) ← 逐元素 kernel
|
||||
causal_mask(scores) ← 逐元素 kernel
|
||||
weights = softmax(scores) ← 分配 [B,H,S,S] weight 矩阵
|
||||
output = batched_matmul(weights, V) ← 最终结果
|
||||
```
|
||||
|
||||
问题:
|
||||
1. **显存 O(S²)**: score 和 weight 矩阵各需 `B × H × S × S × dtype_size`。S=2048, H=32, BF16 → 256 MB。S=8192 → 4 GB。
|
||||
2. **GQA 预处理**: 在调用 attention 前需要 `repeat_kv_gpu` 将 K/V 从 8 heads 扩展到 32 heads,每层额外分配和拷贝。
|
||||
3. **多次 kernel launch**: scale, mask, softmax 各一次 kernel launch + global memory round-trip。
|
||||
4. **K^T materialization**: `K.transpose().contiguous()` 需要分配和拷贝。
|
||||
|
||||
## FA2 算法
|
||||
|
||||
核心思想: **不 materialize S×S 矩阵**。将 Q, K, V 分成 tiles,在 shared memory (SRAM) 中计算,使用 **online softmax trick** 边算边更新 running max 和 sum。
|
||||
|
||||
FA2 (Dao 2023) 相比 FA1 的改进: 外层循环遍历 Q tiles (而非 K/V),减少 HBM 读写次数,提高并行性。
|
||||
|
||||
```
|
||||
scale = 1 / sqrt(head_dim)
|
||||
|
||||
for each Q tile (q_start..q_start + BR): // 外层: Q tiles
|
||||
load Q_tile [BR, D] to shared memory (一次加载,内层复用)
|
||||
init per-row: O[D] = 0, m = -inf, l = 0
|
||||
|
||||
for each K/V tile j (kv_start..kv_start + BC): // 内层: K/V tiles
|
||||
// Causal tile-skip: 如果整个 K tile 在 Q tile "未来",跳过
|
||||
if causal && kv_start > max_q_pos + kv_offset: skip
|
||||
|
||||
load K_tile [BC, D] to shared memory
|
||||
S = Q_tile @ K_tile^T * scale // [BR, BC], in registers
|
||||
if causal: mask S[r][c] = -inf where kv_pos > q_pos
|
||||
|
||||
// Online softmax update
|
||||
m_new = max(m, rowmax(S))
|
||||
P = exp(S - m_new)
|
||||
l_new = exp(m - m_new) * l + rowsum(P)
|
||||
O = exp(m - m_new) * O // rescale accumulator
|
||||
|
||||
load V_tile [BC, D] to shared memory (复用 K 的空间)
|
||||
O += P @ V_tile // accumulate
|
||||
|
||||
m = m_new, l = l_new
|
||||
|
||||
O = O / l // final normalize
|
||||
write O[BR, D] to HBM (convert FP32 → BF16)
|
||||
```
|
||||
|
||||
## 实现细节
|
||||
|
||||
### Kernel 配置
|
||||
|
||||
| 参数 | 值 | 说明 |
|
||||
|------|---|------|
|
||||
| BR (Q tile rows) | 64 | Q tile 大小 |
|
||||
| BC (K/V tile rows) | 64 | K/V tile 大小 |
|
||||
| head_dim | 运行时参数 (≤128) | 支持 64 (GPT-2) 和 128 (Qwen3) |
|
||||
| Block size | 128 threads | 64 线程各 own 一行 Q,其余协助加载 |
|
||||
| Grid | (q_tiles, batch × num_q_heads) | 每个 block 处理一个 Q tile + 一个 head |
|
||||
|
||||
### Shared Memory (BF16 存储)
|
||||
|
||||
```
|
||||
smem_q [BR × head_dim] BF16 = 64 × 128 × 2 = 16 KB (加载一次,内层复用)
|
||||
smem_kv[BC × head_dim] BF16 = 64 × 128 × 2 = 16 KB (K 和 V 交替使用)
|
||||
────────────────────────────────────────────
|
||||
Total: 32 KB (SM120 默认 48 KB,余量充足)
|
||||
```
|
||||
|
||||
### 线程映射
|
||||
|
||||
- Thread 0..63: 各 own Q_tile 的一行。负责该行的全部计算:dot products、softmax、PV 累加。
|
||||
- Thread 64..127: 协助 shared memory 加载 (K/V tile),不参与计算。
|
||||
- 加载模式: 每个 thread 加载 `(BR × head_dim) / 128 = 64` 个 BF16 元素。
|
||||
|
||||
### Per-Thread Register 使用
|
||||
|
||||
```
|
||||
O_acc[128] FP32 = 512 bytes (128 regs) — 输出累加器
|
||||
P[64] FP32 = 256 bytes (64 regs) — 当前 tile 的 softmax 后权重
|
||||
m, l FP32 = 8 bytes (2 regs) — online softmax running state
|
||||
循环变量 + 临时 ≈ 16 regs
|
||||
────────────────────────────────────────────
|
||||
Total: ~210 regs/thread (max 255,在限制内)
|
||||
```
|
||||
|
||||
### GQA 支持
|
||||
|
||||
每个 thread block 处理一个 Q head,通过 `kv_head = q_head / (num_q_heads / num_kv_heads)` 映射到对应的 KV head。K/V 的数据指针直接指向 KV head 的存储,无需 repeat_kv。
|
||||
|
||||
```
|
||||
// 32 Q heads, 8 KV heads → heads_per_group = 4
|
||||
// Q head 0,1,2,3 → KV head 0
|
||||
// Q head 4,5,6,7 → KV head 1
|
||||
// ...
|
||||
kv_head = q_head / heads_per_group;
|
||||
K_ptr = K + (batch * num_kv_heads + kv_head) * kv_len * head_dim;
|
||||
```
|
||||
|
||||
### Causal Mask
|
||||
|
||||
两级优化:
|
||||
1. **Tile-level skip**: 如果 `kv_tile_start > max_q_pos + kv_offset`,整个 K/V tile 都在未来,跳过(减少 ~50% 计算)。
|
||||
2. **Element-level mask**: 在 tile 内部,`if kv_pos > q_pos + kv_offset: S = -inf`。
|
||||
|
||||
`kv_offset = kv_len - q_len` 处理 decode 时 KV cache 长于 Q 的情况。
|
||||
|
||||
## 与 Naive Attention 的对比
|
||||
|
||||
| 特性 | Naive (Phase 5) | FA2 (Phase 14) |
|
||||
|------|----------------|----------------|
|
||||
| 显存 | O(B × H × S²) | O(B × H × S × D) |
|
||||
| GQA | 需要 repeat_kv (分配+拷贝) | Kernel 内部索引 (零开销) |
|
||||
| K^T | 需要 transpose+contiguous | Kernel 内部计算 |
|
||||
| Kernel launches | 6 (matmul, scale, mask, softmax, matmul, ...) | 1 (单个 fused kernel) |
|
||||
| S=8192 可行性 | OOM (~4 GB score matrix) | 可行 (32 KB shared memory) |
|
||||
|
||||
## 源码结构
|
||||
|
||||
```
|
||||
csrc/attention/flash_attention.cu — FA2 kernel (BF16 in, FP32 accumulate, BF16 out)
|
||||
crates/xserv-kernels/src/attention.rs — flash_attention() Rust wrapper + 原 attention() 保留
|
||||
crates/xserv-model/src/qwen3.rs — forward_gpu_cache 调用 flash_attention
|
||||
```
|
||||
|
||||
## 已知局限与后续优化方向
|
||||
|
||||
1. **Decode (Q_len=1) 效率低**: BR=64 线程中只有 1 个 active(owns_row)。应写专用 decode attention kernel,沿 KV 维度 parallel reduction。
|
||||
2. **无向量化加载**: 当前逐元素 bf16→f32 转换,应改用 `float4` 或 `__nv_bfloat162` 批量加载。
|
||||
3. **Register tiling**: 每个 thread 目前串行计算 dot product (128 MADs per K column)。可改为多线程协作。
|
||||
4. **K/V double buffering**: 可在计算当前 tile 时预加载下一个 tile 到另一半 shared memory。
|
||||
5. **Tile size 调优**: 更大的 tile (BR=128) 可能在长 sequence 时更优,需要 opt-in shared memory。
|
||||
|
||||
## Test Plan
|
||||
|
||||
- [x] 正确性: logits 与 HF transformers 对比 (top-1 match 9/10, top-5 overlap 4.0/5)
|
||||
- [x] 生成质量: 52/52 prompt 生成连贯文本,中英文均可
|
||||
- [x] SSE streaming 正常工作
|
||||
- [x] 性能: 12.9 tok/s (vs naive 10.3 tok/s, +25%)
|
||||
- [ ] 长 sequence (S=4096, S=8192): 验证 naive OOM 而 FA2 正常
|
||||
- [ ] ncu profile: compute utilization, memory throughput
|
||||
177
docs/15-performance.md
Normal file
177
docs/15-performance.md
Normal file
@@ -0,0 +1,177 @@
|
||||
# Phase 15: Performance Optimization — Design Document (Milestone ④)
|
||||
|
||||
## Goal
|
||||
|
||||
系统性 profiling + 优化,从 12.9 tok/s (Phase 14 结束) 逼近 RTX 5090 的理论带宽上限 (112 tok/s)。
|
||||
|
||||
## 硬件 Roofline
|
||||
|
||||
RTX 5090 (SM120, CC 12.0) 的 decode 理论极限:
|
||||
|
||||
```
|
||||
模型权重: 16 GB (Qwen3-8B BF16)
|
||||
内存带宽: 1.79 TB/s (GDDR7)
|
||||
理论最优 decode: 16 GB / 1.79 TB/s = 8.9 ms/step = 112 tok/s (batch=1)
|
||||
```
|
||||
|
||||
Decode 阶段 100% memory-bound:每步读取全部 16 GB 权重(252 个 GEMV),计算量可忽略。
|
||||
|
||||
## 瓶颈分析
|
||||
|
||||
Phase 14 结束时性能 12.9 tok/s = 77.5 ms/step,roofline 利用率仅 12%。
|
||||
|
||||
### 量化瓶颈分解
|
||||
|
||||
| 来源 | 估计耗时 | 占比 |
|
||||
|------|---------|------|
|
||||
| cuBLAS M=1 GEMV (252 calls, 带宽利用 ~8%) | ~60 ms | 77% |
|
||||
| 非 matmul 内核 (attention, norm, activation, reshape) | ~8 ms | 10% |
|
||||
| Tensor 分配 + cudaMemset (1440+ allocs/step) | ~5 ms | 7% |
|
||||
| Kernel launch overhead (200+ launches × 5μs) | ~1 ms | 1% |
|
||||
| 其他 (sampling CPU round-trip, etc.) | ~3.5 ms | 5% |
|
||||
|
||||
**核心发现: cuBLAS 对 M=1 GEMM (GEMV) 的带宽利用率极低(~8%),是 9x gap 的根本原因。**
|
||||
|
||||
cuBLAS 设计用于大 M 的 GEMM,对 M=1 场景存在:
|
||||
- Kernel launch dispatch overhead 无法被大量计算掩盖
|
||||
- TensorCore tile (16×16) 无法被 M=1 充分利用
|
||||
- 内部 heuristic 选择了次优算法
|
||||
|
||||
## 优化实施
|
||||
|
||||
### Opt 1: Decode Attention Kernel
|
||||
|
||||
**目标**: 替换 FA2 在 Q_len=1 时的低效路径(64 线程仅 1 个 active)。
|
||||
|
||||
**实现** (`csrc/attention/flash_attention.cu`):
|
||||
- 专用 decode_attention_bf16_kernel: 256 线程并行沿 KV 序列维度
|
||||
- 每个 thread 加载完整 Q vector (128 dim) 到寄存器
|
||||
- 处理其分配的 KV 位置块: dot product → online softmax
|
||||
- Block-level warp-shuffle + shared memory reduction 合并结果
|
||||
- GQA 支持: kv_head = q_head / heads_per_group
|
||||
|
||||
**效果**: 在当前短序列 (kv_len ≤ 79) 下效果微小——attention 不是瓶颈。在长序列时会显著受益。
|
||||
|
||||
### Opt 2: Fused SiLU×Mul
|
||||
|
||||
**目标**: `silu(gate) * up` 两个 element-wise op 合并为一个 kernel。
|
||||
|
||||
**实现** (`csrc/activation/activations.cu`):
|
||||
```
|
||||
Before: read gate → silu → write temp → read temp + up → mul → write out
|
||||
After: read gate + up → silu(gate) * up → write out
|
||||
Saved: 1 HBM read + 1 HBM write per element
|
||||
```
|
||||
|
||||
**效果**: 每层省 1 次 HBM round-trip,36 层总计可观但在 GEMV 瓶颈下被掩盖。
|
||||
|
||||
### Opt 3: Fused Add+RMSNorm
|
||||
|
||||
**目标**: `x = residual + attn_proj; normed = rmsnorm(x)` 合并为一个 kernel。
|
||||
|
||||
**实现** (`csrc/normalization/rmsnorm.cu`):
|
||||
```
|
||||
Before: read residual + x → add → write sum → read sum + gamma → norm → write out
|
||||
After: read residual + x + gamma → add + norm → write sum + normed
|
||||
Saved: 1 full HBM round-trip per attention block
|
||||
```
|
||||
|
||||
### Opt 4: Batched Decode Forward ⭐
|
||||
|
||||
**目标**: 多序列 decode token 合并为 M=batch_size 的 GEMM,提升 cuBLAS 效率。
|
||||
|
||||
**实现** (`crates/xserv-model/src/qwen3.rs` + `crates/xserv-server/src/engine.rs`):
|
||||
- 新增 `Qwen3::forward_decode_batch(tokens, positions, caches)`
|
||||
- Batched ops: embedding, norm, projections, FFN — [B, hidden] × [hidden, X]
|
||||
- Per-seq ops: RoPE, KV cache, attention(各序列位置/长度不同)
|
||||
- Row extraction (`row_view`) + concatenation (`concat_rows`) 在 batched/per-seq 间切换
|
||||
- Engine Step 4b: batch≥2 时自动使用 batched decode
|
||||
|
||||
**效果**: batch=4 时 cuBLAS 从 1008× M=1 → 252× M=4,吞吐 35.1 tok/s (vs serial 13.2)。
|
||||
|
||||
### Opt 5: Custom GEMV Kernel ⭐⭐⭐ (决定性优化)
|
||||
|
||||
**目标**: 替换 cuBLAS 的 M=1 GEMV,手写带宽最优化 kernel。
|
||||
|
||||
**实现** (`csrc/gemm/gemv.cu`):
|
||||
```
|
||||
设计: K-split tiled GEMV
|
||||
- TILE_N = 128 (output columns per block, one thread per column)
|
||||
- TILE_K = 256 (K-dimension slice per block)
|
||||
- BLOCK_SIZE = 128 threads
|
||||
- Grid: (ceil(N/128), ceil(K/256)) — 对 K=N=4096 得到 512 blocks
|
||||
512 blocks / 170 SMs ≈ 3 blocks/SM (良好 occupancy)
|
||||
|
||||
内存访问:
|
||||
- 相邻线程读 W 矩阵的相邻列 → 完美 coalesced
|
||||
- x vector 加载到 shared memory (每 K-chunk 仅加载一次)
|
||||
- FP32 accumulation via atomicAdd (K-split partial sums)
|
||||
- 独立 kernel 做 FP32→BF16 转换
|
||||
|
||||
调度:
|
||||
- matmul() 中检测 M==1 && dtype==BF16 → 自动使用 custom GEMV
|
||||
- M>1 保持 cuBLAS
|
||||
```
|
||||
|
||||
**效果**: 13.2 → 46.6 tok/s (+253%)。带宽利用率从 ~8% 提升到 ~42%。
|
||||
|
||||
### Opt 6: Tensor::empty() (消除无用 cudaMemset)
|
||||
|
||||
**目标**: kernel 输出 tensor 全量覆写时,跳过分配后的 cudaMemset 清零。
|
||||
|
||||
**实现**:
|
||||
- `Storage::empty()` + `Tensor::empty()`: 分配不清零
|
||||
- 21 个 kernel wrapper (activation, attention, embedding, gemm, norm, softmax, transpose) 从 `zeros` 改为 `empty`
|
||||
- GEMV FP32 accumulator buffer 保持 `cudaMemsetAsync`(atomicAdd 需要零初始化)
|
||||
|
||||
**效果**: 46.6 → 50.3 tok/s (+8%)。消除 ~756 个 cudaMemset/step。
|
||||
|
||||
### Infra: CUDA Graph 基础设施
|
||||
|
||||
- FFI bindings: `cudaStreamBeginCapture`, `cudaGraphInstantiate`, `cudaGraphLaunch`
|
||||
- RAII wrapper: `CudaGraph` (capture/instantiate/launch lifecycle)
|
||||
- 当前未在 forward path 使用(variable kv_len 限制),为后续优化预留
|
||||
|
||||
## Ablation 结果
|
||||
|
||||
dash5, RTX 5090, Qwen3-8B BF16, greedy decode, max_tokens=64:
|
||||
|
||||
| 优化叠加 | tok/s | 增量 | vs HF | Roofline |
|
||||
|---------|-------|------|-------|----------|
|
||||
| Phase 14 baseline (FA2) | 12.9 | — | 36% | 12% |
|
||||
| + Decode attention | 12.9 | +0% | 36% | 12% |
|
||||
| + Fused SiLU×Mul | 13.0 | +1% | 36% | 12% |
|
||||
| + Fused Add+RMSNorm | 13.2 | +2% | 37% | 12% |
|
||||
| + Batched decode (batch=4) | 35.1 | — | 97% | — |
|
||||
| + Custom GEMV (M=1) | 46.6 | +253% | 130% | 42% |
|
||||
| + Tensor::empty | **50.3** | +8% | **140%** | **45%** |
|
||||
|
||||
对比:
|
||||
|
||||
| 系统 | tok/s | Roofline |
|
||||
|------|-------|----------|
|
||||
| HF transformers | 36.0 | 32% |
|
||||
| **xserv (Phase 15)** | **50.3** | **45%** |
|
||||
| 理论极限 (1.79 TB/s) | 112.0 | 100% |
|
||||
|
||||
## 剩余 55% Roofline Gap 分析
|
||||
|
||||
| 来源 | 估计占比 | 优化方向 |
|
||||
|------|---------|---------|
|
||||
| GEMV kernel 非满带宽 (atomicAdd contention, K-split overhead) | 25% | 无 K-split GEMV (更大 block), 向量化加载 |
|
||||
| Non-matmul kernels (attention, norm, RoPE, reshape) | 15% | Fused layer kernel, 更高效的 decode attention |
|
||||
| Kernel launch overhead (200+ launches/step) | 5% | CUDA Graphs (需解决 variable kv_len) |
|
||||
| Memory allocator overhead (Arc, SmallVec per tensor) | 5% | Pre-allocated decode workspace |
|
||||
| Sampling D2H copy (pipeline stall) | 3% | GPU-side argmax kernel |
|
||||
| 其他 (host-side logic, channel overhead) | 2% | — |
|
||||
|
||||
## 下一步
|
||||
|
||||
Phase 15 的 Milestone ④ 目标 (50% of HF) 已远超 — 达到 140% of HF, 45% of roofline。
|
||||
|
||||
后续优化路径(按 ROI 排序):
|
||||
1. **无 K-split GEMV**: 消除 atomicAdd,减少 kernel launches → 预期 +15-20%
|
||||
2. **向量化 GEMV loads**: float4 加载 W 矩阵 → 预期 +10%
|
||||
3. **Pre-allocated workspace**: 消除 Tensor 对象分配开销 → 预期 +5%
|
||||
4. **CUDA Graphs**: 需要 fixed-shape decode path → 预期 +5%
|
||||
5. **GPU-side sampling**: 消除 logits D2H pipeline stall → 预期 +3%
|
||||
214
docs/TO-BE-FIXED.md
Normal file
214
docs/TO-BE-FIXED.md
Normal file
@@ -0,0 +1,214 @@
|
||||
# xserv — To Be Fixed (2026-05-23 审查更新)
|
||||
|
||||
> 由全面审查产出的修复清单。每项修复有明确验收标准。
|
||||
> 优先级: P0 (阻塞可用性) > P1 (严重bug/性能) > P2 (重要改进) > P3 (设计债务)
|
||||
|
||||
---
|
||||
|
||||
## 第一批:P0 — 阻塞可用性
|
||||
|
||||
### FIX-01: 全局 cuBLAS handle [P0-性能] ❌未修
|
||||
|
||||
**问题**: `gemm.rs` 中 `matmul` (line 146) 和 `batched_matmul` (line 224) 每次调用都 `CublasContext::new()` 创建+销毁 handle。Qwen3-8B 一次 forward ~252 次 matmul。
|
||||
|
||||
**修复要求**:
|
||||
- 使用 thread-local 单例 cuBLAS handle
|
||||
- handle 生命周期覆盖整个进程
|
||||
- `matmul` / `batched_matmul` 函数体内不再有 `CublasContext::new()`
|
||||
|
||||
**验收标准**:
|
||||
1. `grep -n "CublasContext::new" crates/xserv-kernels/src/gemm.rs` 只出现 1 次(thread_local 初始化处)
|
||||
2. 编译通过,现有 gemm_test 全部通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-16: EOS token 泄漏到 API 响应 [P0-功能] ❌新发现
|
||||
|
||||
**问题**: `engine.rs:218` 中 `emit_token` 先发 `GenerateEvent::Token { text: "<|im_end|>" }` 再发 `Done`。`api.rs:110-111` 把所有 Token text 拼到 content 里,导致最终响应包含 `<|im_end|>` 乱码。
|
||||
|
||||
**修复要求**:
|
||||
- `emit_token` 中,当 token 是 EOS 时,不发送 Token event(或发送空 text),直接发 Done
|
||||
- 或者: API 层收到 Done 时丢弃最后一个 token 的 text(如果 finish_reason == "stop")
|
||||
|
||||
**验收标准**:
|
||||
1. 发送请求,响应 content 不包含 `<|im_end|>` 或其他 special token 文本
|
||||
2. streaming 模式下最后一个 content chunk 不是 EOS 文本
|
||||
3. 编译通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-17: max_seq_len 硬编码 256 [P0-功能] ❌新发现
|
||||
|
||||
**问题**: `engine.rs:53` 硬编码 `let max_seq_len = 256`,超过就 KV cache panic。
|
||||
|
||||
**修复要求**:
|
||||
- `Engine::load` 接受 `max_seq_len` 参数(或从 config 读取,上限为 config.max_seq_len())
|
||||
- `main.rs` 中通过命令行参数或环境变量传入,默认值改为 2048
|
||||
- 同步更新 RoPE cache 上限(当前 `qwen3.rs:45` 限制 8192,应与 max_seq_len 一致)
|
||||
|
||||
**验收标准**:
|
||||
1. `grep -n "let max_seq_len = 256" crates/xserv-server/` 返回 0 行
|
||||
2. 启动 server 时 `--max-seq-len 4096` 可用
|
||||
3. 编译通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-18: max_tokens 无上限校验 [P0-功能] ❌新发现
|
||||
|
||||
**问题**: API 不校验 `max_tokens`,客户端可发 `max_tokens: 1000000` 导致 KV cache panic。
|
||||
|
||||
**修复要求**:
|
||||
- `api.rs` 中 clamp `max_tokens` 到 `engine.max_seq_len - prompt_tokens.len()`
|
||||
- 如果 prompt 已超过 max_seq_len,返回 400 错误
|
||||
|
||||
**验收标准**:
|
||||
1. 发送 `max_tokens: 999999`,不 panic,正常生成到 seq_len 上限
|
||||
2. 发送超长 prompt(> max_seq_len),返回 HTTP 400
|
||||
3. 编译通过
|
||||
|
||||
---
|
||||
|
||||
## 第二批:P1 — 严重 bug/性能
|
||||
|
||||
### FIX-07: 使用 CachingAllocator [P1-性能] ❌未修
|
||||
|
||||
**问题**: `CachingAllocator` 已实现(`allocator.rs`)但从未使用。所有 GPU 分配直接 `cudaMalloc`。
|
||||
|
||||
**修复要求**:
|
||||
- `Tensor::empty` 对 GPU device 使用 `cached_alloc` 而非 `GpuBuffer::alloc`
|
||||
- `GpuBuffer::Drop` 调用 `cached_dealloc` 归还到池(而非 `cudaFree`)
|
||||
- 或者更简单:在 `GpuBuffer::alloc` 内部接入 caching allocator(全局透明替换)
|
||||
|
||||
**验收标准**:
|
||||
1. 连续运行 10 次 decode step,`cudaMalloc` 调用次数应显著低于总分配次数
|
||||
2. 编译通过,现有测试通过
|
||||
3. 推理结果与修复前一致
|
||||
|
||||
---
|
||||
|
||||
### FIX-08: CudaDeviceProp FFI 安全性 [P1-Bug] ❌未修
|
||||
|
||||
**问题**: `ffi.rs:31` 用 `_pad: [u8; 4096]` 猜测 `cudaDeviceProp` struct 大小,CUDA 12.9 可能更大。
|
||||
|
||||
**修复要求**:
|
||||
- 增大 pad 到 `[u8; 8192]` 或使用 `cudaDeviceGetAttribute` 替代 name 查询
|
||||
- 可参考 `device.rs` 中已有的 `cudaDeviceGetAttribute` 用法
|
||||
|
||||
**验收标准**:
|
||||
1. `device_info()` 返回正确的 device name
|
||||
2. 编译通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-09: Tokenizer byte_fallback panic [P1-Bug] ❌未修
|
||||
|
||||
**问题**: `bpe.rs:176-182` 中 Qwen3 tokenizer 遇到不在 vocab 的单字节时 panic。
|
||||
|
||||
**修复要求**:
|
||||
- 当 `byte_fallback == true` 且单字节不在 vocab 时,查找 `<0xNN>` 格式 token
|
||||
- 如果 `<0xNN>` 也不存在,返回 unk_token_id(而非 panic)
|
||||
|
||||
**验收标准**:
|
||||
1. 包含所有 256 个字节值的字符串可以 encode 不 panic
|
||||
2. 编译通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-19: 因果掩码 -1e9 应改为 -inf [P1-Bug] ❌新发现
|
||||
|
||||
**问题**: `csrc/attention/causal_mask.cu:31` 用 `-1e9f` 代替 `-inf`,注释说 "BF16 没有 -inf" 但这是错误的。
|
||||
|
||||
**修复要求**:
|
||||
- BF16 路径改为 `__float2bfloat16(-INFINITY)`
|
||||
- F32 路径改为 `-INFINITY`(如果还没有的话)
|
||||
|
||||
**验收标准**:
|
||||
1. causal mask 中被遮蔽的值为 `-inf`(而非 `-1e9`)
|
||||
2. 编译通过,attention test 通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-20: LayerNorm 数值稳定性 [P1-Bug] ❌新发现
|
||||
|
||||
**问题**: `csrc/normalization/layernorm.cu:19-25` 注释写 "Welford online" 但实际用 `E[x²] - E[x]²`,大均值小方差时会灾难性抵消。
|
||||
|
||||
**修复要求**:
|
||||
- 改为真正的 two-pass 或 Welford online 算法
|
||||
- pass 1: 求 mean; pass 2: 求 variance = E[(x-mean)²]
|
||||
|
||||
**验收标准**:
|
||||
1. 对 mean=1e6, std=1e-3 的输入,layernorm 输出与 PyTorch 一致(relative error < 1e-3)
|
||||
2. 编译通过,现有测试通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-21: LayerNorm/RMSNorm 最小 block size [P1-Bug] ❌新发现
|
||||
|
||||
**问题**: `layernorm.cu:88` 和 `rmsnorm.cu` 对 hidden_size < 32 的输入会崩溃(block_reduce 需要至少一个完整 warp)。
|
||||
|
||||
**修复要求**:
|
||||
- launch 时 `block = max(min(hidden_size, 1024), 32)`
|
||||
|
||||
**验收标准**:
|
||||
1. hidden_size=16 的 layernorm/rmsnorm 不崩溃
|
||||
2. 编译通过
|
||||
|
||||
---
|
||||
|
||||
## 第三批:P2 — 重要改进
|
||||
|
||||
### FIX-22: Engine dummy KV cache 分配 [P2-性能] ❌新发现
|
||||
|
||||
**问题**: `engine.rs:142-148` 每次 batched decode 用 `std::mem::replace` 创建 dummy `GpuKVCache::new(..., 1, ...)` 来绕过 borrow checker,每步分配 `num_layers * 2` 个 GPU buffer。
|
||||
|
||||
**修复要求**:
|
||||
- 将 `running` 从 `Vec<Sequence>` 改为存储方式让 KV cache 可以独立借出
|
||||
- 或使用 `Option<GpuKVCache>` + `.take()` / `.insert()` 避免 dummy 分配
|
||||
|
||||
**验收标准**:
|
||||
1. batched decode 路径不再分配 dummy KV cache
|
||||
2. 编译通过,功能不变
|
||||
|
||||
---
|
||||
|
||||
### FIX-23: RoPE cache 硬限 8192 [P2-功能] ❌新发现
|
||||
|
||||
**问题**: `qwen3.rs:45` `config.max_seq_len().min(8192)` 人为截断。
|
||||
|
||||
**修复要求**:
|
||||
- 去掉 `.min(8192)`,或改为与 engine 的 max_seq_len 一致
|
||||
- 确保 RoPE cache 覆盖实际使用的 max_seq_len
|
||||
|
||||
**验收标准**:
|
||||
1. RoPE cache 长度 >= engine max_seq_len
|
||||
2. 编译通过
|
||||
|
||||
---
|
||||
|
||||
### FIX-15: GPT-2 消除 CPU round-trip [P3-性能] ❌未修
|
||||
|
||||
**问题**: GPT-2 `split_qkv`、`merge_heads`、`add_bias` 全在 CPU 做。优先级低(GPT-2 不是主力模型)。
|
||||
|
||||
---
|
||||
|
||||
## 修复依赖图和执行顺序
|
||||
|
||||
```
|
||||
第一批 P0 (可并行):
|
||||
FIX-01 (cuBLAS handle) ← 独立
|
||||
FIX-16 (EOS 泄漏) ← 独立
|
||||
FIX-17 (max_seq_len) ← 独立,FIX-23 依赖此
|
||||
FIX-18 (max_tokens 校验) ← 依赖 FIX-17(需要知道 max_seq_len)
|
||||
|
||||
第二批 P1 (可并行):
|
||||
FIX-07 (caching allocator) ← 独立
|
||||
FIX-08 (CudaDeviceProp) ← 独立
|
||||
FIX-09 (byte_fallback) ← 独立
|
||||
FIX-19 (causal mask -inf) ← 独立
|
||||
FIX-20 (layernorm 稳定性) ← 独立
|
||||
FIX-21 (min block size) ← 独立
|
||||
|
||||
第三批 P2:
|
||||
FIX-22 (dummy KV cache) ← 独立
|
||||
FIX-23 (RoPE cache) ← 依赖 FIX-17
|
||||
```
|
||||
109
docs/benchmarks/phase14-flash-attention.md
Normal file
109
docs/benchmarks/phase14-flash-attention.md
Normal file
@@ -0,0 +1,109 @@
|
||||
# Phase 14 Benchmark: Flash Attention 2
|
||||
|
||||
**Date**: 2026-05-22
|
||||
**Hardware**: RTX 5090 (32GB GDDR7, SM120 CC 12.0, 170 SMs)
|
||||
**Model**: Qwen3-8B (BF16, 36 layers, 4096 hidden, 32 Q / 8 KV GQA heads, head_dim=128)
|
||||
**Config**: greedy decoding (temperature=0), max_tokens=64, single-request serial
|
||||
|
||||
## Correctness
|
||||
|
||||
Logits comparison with HuggingFace transformers (10 prompts, raw text without ChatML):
|
||||
|
||||
| Metric | Result |
|
||||
|--------|--------|
|
||||
| Prefill Top-1 match vs HF | **9/10 (90%)** |
|
||||
| Avg Top-5 overlap vs HF | **4.0/5** |
|
||||
| Result vs pre-FA2 naive attention | **Identical** (same 9/10 top-1, same 4.0/5 overlap) |
|
||||
|
||||
The single top-1 mismatch ("Explain quantum computing.") has logits differing by 0.125
|
||||
(22.000 vs 21.875) — within BF16 precision. The top-5 sets are identical (5/5 overlap).
|
||||
|
||||
FA2 introduces no precision degradation compared to the naive attention path.
|
||||
|
||||
## API Generation
|
||||
|
||||
52 diverse prompts (English, Chinese, code) via `/v1/chat/completions`:
|
||||
|
||||
| Metric | Result |
|
||||
|--------|--------|
|
||||
| Success rate | **52/52 (100%)** |
|
||||
| SSE streaming | **Working** (role chunk, content chunks, finish_reason, [DONE]) |
|
||||
| Usage stats | Correct (prompt_tokens + completion_tokens = total_tokens) |
|
||||
|
||||
## Performance
|
||||
|
||||
### xserv vs HuggingFace transformers
|
||||
|
||||
8 prompts (short/medium/long) × max_tokens=64, greedy:
|
||||
|
||||
| Category | Prompt Tokens | xserv (tok/s) | HF (tok/s) | Ratio |
|
||||
|----------|--------------|---------------|------------|-------|
|
||||
| Short (~12 tok) | 12-14 | 12.5 | 38.5 | 0.32x |
|
||||
| Medium (~28 tok) | 27-28 | 13.6 | 44.1 | 0.31x |
|
||||
| Long (~60 tok) | 58-64 | 13.0 | 36.0 | 0.36x |
|
||||
| **Overall** | — | **12.9** | **36.6** | **0.35x** |
|
||||
|
||||
### Phase-over-Phase Improvement
|
||||
|
||||
| Phase | Attention | repeat_kv | tok/s | vs HF |
|
||||
|-------|-----------|-----------|-------|-------|
|
||||
| 10 | Naive (O(S²), cuBLAS batched) | CPU round-trip | 6.9 | 15% |
|
||||
| 11 | Naive + GPU KV cache | GPU repeat_kv | 10.3 | 30% |
|
||||
| **14** | **FA2 (O(1), fused kernel)** | **None (GQA in kernel)** | **12.9** | **35%** |
|
||||
|
||||
Phase 14 vs Phase 11: **+25% throughput** (10.3 → 12.9 tok/s).
|
||||
|
||||
### Improvement Breakdown (estimated)
|
||||
|
||||
| Factor | Contribution |
|
||||
|--------|-------------|
|
||||
| Eliminating repeat_kv GPU alloc + copy (per layer) | ~10% |
|
||||
| Eliminating K^T transpose + contiguous | ~5% |
|
||||
| Eliminating S×S score matrix alloc | ~5% |
|
||||
| Fused kernel (1 launch vs 6) | ~5% |
|
||||
|
||||
### Concurrent Requests
|
||||
|
||||
8 concurrent requests, max_batch=4:
|
||||
|
||||
| Metric | Result |
|
||||
|--------|--------|
|
||||
| Wall clock | 22.5s |
|
||||
| Sum of individual latencies | 135.0s |
|
||||
| Scheduling speedup | **6.0x** |
|
||||
| Throughput | 11.4 tok/s |
|
||||
|
||||
Continuous batching scheduling confirmed working (decode batch_size=4 in logs).
|
||||
|
||||
## Remaining Performance Gap
|
||||
|
||||
35% of HF throughput. Main bottlenecks:
|
||||
|
||||
| Bottleneck | Impact | Fix |
|
||||
|-----------|--------|-----|
|
||||
| **Decode Q_len=1 inefficiency** | FA2 kernel: 64 threads, only 1 active (owns_row=true for single query) | Specialized decode attention kernel (vector-dot against KV, parallel reduction along S) |
|
||||
| **No kernel fusion** | RMSNorm+residual, SiLU*up: separate kernels, redundant HBM reads/writes | Fused kernels (Phase 15) |
|
||||
| **No CUDA Graphs** | ~100+ kernel launches per decode step, each has host-side overhead | Capture decode iteration as CUDA Graph (Phase 15) |
|
||||
| **Per-seq forward (no batched decode)** | With batch=4, 4 serial forward passes per iteration | Batched projections + per-seq attention (Phase 15, depends on FA2 decode kernel) |
|
||||
| **No vectorized loads in FA2** | Scalar bf16→f32 conversion in dot product loop | float4 / bfloat162 vectorized loads |
|
||||
|
||||
## Memory Usage
|
||||
|
||||
| Component | Naive (Phase 11) | FA2 (Phase 14) |
|
||||
|-----------|-----------------|----------------|
|
||||
| Score matrix [1, 32, S, S] | S² × 32 × 2B | **0** |
|
||||
| repeat_kv K/V [1, 32, S, 128] | 2 × S × 32 × 128 × 2B per layer | **0** |
|
||||
| K^T contiguous copy | S × 32 × 128 × 2B per layer | **0** |
|
||||
|
||||
For S=256 (current max): savings ~6 MB per layer × 36 layers ≈ 216 MB.
|
||||
For S=2048: savings ~384 MB per layer × 36 layers ≈ 13.5 GB (naive would OOM).
|
||||
|
||||
## Tracking
|
||||
|
||||
| Phase | Attention | tok/s | vs HF | Correctness |
|
||||
|-------|-----------|-------|-------|-------------|
|
||||
| 8 | Naive (no cache) | 2.5 | 5% | 50/50 vs HF |
|
||||
| 9 | Naive + CPU KV cache | 44.3 (GPT-2) | — | 50/50 self |
|
||||
| 10 | Naive + CPU KV cache | 6.9 (Qwen3-8B) | 15% | 100% top-5 |
|
||||
| 11 | Naive + GPU KV cache | 10.3 | 30% | 9/10 top-1 |
|
||||
| **14** | **FA2 + GQA in kernel** | **12.9** | **35%** | **9/10 top-1** |
|
||||
85
docs/benchmarks/phase15-performance.md
Normal file
85
docs/benchmarks/phase15-performance.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# Phase 15 Benchmark: Performance Optimization
|
||||
|
||||
**Date**: 2026-05-23
|
||||
**Hardware**: RTX 5090 (32GB GDDR7, SM120 CC 12.0, 170 SMs, 1.79 TB/s)
|
||||
**Model**: Qwen3-8B (BF16, 36 layers, 4096 hidden, 32 Q / 8 KV GQA heads, head_dim=128)
|
||||
**Config**: greedy decoding (temperature=0), max_tokens=64, serial (batch=1)
|
||||
|
||||
## Ablation: Each Optimization Measured Independently
|
||||
|
||||
| # | Optimization | tok/s | Delta | ms/token | Roofline |
|
||||
|---|-------------|-------|-------|----------|----------|
|
||||
| 0 | Phase 14 baseline (FA2 + naive cuBLAS GEMV) | 12.9 | — | 77.5 | 12% |
|
||||
| 1 | + Decode attention kernel (256 threads) | 12.9 | +0% | 77.5 | 12% |
|
||||
| 2 | + Fused SiLU×Mul | 13.0 | +1% | 76.9 | 12% |
|
||||
| 3 | + Fused Add+RMSNorm | 13.2 | +2% | 75.8 | 12% |
|
||||
| 4 | + Custom GEMV (M=1, K-split tiled) | 46.6 | +253% | 21.5 | 42% |
|
||||
| 5 | + Tensor::empty (skip cudaMemset) | **50.3** | **+8%** | **19.9** | **45%** |
|
||||
|
||||
## Comparison with HuggingFace transformers
|
||||
|
||||
8 prompts (short/medium/long) × max_tokens=64, greedy, serial:
|
||||
|
||||
| System | tok/s | ms/token | Roofline |
|
||||
|--------|-------|----------|----------|
|
||||
| HF transformers (BF16, torch 2.8, SDPA) | 36.0 | 27.8 | 32% |
|
||||
| **xserv Phase 15** | **50.3** | **19.9** | **45%** |
|
||||
| Roofline (1.79 TB/s, 16GB model) | 112.0 | 8.9 | 100% |
|
||||
|
||||
**xserv is 140% of HF transformers throughput.**
|
||||
|
||||
## Per-Prompt Detail (Phase 15 Final)
|
||||
|
||||
| # | Prompt | pt | ct | Time | tok/s |
|
||||
|---|--------|----|----|------|-------|
|
||||
| 1 | What is gravity? | 12 | 64 | 1.39s | 46.0 |
|
||||
| 2 | Hello, how are you? | 14 | 64 | 1.27s | 50.5 |
|
||||
| 3 | Explain DNA briefly. | 13 | 64 | 1.25s | 51.2 |
|
||||
| 4 | Write a detailed explanation of photosynthesis... | 27 | 64 | 1.26s | 50.7 |
|
||||
| 5 | Describe machine learning. | 13 | 64 | 1.25s | 51.2 |
|
||||
| 6 | What causes earthquakes? | 12 | 64 | 1.25s | 51.1 |
|
||||
| 7 | How does the internet work? | 14 | 64 | 1.25s | 51.1 |
|
||||
| 8 | What is the speed of light? | 15 | 64 | 1.25s | 51.0 |
|
||||
|
||||
Prompt 1 is slower (46.0 vs 51.x) due to first-request warmup (caching allocator cold start).
|
||||
|
||||
## Concurrent Throughput
|
||||
|
||||
8 requests concurrent, max_batch=4:
|
||||
|
||||
| Config | tok/s | Wall clock | Speedup |
|
||||
|--------|-------|-----------|---------|
|
||||
| Serial (batch=1, custom GEMV) | 50.3 | — | — |
|
||||
| Concurrent (batch=4, cuBLAS M=4) | 28.2 | 9.09s | 6.47x scheduling |
|
||||
| Concurrent (batch=4, custom GEMV) | 35.1* | ~7.3s | ~6x scheduling |
|
||||
|
||||
*Note: batch=4 with custom GEMV is slower than serial because:
|
||||
1. Batched decode path uses cuBLAS for M>1 matmuls, losing the GEMV advantage
|
||||
2. Per-seq attention/reshape overhead in the batched path adds ~2ms/step
|
||||
3. Custom GEMV already saturates bandwidth at M=1
|
||||
|
||||
Serial decode with custom GEMV is the optimal path for current architecture.
|
||||
|
||||
## Correctness Verification
|
||||
|
||||
| Test | Result |
|
||||
|------|--------|
|
||||
| Top-1 logits match vs HF (10 prompts) | 9/10 (90%) |
|
||||
| Top-5 overlap vs HF (10 prompts) | 4.0/5 avg |
|
||||
| vs pre-optimization baseline | Identical (same 9/10) |
|
||||
| API generation (52 prompts) | 52/52 pass |
|
||||
| SSE streaming | Working |
|
||||
| Chinese prompts | Working |
|
||||
|
||||
## Phase-over-Phase Performance Tracking
|
||||
|
||||
| Phase | Key Change | tok/s | vs HF | Roofline |
|
||||
|-------|-----------|-------|-------|----------|
|
||||
| 8 | GPT-2 inference (no cache) | 2.5 | 7% | — |
|
||||
| 9 | + KV cache (CPU) | 44.3 (GPT-2) | — | — |
|
||||
| 10 | Qwen3-8B (CPU KV cache) | 6.9 | 19% | 6% |
|
||||
| 11 | + GPU KV cache | 10.3 | 29% | 9% |
|
||||
| 14 | + Flash Attention 2 | 12.9 | 36% | 12% |
|
||||
| **15** | **+ Custom GEMV + fused + empty** | **50.3** | **140%** | **45%** |
|
||||
|
||||
Total speedup from Phase 10 to Phase 15: **7.3x** (6.9 → 50.3 tok/s).
|
||||
140
tools/bench_server.py
Normal file
140
tools/bench_server.py
Normal file
@@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Benchmark xserv server performance and check correctness vs HF."""
|
||||
|
||||
import json
|
||||
import time
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8090
|
||||
|
||||
def chat(prompt, max_tokens=80, temperature=0):
|
||||
data = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": max_tokens,
|
||||
"temperature": temperature,
|
||||
"stream": False
|
||||
}).encode()
|
||||
req = urllib.request.Request(
|
||||
f"http://localhost:{PORT}/v1/chat/completions",
|
||||
data=data, headers={"Content-Type": "application/json"}
|
||||
)
|
||||
t0 = time.perf_counter()
|
||||
with urllib.request.urlopen(req, timeout=180) as resp:
|
||||
result = json.loads(resp.read())
|
||||
elapsed = time.perf_counter() - t0
|
||||
usage = result.get("usage", {})
|
||||
content = result["choices"][0]["message"]["content"]
|
||||
finish = result["choices"][0]["finish_reason"]
|
||||
ct = usage.get("completion_tokens", 0)
|
||||
pt = usage.get("prompt_tokens", 0)
|
||||
return ct / elapsed if elapsed > 0 else 0, elapsed, ct, pt, content, finish
|
||||
|
||||
|
||||
def chat_stream(prompt, max_tokens=80, temperature=0):
|
||||
data = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": max_tokens,
|
||||
"temperature": temperature,
|
||||
"stream": True
|
||||
}).encode()
|
||||
req = urllib.request.Request(
|
||||
f"http://localhost:{PORT}/v1/chat/completions",
|
||||
data=data, headers={"Content-Type": "application/json"}
|
||||
)
|
||||
t0 = time.perf_counter()
|
||||
tokens = 0
|
||||
content = ""
|
||||
with urllib.request.urlopen(req, timeout=180) as resp:
|
||||
for line in resp:
|
||||
line = line.decode().strip()
|
||||
if line.startswith("data: "):
|
||||
payload = line[6:]
|
||||
if payload == "[DONE]":
|
||||
break
|
||||
chunk = json.loads(payload)
|
||||
delta = chunk["choices"][0].get("delta", {})
|
||||
c = delta.get("content", "")
|
||||
if c:
|
||||
tokens += 1
|
||||
content += c
|
||||
elapsed = time.perf_counter() - t0
|
||||
return tokens / elapsed if elapsed > 0 else 0, elapsed, tokens, content
|
||||
|
||||
|
||||
print("=" * 60)
|
||||
print(f"xserv Server Benchmark (port {PORT})")
|
||||
print("=" * 60)
|
||||
|
||||
# Health check
|
||||
try:
|
||||
urllib.request.urlopen(f"http://localhost:{PORT}/health", timeout=3)
|
||||
except:
|
||||
print(f"Server not responding on port {PORT}")
|
||||
sys.exit(1)
|
||||
|
||||
# 1. EOS leak check
|
||||
print("\n--- EOS Leak Check ---")
|
||||
tps, t, ct, pt, content, finish = chat("Say hello", 30)
|
||||
has_eos = "<|im_end|>" in content or "<|endoftext|>" in content or "<|im_start|>" in content
|
||||
print(f" finish_reason: {finish}")
|
||||
print(f" EOS in content: {'YES (BUG!)' if has_eos else 'NO (good)'}")
|
||||
print(f" Content: {content[:100]}")
|
||||
|
||||
# 2. Warmup
|
||||
print("\n--- Warmup ---")
|
||||
chat("Hi", 10)
|
||||
time.sleep(0.5)
|
||||
|
||||
# 3. Non-streaming benchmark
|
||||
print("\n--- Non-streaming Performance (greedy, batch=1) ---")
|
||||
prompts = [
|
||||
("short", "What is 2+2?", 50),
|
||||
("medium", "Explain quantum computing in simple terms.", 80),
|
||||
("long", "Write a detailed comparison of Python and Rust programming languages, covering syntax, performance, memory management, and ecosystem.", 150),
|
||||
]
|
||||
|
||||
for name, prompt, max_tok in prompts:
|
||||
tps, t, ct, pt, content, finish = chat(prompt, max_tok)
|
||||
print(f" [{name}] {tps:.1f} tok/s | {ct} tokens in {t:.2f}s | prompt={pt} | finish={finish}")
|
||||
|
||||
# 4. Streaming benchmark
|
||||
print("\n--- Streaming Performance ---")
|
||||
tps, t, ct, content = chat_stream("Explain the theory of relativity.", 80)
|
||||
print(f" stream: {tps:.1f} tok/s | {ct} tokens in {t:.2f}s")
|
||||
|
||||
# 5. max_tokens validation
|
||||
print("\n--- max_tokens Validation ---")
|
||||
try:
|
||||
tps, t, ct, pt, content, finish = chat("Hi", 999999)
|
||||
print(f" max_tokens=999999: OK (server clamped to {ct} tokens, no crash)")
|
||||
except Exception as e:
|
||||
print(f" max_tokens=999999: {e}")
|
||||
|
||||
# 6. Concurrent requests (if server supports batching)
|
||||
print("\n--- Concurrent Requests (2 parallel) ---")
|
||||
import threading
|
||||
results = [None, None]
|
||||
|
||||
def do_request(idx, prompt, max_tok):
|
||||
results[idx] = chat(prompt, max_tok)
|
||||
|
||||
t1 = threading.Thread(target=do_request, args=(0, "What is gravity?", 50))
|
||||
t2 = threading.Thread(target=do_request, args=(1, "What is light?", 50))
|
||||
t0 = time.perf_counter()
|
||||
t1.start(); t2.start()
|
||||
t1.join(); t2.join()
|
||||
wall_time = time.perf_counter() - t0
|
||||
|
||||
total_tokens = sum(r[2] for r in results if r)
|
||||
combined_tps = total_tokens / wall_time
|
||||
print(f" 2 concurrent: {combined_tps:.1f} tok/s total | wall={wall_time:.2f}s")
|
||||
for i, r in enumerate(results):
|
||||
if r:
|
||||
print(f" req{i}: {r[0]:.1f} tok/s, {r[2]} tokens in {r[1]:.2f}s")
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
print("DONE")
|
||||
print("=" * 60)
|
||||
196
tools/bench_vs_hf.py
Normal file
196
tools/bench_vs_hf.py
Normal file
@@ -0,0 +1,196 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Benchmark xserv vs HuggingFace transformers on Qwen3-8B.
|
||||
Measures: prefill latency, decode throughput, end-to-end latency.
|
||||
|
||||
Usage:
|
||||
# xserv server should be running on port 9090
|
||||
python3 tools/bench_vs_hf.py
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
MODEL_DIR = "/opt/wjh/models/qwen3-8b"
|
||||
XSERV_URL = "http://localhost:9090"
|
||||
|
||||
BENCH_PROMPTS = [
|
||||
# Short prompts (~10 tokens)
|
||||
("short", "What is gravity?"),
|
||||
("short", "Hello, how are you?"),
|
||||
("short", "Explain DNA briefly."),
|
||||
# Medium prompts (~30 tokens)
|
||||
("medium", "Write a detailed explanation of how photosynthesis works in plants, including the light and dark reactions."),
|
||||
("medium", "Describe the process of machine learning training, including forward pass, loss computation, and backpropagation."),
|
||||
("medium", "Explain the differences between TCP and UDP protocols, including when you would use each one in practice."),
|
||||
# Longer prompts (~60 tokens)
|
||||
("long", "You are an expert computer scientist. Please write a comprehensive explanation of how modern GPUs work, including the architecture of streaming multiprocessors, the memory hierarchy from registers to global memory, and how thousands of threads are scheduled concurrently. Include specific technical details."),
|
||||
("long", "You are a historian specializing in ancient civilizations. Please provide a detailed analysis of the rise and fall of the Roman Empire, covering the key factors that led to its expansion, the political and social structures that sustained it, and the multiple causes that contributed to its eventual decline and collapse."),
|
||||
]
|
||||
|
||||
MAX_TOKENS = 64
|
||||
|
||||
|
||||
def bench_xserv():
|
||||
"""Benchmark xserv HTTP API."""
|
||||
print("\n" + "=" * 60)
|
||||
print("BENCHMARK: xserv (HTTP API, greedy, max_tokens={})".format(MAX_TOKENS))
|
||||
print("=" * 60)
|
||||
|
||||
# Warmup
|
||||
body = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": "Hi"}],
|
||||
"max_tokens": 8,
|
||||
"temperature": 0.0,
|
||||
}).encode()
|
||||
req = urllib.request.Request(
|
||||
f"{XSERV_URL}/v1/chat/completions",
|
||||
data=body, headers={"Content-Type": "application/json"},
|
||||
)
|
||||
urllib.request.urlopen(req, timeout=120)
|
||||
print("Warmup done.\n")
|
||||
|
||||
results = []
|
||||
for category, prompt in BENCH_PROMPTS:
|
||||
body = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": MAX_TOKENS,
|
||||
"temperature": 0.0,
|
||||
}).encode()
|
||||
req = urllib.request.Request(
|
||||
f"{XSERV_URL}/v1/chat/completions",
|
||||
data=body, headers={"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
t0 = time.perf_counter()
|
||||
resp = urllib.request.urlopen(req, timeout=300)
|
||||
elapsed = time.perf_counter() - t0
|
||||
data = json.loads(resp.read())
|
||||
|
||||
usage = data.get("usage", {})
|
||||
pt = usage.get("prompt_tokens", 0)
|
||||
ct = usage.get("completion_tokens", 0)
|
||||
tok_per_sec = ct / elapsed if elapsed > 0 else 0
|
||||
|
||||
print(f" [{category:>6}] pt={pt:3d} ct={ct:2d} | {elapsed:6.2f}s | {tok_per_sec:5.1f} tok/s | {prompt[:50]}...")
|
||||
results.append({
|
||||
"category": category,
|
||||
"prompt_tokens": pt,
|
||||
"completion_tokens": ct,
|
||||
"elapsed": elapsed,
|
||||
"tok_per_sec": tok_per_sec,
|
||||
})
|
||||
|
||||
# Summary
|
||||
total_ct = sum(r["completion_tokens"] for r in results)
|
||||
total_time = sum(r["elapsed"] for r in results)
|
||||
avg_tok_per_sec = total_ct / total_time if total_time > 0 else 0
|
||||
|
||||
print(f"\n xserv total: {total_ct} tokens in {total_time:.2f}s = {avg_tok_per_sec:.1f} tok/s")
|
||||
return results
|
||||
|
||||
|
||||
def bench_hf():
|
||||
"""Benchmark HuggingFace transformers generate()."""
|
||||
print("\n" + "=" * 60)
|
||||
print("BENCHMARK: HuggingFace transformers (greedy, max_new_tokens={})".format(MAX_TOKENS))
|
||||
print("=" * 60)
|
||||
|
||||
import torch
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
|
||||
print(f"Loading model on GPU 1...")
|
||||
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
MODEL_DIR, dtype=torch.bfloat16, device_map="cuda:1", trust_remote_code=True)
|
||||
model.eval()
|
||||
print("Model loaded.\n")
|
||||
|
||||
# Warmup
|
||||
inputs = tokenizer("Hi", return_tensors="pt").to(model.device)
|
||||
with torch.no_grad():
|
||||
model.generate(**inputs, max_new_tokens=8, do_sample=False)
|
||||
print("Warmup done.\n")
|
||||
|
||||
results = []
|
||||
for category, prompt in BENCH_PROMPTS:
|
||||
# Apply chat template (same as xserv)
|
||||
messages = [{"role": "user", "content": prompt}]
|
||||
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
||||
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
||||
pt = inputs["input_ids"].shape[1]
|
||||
|
||||
torch.cuda.synchronize()
|
||||
t0 = time.perf_counter()
|
||||
with torch.no_grad():
|
||||
output = model.generate(
|
||||
**inputs,
|
||||
max_new_tokens=MAX_TOKENS,
|
||||
do_sample=False,
|
||||
)
|
||||
torch.cuda.synchronize()
|
||||
elapsed = time.perf_counter() - t0
|
||||
|
||||
ct = output.shape[1] - pt
|
||||
tok_per_sec = ct / elapsed if elapsed > 0 else 0
|
||||
|
||||
print(f" [{category:>6}] pt={pt:3d} ct={ct:2d} | {elapsed:6.2f}s | {tok_per_sec:5.1f} tok/s | {prompt[:50]}...")
|
||||
results.append({
|
||||
"category": category,
|
||||
"prompt_tokens": pt,
|
||||
"completion_tokens": ct,
|
||||
"elapsed": elapsed,
|
||||
"tok_per_sec": tok_per_sec,
|
||||
})
|
||||
|
||||
total_ct = sum(r["completion_tokens"] for r in results)
|
||||
total_time = sum(r["elapsed"] for r in results)
|
||||
avg_tok_per_sec = total_ct / total_time if total_time > 0 else 0
|
||||
|
||||
print(f"\n HF total: {total_ct} tokens in {total_time:.2f}s = {avg_tok_per_sec:.1f} tok/s")
|
||||
|
||||
del model
|
||||
torch.cuda.empty_cache()
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
xserv_results = bench_xserv()
|
||||
hf_results = bench_hf()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("COMPARISON SUMMARY")
|
||||
print("=" * 60)
|
||||
|
||||
print(f"\n{'Category':<10} {'Metric':<20} {'xserv':>10} {'HF':>10} {'Ratio':>10}")
|
||||
print("-" * 62)
|
||||
|
||||
for cat in ["short", "medium", "long"]:
|
||||
xs = [r for r in xserv_results if r["category"] == cat]
|
||||
hf = [r for r in hf_results if r["category"] == cat]
|
||||
if xs and hf:
|
||||
xs_avg_tps = sum(r["tok_per_sec"] for r in xs) / len(xs)
|
||||
hf_avg_tps = sum(r["tok_per_sec"] for r in hf) / len(hf)
|
||||
xs_avg_lat = sum(r["elapsed"] for r in xs) / len(xs)
|
||||
hf_avg_lat = sum(r["elapsed"] for r in hf) / len(hf)
|
||||
ratio_tps = xs_avg_tps / hf_avg_tps if hf_avg_tps > 0 else 0
|
||||
ratio_lat = xs_avg_lat / hf_avg_lat if hf_avg_lat > 0 else 0
|
||||
|
||||
print(f"{cat:<10} {'Throughput (tok/s)':<20} {xs_avg_tps:>10.1f} {hf_avg_tps:>10.1f} {ratio_tps:>9.2f}x")
|
||||
print(f"{'':<10} {'Latency (s)':<20} {xs_avg_lat:>10.2f} {hf_avg_lat:>10.2f} {ratio_lat:>9.2f}x")
|
||||
|
||||
xs_total_tps = sum(r["completion_tokens"] for r in xserv_results) / sum(r["elapsed"] for r in xserv_results)
|
||||
hf_total_tps = sum(r["completion_tokens"] for r in hf_results) / sum(r["elapsed"] for r in hf_results)
|
||||
ratio = xs_total_tps / hf_total_tps if hf_total_tps > 0 else 0
|
||||
|
||||
print("-" * 62)
|
||||
print(f"{'OVERALL':<10} {'Throughput (tok/s)':<20} {xs_total_tps:>10.1f} {hf_total_tps:>10.1f} {ratio:>9.2f}x")
|
||||
print(f"\nxserv is {ratio:.1%} of HF transformers throughput")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
115
tools/compare_logits.py
Normal file
115
tools/compare_logits.py
Normal file
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare xserv prefill logits with HuggingFace transformers on 10 prompts."""
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
MODEL_DIR = "/opt/wjh/models/qwen3-8b"
|
||||
TOP_K = 10
|
||||
|
||||
PROMPTS = [
|
||||
"What is the capital of France?",
|
||||
"Explain quantum computing.",
|
||||
"Hello world",
|
||||
"def fibonacci(n):",
|
||||
"The weather today is",
|
||||
"1 + 1 =",
|
||||
"Machine learning is",
|
||||
"Once upon a time",
|
||||
"Paris is known for",
|
||||
"How does gravity work?",
|
||||
]
|
||||
|
||||
|
||||
def get_hf_topk(prompt, tokenizer, model, k=10):
|
||||
import torch
|
||||
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
||||
with torch.no_grad():
|
||||
outputs = model(**inputs)
|
||||
logits = outputs.logits[0, -1, :].float().cpu()
|
||||
topk = torch.topk(logits, k)
|
||||
return list(zip(topk.indices.tolist(), topk.values.tolist()))
|
||||
|
||||
|
||||
def get_xserv_topk(prompt, k=10):
|
||||
xserv_bin = "/opt/wjh/projects/xserv/target/release/dump-logits"
|
||||
env = {**os.environ, "CUDA_VISIBLE_DEVICES": "0",
|
||||
"PATH": "/usr/local/cuda-12.9/bin:" + os.environ.get("PATH", "")}
|
||||
result = subprocess.run(
|
||||
[xserv_bin, MODEL_DIR, prompt],
|
||||
capture_output=True, text=True, timeout=180, env=env,
|
||||
)
|
||||
# Parse output: " [ 0] id= 3555 logit= 24.5000 token=..."
|
||||
topk = []
|
||||
for line in result.stdout.strip().split('\n'):
|
||||
m = re.match(r'\s*\[\s*\d+\]\s+id=\s*(\d+)\s+logit=\s*([\d.\-]+)', line)
|
||||
if m:
|
||||
topk.append((int(m.group(1)), float(m.group(2))))
|
||||
if len(topk) >= k:
|
||||
break
|
||||
return topk
|
||||
|
||||
|
||||
def main():
|
||||
import torch
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
|
||||
print(f"Loading HF model on GPU 1...")
|
||||
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
MODEL_DIR, dtype=torch.bfloat16, device_map="cuda:1", trust_remote_code=True)
|
||||
model.eval()
|
||||
print("HF model loaded.\n")
|
||||
|
||||
total = len(PROMPTS)
|
||||
top1_matches = 0
|
||||
top5_overlaps = []
|
||||
|
||||
for i, prompt in enumerate(PROMPTS):
|
||||
print(f"[{i+1}/{total}] \"{prompt}\"")
|
||||
|
||||
hf_top = get_hf_topk(prompt, tokenizer, model, TOP_K)
|
||||
xs_top = get_xserv_topk(prompt, TOP_K)
|
||||
|
||||
if not xs_top:
|
||||
print(" xserv: NO OUTPUT")
|
||||
continue
|
||||
|
||||
hf_ids = [t[0] for t in hf_top]
|
||||
xs_ids = [t[0] for t in xs_top]
|
||||
|
||||
top1_match = hf_ids[0] == xs_ids[0]
|
||||
if top1_match:
|
||||
top1_matches += 1
|
||||
|
||||
top5_overlap = len(set(hf_ids[:5]) & set(xs_ids[:5]))
|
||||
top5_overlaps.append(top5_overlap)
|
||||
|
||||
# Show comparison
|
||||
hf_tok = tokenizer.decode([hf_ids[0]])
|
||||
xs_tok = tokenizer.decode([xs_ids[0]])
|
||||
status = "MATCH" if top1_match else "DIFF"
|
||||
|
||||
print(f" Top-1: HF={hf_ids[0]:>6}({hf_tok!r:>10}) | xserv={xs_ids[0]:>6}({xs_tok!r:>10}) [{status}]")
|
||||
print(f" Top-5 overlap: {top5_overlap}/5")
|
||||
|
||||
# Show top-5 side by side
|
||||
print(f" {'HF':>25} | {'xserv':>25}")
|
||||
for j in range(min(5, len(hf_top), len(xs_top))):
|
||||
h_id, h_val = hf_top[j]
|
||||
x_id, x_val = xs_top[j]
|
||||
h_tok = tokenizer.decode([h_id])
|
||||
x_tok = tokenizer.decode([x_id])
|
||||
print(f" {h_id:>6} {h_val:>8.3f} {h_tok!r:>8} | {x_id:>6} {x_val:>8.3f} {x_tok!r:>8}")
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"Top-1 match rate: {top1_matches}/{total} ({100*top1_matches/total:.0f}%)")
|
||||
avg_overlap = sum(top5_overlaps) / max(len(top5_overlaps), 1)
|
||||
print(f"Avg top-5 overlap: {avg_overlap:.1f}/5")
|
||||
print(f"Verdict: {'PASS' if top1_matches >= total * 0.7 else 'FAIL'}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
394
tools/e2e_validate.py
Normal file
394
tools/e2e_validate.py
Normal file
@@ -0,0 +1,394 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
End-to-end validation for xserv after bug fixes.
|
||||
1. Correctness: compare top-k logits with HuggingFace transformers
|
||||
2. Generation: run 50+ prompts through the HTTP API
|
||||
3. Performance: measure latency and throughput
|
||||
|
||||
Usage:
|
||||
# Step 1: Start xserv server in background:
|
||||
# ./target/release/xserv-server /opt/wjh/models/qwen3-8b --port 8080
|
||||
#
|
||||
# Step 2: Run this script:
|
||||
# python3 tools/e2e_validate.py --mode all
|
||||
# python3 tools/e2e_validate.py --mode logits # correctness only
|
||||
# python3 tools/e2e_validate.py --mode api # API + perf only
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import time
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
MODEL_DIR = "/opt/wjh/models/qwen3-8b"
|
||||
XSERV_URL = "http://localhost:8080"
|
||||
TOP_K = 10
|
||||
|
||||
# 50+ diverse test prompts
|
||||
TEST_PROMPTS = [
|
||||
"What is the capital of France?",
|
||||
"Explain quantum computing in simple terms.",
|
||||
"Write a Python function to sort a list.",
|
||||
"你好,请用中文介绍一下你自己。",
|
||||
"What is 2 + 2?",
|
||||
"The theory of relativity states that",
|
||||
"In a far away galaxy,",
|
||||
"def fibonacci(n):",
|
||||
"请解释什么是机器学习。",
|
||||
"How does photosynthesis work?",
|
||||
"What are the benefits of exercise?",
|
||||
"Once upon a time in a small village,",
|
||||
"The most important invention of the 20th century was",
|
||||
"Translate 'hello world' to Japanese.",
|
||||
"What is the meaning of life?",
|
||||
"Describe the process of making bread.",
|
||||
"Why is the sky blue?",
|
||||
"What is the difference between AI and ML?",
|
||||
"如何评价GPT-4?",
|
||||
"Write a haiku about autumn.",
|
||||
"Explain the Pythagorean theorem.",
|
||||
"What causes earthquakes?",
|
||||
"How does the internet work?",
|
||||
"What is the speed of light?",
|
||||
"Describe the water cycle.",
|
||||
"What is democracy?",
|
||||
"How do vaccines work?",
|
||||
"What is blockchain technology?",
|
||||
"Explain supply and demand.",
|
||||
"What is the Big Bang theory?",
|
||||
"How do airplanes fly?",
|
||||
"What is climate change?",
|
||||
"Describe the human digestive system.",
|
||||
"What is artificial intelligence?",
|
||||
"How does electricity work?",
|
||||
"What is the solar system?",
|
||||
"Explain the concept of gravity.",
|
||||
"What is DNA?",
|
||||
"How do computers store data?",
|
||||
"What is the greenhouse effect?",
|
||||
"Describe the structure of an atom.",
|
||||
"What is machine learning?",
|
||||
"How does Wi-Fi work?",
|
||||
"What is the stock market?",
|
||||
"Explain natural selection.",
|
||||
"What is renewable energy?",
|
||||
"How do batteries work?",
|
||||
"What is the United Nations?",
|
||||
"Describe the process of evolution.",
|
||||
"What is cryptography?",
|
||||
"请用三句话总结量子力学的核心概念。",
|
||||
"用Python写一个计算斐波那契数列的函数。",
|
||||
]
|
||||
|
||||
|
||||
def logits_correctness_test():
|
||||
"""Compare xserv prefill logits with HuggingFace transformers."""
|
||||
print("\n" + "=" * 60)
|
||||
print("CORRECTNESS TEST: Comparing logits with HuggingFace")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
import torch
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
except ImportError:
|
||||
print("SKIP: transformers/torch not installed")
|
||||
return None
|
||||
|
||||
print(f"Loading HF model from {MODEL_DIR}...")
|
||||
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
MODEL_DIR,
|
||||
torch_dtype=torch.bfloat16,
|
||||
device_map="cuda:1", # Use GPU 1 (xserv uses GPU 0)
|
||||
trust_remote_code=True,
|
||||
)
|
||||
model.eval()
|
||||
|
||||
test_prompts = TEST_PROMPTS[:10] # Use first 10 for logits comparison
|
||||
xserv_bin = "/opt/wjh/projects/xserv/target/release/dump-logits"
|
||||
|
||||
results = []
|
||||
for i, prompt in enumerate(test_prompts):
|
||||
print(f"\n[{i+1}/{len(test_prompts)}] Prompt: {prompt[:50]}...")
|
||||
|
||||
# --- HuggingFace ---
|
||||
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
||||
with torch.no_grad():
|
||||
outputs = model(**inputs)
|
||||
hf_logits = outputs.logits[0, -1, :].float().cpu()
|
||||
hf_top = torch.topk(hf_logits, TOP_K)
|
||||
hf_ids = hf_top.indices.tolist()
|
||||
hf_vals = hf_top.values.tolist()
|
||||
|
||||
# --- xserv ---
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[xserv_bin, MODEL_DIR, prompt],
|
||||
capture_output=True, text=True, timeout=120,
|
||||
env={**os.environ, "CUDA_VISIBLE_DEVICES": "0",
|
||||
"PATH": "/usr/local/cuda-12.9/bin:" + os.environ.get("PATH", "")},
|
||||
)
|
||||
xserv_lines = [l for l in result.stdout.strip().split('\n') if l.strip().startswith('[')]
|
||||
xserv_top = []
|
||||
for line in xserv_lines[:TOP_K]:
|
||||
parts = line.strip().split()
|
||||
tid = int([p for p in parts if p.startswith('id=')][0].split('=')[1])
|
||||
val = float([p for p in parts if p.startswith('logit=')][0].split('=')[1])
|
||||
xserv_top.append((tid, val))
|
||||
except Exception as e:
|
||||
print(f" xserv FAILED: {e}")
|
||||
results.append({"prompt": prompt, "match": False, "error": str(e)})
|
||||
continue
|
||||
|
||||
# --- Compare ---
|
||||
xserv_ids = [t[0] for t in xserv_top]
|
||||
xserv_vals = [t[1] for t in xserv_top]
|
||||
|
||||
# Top-1 match
|
||||
top1_match = hf_ids[0] == xserv_ids[0] if xserv_ids else False
|
||||
# Top-5 overlap
|
||||
top5_overlap = len(set(hf_ids[:5]) & set(xserv_ids[:5]))
|
||||
# Max logit difference for matching tokens
|
||||
max_diff = 0
|
||||
for j, (hid, hval) in enumerate(zip(hf_ids[:5], hf_vals[:5])):
|
||||
for xid, xval in xserv_top[:5]:
|
||||
if hid == xid:
|
||||
max_diff = max(max_diff, abs(hval - xval))
|
||||
|
||||
hf_tok = tokenizer.decode([hf_ids[0]])
|
||||
xs_tok = tokenizer.decode([xserv_ids[0]]) if xserv_ids else "???"
|
||||
|
||||
status = "PASS" if top1_match else "WARN"
|
||||
print(f" Top-1: HF={hf_ids[0]}({hf_tok!r}) vs xserv={xserv_ids[0]}({xs_tok!r}) → {status}")
|
||||
print(f" Top-5 overlap: {top5_overlap}/5, max logit diff: {max_diff:.4f}")
|
||||
|
||||
results.append({
|
||||
"prompt": prompt[:50],
|
||||
"top1_match": top1_match,
|
||||
"top5_overlap": top5_overlap,
|
||||
"max_logit_diff": max_diff,
|
||||
"hf_top1": f"{hf_ids[0]}({hf_tok})",
|
||||
"xserv_top1": f"{xserv_ids[0]}({xs_tok})" if xserv_ids else "???",
|
||||
})
|
||||
|
||||
# Summary
|
||||
print("\n" + "-" * 40)
|
||||
top1_matches = sum(1 for r in results if r.get("top1_match"))
|
||||
avg_overlap = sum(r.get("top5_overlap", 0) for r in results) / max(len(results), 1)
|
||||
print(f"Top-1 match: {top1_matches}/{len(results)}")
|
||||
print(f"Avg top-5 overlap: {avg_overlap:.1f}/5")
|
||||
print(f"Verdict: {'PASS' if top1_matches >= len(results) * 0.8 else 'FAIL'}")
|
||||
|
||||
# Cleanup
|
||||
del model
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def api_generation_test():
|
||||
"""Test 50+ prompts through the HTTP API."""
|
||||
print("\n" + "=" * 60)
|
||||
print("API GENERATION TEST: 50+ prompts via /v1/chat/completions")
|
||||
print("=" * 60)
|
||||
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
# Health check
|
||||
try:
|
||||
req = urllib.request.Request(f"{XSERV_URL}/health")
|
||||
resp = urllib.request.urlopen(req, timeout=5)
|
||||
assert resp.read().decode() == "ok"
|
||||
print("Health check: OK")
|
||||
except Exception as e:
|
||||
print(f"FAIL: Server not reachable at {XSERV_URL}: {e}")
|
||||
print("Start the server first: ./target/release/xserv-server /opt/wjh/models/qwen3-8b")
|
||||
return None
|
||||
|
||||
# Models endpoint
|
||||
try:
|
||||
req = urllib.request.Request(f"{XSERV_URL}/v1/models")
|
||||
resp = urllib.request.urlopen(req, timeout=5)
|
||||
models = json.loads(resp.read())
|
||||
print(f"Models: {[m['id'] for m in models['data']]}")
|
||||
except Exception as e:
|
||||
print(f"WARN: /v1/models failed: {e}")
|
||||
|
||||
results = []
|
||||
total_prompt_tokens = 0
|
||||
total_completion_tokens = 0
|
||||
total_latency = 0
|
||||
failures = 0
|
||||
|
||||
for i, prompt in enumerate(TEST_PROMPTS):
|
||||
body = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": 32,
|
||||
"temperature": 0.0,
|
||||
}).encode()
|
||||
|
||||
try:
|
||||
req = urllib.request.Request(
|
||||
f"{XSERV_URL}/v1/chat/completions",
|
||||
data=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
t0 = time.time()
|
||||
resp = urllib.request.urlopen(req, timeout=120)
|
||||
latency = time.time() - t0
|
||||
data = json.loads(resp.read())
|
||||
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
finish = data["choices"][0]["finish_reason"]
|
||||
usage = data.get("usage", {})
|
||||
pt = usage.get("prompt_tokens", 0)
|
||||
ct = usage.get("completion_tokens", 0)
|
||||
|
||||
total_prompt_tokens += pt
|
||||
total_completion_tokens += ct
|
||||
total_latency += latency
|
||||
|
||||
# Basic quality checks
|
||||
has_content = len(content.strip()) > 0
|
||||
reasonable_length = ct > 0
|
||||
|
||||
status = "OK" if has_content and reasonable_length else "WARN"
|
||||
if not has_content:
|
||||
status = "FAIL"
|
||||
failures += 1
|
||||
|
||||
truncated = content[:60].replace('\n', ' ')
|
||||
print(f" [{i+1:2d}/{len(TEST_PROMPTS)}] {status} | {latency:5.2f}s | pt={pt:3d} ct={ct:2d} | {truncated}...")
|
||||
|
||||
results.append({
|
||||
"prompt": prompt[:40],
|
||||
"status": status,
|
||||
"latency": latency,
|
||||
"prompt_tokens": pt,
|
||||
"completion_tokens": ct,
|
||||
"finish_reason": finish,
|
||||
"content_preview": content[:80],
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
print(f" [{i+1:2d}/{len(TEST_PROMPTS)}] FAIL | {e}")
|
||||
failures += 1
|
||||
results.append({"prompt": prompt[:40], "status": "FAIL", "error": str(e)})
|
||||
|
||||
# Summary
|
||||
successes = len(results) - failures
|
||||
avg_latency = total_latency / max(successes, 1)
|
||||
tok_per_sec = total_completion_tokens / max(total_latency, 0.001)
|
||||
|
||||
print("\n" + "-" * 40)
|
||||
print(f"Results: {successes}/{len(TEST_PROMPTS)} succeeded, {failures} failed")
|
||||
print(f"Total prompt tokens: {total_prompt_tokens}")
|
||||
print(f"Total completion tokens: {total_completion_tokens}")
|
||||
print(f"Average latency: {avg_latency:.2f}s per request")
|
||||
print(f"Throughput: {tok_per_sec:.1f} tokens/s (completion only)")
|
||||
print(f"Verdict: {'PASS' if failures <= 2 else 'FAIL'}")
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def streaming_test():
|
||||
"""Test SSE streaming works correctly."""
|
||||
print("\n" + "=" * 60)
|
||||
print("STREAMING TEST: SSE /v1/chat/completions?stream=true")
|
||||
print("=" * 60)
|
||||
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
body = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": "Count from 1 to 5."}],
|
||||
"max_tokens": 32,
|
||||
"temperature": 0.0,
|
||||
"stream": True,
|
||||
}).encode()
|
||||
|
||||
req = urllib.request.Request(
|
||||
f"{XSERV_URL}/v1/chat/completions",
|
||||
data=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
try:
|
||||
resp = urllib.request.urlopen(req, timeout=60)
|
||||
content_type = resp.headers.get("content-type", "")
|
||||
print(f"Content-Type: {content_type}")
|
||||
|
||||
chunks = []
|
||||
full_text = ""
|
||||
has_role_chunk = False
|
||||
has_done = False
|
||||
has_finish = False
|
||||
|
||||
for line in resp:
|
||||
line = line.decode().strip()
|
||||
if not line:
|
||||
continue
|
||||
if line.startswith("data: "):
|
||||
data = line[6:]
|
||||
if data == "[DONE]":
|
||||
has_done = True
|
||||
chunks.append("[DONE]")
|
||||
continue
|
||||
try:
|
||||
obj = json.loads(data)
|
||||
delta = obj["choices"][0]["delta"]
|
||||
fr = obj["choices"][0].get("finish_reason")
|
||||
if "role" in delta:
|
||||
has_role_chunk = True
|
||||
if "content" in delta:
|
||||
full_text += delta["content"]
|
||||
if fr is not None:
|
||||
has_finish = True
|
||||
chunks.append(delta)
|
||||
except json.JSONDecodeError:
|
||||
print(f" WARN: bad JSON: {data[:80]}")
|
||||
|
||||
print(f"Chunks received: {len(chunks)}")
|
||||
print(f"Has role chunk: {has_role_chunk}")
|
||||
print(f"Has finish_reason: {has_finish}")
|
||||
print(f"Has [DONE]: {has_done}")
|
||||
print(f"Full text: {full_text[:100]!r}")
|
||||
|
||||
ok = has_role_chunk and has_done and has_finish and len(full_text) > 0
|
||||
# SSE content-type check
|
||||
if "text/event-stream" in content_type:
|
||||
print("Content-Type: OK (text/event-stream)")
|
||||
else:
|
||||
print(f"WARN: Expected text/event-stream, got {content_type}")
|
||||
|
||||
print(f"Verdict: {'PASS' if ok else 'FAIL'}")
|
||||
return ok
|
||||
|
||||
except Exception as e:
|
||||
print(f"FAIL: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--mode", choices=["all", "logits", "api", "stream"], default="all")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.mode in ("all", "logits"):
|
||||
logits_correctness_test()
|
||||
|
||||
if args.mode in ("all", "api"):
|
||||
api_generation_test()
|
||||
|
||||
if args.mode in ("all", "stream"):
|
||||
streaming_test()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,107 +1,66 @@
|
||||
"""
|
||||
Test concurrent request handling.
|
||||
Sends N requests simultaneously, verifies they all produce tokens concurrently.
|
||||
|
||||
Usage: python3 tools/test_concurrent.py <server_url> [num_requests]
|
||||
"""
|
||||
import sys
|
||||
import time
|
||||
#!/usr/bin/env python3
|
||||
"""Test concurrent requests to verify continuous batching scheduling."""
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import concurrent.futures
|
||||
|
||||
URL = "http://localhost:9090/v1/chat/completions"
|
||||
|
||||
def send_request(url, prompt, max_tokens, results, idx):
|
||||
"""Send a chat completion request and record timing."""
|
||||
PROMPTS = [
|
||||
"What is 1+1?",
|
||||
"What is 2+2?",
|
||||
"What is 3+3?",
|
||||
"What is 4+4?",
|
||||
"What is 5+5?",
|
||||
"What is 6+6?",
|
||||
"What is 7+7?",
|
||||
"What is 8+8?",
|
||||
]
|
||||
|
||||
def send_request(prompt, idx):
|
||||
body = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": max_tokens,
|
||||
"max_tokens": 32,
|
||||
"temperature": 0.0,
|
||||
}).encode()
|
||||
|
||||
req = urllib.request.Request(
|
||||
f"{url}/v1/chat/completions",
|
||||
data=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
t0 = time.time()
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=120) as resp:
|
||||
data = json.loads(resp.read())
|
||||
t1 = time.time()
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
results[idx] = {
|
||||
"status": "ok",
|
||||
"content": content,
|
||||
"duration_s": t1 - t0,
|
||||
"finish_reason": data["choices"][0]["finish_reason"],
|
||||
}
|
||||
except Exception as e:
|
||||
t1 = time.time()
|
||||
results[idx] = {"status": "error", "error": str(e), "duration_s": t1 - t0}
|
||||
|
||||
req = urllib.request.Request(URL, data=body, headers={"Content-Type": "application/json"})
|
||||
t0 = time.perf_counter()
|
||||
resp = urllib.request.urlopen(req, timeout=120)
|
||||
elapsed = time.perf_counter() - t0
|
||||
data = json.loads(resp.read())
|
||||
content = data["choices"][0]["message"]["content"][:50].replace('\n', ' ')
|
||||
ct = data["usage"]["completion_tokens"]
|
||||
return idx, prompt, elapsed, ct, content
|
||||
|
||||
def main():
|
||||
url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:9090"
|
||||
n = int(sys.argv[2]) if len(sys.argv) > 2 else 3
|
||||
max_tokens = 10
|
||||
print("=== Concurrent request test (8 requests, max_batch=4) ===\n")
|
||||
|
||||
prompts = [
|
||||
"What is the capital of France?",
|
||||
"Tell me about quantum computing",
|
||||
"How do airplanes fly?",
|
||||
"What is machine learning?",
|
||||
"Explain gravity in simple terms",
|
||||
][:n]
|
||||
# Fire all 8 requests concurrently
|
||||
t_start = time.perf_counter()
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
|
||||
futures = [pool.submit(send_request, p, i) for i, p in enumerate(PROMPTS)]
|
||||
results = [f.result() for f in concurrent.futures.as_completed(futures)]
|
||||
t_total = time.perf_counter() - t_start
|
||||
|
||||
print(f"Sending {n} concurrent requests to {url} (max_tokens={max_tokens})")
|
||||
print("=" * 70)
|
||||
results.sort(key=lambda r: r[0])
|
||||
total_tokens = 0
|
||||
for idx, prompt, elapsed, ct, content in results:
|
||||
total_tokens += ct
|
||||
print(f" [{idx}] {elapsed:5.2f}s | ct={ct:2d} | {prompt} -> {content}...")
|
||||
|
||||
results = [None] * n
|
||||
threads = []
|
||||
|
||||
t_start = time.time()
|
||||
for i, prompt in enumerate(prompts):
|
||||
t = threading.Thread(target=send_request, args=(url, prompt, max_tokens, results, i))
|
||||
threads.append(t)
|
||||
t.start()
|
||||
|
||||
for t in threads:
|
||||
t.join()
|
||||
t_total = time.time() - t_start
|
||||
|
||||
print(f"\n{'#':>2} {'Status':>6} {'Duration':>8} {'Content':<50}")
|
||||
print("-" * 70)
|
||||
for i, r in enumerate(results):
|
||||
if r["status"] == "ok":
|
||||
content_short = r["content"].replace("\n", " ")[:48]
|
||||
print(f"{i+1:>2} {'OK':>6} {r['duration_s']:>6.1f}s {content_short}")
|
||||
else:
|
||||
print(f"{i+1:>2} {'FAIL':>6} {r['duration_s']:>6.1f}s {r['error'][:48]}")
|
||||
|
||||
print("=" * 70)
|
||||
print(f"Total wall time: {t_total:.1f}s")
|
||||
|
||||
# Analyze concurrency
|
||||
durations = [r["duration_s"] for r in results if r["status"] == "ok"]
|
||||
if len(durations) >= 2:
|
||||
sequential_estimate = sum(durations)
|
||||
actual_wall = t_total
|
||||
concurrency_ratio = sequential_estimate / actual_wall if actual_wall > 0 else 0
|
||||
|
||||
print(f"Sum of individual durations: {sequential_estimate:.1f}s")
|
||||
print(f"Actual wall time: {actual_wall:.1f}s")
|
||||
print(f"Concurrency ratio: {concurrency_ratio:.2f}x")
|
||||
|
||||
if concurrency_ratio > 1.5:
|
||||
print("✓ CONCURRENT: requests are being processed in parallel")
|
||||
else:
|
||||
print("✗ SERIAL: requests appear to be processed sequentially")
|
||||
|
||||
all_ok = all(r["status"] == "ok" for r in results)
|
||||
print(f"\nAll requests succeeded: {all_ok}")
|
||||
serial_estimate = sum(r[2] for r in results)
|
||||
print(f"\n Wall clock: {t_total:.2f}s")
|
||||
print(f" Sum of individual latencies: {serial_estimate:.2f}s")
|
||||
print(f" Concurrency speedup: {serial_estimate/t_total:.2f}x (1.0x = no batching)")
|
||||
print(f" Total tokens: {total_tokens}")
|
||||
print(f" Throughput: {total_tokens/t_total:.1f} tok/s")
|
||||
|
||||
if t_total < serial_estimate * 0.85:
|
||||
print(f"\n Concurrent scheduling is working (wall < 85% of serial sum)")
|
||||
else:
|
||||
print(f"\n Limited concurrency benefit (scheduling correct, GPU still per-seq)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
239
tools/test_correctness.py
Normal file
239
tools/test_correctness.py
Normal file
@@ -0,0 +1,239 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare xserv vs HuggingFace transformers for correctness and performance.
|
||||
|
||||
Strategy: run xserv first (on GPU 0), collect results, then load HF model
|
||||
on GPU 0 (xserv process exits and frees VRAM).
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import time
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
|
||||
DEVICE = "cuda:0"
|
||||
MODEL_DIR = "/opt/wjh/models/qwen3-8b"
|
||||
XSERV_DUMP = "/opt/wjh/projects/xserv/target/release/dump-logits"
|
||||
|
||||
|
||||
def xserv_dump_logits(prompt):
|
||||
"""Run xserv dump-logits and parse top-20."""
|
||||
env = os.environ.copy()
|
||||
env["PATH"] = "/usr/local/cuda-12.9/bin:" + env.get("PATH", "")
|
||||
env["CUDA_VISIBLE_DEVICES"] = "0"
|
||||
t0 = time.perf_counter()
|
||||
result = subprocess.run(
|
||||
[XSERV_DUMP, MODEL_DIR, prompt],
|
||||
capture_output=True, text=True, timeout=180, env=env
|
||||
)
|
||||
elapsed = time.perf_counter() - t0
|
||||
if result.returncode != 0:
|
||||
print(f" xserv error: {result.stderr[-500:]}")
|
||||
return None, elapsed
|
||||
|
||||
top20 = []
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
m = re.match(r'\s*\[\s*\d+\]\s+id=\s*(\d+)\s+logit=\s*([\-\d.]+)', line)
|
||||
if m:
|
||||
top20.append((int(m.group(1)), float(m.group(2))))
|
||||
return top20, elapsed
|
||||
|
||||
|
||||
def hf_prefill_top20(model, tokenizer, prompt):
|
||||
"""Get top-20 logits from HF."""
|
||||
import torch
|
||||
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
||||
with torch.no_grad():
|
||||
outputs = model(**inputs)
|
||||
logits = outputs.logits[0, -1, :].float().cpu().numpy()
|
||||
import numpy as np
|
||||
top_ids = np.argsort(logits)[-20:][::-1]
|
||||
return [(int(i), float(logits[i])) for i in top_ids]
|
||||
|
||||
|
||||
def hf_generate(model, tokenizer, prompt, max_new=80):
|
||||
"""Greedy generation from HF."""
|
||||
import torch
|
||||
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
||||
prompt_len = inputs["input_ids"].shape[1]
|
||||
torch.cuda.synchronize()
|
||||
t0 = time.perf_counter()
|
||||
with torch.no_grad():
|
||||
out = model.generate(**inputs, max_new_tokens=max_new, do_sample=False)
|
||||
torch.cuda.synchronize()
|
||||
elapsed = time.perf_counter() - t0
|
||||
gen_tokens = out.shape[1] - prompt_len
|
||||
text = tokenizer.decode(out[0][prompt_len:], skip_special_tokens=True)
|
||||
return gen_tokens / elapsed, elapsed, gen_tokens, text
|
||||
|
||||
|
||||
def compare_top20(hf_top20, xs_top20, name):
|
||||
if xs_top20 is None:
|
||||
print(f" [{name}] SKIP (xserv failed)")
|
||||
return False
|
||||
|
||||
hf_ids = [x[0] for x in hf_top20]
|
||||
xs_ids = [x[0] for x in xs_top20]
|
||||
top1_match = hf_ids[0] == xs_ids[0]
|
||||
top5_match = set(hf_ids[:5]) == set(xs_ids[:5])
|
||||
top10_overlap = len(set(hf_ids[:10]) & set(xs_ids[:10]))
|
||||
|
||||
hf_dict = dict(hf_top20)
|
||||
xs_dict = dict(xs_top20)
|
||||
common = set(hf_dict.keys()) & set(xs_dict.keys())
|
||||
if common:
|
||||
diffs = [abs(hf_dict[k] - xs_dict[k]) for k in common]
|
||||
max_diff = max(diffs)
|
||||
mean_diff = sum(diffs) / len(diffs)
|
||||
else:
|
||||
max_diff = mean_diff = float('inf')
|
||||
|
||||
status = "PASS" if top1_match and top5_match else "FAIL"
|
||||
print(f" [{name}] {status}: top1={'Y' if top1_match else 'N'}, "
|
||||
f"top5={'Y' if top5_match else 'N'}, top10={top10_overlap}/10, "
|
||||
f"max_diff={max_diff:.4f}, mean_diff={mean_diff:.4f}")
|
||||
print(f" HF top5: {[(i, f'{v:.2f}') for i, v in hf_top20[:5]]}")
|
||||
print(f" XS top5: {[(i, f'{v:.2f}') for i, v in xs_top20[:5]]}")
|
||||
return status == "PASS"
|
||||
|
||||
|
||||
def benchmark_xserv_server(prompt, num_tokens=80, port=8080):
|
||||
import urllib.request
|
||||
data = json.dumps({
|
||||
"model": "qwen3-8b",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": num_tokens,
|
||||
"temperature": 0,
|
||||
"stream": False
|
||||
}).encode()
|
||||
req = urllib.request.Request(
|
||||
f"http://localhost:{port}/v1/chat/completions",
|
||||
data=data, headers={"Content-Type": "application/json"}
|
||||
)
|
||||
start = time.perf_counter()
|
||||
with urllib.request.urlopen(req, timeout=120) as resp:
|
||||
result = json.loads(resp.read())
|
||||
elapsed = time.perf_counter() - start
|
||||
content = result["choices"][0]["message"]["content"]
|
||||
usage = result.get("usage", {})
|
||||
ct = usage.get("completion_tokens", 0)
|
||||
return ct / elapsed if elapsed > 0 else 0, elapsed, ct, content
|
||||
|
||||
|
||||
def main():
|
||||
with_server = "--with-server" in sys.argv
|
||||
|
||||
print("=" * 70)
|
||||
print("xserv vs HuggingFace Transformers — Correctness & Performance")
|
||||
print("=" * 70)
|
||||
print(f"Model: {MODEL_DIR}")
|
||||
print(f"Device: {DEVICE}\n")
|
||||
|
||||
# ── Phase A: Run xserv first (separate processes, each loads+runs+exits) ──
|
||||
test_prompts = [
|
||||
("english", "<|im_start|>user\nWhat is the capital of France?<|im_end|>\n<|im_start|>assistant\n"),
|
||||
("chinese", "<|im_start|>user\n请介绍一下量子计算<|im_end|>\n<|im_start|>assistant\n"),
|
||||
("code", "<|im_start|>user\nWrite a Python function to sort a list<|im_end|>\n<|im_start|>assistant\n"),
|
||||
("multi_turn",
|
||||
"<|im_start|>user\nHello<|im_end|>\n<|im_start|>assistant\nHi!<|im_end|>\n<|im_start|>user\nWhat is 2+2?<|im_end|>\n<|im_start|>assistant\n"),
|
||||
]
|
||||
|
||||
print("=" * 50)
|
||||
print("PART 1: Collecting xserv prefill logits")
|
||||
print("=" * 50)
|
||||
|
||||
xs_results = {}
|
||||
for name, prompt in test_prompts:
|
||||
print(f" Running xserv dump-logits [{name}]...")
|
||||
top20, elapsed = xserv_dump_logits(prompt)
|
||||
xs_results[name] = top20
|
||||
if top20:
|
||||
print(f" OK ({len(top20)} logits, {elapsed:.1f}s)")
|
||||
else:
|
||||
print(f" FAILED ({elapsed:.1f}s)")
|
||||
|
||||
# ── Phase B: Load HF model and compare ──
|
||||
print(f"\n{'=' * 50}")
|
||||
print("PART 2: Loading HF model for comparison")
|
||||
print("=" * 50)
|
||||
|
||||
import torch
|
||||
import numpy as np
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
|
||||
print("Loading HF model (BF16)...")
|
||||
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=True)
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
MODEL_DIR, dtype=torch.bfloat16,
|
||||
device_map=DEVICE, trust_remote_code=True
|
||||
)
|
||||
model.eval()
|
||||
print("HF model loaded.\n")
|
||||
|
||||
print("=" * 50)
|
||||
print("PART 3: Correctness Comparison")
|
||||
print("=" * 50)
|
||||
|
||||
all_pass = True
|
||||
for name, prompt in test_prompts:
|
||||
hf_top20 = hf_prefill_top20(model, tokenizer, prompt)
|
||||
if not compare_top20(hf_top20, xs_results[name], name):
|
||||
all_pass = False
|
||||
|
||||
print(f"\n Overall: {'ALL PASS' if all_pass else 'SOME FAILED'}\n")
|
||||
|
||||
# ── Phase C: Performance benchmark ──
|
||||
print("=" * 50)
|
||||
print("PART 4: HF Decode Performance (greedy, batch=1)")
|
||||
print("=" * 50)
|
||||
|
||||
bench_prompt = "<|im_start|>user\nExplain the theory of relativity in simple terms.<|im_end|>\n<|im_start|>assistant\n"
|
||||
|
||||
# Warmup
|
||||
print("\nWarmup...")
|
||||
hf_generate(model, tokenizer, bench_prompt, max_new=5)
|
||||
|
||||
# Benchmark multiple token counts
|
||||
for num_tokens in [50, 80]:
|
||||
hf_tps, hf_time, hf_gen, hf_text = hf_generate(model, tokenizer, bench_prompt, max_new=num_tokens)
|
||||
print(f" HF ({num_tokens} tokens): {hf_tps:.1f} tok/s, {hf_time:.2f}s, {hf_gen} generated")
|
||||
|
||||
# xserv server benchmark
|
||||
if with_server:
|
||||
print(f"\n{'=' * 50}")
|
||||
print("PART 5: xserv Server Performance")
|
||||
print("=" * 50)
|
||||
try:
|
||||
import urllib.request
|
||||
urllib.request.urlopen("http://localhost:8080/health", timeout=3)
|
||||
print("Server available. Benchmarking...\n")
|
||||
|
||||
# Warmup
|
||||
benchmark_xserv_server("Hi", 5)
|
||||
time.sleep(0.5)
|
||||
|
||||
for num_tokens in [50, 80]:
|
||||
xs_tps, xs_time, xs_gen, xs_text = benchmark_xserv_server(
|
||||
"Explain the theory of relativity in simple terms.", num_tokens
|
||||
)
|
||||
print(f" xserv ({num_tokens} tokens): {xs_tps:.1f} tok/s, {xs_time:.2f}s, {xs_gen} generated")
|
||||
print(f" Text: {xs_text[:120]}...")
|
||||
|
||||
# EOS leak check
|
||||
print(f"\n EOS Leak Check:")
|
||||
_, _, _, content = benchmark_xserv_server("Say hello", 20)
|
||||
has_eos = "<|im_end|>" in content or "<|endoftext|>" in content or "<|im_start|>" in content
|
||||
print(f" Response has EOS token: {'YES (FAIL)' if has_eos else 'NO (PASS)'}")
|
||||
if has_eos:
|
||||
print(f" Content: {content}")
|
||||
except Exception as e:
|
||||
print(f"Server not available: {e}")
|
||||
|
||||
print(f"\n{'=' * 50}")
|
||||
print("DONE")
|
||||
print("=" * 50)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user