Compare commits
2
Commits
0a46c4dd7b
...
dfa6013bfd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dfa6013bfd | ||
|
|
4bc268f69c |
No files matched your search
@@ -130,9 +130,33 @@ pub struct DeviceBuf {
|
||||
pub bytes: usize,
|
||||
}
|
||||
|
||||
/// Size-bucketed free list of device buffers. The layer forward allocates and
|
||||
/// frees ~25 short-lived intermediates per visit (~1200 hipMalloc/hipFree per
|
||||
/// token); hipMalloc/hipFree each synchronize the device, dominating decode
|
||||
/// latency. Reusing freed buffers by exact size removes that churn. Safe because
|
||||
/// all launches share the default stream and every host copy (hipMemcpy) is
|
||||
/// blocking: a buffer is only recycled after its last use is ordered before the
|
||||
/// next launch that reuses it. Thread-local (cargo test runs tests in parallel).
|
||||
/// Escape hatch: VETTA_NO_BUF_POOL=1 forces raw hipMalloc/hipFree.
|
||||
#[cfg(feature = "hip")]
|
||||
thread_local! {
|
||||
static BUF_POOL: std::cell::RefCell<std::collections::HashMap<usize, Vec<*mut std::os::raw::c_void>>> =
|
||||
std::cell::RefCell::new(std::collections::HashMap::new());
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
fn pool_enabled() -> bool {
|
||||
std::env::var("VETTA_NO_BUF_POOL").is_err()
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
impl DeviceBuf {
|
||||
pub fn alloc(bytes: usize) -> Result<Self, String> {
|
||||
if pool_enabled() {
|
||||
if let Some(ptr) = BUF_POOL.with(|p| p.borrow_mut().get_mut(&bytes).and_then(|v| v.pop())) {
|
||||
return Ok(Self { ptr, bytes });
|
||||
}
|
||||
}
|
||||
let mut ptr: *mut std::os::raw::c_void = std::ptr::null_mut();
|
||||
let rc = unsafe { ffi::vetta_buf_alloc(&mut ptr, bytes) };
|
||||
if rc != 0 || ptr.is_null() { return Err(format!("hipMalloc({bytes}) failed")); }
|
||||
@@ -169,7 +193,11 @@ impl DeviceBuf {
|
||||
#[cfg(feature = "hip")]
|
||||
impl Drop for DeviceBuf {
|
||||
fn drop(&mut self) {
|
||||
unsafe { ffi::vetta_buf_free(self.ptr); }
|
||||
if pool_enabled() && !self.ptr.is_null() {
|
||||
BUF_POOL.with(|p| p.borrow_mut().entry(self.bytes).or_default().push(self.ptr));
|
||||
} else {
|
||||
unsafe { ffi::vetta_buf_free(self.ptr); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -423,6 +423,7 @@ fn dbg_norm(name: &str, buf: &DeviceBuf, n: usize) {
|
||||
fn run_experts(
|
||||
cache: &WeightCache, ip: usize, ids_h: &[i32], mlpin: &DeviceBuf, d: &Dims, h: usize,
|
||||
) -> Result<Vec<f32>, String> {
|
||||
let t0 = std::time::Instant::now();
|
||||
let gu = DeviceBuf::alloc(2 * d.inter * 4)?;
|
||||
let mut y_part_h = vec![0f32; d.top_k * h];
|
||||
if std::env::var("VETTA_F16_EXPERT").is_ok() {
|
||||
@@ -452,6 +453,7 @@ fn run_experts(
|
||||
ye.d2h(&mut y_part_h[kk * h..(kk + 1) * h])?;
|
||||
}
|
||||
}
|
||||
EXPERT_NS.with(|c| c.set(c.get() + t0.elapsed().as_nanos()));
|
||||
Ok(y_part_h)
|
||||
}
|
||||
|
||||
@@ -499,7 +501,10 @@ pub fn forward_inkling_with_state(
|
||||
states: &mut [Option<LayerState>],
|
||||
) -> Result<DeviceBuf, String> {
|
||||
let h = d.hidden;
|
||||
let prof = std::env::var("VETTA_PROF").is_ok();
|
||||
let tp0 = std::time::Instant::now();
|
||||
let cur = embed_token(cache, token, h, vocab)?;
|
||||
let t_embed = tp0.elapsed().as_nanos();
|
||||
if std::env::var("VETTA_DBG_NORM").is_ok() {
|
||||
let mut eh = vec![0f32; h]; cur.d2h(&mut eh).unwrap();
|
||||
let n = eh.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
@@ -507,7 +512,9 @@ pub fn forward_inkling_with_state(
|
||||
}
|
||||
let mut blocks: Vec<DeviceBuf> = Vec::new();
|
||||
// Prelude.
|
||||
let tp1 = std::time::Instant::now();
|
||||
for ip in 0..6 { run_layer(cache, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; }
|
||||
let t_prelude = tp1.elapsed().as_nanos();
|
||||
let mut prelude = vec![0f32; h]; cur.d2h(&mut prelude)?;
|
||||
let n_prelude_blocks = blocks.len();
|
||||
if std::env::var("VETTA_DBG_NORM").is_ok() {
|
||||
@@ -515,6 +522,7 @@ pub fn forward_inkling_with_state(
|
||||
eprintln!("[dbg] after prelude: cur_norm={n:.3} blocks={}", blocks.len());
|
||||
}
|
||||
// Core: 3 passes.
|
||||
let tp2 = std::time::Instant::now();
|
||||
for p in 0..3 {
|
||||
let mut bank: Vec<DeviceBuf> = Vec::new();
|
||||
let bank_out = DeviceBuf::alloc(h * 4)?;
|
||||
@@ -547,14 +555,29 @@ pub fn forward_inkling_with_state(
|
||||
}
|
||||
}
|
||||
// Coda.
|
||||
let t_core = tp2.elapsed().as_nanos();
|
||||
let tp3 = std::time::Instant::now();
|
||||
for ip in 18..24 { run_layer(cache, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; }
|
||||
let t_coda = tp3.elapsed().as_nanos();
|
||||
if std::env::var("VETTA_DBG_NORM").is_ok() {
|
||||
let mut ch = vec![0f32; h]; cur.d2h(&mut ch).unwrap();
|
||||
let n = ch.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
eprintln!("[dbg] after coda: cur_norm={n:.3} blocks={}", blocks.len());
|
||||
for (i, b) in blocks.iter().enumerate() { let mut bh = vec![0f32; h]; b.d2h(&mut bh).unwrap(); let bn = bh.iter().map(|v| v * v).sum::<f32>().sqrt(); eprintln!("[dbg] block[{i}]_norm={bn:.3}"); }
|
||||
}
|
||||
final_logits(cache, &cur, &blocks, h, vocab, d.eps, cap)
|
||||
let tp4 = std::time::Instant::now();
|
||||
let out = final_logits(cache, &cur, &blocks, h, vocab, d.eps, cap);
|
||||
if prof {
|
||||
let t_final = tp4.elapsed().as_nanos();
|
||||
let exp = EXPERT_NS.with(|c| c.replace(0));
|
||||
eprintln!("[PROF] pos={pos} embed={:.2}ms prelude={:.2}ms core={:.2}ms coda={:.2}ms final={:.2}ms experts={:.2}ms total={:.2}ms",
|
||||
t_embed as f64 / 1e6, t_prelude as f64 / 1e6, t_core as f64 / 1e6, t_coda as f64 / 1e6, t_final as f64 / 1e6, exp as f64 / 1e6, tp0.elapsed().as_nanos() as f64 / 1e6);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static EXPERT_NS: std::cell::Cell<u128> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Load a full-attn ShortConv static weight tensor [4, C] transposed to the
|
||||
|
||||
@@ -586,7 +586,7 @@ extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
|
||||
int N, int K, vetta_stream_t stream) {
|
||||
if (N <= 0 || K <= 0 || (K % 16) != 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_nvfp4_gemv<<<N, 256, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
|
||||
k_nvfp4_gemv<<<N, 128, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
@@ -683,7 +683,7 @@ extern "C" int vetta_mxfp8_gemv(const uint8_t* weights, const uint8_t* scales,
|
||||
vetta_stream_t stream) {
|
||||
if (N <= 0 || K <= 0 || (K % 32) != 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_mxfp8_gemv<<<N, 256, 0, s>>>(weights, scales, x, y, N, K);
|
||||
k_mxfp8_gemv<<<N, 64, 0, s>>>(weights, scales, x, y, N, K);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user