A high-performance inference engine for AI models
Fix kernel execution time (#245)
eugene4f233f73bb6b
diff --git a/crates/uzu/src/backends/common/command_buffer.rs b/crates/uzu/src/backends/common/command_buffer.rsindex a9bdb96cc..d597c899b 100644--- a/crates/uzu/src/backends/common/command_buffer.rs+++ b/crates/uzu/src/backends/common/command_buffer.rs@@ -1,4 +1,4 @@-use std::ops::Range;+use std::{ops::Range, time::Duration};use super::Backend;@@ -80,5 +80,6 @@ pub trait CommandBufferPending {pub trait CommandBufferCompleted {type CommandBuffer: CommandBuffer<Completed = Self>;- fn gpu_execution_time_ms(&self) -> Option<f64>;+ /// Returns GPU execution time.+ fn gpu_execution_time(&self) -> Option<Duration>;}diff --git a/crates/uzu/src/backends/cpu/command_buffer.rs b/crates/uzu/src/backends/cpu/command_buffer.rsindex 726f09ff6..1896a56f1 100644--- a/crates/uzu/src/backends/cpu/command_buffer.rs+++ b/crates/uzu/src/backends/cpu/command_buffer.rs@@ -1,6 +1,6 @@use std::{cell::{OnceCell, RefCell},- time::Instant,+ time::{Duration, Instant},};use super::Cpu;@@ -15,7 +15,7 @@ use crate::backends::{pub struct CpuCommandBuffer {commands: Vec<Box<dyn Fn()>>,completion_handlers: Vec<Box<dyn FnOnce(Result<&CpuCommandBuffer, CpuError>)>>,- gpu_execution_time_ms: OnceCell<f64>,+ gpu_execution_time: OnceCell<Duration>,}impl CommandBuffer for CpuCommandBuffer {@@ -33,7 +33,7 @@ impl CpuCommandBuffer {CpuCommandBuffer {commands: Vec::new(),completion_handlers: Vec::new(),- gpu_execution_time_ms: OnceCell::new(),+ gpu_execution_time: OnceCell::new(),}}@@ -121,9 +121,7 @@ impl CommandBufferExecutable for CpuCommandBuffer {command()}- self.gpu_execution_time_ms- .set((Instant::now() - start).as_secs_f64() * 1000.0)- .expect("gpu execution time already set");+ self.gpu_execution_time.set(Instant::now() - start).expect("gpu execution time already set");for completion_handler in self.completion_handlers.drain(..).collect::<Vec<_>>() {completion_handler(Ok(&self));@@ -132,7 +130,7 @@ impl CommandBufferExecutable for CpuCommandBuffer {CpuCommandBuffer {commands: Vec::new(),completion_handlers: Vec::new(),- gpu_execution_time_ms: self.gpu_execution_time_ms,+ gpu_execution_time: self.gpu_execution_time,}}}@@ -148,7 +146,7 @@ impl CommandBufferPending for CpuCommandBuffer {impl CommandBufferCompleted for CpuCommandBuffer {type CommandBuffer = CpuCommandBuffer;- fn gpu_execution_time_ms(&self) -> Option<f64> {- self.gpu_execution_time_ms.get().copied()+ fn gpu_execution_time(&self) -> Option<Duration> {+ self.gpu_execution_time.get().copied()}}diff --git a/crates/uzu/src/backends/metal/command_buffer.rs b/crates/uzu/src/backends/metal/command_buffer.rsindex 8b72aaf00..7f97b6dac 100644--- a/crates/uzu/src/backends/metal/command_buffer.rs+++ b/crates/uzu/src/backends/metal/command_buffer.rs@@ -1,4 +1,4 @@-use std::cell::Cell;+use std::{cell::Cell, time::Duration};use metal::{MTLBlitCommandEncoder, MTLBlitCommandEncoderExt, MTLBuffer, MTLCommandBuffer, MTLCommandBufferExt,@@ -222,9 +222,9 @@ pub struct MetalCommandBufferCompleted {impl CommandBufferCompleted for MetalCommandBufferCompleted {type CommandBuffer = MetalCommandBuffer;- fn gpu_execution_time_ms(&self) -> Option<f64> {- match (self.command_buffer.kernel_start_time(), self.command_buffer.kernel_end_time()) {- (Some(start), Some(end)) => Some((end - start) * 1000.0),+ fn gpu_execution_time(&self) -> Option<Duration> {+ match (self.command_buffer.gpu_start_time(), self.command_buffer.gpu_end_time()) {+ (Some(start), Some(end)) => Some(Duration::from_secs_f64(end - start)),_ => None,}}diff --git a/crates/uzu/tests/attention_kernel_test.rs b/crates/uzu/tests/attention_kernel_test.rsindex 5e52c29db..f62a0399b 100644--- a/crates/uzu/tests/attention_kernel_test.rs+++ b/crates/uzu/tests/attention_kernel_test.rs@@ -1198,10 +1198,7 @@ fn perf_two_pass_attention() {let completed = command_buffer.end_encoding().submit().wait_until_completed().unwrap();let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;- // Get actual GPU execution time- let gpu_elapsed_ms = completed.gpu_execution_time_ms();-- match gpu_elapsed_ms {+ match completed.gpu_execution_time() {Some(gpu_time) => {println!("Two-pass attention perf (heads={}, prefix={}, suffix={}, head_dim={}): GPU={:.2} ms, Host-side={:.2} ms",@@ -1209,7 +1206,7 @@ fn perf_two_pass_attention() {seq_len - suffix_length,suffix_length,head_dim,- gpu_time,+ gpu_time.as_secs_f64() * 1e3,host_elapsed_ms);},diff --git a/crates/uzu/tests/kernel/rms_norm_test.rs b/crates/uzu/tests/kernel/rms_norm_test.rsindex 3b13ab441..3f0b13ad2 100644--- a/crates/uzu/tests/kernel/rms_norm_test.rs+++ b/crates/uzu/tests/kernel/rms_norm_test.rs@@ -1,7 +1,7 @@use std::{fmt::{Debug, Display},ops::{Deref, DerefMut},- time::Instant,+ time::{Duration, Instant},};use half::{bf16, f16};@@ -122,7 +122,7 @@ fn get_output<AccumT: ArrayElement + Float,>(input: &Input<InputT, ScaleT, OutputT>-) -> (Vec<OutputT>, f64, Option<f64>) {+) -> (Vec<OutputT>, f64, Option<Duration>) {let context = B::Context::new().expect("Failed to create Context");let kernel = <<B as Backend>::Kernels as Kernels>::RMSNormKernel::new(@@ -165,7 +165,7 @@ fn get_output<let completed =command_buffer.end_encoding().submit().wait_until_completed().expect("Failed to wait command buffer");let host_elapsed_ms = instant.elapsed().as_secs_f64() * 1e3;- let gpu_elapsed_ms = completed.gpu_execution_time_ms();+ let gpu_elapsed_ms = completed.gpu_execution_time();(output_array.as_slice().to_vec(), host_elapsed_ms, gpu_elapsed_ms)}@@ -271,15 +271,15 @@ fn test_performance<B: Backend>(full_layer: false,in_place: false,};- let (output_data, host_elapsed_ms, gpu_elapsed_ms) = get_output::<B, f32, f32, f32, f32>(&input);- match gpu_elapsed_ms {+ let (output_data, host_elapsed_ms, gpu_time) = get_output::<B, f32, f32, f32, f32>(&input);+ match gpu_time {Some(gpu_time) => {println!("RMS norm perf (backend={}, batch={}, model_dim={}): GPU={:.2} ms, Host-side={:.2} ms",std::any::type_name::<B>(),batch_size,model_dim,- gpu_time,+ gpu_time.as_secs_f64() * 1e3,host_elapsed_ms);},diff --git a/crates/uzu/tests/sampling_test.rs b/crates/uzu/tests/sampling_test.rsindex dd2839838..6eed789db 100644--- a/crates/uzu/tests/sampling_test.rs+++ b/crates/uzu/tests/sampling_test.rs@@ -477,14 +477,14 @@ fn perf_topp_128k_vocab() {let completed = command_buffer.end_encoding().submit().wait_until_completed().unwrap();let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;- // Get actual GPU execution time- let gpu_elapsed_ms = completed.gpu_execution_time_ms();-- match gpu_elapsed_ms {+ match completed.gpu_execution_time() {Some(gpu_time) => {println!("Top‑p sampling perf (batch={}, vocab={}): GPU={:.2} ms, Host-side={:.2} ms",- BATCH, VOCAB, gpu_time, host_elapsed_ms+ BATCH,+ VOCAB,+ gpu_time.as_secs_f64() * 1e3,+ host_elapsed_ms);},None => {@@ -572,14 +572,15 @@ fn perf_argmax_128k_vocab_with_strategy(strategy: ArgmaxStrategy) {let completed = command_buffer.end_encoding().submit().wait_until_completed().unwrap();let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;- // Get actual GPU execution time- let gpu_elapsed_ms = completed.gpu_execution_time_ms();-- match gpu_elapsed_ms {+ match completed.gpu_execution_time() {Some(gpu_time) => {println!("Argmax sampling perf (batch={}, vocab={}, strategy={:?}): GPU={:.2} ms, Host-side={:.2} ms",- BATCH, VOCAB, strategy, gpu_time, host_elapsed_ms+ BATCH,+ VOCAB,+ strategy,+ gpu_time.as_secs_f64() * 1e3,+ host_elapsed_ms);},None => {@@ -930,13 +931,14 @@ fn perf_categorical_128k_vocab() {let completed = command_buffer.end_encoding().submit().wait_until_completed().unwrap();let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;- let gpu_elapsed_ms = completed.gpu_execution_time_ms();-- match gpu_elapsed_ms {+ match completed.gpu_execution_time() {Some(gpu_time) => {println!("Categorical sampling perf (batch={}, vocab={}): GPU={:.2} ms, Host-side={:.2} ms",- BATCH, VOCAB, gpu_time, host_elapsed_ms+ BATCH,+ VOCAB,+ gpu_time.as_secs_f64() * 1e3,+ host_elapsed_ms);},None => {