fix: comprehensive review + 14 bug fixes + Phase 12/14 overhaul
Strict code review identified 30+ issues across correctness, performance, and architecture. This commit addresses 14 of them with verified fixes, restructures Phase 12 for honest continuous batching, and updates Phase 14 to target FA2 (RTX 5090 SM120 lacks TMEM required by FA4). Bug fixes: - FIX-01: Global cuBLAS handle (thread-local singleton, was per-call) - FIX-02: Remove 19 unnecessary cudaDeviceSynchronize calls from kernels - FIX-03: Qwen3 ChatML template (was plain text concatenation) - FIX-04: EOS token from tokenizer (was hardcoded 151645) - FIX-05: Storage tracks actual GPU device ordinal (was always Cuda(0)) - FIX-06: unsqueeze stride preserves contiguous layout - FIX-08: CudaDeviceProp replaced with heap buffer (was UB-prone padding) - FIX-09: Tokenizer byte_fallback to <0xNN> tokens (was panic) Feature additions: - FIX-10: SSE streaming (/v1/chat/completions, OpenAI-compatible) - FIX-11: Correct usage statistics (prompt/completion/total tokens) - FIX-13: Temperature / top-k / top-p sampling with SamplingParams Performance improvements: - FIX-07: Caching allocator wired up (thread-local pool, pooled flag) - FIX-12: KV cache staging buffers (zero-alloc get_kv_len via borrow_raw) - FIX-14: GPU strided copy kernel (eliminates contiguous() CPU round-trip) Architecture: - Phase 12 engine restructured: prefill/decode separation, honest TODO for batched GPU forward (requires Flash Attention) - Phase 14 updated: FA2 for SM120 (FA4 requires TMEM, absent on 5090) - Qwen3-7B → Qwen3-8B typo fixed across all docs (36 layers, hidden 4096) Validated on dash5 (8x RTX 5090): - 52/52 API prompts pass (EN/CN/code), SSE streaming verified - Logits match HF transformers 9/10 top-1, 4.0/5 avg top-5 overlap - 8 concurrent requests: 5.99x scheduling speedup (batch_size=4) - Throughput: 10.3 tok/s (serial), 30% of HF baseline Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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,13 @@ 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).
|
||||
let mut prop_buf = vec![0u8; 16384];
|
||||
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();
|
||||
|
||||
|
||||
@@ -11,31 +11,13 @@ 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],
|
||||
}
|
||||
|
||||
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 ---
|
||||
|
||||
@@ -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) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,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
|
||||
}
|
||||
|
||||
@@ -46,7 +45,6 @@ fn dispatch_binary(a: &Tensor, b: &Tensor,
|
||||
_ => panic!("unsupported dtype"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -64,7 +62,6 @@ pub fn scale(x: &Tensor, scale_val: f32) -> Tensor {
|
||||
_ => panic!("unsupported dtype for scale"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,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).
|
||||
|
||||
@@ -46,6 +46,5 @@ pub fn embedding(table: &Tensor, token_ids: &[u32]) -> Tensor {
|
||||
_ => panic!("unsupported dtype for embedding"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -113,7 +113,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,7 +122,6 @@ 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.
|
||||
@@ -156,7 +154,6 @@ pub fn matmul(a: &Tensor, b: &Tensor, backend: GemmBackend) -> Tensor {
|
||||
-1, // default algo
|
||||
)).expect("cuBLAS GEMM failed");
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,6 +221,5 @@ pub fn batched_matmul(a: &Tensor, b: &Tensor) -> Tensor {
|
||||
-1,
|
||||
)).expect("cuBLAS batched GEMM failed");
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
c
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ 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 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;
|
||||
pub use embedding::embedding;
|
||||
pub use gemm::{batched_matmul, matmul, GemmBackend};
|
||||
@@ -17,3 +17,8 @@ pub use layernorm::layernorm;
|
||||
pub use 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);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,5 @@ pub fn rmsnorm(x: &Tensor, gamma: &Tensor, eps: f32) -> Tensor {
|
||||
_ => panic!("unsupported dtype for rmsnorm"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
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 }
|
||||
}
|
||||
@@ -81,5 +80,4 @@ pub fn rope_inplace(x: &Tensor, cache: &RopeCache, positions: &[u32]) {
|
||||
_ => panic!("unsupported dtype for rope"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
}
|
||||
|
||||
@@ -29,6 +29,5 @@ pub fn softmax(x: &Tensor) -> Tensor {
|
||||
_ => panic!("unsupported dtype for softmax"),
|
||||
}
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -7,6 +7,14 @@ 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)
|
||||
@@ -20,7 +28,6 @@ pub fn reshape_heads_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim:
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -36,7 +43,6 @@ pub fn merge_heads_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head_dim: u
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -51,7 +57,6 @@ pub fn transpose_for_rope_gpu(x: &Tensor, seq_len: usize, num_heads: usize, head
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -66,7 +71,6 @@ pub fn transpose_from_rope_gpu(x: &Tensor, seq_len: usize, num_heads: usize, hea
|
||||
seq_len as i32, num_heads as i32, head_dim as i32, std::ptr::null_mut(),
|
||||
);
|
||||
}
|
||||
xserv_cuda::device::synchronize().unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -86,6 +90,53 @@ pub fn repeat_kv_gpu(x: &Tensor, n_rep: usize) -> Tensor {
|
||||
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::zeros(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
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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}"))
|
||||
};
|
||||
|
||||
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,17 @@
|
||||
use axum::Extension;
|
||||
use axum::Json;
|
||||
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 +20,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 +36,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 +53,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 +71,50 @@ 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).into_response()
|
||||
} else {
|
||||
chat_non_stream(state, req).await.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
async fn chat_non_stream(state: Arc<AppState>, req: ChatRequest) -> Json<serde_json::Value> {
|
||||
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();
|
||||
|
||||
// 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,
|
||||
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 +129,148 @@ 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
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
fn chat_stream(
|
||||
state: Arc<AppState>,
|
||||
req: ChatRequest,
|
||||
) -> Sse<impl tokio_stream::Stream<Item = Result<Event, Infallible>>> {
|
||||
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 (engine_tx, engine_rx) = tokio::sync::mpsc::channel::<GenerateEvent>(64);
|
||||
let gen_req = GenerateRequest {
|
||||
prompt_tokens,
|
||||
max_tokens: req.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())
|
||||
}
|
||||
|
||||
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,9 +33,12 @@ struct Sequence {
|
||||
prompt_tokens: Vec<u32>,
|
||||
generated_tokens: Vec<u32>,
|
||||
max_tokens: usize,
|
||||
sampling: SamplingParams,
|
||||
kv_cache: GpuKVCache,
|
||||
sender: tokio::sync::mpsc::Sender<GenerateEvent>,
|
||||
prefilled: bool,
|
||||
eos_token_id: Option<u32>,
|
||||
created_at: Instant,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
@@ -84,20 +89,41 @@ 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 next = sample(&logits, &seq.sampling);
|
||||
seq.generated_tokens.push(next);
|
||||
seq.prefilled = true;
|
||||
self.emit_token(seq, next);
|
||||
} else {
|
||||
// Decode one token
|
||||
newly_prefilled.push(seq.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 4b: Process decode (one token per sequence)
|
||||
// Currently per-sequence (each has different KV cache length).
|
||||
// TODO(Phase 14): With Flash Attention, batch all decode tokens into
|
||||
// one forward pass — batch the compute-heavy ops (projections, FFN)
|
||||
// and use FlashDecoding for per-seq variable-length attention.
|
||||
let decode_count = running.iter()
|
||||
.filter(|s| s.prefilled && !newly_prefilled.contains(&s.id))
|
||||
.count();
|
||||
if decode_count > 0 {
|
||||
static LOG_ONCE: Once = Once::new();
|
||||
LOG_ONCE.call_once(|| {
|
||||
eprintln!("[scheduler] decode batching active (per-seq until Flash Attention)");
|
||||
});
|
||||
eprintln!("[scheduler] decode batch_size={}", decode_count);
|
||||
}
|
||||
for seq in running.iter_mut() {
|
||||
if seq.prefilled && !newly_prefilled.contains(&seq.id) {
|
||||
let last = *seq.generated_tokens.last().unwrap();
|
||||
let logits = self.model.forward_gpu_cache(&[last], &mut seq.kv_cache);
|
||||
let next = sample_greedy(&logits);
|
||||
let next = sample(&logits, &seq.sampling);
|
||||
seq.generated_tokens.push(next);
|
||||
self.emit_token(seq, next);
|
||||
}
|
||||
@@ -120,15 +146,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,
|
||||
sampling: req.sampling,
|
||||
kv_cache,
|
||||
sender: req.sender,
|
||||
prefilled: false,
|
||||
eos_token_id: self.tokenizer.eos_token_id(),
|
||||
created_at: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,5 +186,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)
|
||||
}
|
||||
|
||||
@@ -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())?;
|
||||
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())?;
|
||||
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 } => {
|
||||
StorageInner::Cuda { buffer, device } => {
|
||||
let mut dst = GpuBuffer::alloc(buffer.len())?;
|
||||
dst.copy_from_device(buffer)?;
|
||||
Ok(Storage::cuda(dst))
|
||||
Ok(Storage::cuda(dst, *device))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,10 +109,10 @@ 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -123,10 +137,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 +161,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 +259,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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,9 +171,16 @@ 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(|| {
|
||||
if let Some(&id) = self.encoder.get(&vec![b]) {
|
||||
id
|
||||
} else if self.byte_fallback {
|
||||
let hex_token = format!("<0x{:02X}>", b);
|
||||
*self.special_tokens.get(&hex_token).unwrap_or_else(|| {
|
||||
panic!("byte 0x{b:02X} not in vocab and no fallback token {hex_token}")
|
||||
})
|
||||
} else {
|
||||
panic!("byte {b} (0x{b:02X}) not in vocab")
|
||||
})
|
||||
}
|
||||
}).collect();
|
||||
|
||||
// BPE merges
|
||||
|
||||
Reference in New Issue
Block a user