Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

clean up error enums and don't ignore failed command buffers (#235)

uuuvna3687e990a5a
diff --git a/.cargo/config.toml b/.cargo/config.toml
new file mode 100644
index 000000000..8af59dd8c
--- /dev/null
+++ b/.cargo/config.toml
@@ -0,0 +1,2 @@
+[env]
+RUST_TEST_THREADS = "1"
diff --git a/crates/uzu/src/backends/common/command_buffer.rs b/crates/uzu/src/backends/common/command_buffer.rs
index b55f5ee23..a23e00622 100644
--- a/crates/uzu/src/backends/common/command_buffer.rs
+++ b/crates/uzu/src/backends/common/command_buffer.rs
@@ -27,12 +27,12 @@ pub trait CommandBuffer {
fn add_completion_handler(
&mut self,
- handler: impl FnOnce() + 'static,
+ handler: impl FnOnce(Result<(), <Self::Backend as Backend>::Error>) + 'static,
);
fn submit(&mut self);
- fn wait_until_completed(&self);
+ fn wait_until_completed(&self) -> Result<(), <Self::Backend as Backend>::Error>;
fn gpu_execution_time_ms(&self) -> Option<f64>;
}
diff --git a/crates/uzu/src/backends/common/kernel/matmul/full_precision.rs b/crates/uzu/src/backends/common/kernel/matmul/full_precision.rs
index 561a72857..ad3abda7f 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/full_precision.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/full_precision.rs
@@ -1,4 +1,4 @@
-use super::MatmulKernels;
+use super::{MatmulError, MatmulKernels};
use crate::{DataType, backends::common::Backend};
pub struct FullPrecisionMatmulArguments<'a, B: Backend> {
@@ -18,7 +18,7 @@ pub trait FullPrecisionMatmulKernel: Sized {
fn new(
context: &<Self::Backend as Backend>::Context,
data_type: DataType,
- ) -> Result<Self, <Self::Backend as Backend>::Error>;
+ ) -> Result<Self, MatmulError<Self::Backend>>;
fn encode(
&mut self,
diff --git a/crates/uzu/src/backends/common/kernel/matmul/gemm/kernel.rs b/crates/uzu/src/backends/common/kernel/matmul/gemm/kernel.rs
index dc10f744f..4c78c8d84 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/gemm/kernel.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/gemm/kernel.rs
@@ -5,7 +5,10 @@ use super::{
};
use crate::{
DataType,
- backends::common::{Backend, Kernels, kernel::MatmulGemmKernel},
+ backends::common::{
+ Backend, Kernels,
+ kernel::{MatmulGemmKernel, matmul::MatmulError},
+ },
};
pub struct GemmKernel<B: Backend> {
@@ -13,13 +16,10 @@ pub struct GemmKernel<B: Backend> {
pipelines: HashMap<Specialization, <B::Kernels as Kernels>::MatmulGemmKernel>,
}
-impl<B: Backend> GemmKernel<B>
-where
- B::Error: From<String>,
-{
- pub fn new(data_type: DataType) -> Result<Self, B::Error> {
+impl<B: Backend> GemmKernel<B> {
+ pub fn new(data_type: DataType) -> Result<Self, MatmulError<B>> {
if !matches!(data_type, DataType::F16 | DataType::BF16 | DataType::F32) {
- return Err(B::Error::from(format!("Unsupported dtype for GEMM: {data_type:?}")));
+ return Err(MatmulError::UnsupportedDataType(data_type));
}
Ok(Self {
data_type,
@@ -30,7 +30,7 @@ where
pub fn precompile(
&mut self,
context: &B::Context,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
for &config in Specialization::precompile_configs(self.data_type) {
self.get_or_create_kernel(context, config)?;
}
@@ -41,7 +41,7 @@ where
&mut self,
context: &B::Context,
config: Specialization,
- ) -> Result<&<B::Kernels as Kernels>::MatmulGemmKernel, B::Error> {
+ ) -> Result<&<B::Kernels as Kernels>::MatmulGemmKernel, MatmulError<B>> {
if !self.pipelines.contains_key(&config) {
let kernel = <B::Kernels as Kernels>::MatmulGemmKernel::new(
context,
@@ -54,7 +54,8 @@ where
config.align_m,
config.align_n,
config.align_k,
- )?;
+ )
+ .map_err(MatmulError::BackendError)?;
self.pipelines.insert(config, kernel);
}
Ok(self.pipelines.get(&config).unwrap())
@@ -66,18 +67,15 @@ where
arguments: &mut MatmulArguments<B>,
dispatch_descriptor: &DispatchDescriptor,
encoder: &mut B::ComputeEncoder,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
let config = dispatch_descriptor.specialization;
- let group_count_x = u32::try_from(dispatch_descriptor.threadgroups.x).map_err(|_| {
- B::Error::from(format!("GEMM group count x overflows u32: {}", dispatch_descriptor.threadgroups.x))
- })?;
- let group_count_y = u32::try_from(dispatch_descriptor.threadgroups.y).map_err(|_| {
- B::Error::from(format!("GEMM group count y overflows u32: {}", dispatch_descriptor.threadgroups.y))
- })?;
- let group_count_z = u32::try_from(dispatch_descriptor.threadgroups.z).map_err(|_| {
- B::Error::from(format!("GEMM group count z overflows u32: {}", dispatch_descriptor.threadgroups.z))
- })?;
+ let group_count_x = u32::try_from(dispatch_descriptor.threadgroups.x)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.threadgroups.x))?;
+ let group_count_y = u32::try_from(dispatch_descriptor.threadgroups.y)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.threadgroups.y))?;
+ let group_count_z = u32::try_from(dispatch_descriptor.threadgroups.z)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.threadgroups.z))?;
let pipeline = self.get_or_create_kernel(context, config)?;
pipeline.encode(
diff --git a/crates/uzu/src/backends/common/kernel/matmul/gemv/dispatch_descriptor.rs b/crates/uzu/src/backends/common/kernel/matmul/gemv/dispatch_descriptor.rs
index e458f0f87..58132550c 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/gemv/dispatch_descriptor.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/gemv/dispatch_descriptor.rs
@@ -1,7 +1,10 @@
use std::sync::OnceLock;
use super::{super::matmul_arguments::MatmulArguments, specialization::Specialization};
-use crate::{DataType, backends::common::Backend};
+use crate::{
+ DataType,
+ backends::common::{Backend, kernel::matmul::MatmulError},
+};
const DEFAULT_GEMV_MAX_BATCH: i32 = 8;
@@ -41,12 +44,9 @@ impl DispatchDescriptor {
pub fn try_new<B: Backend>(
data_type: DataType,
arguments: &MatmulArguments<B>,
- ) -> Result<Option<Self>, B::Error>
- where
- B::Error: From<String>,
- {
+ ) -> Result<Option<Self>, MatmulError<B>> {
if !matches!(data_type, DataType::F16 | DataType::BF16 | DataType::F32) {
- return Err(B::Error::from(format!("Unsupported data type for GEMV: {data_type:?}")));
+ return Err(MatmulError::UnsupportedDataType(data_type));
}
if !arguments.transpose_b {
diff --git a/crates/uzu/src/backends/common/kernel/matmul/gemv/kernel.rs b/crates/uzu/src/backends/common/kernel/matmul/gemv/kernel.rs
index 187479472..2a3a83c15 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/gemv/kernel.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/gemv/kernel.rs
@@ -7,7 +7,10 @@ use super::{
};
use crate::{
DataType,
- backends::common::{Backend, Kernels, kernel::MatmulGemvKernel},
+ backends::common::{
+ Backend, Kernels,
+ kernel::{MatmulGemvKernel, matmul::MatmulError},
+ },
};
pub struct GemvKernel<B: Backend> {
@@ -15,13 +18,10 @@ pub struct GemvKernel<B: Backend> {
pipelines: HashMap<Specialization, <B::Kernels as Kernels>::MatmulGemvKernel>,
}
-impl<B: Backend> GemvKernel<B>
-where
- B::Error: From<String>,
-{
- pub fn new(data_type: DataType) -> Result<Self, B::Error> {
+impl<B: Backend> GemvKernel<B> {
+ pub fn new(data_type: DataType) -> Result<Self, MatmulError<B>> {
if !matches!(data_type, DataType::F16 | DataType::BF16 | DataType::F32) {
- return Err(B::Error::from(format!("Unsupported data type for GEMV: {data_type:?}")));
+ return Err(MatmulError::UnsupportedDataType(data_type));
}
Ok(Self {
data_type,
@@ -32,7 +32,7 @@ where
pub fn precompile(
&mut self,
context: &B::Context,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
for &config in Specialization::precompile_configs(self.data_type) {
self.get_or_create_kernel(context, config)?;
}
@@ -43,7 +43,7 @@ where
&mut self,
context: &B::Context,
config: Specialization,
- ) -> Result<&<B::Kernels as Kernels>::MatmulGemvKernel, B::Error> {
+ ) -> Result<&<B::Kernels as Kernels>::MatmulGemvKernel, MatmulError<B>> {
if !self.pipelines.contains_key(&config) {
let kernel = <B::Kernels as Kernels>::MatmulGemvKernel::new(
context,
@@ -55,7 +55,8 @@ where
config.elements_per_thread_row,
config.elements_per_thread_col,
config.apply_output_scale_and_accumulate,
- )?;
+ )
+ .map_err(MatmulError::BackendError)?;
self.pipelines.insert(config, kernel);
}
Ok(self.pipelines.get(&config).unwrap())
@@ -67,7 +68,7 @@ where
arguments: &mut MatmulArguments<B>,
dispatch_descriptor: &DispatchDescriptor,
encoder: &mut B::ComputeEncoder,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
let config = dispatch_descriptor.specialization;
let pipeline = self.get_or_create_kernel(context, config)?;
@@ -85,37 +86,20 @@ where
let output_source = if config.apply_output_scale_and_accumulate {
match dispatch_descriptor.output_source {
OutputSource::None => {
- return Err(B::Error::from(
- "GEMV descriptor mismatch: apply_output_scale_and_accumulate=true but output_source=None"
- .to_owned(),
- ));
+ return Err(MatmulError::GemvOutputSourceMismatch);
},
- OutputSource::Bias => Some(
- arguments.bias.ok_or_else(|| B::Error::from("GEMV descriptor requires bias buffer".to_owned()))?,
- ),
+ OutputSource::Bias => Some(arguments.bias.ok_or(MatmulError::<B>::GemvMissingBias)?),
}
} else {
None
};
- let vector_batch_stride = [i32::try_from(dispatch_descriptor.vector_batch_stride[0]).map_err(|_| {
- B::Error::from(format!(
- "GEMV path requires i32 vector_batch_stride but got {}",
- dispatch_descriptor.vector_batch_stride[0]
- ))
- })?];
- let matrix_batch_stride = [i32::try_from(dispatch_descriptor.matrix_batch_stride[0]).map_err(|_| {
- B::Error::from(format!(
- "GEMV path requires i32 matrix_batch_stride but got {}",
- dispatch_descriptor.matrix_batch_stride[0]
- ))
- })?];
- let output_source_batch_stride = [i32::try_from(dispatch_descriptor.bias_batch_stride[0]).map_err(|_| {
- B::Error::from(format!(
- "GEMV path requires i32 output_source_batch_stride but got {}",
- dispatch_descriptor.bias_batch_stride[0]
- ))
- })?];
+ let vector_batch_stride = [i32::try_from(dispatch_descriptor.vector_batch_stride[0])
+ .map_err(|_| MatmulError::<B>::GemvStrideOverflow(dispatch_descriptor.vector_batch_stride[0]))?];
+ let matrix_batch_stride = [i32::try_from(dispatch_descriptor.matrix_batch_stride[0])
+ .map_err(|_| MatmulError::<B>::GemvStrideOverflow(dispatch_descriptor.matrix_batch_stride[0]))?];
+ let output_source_batch_stride = [i32::try_from(dispatch_descriptor.bias_batch_stride[0])
+ .map_err(|_| MatmulError::<B>::GemvStrideOverflow(dispatch_descriptor.bias_batch_stride[0]))?];
pipeline.encode(
(matrix, matrix_offset),
diff --git a/crates/uzu/src/backends/common/kernel/matmul/kernel.rs b/crates/uzu/src/backends/common/kernel/matmul/kernel.rs
index eafec8636..c1c8d0954 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/kernel.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/kernel.rs
@@ -1,7 +1,7 @@
use std::ops::DerefMut;
use super::{
- dispatch_descriptor::MatmulDispatchDescriptor, gemm::GemmKernel, gemv::GemvKernel,
+ MatmulError, dispatch_descriptor::MatmulDispatchDescriptor, gemm::GemmKernel, gemv::GemvKernel,
matmul_arguments::MatmulArguments, split_k::SplitKKernel,
};
use crate::{
@@ -17,13 +17,10 @@ pub struct MatmulKernel<B: Backend> {
bias_add: Option<<B::Kernels as Kernels>::TensorAddBiasKernel>,
}
-impl<B: Backend> MatmulKernel<B>
-where
- B::Error: From<String>,
-{
- pub fn new(data_type: DataType) -> Result<Self, B::Error> {
+impl<B: Backend> MatmulKernel<B> {
+ pub fn new(data_type: DataType) -> Result<Self, MatmulError<B>> {
if !matches!(data_type, DataType::F16 | DataType::BF16 | DataType::F32) {
- return Err(B::Error::from(format!("Unsupported dtype for MatmulKernel: {data_type:?}")));
+ return Err(MatmulError::UnsupportedDataType(data_type));
}
Ok(Self {
@@ -38,7 +35,7 @@ where
pub fn precompile(
&mut self,
context: &B::Context,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
let gemm = self.get_or_create_gemm()?;
gemm.precompile(context)?;
@@ -51,21 +48,21 @@ where
Ok(())
}
- fn get_or_create_gemm(&mut self) -> Result<&mut GemmKernel<B>, B::Error> {
+ fn get_or_create_gemm(&mut self) -> Result<&mut GemmKernel<B>, MatmulError<B>> {
if self.gemm.is_none() {
self.gemm = Some(GemmKernel::<B>::new(self.data_type)?);
}
Ok(self.gemm.as_mut().unwrap())
}
- fn get_or_create_gemv(&mut self) -> Result<&mut GemvKernel<B>, B::Error> {
+ fn get_or_create_gemv(&mut self) -> Result<&mut GemvKernel<B>, MatmulError<B>> {
if self.gemv.is_none() {
self.gemv = Some(GemvKernel::<B>::new(self.data_type)?);
}
Ok(self.gemv.as_mut().unwrap())
}
- fn get_or_create_splitk(&mut self) -> Result<&mut SplitKKernel<B>, B::Error> {
+ fn get_or_create_splitk(&mut self) -> Result<&mut SplitKKernel<B>, MatmulError<B>> {
if self.splitk.is_none() {
self.splitk = Some(SplitKKernel::<B>::new(self.data_type)?);
}
@@ -78,7 +75,7 @@ where
arguments: &mut MatmulArguments<B>,
dispatch_descriptor: &MatmulDispatchDescriptor,
encoder: &mut B::ComputeEncoder,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
match dispatch_descriptor {
MatmulDispatchDescriptor::Gemv(d) => {
let gemv = self.get_or_create_gemv()?;
@@ -101,7 +98,7 @@ where
mut arguments: MatmulArguments<B>,
dispatch_descriptor: &MatmulDispatchDescriptor,
encoder: &mut B::ComputeEncoder,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
self.encode_dispatch_descriptor(context, &mut arguments, dispatch_descriptor, encoder)?;
if let Some(bias) = arguments.bias {
@@ -119,7 +116,7 @@ where
arguments: &mut MatmulArguments<B>,
bias: &B::NativeBuffer,
encoder: &mut B::ComputeEncoder,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
let m = arguments.batch as usize;
let n = arguments.output_dim as usize;
let batch_count = arguments.batch_count as usize;
@@ -129,7 +126,10 @@ where
}
if self.bias_add.is_none() {
- self.bias_add = Some(<B::Kernels as Kernels>::TensorAddBiasKernel::new(context, self.data_type, true)?);
+ self.bias_add = Some(
+ <B::Kernels as Kernels>::TensorAddBiasKernel::new(context, self.data_type, true)
+ .map_err(MatmulError::BackendError)?,
+ );
}
let bias_add = self.bias_add.as_ref().unwrap();
bias_add.encode(None::<&B::NativeBuffer>, bias, arguments.d.deref_mut(), n as u32, total_len as u32, encoder);
diff --git a/crates/uzu/src/backends/common/kernel/matmul/mod.rs b/crates/uzu/src/backends/common/kernel/matmul/mod.rs
index 41fe391d7..267ea32a5 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/mod.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/mod.rs
@@ -12,9 +12,27 @@ pub use full_precision::{FullPrecisionMatmulArguments, FullPrecisionMatmulKernel
pub use grid_size::GridSize;
pub use kernel::MatmulKernel;
pub use matmul_arguments::MatmulArguments;
+use thiserror::Error;
use super::Kernels;
+use crate::backends::common::Backend;
pub trait MatmulKernels: Kernels {
type FullPrecisionMatmulKernel: FullPrecisionMatmulKernel<Backend = Self::Backend>;
}
+
+#[derive(Debug, Error)]
+pub enum MatmulError<B: Backend> {
+ #[error("Unsupported data type: {0:?}")]
+ UnsupportedDataType(crate::DataType),
+ #[error("Threadgroup dimension overflows u32: {0}")]
+ ThreadgroupOverflow(usize),
+ #[error("GEMV descriptor mismatch: apply_output_scale_and_accumulate=true but output_source=None")]
+ GemvOutputSourceMismatch,
+ #[error("GEMV descriptor requires bias buffer")]
+ GemvMissingBias,
+ #[error("GEMV stride overflows i32: {0}")]
+ GemvStrideOverflow(i64),
+ #[error("Backend error: {0}")]
+ BackendError(#[source] B::Error),
+}
diff --git a/crates/uzu/src/backends/common/kernel/matmul/split_k/dispatch_descriptor.rs b/crates/uzu/src/backends/common/kernel/matmul/split_k/dispatch_descriptor.rs
index 11cd3d489..db0641fb9 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/split_k/dispatch_descriptor.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/split_k/dispatch_descriptor.rs
@@ -5,7 +5,7 @@ use super::{
};
use crate::{
DataType,
- backends::common::{Backend, gpu_types::GEMMSpiltKParams as SplitKGEMMParams},
+ backends::common::{Backend, gpu_types::GEMMSpiltKParams as SplitKGEMMParams, kernel::matmul::MatmulError},
};
#[derive(Debug, Clone)]
@@ -22,7 +22,7 @@ impl DispatchDescriptor {
pub fn try_new<B: Backend>(
data_type: DataType,
arguments: &MatmulArguments<B>,
- ) -> Result<Option<Self>, B::Error> {
+ ) -> Result<Option<Self>, MatmulError<B>> {
if !matches!(data_type, DataType::BF16) {
return Ok(None);
}
diff --git a/crates/uzu/src/backends/common/kernel/matmul/split_k/kernel.rs b/crates/uzu/src/backends/common/kernel/matmul/split_k/kernel.rs
index b3c8f450e..0116066c1 100644
--- a/crates/uzu/src/backends/common/kernel/matmul/split_k/kernel.rs
+++ b/crates/uzu/src/backends/common/kernel/matmul/split_k/kernel.rs
@@ -5,7 +5,7 @@ use crate::{
DataType,
backends::common::{
Backend, Context, Kernels,
- kernel::{MatmulSplitKAccumBfloat16Kernel, MatmulSplitKPartialBfloat16Kernel},
+ kernel::{MatmulSplitKAccumBfloat16Kernel, MatmulSplitKPartialBfloat16Kernel, matmul::MatmulError},
},
};
@@ -17,13 +17,10 @@ pub struct SplitKKernel<B: Backend> {
accumulator_buffer_bytes: usize,
}
-impl<B: Backend> SplitKKernel<B>
-where
- B::Error: From<String>,
-{
- pub fn new(data_type: DataType) -> Result<Self, B::Error> {
+impl<B: Backend> SplitKKernel<B> {
+ pub fn new(data_type: DataType) -> Result<Self, MatmulError<B>> {
if !matches!(data_type, DataType::F16 | DataType::BF16 | DataType::F32) {
- return Err(B::Error::from(format!("Unsupported dtype for Split-K: {data_type:?}")));
+ return Err(MatmulError::UnsupportedDataType(data_type));
}
Ok(Self {
data_type,
@@ -37,7 +34,7 @@ where
pub fn precompile(
&mut self,
context: &B::Context,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
if !matches!(self.data_type, DataType::BF16) {
return Ok(());
}
@@ -47,15 +44,21 @@ where
fn ensure_kernels(
&mut self,
context: &B::Context,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
if !matches!(self.data_type, DataType::BF16) {
- return Err(B::Error::from(format!("Split-K currently supports BF16 only, got {:?}", self.data_type)));
+ return Err(MatmulError::UnsupportedDataType(self.data_type));
}
if self.partial_bfloat16.is_none() {
- self.partial_bfloat16 = Some(<B::Kernels as Kernels>::MatmulSplitKPartialBfloat16Kernel::new(context)?);
+ self.partial_bfloat16 = Some(
+ <B::Kernels as Kernels>::MatmulSplitKPartialBfloat16Kernel::new(context)
+ .map_err(MatmulError::BackendError)?,
+ );
}
if self.accum_bfloat16.is_none() {
- self.accum_bfloat16 = Some(<B::Kernels as Kernels>::MatmulSplitKAccumBfloat16Kernel::new(context)?);
+ self.accum_bfloat16 = Some(
+ <B::Kernels as Kernels>::MatmulSplitKAccumBfloat16Kernel::new(context)
+ .map_err(MatmulError::BackendError)?,
+ );
}
Ok(())
}
@@ -66,29 +69,17 @@ where
arguments: &mut MatmulArguments<B>,
dispatch_descriptor: &DispatchDescriptor,
encoder: &mut B::ComputeEncoder,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
self.ensure_kernels(context)?;
self.ensure_accumulator_buffer(context, dispatch_descriptor.accumulator_bytes)?;
let mut accumulator_buffer = self.accumulator_buffer.as_mut().expect("Accumulator buffer must be initialized");
- let partial_group_count_x = u32::try_from(dispatch_descriptor.partial_threadgroups.x).map_err(|_| {
- B::Error::from(format!(
- "Split-K partial group count x overflows u32: {}",
- dispatch_descriptor.partial_threadgroups.x
- ))
- })?;
- let partial_group_count_y = u32::try_from(dispatch_descriptor.partial_threadgroups.y).map_err(|_| {
- B::Error::from(format!(
- "Split-K partial group count y overflows u32: {}",
- dispatch_descriptor.partial_threadgroups.y
- ))
- })?;
- let partial_group_count_z = u32::try_from(dispatch_descriptor.partial_threadgroups.z).map_err(|_| {
- B::Error::from(format!(
- "Split-K partial group count z overflows u32: {}",
- dispatch_descriptor.partial_threadgroups.z
- ))
- })?;
+ let partial_group_count_x = u32::try_from(dispatch_descriptor.partial_threadgroups.x)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.partial_threadgroups.x))?;
+ let partial_group_count_y = u32::try_from(dispatch_descriptor.partial_threadgroups.y)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.partial_threadgroups.y))?;
+ let partial_group_count_z = u32::try_from(dispatch_descriptor.partial_threadgroups.z)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.partial_threadgroups.z))?;
let partial = self.partial_bfloat16.as_ref().unwrap();
partial.encode(
(arguments.a, arguments.a_offset as usize),
@@ -101,18 +92,10 @@ where
encoder,
);
- let accum_total_threads_x = u32::try_from(dispatch_descriptor.accum_total_threads.x).map_err(|_| {
- B::Error::from(format!(
- "Split-K accum total threads x overflows u32: {}",
- dispatch_descriptor.accum_total_threads.x
- ))
- })?;
- let accum_total_threads_y = u32::try_from(dispatch_descriptor.accum_total_threads.y).map_err(|_| {
- B::Error::from(format!(
- "Split-K accum total threads y overflows u32: {}",
- dispatch_descriptor.accum_total_threads.y
- ))
- })?;
+ let accum_total_threads_x = u32::try_from(dispatch_descriptor.accum_total_threads.x)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.accum_total_threads.x))?;
+ let accum_total_threads_y = u32::try_from(dispatch_descriptor.accum_total_threads.y)
+ .map_err(|_| MatmulError::<B>::ThreadgroupOverflow(dispatch_descriptor.accum_total_threads.y))?;
let accum = self.accum_bfloat16.as_ref().unwrap();
accum.encode(
accumulator_buffer.deref(),
@@ -132,11 +115,11 @@ where
&mut self,
context: &B::Context,
required_bytes: usize,
- ) -> Result<(), B::Error> {
+ ) -> Result<(), MatmulError<B>> {
if required_bytes <= self.accumulator_buffer_bytes && self.accumulator_buffer.is_some() {
return Ok(());
}
- self.accumulator_buffer = Some(context.create_buffer(required_bytes)?);
+ self.accumulator_buffer = Some(context.create_buffer(required_bytes).map_err(MatmulError::BackendError)?);
self.accumulator_buffer_bytes = required_bytes;
Ok(())
}
diff --git a/crates/uzu/src/backends/cpu/command_buffer.rs b/crates/uzu/src/backends/cpu/command_buffer.rs
index 13a69bea8..600e880d6 100644
--- a/crates/uzu/src/backends/cpu/command_buffer.rs
+++ b/crates/uzu/src/backends/cpu/command_buffer.rs
@@ -4,11 +4,14 @@ use std::{
};
use super::Cpu;
-use crate::backends::common::{CommandBuffer, CopyEncoder};
+use crate::backends::{
+ common::{CommandBuffer, CopyEncoder},
+ cpu::error::CpuError,
+};
pub struct CpuCommandBuffer {
commands: Vec<Box<dyn Fn()>>,
- completion_handlers: Vec<Box<dyn FnOnce()>>,
+ completion_handlers: Vec<Box<dyn FnOnce(Result<(), CpuError>)>>,
gpu_execution_time_ms: OnceCell<f64>,
}
@@ -66,7 +69,7 @@ impl CommandBuffer for CpuCommandBuffer {
fn add_completion_handler(
&mut self,
- handler: impl FnOnce() + 'static,
+ handler: impl FnOnce(Result<(), CpuError>) + 'static,
) {
self.completion_handlers.push(Box::new(handler))
}
@@ -80,11 +83,13 @@ impl CommandBuffer for CpuCommandBuffer {
.set((Instant::now() - start).as_secs_f64() * 1000.0)
.expect("gpu execution time already set");
for completion_handler in self.completion_handlers.drain(..) {
- completion_handler();
+ completion_handler(Ok(()));
}
}
- fn wait_until_completed(&self) {}
+ fn wait_until_completed(&self) -> Result<(), CpuError> {
+ Ok(())
+ }
fn gpu_execution_time_ms(&self) -> Option<f64> {
self.gpu_execution_time_ms.get().copied()
diff --git a/crates/uzu/src/backends/cpu/kernel/mod.rs b/crates/uzu/src/backends/cpu/kernel/mod.rs
index 406210962..49d1e7de5 100644
--- a/crates/uzu/src/backends/cpu/kernel/mod.rs
+++ b/crates/uzu/src/backends/cpu/kernel/mod.rs
@@ -28,9 +28,10 @@ use crate::{
DataType,
backends::{
common::kernel::matmul::{
- FullPrecisionMatmulArguments, FullPrecisionMatmulKernel as FullPrecisionMatmulKernelTrait, MatmulKernels,
+ FullPrecisionMatmulArguments, FullPrecisionMatmulKernel as FullPrecisionMatmulKernelTrait, MatmulError,
+ MatmulKernels,
},
- cpu::{Cpu, command_buffer::CpuCommandBuffer, context::CpuContext, error::CpuError},
+ cpu::{Cpu, command_buffer::CpuCommandBuffer, context::CpuContext},
},
};
@@ -42,7 +43,7 @@ impl FullPrecisionMatmulKernelTrait for FullPrecisionMatmulCpuKernel {
fn new(
#[allow(unused)] context: &CpuContext,
#[allow(unused)] data_type: DataType,
- ) -> Result<Self, CpuError> {
+ ) -> Result<Self, MatmulError<Cpu>> {
Ok(Self)
}
diff --git a/crates/uzu/src/backends/metal/command_buffer.rs b/crates/uzu/src/backends/metal/command_buffer.rs
index 188e4a97c..8c7608cd8 100644
--- a/crates/uzu/src/backends/metal/command_buffer.rs
+++ b/crates/uzu/src/backends/metal/command_buffer.rs
@@ -1,10 +1,20 @@
use std::{cell::Cell, ops::Deref};
-use metal::{MTLCommandBuffer, MTLCommandBufferExt, MTLCommandBufferHandler, MTLCommandEncoder, MTLEvent};
+use metal::{
+ MTLCommandBuffer, MTLCommandBufferExt, MTLCommandBufferHandler, MTLCommandBufferStatus, MTLCommandEncoder, MTLEvent,
+};
use objc2::{rc::Retained, runtime::ProtocolObject};
use super::Metal;
-use crate::backends::common::CommandBuffer;
+use crate::backends::{common::CommandBuffer, metal::error::MetalError};
+
+fn command_buffer_result(command_buffer: &ProtocolObject<dyn MTLCommandBuffer>) -> Result<(), MetalError> {
+ match (command_buffer.status(), command_buffer.error()) {
+ (MTLCommandBufferStatus::Completed, None) => Ok(()),
+ (status, Some(nserror)) => Err(MetalError::CommandBufferExecutionFailed(format!("{status:?}: {nserror:?}"))),
+ (status, None) => Err(MetalError::CommandBufferExecutionFailed(format!("{status:?}"))),
+ }
+}
impl CommandBuffer for Retained<ProtocolObject<dyn MTLCommandBuffer>> {
type Backend = Metal;
@@ -53,11 +63,11 @@ impl CommandBuffer for Retained<ProtocolObject<dyn MTLCommandBuffer>> {
fn add_completion_handler(
&mut self,
- handler: impl FnOnce() + 'static,
+ handler: impl FnOnce(Result<(), MetalError>) + 'static,
) {
let cell = Cell::new(Some(handler));
- self.deref().add_completed_handler(&MTLCommandBufferHandler::new(move |_| {
- cell.take().expect("completion handler called more than once")()
+ self.deref().add_completed_handler(&MTLCommandBufferHandler::new(move |command_buffer| {
+ cell.take().expect("completion handler called more than once")(command_buffer_result(command_buffer))
}));
}
@@ -65,8 +75,9 @@ impl CommandBuffer for Retained<ProtocolObject<dyn MTLCommandBuffer>> {
self.commit();
}
- fn wait_until_completed(&self) {
+ fn wait_until_completed(&self) -> Result<(), MetalError> {
self.deref().wait_until_completed();
+ command_buffer_result(self)
}
fn gpu_execution_time_ms(&self) -> Option<f64> {
diff --git a/crates/uzu/src/backends/metal/context.rs b/crates/uzu/src/backends/metal/context.rs
index 930233183..89973e2a0 100644
--- a/crates/uzu/src/backends/metal/context.rs
+++ b/crates/uzu/src/backends/metal/context.rs
@@ -178,16 +178,15 @@ impl Context for MetalContext {
type Backend = Metal;
fn new() -> Result<Rc<Self>, MetalError> {
- let device: Retained<ProtocolObject<dyn MTLDevice>> = <dyn metal::MTLDevice>::system_default()
- .ok_or(MetalError::Generic("cannot open system default device".into()))?;
+ let device: Retained<ProtocolObject<dyn MTLDevice>> =
+ <dyn metal::MTLDevice>::system_default().ok_or(MetalError::CannotOpenDevice)?;
- let command_queue = device
- .new_command_queue_with_max_command_buffer_count(1024)
- .ok_or(MetalError::Generic("cannot create command queue".into()))?;
+ let command_queue =
+ device.new_command_queue_with_max_command_buffer_count(1024).ok_or(MetalError::CannotCreateCommandQueue)?;
let library = device
.new_library_with_data(kernel::MTLB)
- .map_err(|nserror| MetalError::Generic(format!("Failed to create Metal library: {}", nserror)))?;
+ .map_err(|nserror| MetalError::CannotCreateLibrary(nserror.to_string()))?;
let architecture = DeviceArchitecture::from_device(&device);
@@ -230,17 +229,15 @@ impl Context for MetalContext {
&self,
size: usize,
) -> Result<Retained<ProtocolObject<dyn MTLBuffer>>, MetalError> {
- self.device
- .new_buffer(size, MTLResourceOptions::STORAGE_MODE_SHARED)
- .ok_or(MetalError::Generic("cannot create buffer".into()))
+ self.device.new_buffer(size, MTLResourceOptions::STORAGE_MODE_SHARED).ok_or(MetalError::CannotCreateBuffer)
}
fn create_command_buffer(&self) -> Result<Retained<ProtocolObject<dyn MTLCommandBuffer>>, MetalError> {
- self.command_queue.command_buffer().ok_or(MetalError::Generic("cannot create command buffer".into()))
+ self.command_queue.command_buffer().ok_or(MetalError::CannotCreateCommandBuffer)
}
fn create_event(&self) -> Result<Retained<ProtocolObject<dyn MTLEvent>>, MetalError> {
- self.device.new_event().ok_or(MetalError::Generic("cannot create event".into()))
+ self.device.new_event().ok_or(MetalError::CannotCreateEvent)
}
fn enable_capture() {
@@ -263,7 +260,7 @@ impl Context for MetalContext {
capture_manager
.start_capture_with_descriptor_error(&capture_descriptor)
- .map_err(|e| format!("Failed to start GPU capture: {}", e))?;
+ .map_err(|nserror| MetalError::CannotStartGpuCapture(nserror.to_string()))?;
Ok(())
}
diff --git a/crates/uzu/src/backends/metal/error.rs b/crates/uzu/src/backends/metal/error.rs
index 5c328c7ab..208238b05 100644
--- a/crates/uzu/src/backends/metal/error.rs
+++ b/crates/uzu/src/backends/metal/error.rs
@@ -2,177 +2,24 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum MetalError {
- #[error("MetalContext error: {0}")]
- Context(#[from] ContextError),
- #[error("MTLDevice error: {0}")]
- Device(#[from] DeviceError),
- #[error("MTLHeap error: {0}")]
- Heap(#[from] HeapError),
- #[error("MTLCommandBuffer error: {0}")]
- CommandBuffer(#[from] CommandBufferError),
- #[error("MTLCommandQueue error: {0}")]
- CommandQueue(#[from] CommandQueueError),
- #[error("MTLLibrary error: {0}")]
- Library(#[from] LibraryError),
- #[error("MTLTexture error: {0}")]
- Texture(#[from] TextureError),
- #[error("MTLTexture serialization error: {0}")]
- TextureSerialization(#[from] TextureSerializationError),
- #[error("MTLResource error: {0}")]
- Resource(#[from] ResourceError),
- #[error("MTLBuffer error: {0}")]
- Buffer(#[from] BufferError),
- #[error("MTLPixelFormat error: {0}")]
- PixelFormat(#[from] PixelFormatError),
- #[error("Generic Metal error: {0}")]
- Generic(String),
-}
-
-impl From<&str> for MetalError {
- fn from(error: &str) -> Self {
- MetalError::Generic(error.to_string())
- }
-}
-
-impl From<String> for MetalError {
- fn from(error: String) -> Self {
- MetalError::Generic(error)
- }
-}
-
-// Context errors
-#[derive(Debug, Error)]
-pub enum ContextError {
- #[error("Failed to create texture cache")]
- TextureCacheCreationFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Device errors
-#[derive(Debug, Error)]
-pub enum DeviceError {
- #[error("No default Metal device found")]
- NoDefaultDevice,
- #[error("Failed to create argument encoder")]
- ArgumentEncoderCreationFailed,
- #[error("Failed to create buffer")]
- BufferCreationFailed,
- #[error("Failed to create command queue")]
- CommandQueueCreationFailed,
- #[error("Failed to create depth stencil state")]
- DepthStencilStateCreationFailed,
- #[error("Failed to create event")]
- EventCreationFailed,
- #[error("Failed to create fence")]
- FenceCreationFailed,
- #[error("Failed to create heap")]
- HeapCreationFailed,
- #[error("Failed to create indirect command buffer")]
- IndirectCommandBufferCreationFailed,
- #[error("Failed to create library")]
- LibraryCreationFailed,
- #[error("Failed to create rasterization rate map")]
- RasterizationRateMapCreationFailed,
- #[error("Failed to create sampler state")]
- SamplerStateCreationFailed,
- #[error("Failed to create texture")]
- TextureCreationFailed,
- #[error("Failed to create texture view")]
- TextureViewCreationFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Heap errors
-#[derive(Debug, Error)]
-pub enum HeapError {
- #[error("Failed to create buffer from heap")]
- BufferCreationFailed,
- #[error("Failed to create texture from heap")]
- TextureCreationFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Command buffer errors
-#[derive(Debug, Error)]
-pub enum CommandBufferError {
- #[error("Failed to execute command buffer")]
- ExecutionFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Command queue errors
-#[derive(Debug, Error)]
-pub enum CommandQueueError {
- #[error("Failed to create command buffer")]
- CommandBufferCreationFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Library errors
-#[derive(Debug, Error)]
-pub enum LibraryError {
- #[error("Failed to create function")]
- FunctionCreationFailed,
- #[error("Failed to create pipeline state")]
- PipelineStateCreationFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Texture errors
-#[derive(Debug, Error)]
-pub enum TextureError {
- #[error("Failed to create image from texture")]
- ImageCreationFailed,
- #[error("Incompatible pixel format")]
- IncompatiblePixelFormat,
- #[error("{0}")]
- Custom(String),
-}
-
-// Texture serialization errors
-#[derive(Debug, Error)]
-pub enum TextureSerializationError {
- #[error("Failed to allocate memory for texture serialization")]
- AllocationFailed,
- #[error("Failed to access texture data")]
- DataAccessFailure,
- #[error("Unsupported pixel format for serialization")]
- UnsupportedPixelFormat,
- #[error("{0}")]
- Custom(String),
-}
-
-// Resource errors
-#[derive(Debug, Error)]
-pub enum ResourceError {
- #[error("Resource unavailable")]
- ResourceUnavailable,
- #[error("{0}")]
- Custom(String),
-}
-
-// Buffer errors
-#[derive(Debug, Error)]
-pub enum BufferError {
- #[error("Incompatible data for buffer")]
- IncompatibleData,
- #[error("Failed to create texture from buffer")]
- TextureCreationFailed,
- #[error("{0}")]
- Custom(String),
-}
-
-// Pixel format errors
-#[derive(Debug, Error)]
-pub enum PixelFormatError {
- #[error("Incompatible CV pixel format")]
- IncompatibleCVPixelFormat,
- #[error("{0}")]
- Custom(String),
+ #[error("Cannot open device")]
+ CannotOpenDevice,
+ #[error("Cannot start gpu capture {0}")]
+ CannotStartGpuCapture(String),
+ #[error("Cannot create library: {0}")]
+ CannotCreateLibrary(String),
+ #[error("Cannot create command queue")]
+ CannotCreateCommandQueue,
+ #[error("Cannot create buffer")]
+ CannotCreateBuffer,
+ #[error("Cannot create command buffer")]
+ CannotCreateCommandBuffer,
+ #[error("Command buffer execution failed: {0}")]
+ CommandBufferExecutionFailed(String),
+ #[error("Cannot create event")]
+ CannotCreateEvent,
+ #[error("Cannot create function")]
+ CannotCreateFunction,
+ #[error("Cannot create pipeline state: {0}")]
+ CannotCreatePipelineState(String),
}
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs b/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs
index 0e562b3f8..25a1e42c8 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs
@@ -2,8 +2,8 @@ use super::gemm;
use crate::{
DataType,
backends::{
- common::kernel::matmul::{MatmulArguments, MatmulDispatchDescriptor, gemv, split_k},
- metal::{Metal, context::MetalContext, error::MetalError},
+ common::kernel::matmul::{MatmulArguments, MatmulDispatchDescriptor, MatmulError, gemv, split_k},
+ metal::{Metal, context::MetalContext},
},
};
@@ -11,7 +11,7 @@ pub fn choose_dispatch_descriptor(
context: &MetalContext,
data_type: DataType,
arguments: &MatmulArguments<Metal>,
-) -> Result<MatmulDispatchDescriptor, MetalError> {
+) -> Result<MatmulDispatchDescriptor, MatmulError<Metal>> {
if let Some(descriptor) = gemv::DispatchDescriptor::try_new::<Metal>(data_type, arguments)? {
return Ok(MatmulDispatchDescriptor::Gemv(descriptor));
}
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/gemm/dispatch_descriptor.rs b/crates/uzu/src/backends/metal/kernel/matmul/gemm/dispatch_descriptor.rs
index b0c2087c3..009c1808d 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/gemm/dispatch_descriptor.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/gemm/dispatch_descriptor.rs
@@ -5,9 +5,9 @@ use crate::{
backends::{
common::{
gpu_types::GEMMParams,
- kernel::matmul::{GridSize, MatmulArguments, gemm::Specialization},
+ kernel::matmul::{GridSize, MatmulArguments, MatmulError, gemm::Specialization},
},
- metal::{Metal, context::MetalContext, error::MetalError},
+ metal::{Metal, context::MetalContext},
},
};
@@ -16,9 +16,9 @@ impl DispatchDescriptor {
context: &MetalContext,
data_type: DataType,
arguments: &MatmulArguments<Metal>,
- ) -> Result<Self, MetalError> {
+ ) -> Result<Self, MatmulError<Metal>> {
if !matches!(data_type, DataType::F16 | DataType::BF16 | DataType::F32) {
- return Err(MetalError::Generic(format!("Unsupported dtype for GEMM: {data_type:?}")));
+ return Err(MatmulError::UnsupportedDataType(data_type));
}
let config = Specialization::select(context, data_type, arguments);
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/mod.rs b/crates/uzu/src/backends/metal/kernel/matmul/mod.rs
index 33395af71..d9482d193 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/mod.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/mod.rs
@@ -8,9 +8,9 @@ use crate::{
backends::{
common::kernel::matmul::{
FullPrecisionMatmulArguments, FullPrecisionMatmulKernel as FullPrecisionMatmulKernelTrait, MatmulArguments,
- MatmulKernel,
+ MatmulError, MatmulKernel,
},
- metal::{Metal, context::MetalContext, error::MetalError},
+ metal::{Metal, context::MetalContext},
},
};
@@ -20,7 +20,7 @@ impl FullPrecisionMatmulKernelTrait for MatmulKernel<Metal> {
fn new(
context: &MetalContext,
data_type: DataType,
- ) -> Result<Self, MetalError> {
+ ) -> Result<Self, MatmulError<Metal>> {
let mut kernel = MatmulKernel::new(data_type)?;
kernel.precompile(context)?;
Ok(kernel)
diff --git a/crates/uzu/src/backends/metal/metal_extensions/library_extensions_pipeline.rs b/crates/uzu/src/backends/metal/metal_extensions/library_extensions_pipeline.rs
index 2e974057f..a5508b9fa 100644
--- a/crates/uzu/src/backends/metal/metal_extensions/library_extensions_pipeline.rs
+++ b/crates/uzu/src/backends/metal/metal_extensions/library_extensions_pipeline.rs
@@ -1,7 +1,7 @@
use metal::{MTLComputePipelineState, MTLDeviceExt, MTLFunctionConstantValues, MTLLibrary, MTLLibraryExt};
use objc2::{rc::Retained, runtime::ProtocolObject};
-use crate::backends::metal::error::{LibraryError, MetalError};
+use crate::backends::metal::error::MetalError;
/// Extensions for Library to create compute pipeline states
pub trait LibraryPipelineExtensions {
@@ -26,12 +26,12 @@ impl LibraryPipelineExtensions for ProtocolObject<dyn MTLLibrary> {
},
None => self.new_function_with_name(function_name),
}
- .ok_or(MetalError::Library(LibraryError::FunctionCreationFailed))?;
+ .ok_or(MetalError::CannotCreateFunction)?;
let device = self.device();
device
.new_compute_pipeline_state_with_function(&function)
- .map_err(|e| MetalError::Library(LibraryError::Custom(format!("Pipeline state creation failed: {}", e))))
+ .map_err(|nserror| MetalError::CannotCreatePipelineState(nserror.to_string()))
}
}
diff --git a/crates/uzu/src/classifier/classifier.rs b/crates/uzu/src/classifier/classifier.rs
index 151d77fa4..18dcfdf47 100644
--- a/crates/uzu/src/classifier/classifier.rs
+++ b/crates/uzu/src/classifier/classifier.rs
@@ -115,8 +115,14 @@ impl<B: Backend> Classifier<B> {
let encoding_params = EncodingParameters::new(false, true, false);
- self.context.embed.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
- self.context.embedding_norm.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ self.context
+ .embed
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
+ self.context
+ .embedding_norm
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
#[cfg(feature = "tracing")]
{
let traces = state.traces().clone();
@@ -128,9 +134,14 @@ impl<B: Backend> Classifier<B> {
}
for layer in self.context.layers.iter() {
- layer.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ layer
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
}
- self.context.output_norm.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ self.context
+ .output_norm
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
#[cfg(feature = "tracing")]
{
let traces = state.traces().clone();
@@ -141,12 +152,18 @@ impl<B: Backend> Classifier<B> {
);
}
- self.context.pooling.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ self.context
+ .pooling
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
- self.context.prediction_head.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ self.context
+ .prediction_head
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
self.context.command_buffer.submit();
- self.context.command_buffer.wait_until_completed();
+ self.context.command_buffer.wait_until_completed().map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
let logits = self.copy_logits_from_state(&state)?;
@@ -178,17 +195,34 @@ impl<B: Backend> Classifier<B> {
let encoding_params = EncodingParameters::new(false, true, false);
- self.context.embed.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
- self.context.embedding_norm.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ self.context
+ .embed
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
+ self.context
+ .embedding_norm
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
for layer in self.context.layers.iter() {
- layer.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ layer
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
}
- self.context.output_norm.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
- self.context.pooling.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
- self.context.prediction_head.encode(&mut state, &encoding_params, &mut self.context.command_buffer);
+ self.context
+ .output_norm
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
+ self.context
+ .pooling
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
+ self.context
+ .prediction_head
+ .encode(&mut state, &encoding_params, &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
self.context.command_buffer.submit();
- self.context.command_buffer.wait_until_completed();
+ self.context.command_buffer.wait_until_completed().map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
let logits = self.copy_logits_from_state(&state)?;
diff --git a/crates/uzu/src/encodable_block/attention.rs b/crates/uzu/src/encodable_block/attention.rs
index 68e7e1ccf..bf8f729fa 100644
--- a/crates/uzu/src/encodable_block/attention.rs
+++ b/crates/uzu/src/encodable_block/attention.rs
@@ -141,13 +141,14 @@ impl<B: Backend> EncodableBlock<B> for Attention<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
command_buffer.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, parameters, encoder));
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/classifier_layer.rs b/crates/uzu/src/encodable_block/classifier_layer.rs
index c1c1b8a8d..1c5637417 100644
--- a/crates/uzu/src/encodable_block/classifier_layer.rs
+++ b/crates/uzu/src/encodable_block/classifier_layer.rs
@@ -251,13 +251,13 @@ impl<B: Backend> EncodableBlock<B> for ClassifierLayer<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
#[cfg(not(feature = "tracing"))]
{
if self.supports_shared_encoder() {
command_buffer
.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, parameters, encoder));
- return;
+ return Ok(());
}
}
@@ -269,10 +269,10 @@ impl<B: Backend> EncodableBlock<B> for ClassifierLayer<B> {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().inputs.clone());
}
- self.copy_main_to_shortcut_mixer.encode(state, parameters, command_buffer);
+ self.copy_main_to_shortcut_mixer.encode(state, parameters, command_buffer)?;
if let Some(ref pre_attn_norm) = self.pre_attention_norm {
- pre_attn_norm.encode(state, parameters, command_buffer);
+ pre_attn_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(
@@ -292,20 +292,20 @@ impl<B: Backend> EncodableBlock<B> for ClassifierLayer<B> {
}
}
- self.qkv_projection.encode(state, parameters, command_buffer);
+ self.qkv_projection.encode(state, parameters, command_buffer)?;
if let Some(ref qk_norm) = self.qk_norm {
- qk_norm.encode(state, parameters, command_buffer);
+ qk_norm.encode(state, parameters, command_buffer)?;
}
- self.rope.encode(state, parameters, command_buffer);
- self.attention.encode(state, parameters, command_buffer);
- self.out_projection.encode(state, parameters, command_buffer);
+ self.rope.encode(state, parameters, command_buffer)?;
+ self.attention.encode(state, parameters, command_buffer)?;
+ self.out_projection.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().attention.clone());
}
if let Some(ref post_attn_norm) = self.post_attention_norm {
- post_attn_norm.encode(state, parameters, command_buffer);
+ post_attn_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(
@@ -316,41 +316,42 @@ impl<B: Backend> EncodableBlock<B> for ClassifierLayer<B> {
}
}
- self.mixer_residual_add.encode(state, parameters, command_buffer);
+ self.mixer_residual_add.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().mlp_inputs.clone());
}
- self.copy_main_to_shortcut_mlp.encode(state, parameters, command_buffer);
+ self.copy_main_to_shortcut_mlp.encode(state, parameters, command_buffer)?;
- self.pre_mlp_norm.encode(state, parameters, command_buffer);
+ self.pre_mlp_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().pre_mlp_norm.clone());
}
- self.mlp.encode(state, parameters, command_buffer);
+ self.mlp.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().mlp.clone());
}
if let Some(ref post_mlp_norm) = self.post_mlp_norm {
- post_mlp_norm.encode(state, parameters, command_buffer);
+ post_mlp_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().post_mlp_norm.clone());
}
}
- self.mlp_residual_add.encode(state, parameters, command_buffer);
+ self.mlp_residual_add.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().outputs.clone());
}
let _ = parameters;
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/decoder.rs b/crates/uzu/src/encodable_block/decoder.rs
index 39adbb7eb..56f88326f 100644
--- a/crates/uzu/src/encodable_block/decoder.rs
+++ b/crates/uzu/src/encodable_block/decoder.rs
@@ -176,29 +176,30 @@ impl<B: Backend> EncodableBlock<B> for Decoder<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
- self.embed.encode(state, parameters, command_buffer);
+ ) -> Result<(), B::Error> {
+ self.embed.encode(state, parameters, command_buffer)?;
for layer in self.layers.iter() {
- layer.encode(state, parameters, command_buffer);
+ layer.encode(state, parameters, command_buffer)?;
}
if state.is_prefilling() {
- return;
+ return Ok(());
}
- self.norm.encode(state, parameters, command_buffer);
+ self.norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
{
let traces = state.traces().clone();
state.encode_copy_array(command_buffer, ArrayId::Main, traces.borrow().output_norm.clone());
}
- self.readout.encode(state, parameters, command_buffer);
+ self.readout.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
{
let traces = state.traces().clone();
state.encode_copy_array(command_buffer, ArrayId::Logits, traces.borrow().logits.clone());
}
+ Ok(())
}
}
diff --git a/crates/uzu/src/encodable_block/full_precision_embedding_readout.rs b/crates/uzu/src/encodable_block/full_precision_embedding_readout.rs
index 30580bf98..2af5bc31b 100644
--- a/crates/uzu/src/encodable_block/full_precision_embedding_readout.rs
+++ b/crates/uzu/src/encodable_block/full_precision_embedding_readout.rs
@@ -11,7 +11,7 @@ use crate::{
DataType,
backends::common::{
Backend,
- kernel::matmul::{FullPrecisionMatmulArguments, FullPrecisionMatmulKernel, MatmulKernels},
+ kernel::matmul::{FullPrecisionMatmulArguments, FullPrecisionMatmulKernel, MatmulError, MatmulKernels},
},
forward_pass::state::{ArrayId, ForwardPassState},
parameters::{ParameterLoaderError, ParameterTree},
@@ -19,8 +19,8 @@ use crate::{
#[derive(Debug, Error)]
pub enum FullPrecisionEmbeddingReadoutError<B: Backend> {
- #[error("Backend error: {0}")]
- BackendError(#[source] B::Error),
+ #[error("Matmul error: {0}")]
+ MatmulError(#[from] MatmulError<B>),
#[error("Parameter loading error: {0}")]
ParameterError(ParameterLoaderError),
#[error("Unsupported data type: {0:?}")]
@@ -82,8 +82,7 @@ impl<B: Backend> FullPrecisionEmbeddingReadout<B> {
});
}
- let kernel = <B::Kernels as MatmulKernels>::FullPrecisionMatmulKernel::new(context, data_type)
- .map_err(FullPrecisionEmbeddingReadoutError::BackendError)?;
+ let kernel = <B::Kernels as MatmulKernels>::FullPrecisionMatmulKernel::new(context, data_type)?;
Ok(Self {
kernel: RefCell::new(kernel),
diff --git a/crates/uzu/src/encodable_block/full_precision_linear.rs b/crates/uzu/src/encodable_block/full_precision_linear.rs
index 33dd4824d..9c5daed29 100644
--- a/crates/uzu/src/encodable_block/full_precision_linear.rs
+++ b/crates/uzu/src/encodable_block/full_precision_linear.rs
@@ -11,7 +11,7 @@ use crate::{
DataType,
backends::common::{
Backend,
- kernel::matmul::{FullPrecisionMatmulArguments, FullPrecisionMatmulKernel, MatmulKernels},
+ kernel::matmul::{FullPrecisionMatmulArguments, FullPrecisionMatmulKernel, MatmulError, MatmulKernels},
},
forward_pass::state::{ArrayId, ForwardPassState},
parameters::{ParameterLoaderError, ParameterTree},
@@ -19,8 +19,8 @@ use crate::{
#[derive(Debug, Error)]
pub enum FullPrecisionLinearError<B: Backend> {
- #[error("Backend error: {0}")]
- BackendError(#[source] B::Error),
+ #[error("Matmul error: {0}")]
+ MatmulError(#[from] MatmulError<B>),
#[error("Parameter loading error: {0}")]
ParameterError(ParameterLoaderError),
#[error("Unsupported data type for full precision linear kernel: {0:?}")]
@@ -111,8 +111,7 @@ impl<B: Backend> FullPrecisionLinear<B> {
Err(_) => None,
};
- let kernel = <B::Kernels as MatmulKernels>::FullPrecisionMatmulKernel::new(context, precision)
- .map_err(FullPrecisionLinearError::BackendError)?;
+ let kernel = <B::Kernels as MatmulKernels>::FullPrecisionMatmulKernel::new(context, precision)?;
Ok(Self {
kernel: RefCell::new(kernel),
diff --git a/crates/uzu/src/encodable_block/layer/executables.rs b/crates/uzu/src/encodable_block/layer/executables.rs
index af8d857e4..25148726c 100644
--- a/crates/uzu/src/encodable_block/layer/executables.rs
+++ b/crates/uzu/src/encodable_block/layer/executables.rs
@@ -262,7 +262,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
// In non-tracing builds, if every sub-block supports shared encoding,
// we can run the entire layer in a single compute encoder.
#[cfg(not(feature = "tracing"))]
@@ -270,7 +270,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
if self.supports_shared_encoder() {
command_buffer
.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, parameters, encoder));
- return;
+ return Ok(());
}
}
@@ -282,10 +282,10 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().inputs.clone());
}
- self.copy_main_to_shortcut.encode(state, parameters, command_buffer);
+ self.copy_main_to_shortcut.encode(state, parameters, command_buffer)?;
// shortcut = input
- self.pre_attention_norm.encode(state, parameters, command_buffer);
+ self.pre_attention_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().pre_attention_norm.clone());
@@ -299,13 +299,13 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
attention,
out_projection,
} => {
- qkv_projection.encode(state, parameters, command_buffer);
+ qkv_projection.encode(state, parameters, command_buffer)?;
if let Some(norm) = qk_norm {
- norm.encode(state, parameters, command_buffer);
+ norm.encode(state, parameters, command_buffer)?;
}
- rope.encode(state, parameters, command_buffer);
- attention.encode(state, parameters, command_buffer);
- out_projection.encode(state, parameters, command_buffer);
+ rope.encode(state, parameters, command_buffer)?;
+ attention.encode(state, parameters, command_buffer)?;
+ out_projection.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().attention.clone());
@@ -314,7 +314,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
MixerExecutables::StateSpace {
mixer,
} => {
- mixer.encode(state, parameters, command_buffer);
+ mixer.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().attention.clone());
@@ -323,7 +323,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
MixerExecutables::ShortConv {
mixer,
} => {
- mixer.encode(state, parameters, command_buffer);
+ mixer.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().attention.clone());
@@ -332,7 +332,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
}
if let Some(post_attention_norm) = &self.post_attention_norm {
- post_attention_norm.encode(state, parameters, command_buffer);
+ post_attention_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(
@@ -344,7 +344,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
}
//main = attention_result
- self.main_shortcut_add_swap.encode(state, parameters, command_buffer);
+ self.main_shortcut_add_swap.encode(state, parameters, command_buffer)?;
// shortcut = input + attention_result
// main = input + attention_result
#[cfg(feature = "tracing")]
@@ -352,20 +352,20 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().mlp_inputs.clone());
}
- self.pre_mlp_norm.encode(state, parameters, command_buffer);
+ self.pre_mlp_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().pre_mlp_norm.clone());
}
- self.mlp.encode(state, parameters, command_buffer);
+ self.mlp.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().mlp.clone());
}
if let Some(post_mlp_norm) = &self.post_mlp_norm {
- post_mlp_norm.encode(state, parameters, command_buffer);
+ post_mlp_norm.encode(state, parameters, command_buffer)?;
#[cfg(feature = "tracing")]
if let Some(ref layer_traces) = layer_traces {
state.encode_copy_array(command_buffer, ArrayId::Main, layer_traces.borrow().post_mlp_norm.clone());
@@ -373,7 +373,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
}
// main = mlp_result
- self.main_shortcut_add_swap.encode(state, parameters, command_buffer);
+ self.main_shortcut_add_swap.encode(state, parameters, command_buffer)?;
// shortcut = input + attention_result + mlp_result
// main = input + attention_result + mlp_result
#[cfg(feature = "tracing")]
@@ -382,6 +382,7 @@ impl<B: Backend> EncodableBlock<B> for LayerExecutables<B> {
}
let _ = parameters;
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/mamba_mixer.rs b/crates/uzu/src/encodable_block/mamba_mixer.rs
index 05ab19908..318fd7f28 100644
--- a/crates/uzu/src/encodable_block/mamba_mixer.rs
+++ b/crates/uzu/src/encodable_block/mamba_mixer.rs
@@ -440,24 +440,24 @@ impl<B: Backend> EncodableBlock<B> for MambaMixer<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
if self.supports_shared_encoder() {
command_buffer
.with_compute_encoder(|encoder| self.encode_pipeline_with_encoder(state, encoder, parameters));
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
- return;
+ return Ok(());
}
let active_suffix_length = state.active_suffix_length();
if active_suffix_length == 0 {
- return;
+ return Ok(());
}
- self.in_projection.encode(state, parameters, command_buffer);
+ self.in_projection.encode(state, parameters, command_buffer)?;
command_buffer.with_compute_encoder(|encoder| {
self.run_split_inproj(state, encoder, active_suffix_length);
@@ -469,12 +469,13 @@ impl<B: Backend> EncodableBlock<B> for MambaMixer<B> {
}
});
- self.out_projection.encode(state, parameters, command_buffer);
+ self.out_projection.encode(state, parameters, command_buffer)?;
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/mlp.rs b/crates/uzu/src/encodable_block/mlp.rs
index 068f50644..df0d31348 100644
--- a/crates/uzu/src/encodable_block/mlp.rs
+++ b/crates/uzu/src/encodable_block/mlp.rs
@@ -34,12 +34,12 @@ impl<B: Backend> EncodableBlock<B> for MlpBlock<B> {
state: &mut ForwardPassState<B>,
params: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
if self.supports_shared_encoder() {
command_buffer.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, params, encoder));
} else {
// Up
- self.up.encode(state, params, command_buffer);
+ self.up.encode(state, params, command_buffer)?;
// Gate act+mul (fused_up -> hidden)
command_buffer.with_compute_encoder(|encoder| {
@@ -58,13 +58,14 @@ impl<B: Backend> EncodableBlock<B> for MlpBlock<B> {
});
// Down
- self.down.encode(state, params, command_buffer);
+ self.down.encode(state, params, command_buffer)?;
}
if params.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/mod.rs b/crates/uzu/src/encodable_block/mod.rs
index 2f1abc358..27847edb9 100644
--- a/crates/uzu/src/encodable_block/mod.rs
+++ b/crates/uzu/src/encodable_block/mod.rs
@@ -119,7 +119,8 @@ pub trait EncodableBlock<B: Backend> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
command_buffer.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, parameters, encoder));
+ Ok(())
}
}
diff --git a/crates/uzu/src/encodable_block/moe_block.rs b/crates/uzu/src/encodable_block/moe_block.rs
index 283f40419..07869e271 100644
--- a/crates/uzu/src/encodable_block/moe_block.rs
+++ b/crates/uzu/src/encodable_block/moe_block.rs
@@ -194,7 +194,7 @@ impl<B: Backend> EncodableBlock<B> for MoeBlock<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
let suffix_length = state.active_suffix_length();
let arrays = state.arrays(&[
ArrayId::Main,
@@ -482,8 +482,9 @@ impl<B: Backend> EncodableBlock<B> for MoeBlock<B> {
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/pooling.rs b/crates/uzu/src/encodable_block/pooling.rs
index c197040cf..00caf4324 100644
--- a/crates/uzu/src/encodable_block/pooling.rs
+++ b/crates/uzu/src/encodable_block/pooling.rs
@@ -66,7 +66,7 @@ impl<B: Backend> EncodableBlock<B> for Pooling<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
command_buffer.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, parameters, encoder));
#[cfg(feature = "tracing")]
@@ -74,6 +74,7 @@ impl<B: Backend> EncodableBlock<B> for Pooling<B> {
let output_pooling_trace = state.traces().borrow().output_pooling().clone();
state.encode_copy_array(command_buffer, ArrayId::ClassifierPooling, output_pooling_trace);
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/prediction_head.rs b/crates/uzu/src/encodable_block/prediction_head.rs
index 0b5bf96aa..c41133e6c 100644
--- a/crates/uzu/src/encodable_block/prediction_head.rs
+++ b/crates/uzu/src/encodable_block/prediction_head.rs
@@ -41,14 +41,14 @@ impl<B: Backend> EncodableBlock<B> for ClassifierPredictionHead<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
if self.supports_shared_encoder() {
command_buffer.with_compute_encoder(|encoder| self.encode_with_shared_encoder(state, parameters, encoder));
} else {
- self.dense.encode(state, parameters, command_buffer);
- self.activation.encode(state, parameters, command_buffer);
- self.norm.encode(state, parameters, command_buffer);
- self.readout.encode(state, parameters, command_buffer);
+ self.dense.encode(state, parameters, command_buffer)?;
+ self.activation.encode(state, parameters, command_buffer)?;
+ self.norm.encode(state, parameters, command_buffer)?;
+ self.readout.encode(state, parameters, command_buffer)?;
}
#[cfg(feature = "tracing")]
@@ -63,8 +63,9 @@ impl<B: Backend> EncodableBlock<B> for ClassifierPredictionHead<B> {
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/encodable_block/short_conv_mixer.rs b/crates/uzu/src/encodable_block/short_conv_mixer.rs
index dadef2127..c6e272ccc 100644
--- a/crates/uzu/src/encodable_block/short_conv_mixer.rs
+++ b/crates/uzu/src/encodable_block/short_conv_mixer.rs
@@ -108,22 +108,23 @@ impl<B: Backend> ShortConvMixer<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
let active_suffix_length = state.active_suffix_length();
if active_suffix_length == 0 {
- return;
+ return Ok(());
}
- self.in_projection.encode(state, parameters, command_buffer);
+ self.in_projection.encode(state, parameters, command_buffer)?;
command_buffer.with_compute_encoder(|encoder| self.run_conv(state, encoder, active_suffix_length));
- self.out_projection.encode(state, parameters, command_buffer);
+ self.out_projection.encode(state, parameters, command_buffer)?;
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
+ Ok(())
}
fn encode_pipeline_with_encoder(
@@ -371,18 +372,19 @@ impl<B: Backend> EncodableBlock<B> for ShortConvMixer<B> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
command_buffer: &mut B::CommandBuffer,
- ) {
+ ) -> Result<(), B::Error> {
if self.supports_shared_encoder() {
command_buffer
.with_compute_encoder(|encoder| self.encode_pipeline_with_encoder(state, encoder, parameters));
if parameters.wait_until_completed {
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed()?;
}
} else {
- self.encode_pipeline(state, parameters, command_buffer);
+ self.encode_pipeline(state, parameters, command_buffer)?;
}
+ Ok(())
}
fn supports_shared_encoder(&self) -> bool {
diff --git a/crates/uzu/src/language_model/language_model_generator.rs b/crates/uzu/src/language_model/language_model_generator.rs
index 8dd979fab..7d59e7404 100644
--- a/crates/uzu/src/language_model/language_model_generator.rs
+++ b/crates/uzu/src/language_model/language_model_generator.rs
@@ -234,7 +234,7 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
self.allow_pre_encode(),
sampling_method,
self.should_fill_attention_bias(),
- );
+ )?;
if should_capture {
self.gpu_capture.stop_capture(&self.context.context, "prefill").map_err(|_| Error::CaptureFailed)?;
@@ -255,7 +255,7 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
if !positions_for_step.is_empty() {
let accept_indices_for_step: Vec<usize> = (0..positions_for_step.len()).collect();
if !accept_indices_for_step.is_empty() {
- self.update_cache_layers(&accept_indices_for_step, None, true);
+ self.update_cache_layers(&accept_indices_for_step, None, true)?;
}
self.context.cache_layers.borrow_mut().register_accepted_tokens(&positions_for_step);
@@ -290,7 +290,7 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
&accepted_token_indices.into_iter().map(|p| suffix_root_index + p).collect::<Box<[usize]>>(),
Some(last_suffix_start),
false,
- );
+ )?;
self.tokens.extend(accepted_tokens.clone());
self.sync_prefix();
@@ -365,14 +365,14 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
};
let (mut state, run_time) =
- self.run_model(task, false, self.allow_pre_encode(), sampling_method, self.should_fill_attention_bias());
+ self.run_model(task, false, self.allow_pre_encode(), sampling_method, self.should_fill_attention_bias())?;
let sampled_tokens = self.sample(&mut state)?;
let (accepted_tokens, accepted_token_indices) =
flat_trie.accept(&sampled_tokens, compiled_grammar.as_deref_mut());
- self.update_cache_layers(&accepted_token_indices, None, false);
+ self.update_cache_layers(&accepted_token_indices, None, false)?;
self.tokens.extend(accepted_tokens.clone());
self.sync_prefix();
@@ -494,18 +494,16 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
self.context.command_buffer.encode_wait_for_event(&self.context.async_buffers.event, current_counter);
}
- self.context.executables.encode(
- &mut state,
- &EncodingParameters::new(false, false, false),
- &mut self.context.command_buffer,
- );
+ self.context
+ .executables
+ .encode(&mut state, &EncodingParameters::new(false, false, false), &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
// Encode sampling
- self.context.gpu_sampler.encode(
- &mut state,
- &EncodingParameters::new(false, false, false),
- &mut self.context.command_buffer,
- );
+ self.context
+ .gpu_sampler
+ .encode(&mut state, &EncodingParameters::new(false, false, false), &mut self.context.command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
// Copy sampled token: sampling_output → token_ids (for next pass)
// and sampling_output → results[slot] (for callback)
@@ -569,7 +567,8 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
// Add completion handler
let results_buffer_ptr = results_buffer.borrow().cpu_ptr().as_ptr() as *const u32;
- let handler = move || {
+ let handler = move |result: Result<(), B::Error>| {
+ result.expect("async decoding forward pass completed with error");
let token = { unsafe { *results_buffer_ptr.add(slot) as u64 } };
on_complete(token);
};
@@ -578,7 +577,7 @@ impl<B: Backend> LanguageModelGeneratorTrait for LanguageModelGenerator<B> {
self.context.command_buffer.submit();
if should_capture {
- self.context.command_buffer.wait_until_completed();
+ self.context.command_buffer.wait_until_completed().map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
self.gpu_capture.stop_capture(&self.context.context, "decode").map_err(|_| Error::CaptureFailed)?;
}
@@ -711,8 +710,8 @@ impl<B: Backend> LanguageModelGenerator<B> {
};
//Warmup
- generator.warmup(prefill_step_size);
- generator.warmup(generate_suffix_length);
+ generator.warmup(prefill_step_size)?;
+ generator.warmup(generate_suffix_length)?;
Ok(generator)
}
@@ -720,7 +719,7 @@ impl<B: Backend> LanguageModelGenerator<B> {
fn warmup(
&mut self,
suffix_length: usize,
- ) {
+ ) -> Result<(), Error> {
let token_ids: Vec<u64> = vec![0; suffix_length];
let token_positions: Vec<usize> = (0..suffix_length).collect();
let token_seeds: Vec<u64> = vec![0; suffix_length];
@@ -737,7 +736,8 @@ impl<B: Backend> LanguageModelGenerator<B> {
is_prefilling: false,
};
- let (_, _) = self.run_model(task, true, false, SamplingMethod::default(), false);
+ self.run_model(task, true, false, SamplingMethod::default(), false)?;
+ Ok(())
}
fn run_model(
@@ -747,7 +747,7 @@ impl<B: Backend> LanguageModelGenerator<B> {
allow_pre_encode: bool,
sampling_method: SamplingMethod,
should_fill_attention_bias: bool,
- ) -> (ForwardPassState<B>, f64) {
+ ) -> Result<(ForwardPassState<B>, f64), Error> {
objc2::rc::autoreleasepool(|_pool| {
let run_start = Instant::now();
@@ -774,21 +774,25 @@ impl<B: Backend> LanguageModelGenerator<B> {
} else {
self.context.reset_command_buffer();
- _ = task.build_encoded_task(
+ task.build_encoded_task(
&mut self.context,
&mut state,
&EncodingParameters::new(warmup, true, false),
encoded_task_key.clone(),
- );
+ )
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
}
if !warmup {
if !task.is_prefilling {
- self.context.gpu_sampler.encode(
- &mut state,
- &EncodingParameters::new(warmup, true, false),
- &mut self.context.command_buffer,
- );
+ self.context
+ .gpu_sampler
+ .encode(
+ &mut state,
+ &EncodingParameters::new(warmup, true, false),
+ &mut self.context.command_buffer,
+ )
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
}
}
@@ -799,29 +803,31 @@ impl<B: Backend> LanguageModelGenerator<B> {
let next_task_key: String = task.encoded_task_key(self.tokens.len() + 1);
- let next_encoded_task = task.build_encoded_task(
- &mut self.context,
- &mut state,
- &EncodingParameters::new(warmup, false, false).with_projection(1),
- next_task_key.clone(),
- );
+ let next_encoded_task = task
+ .build_encoded_task(
+ &mut self.context,
+ &mut state,
+ &EncodingParameters::new(warmup, false, false).with_projection(1),
+ next_task_key.clone(),
+ )
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
self.encoded_tasks.insert(next_task_key.clone(), next_encoded_task);
- in_flight_command_buffer.wait_until_completed();
+ in_flight_command_buffer.wait_until_completed().map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
} else {
- self.context.command_buffer.wait_until_completed();
+ self.context
+ .command_buffer
+ .wait_until_completed()
+ .map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
}
let run_time = run_start.elapsed().as_secs_f64();
if should_capture {
- self.gpu_capture
- .stop_capture(&self.context.context, "decode")
- .map_err(|_| Error::CaptureFailed)
- .expect("failed to stop capture");
+ self.gpu_capture.stop_capture(&self.context.context, "decode").map_err(|_| Error::CaptureFailed)?;
}
- (state, run_time)
+ Ok((state, run_time))
})
}
@@ -850,7 +856,7 @@ impl<B: Backend> LanguageModelGenerator<B> {
accepted_token_indices: &[usize],
suffix_start: Option<usize>,
wait_until_completed: bool,
- ) {
+ ) -> Result<(), Error> {
let mut command_buffer = self.context.context.create_command_buffer().expect("Failed to create command buffer");
{
@@ -866,8 +872,9 @@ impl<B: Backend> LanguageModelGenerator<B> {
command_buffer.submit();
if wait_until_completed {
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
}
+ Ok(())
}
fn allow_pre_encode(&self) -> bool {
diff --git a/crates/uzu/src/language_model/tasks.rs b/crates/uzu/src/language_model/tasks.rs
index f8395b963..5063c8570 100644
--- a/crates/uzu/src/language_model/tasks.rs
+++ b/crates/uzu/src/language_model/tasks.rs
@@ -113,15 +113,15 @@ impl<'a> LanguageModelGeneratorRunTask<'a> {
state: &mut ForwardPassState<B>,
parameters: &EncodingParameters<B>,
key: String,
- ) -> LanguageModelGeneratorEncodedTask<B> {
- context.executables.encode(state, parameters, &mut context.command_buffer);
+ ) -> Result<LanguageModelGeneratorEncodedTask<B>, B::Error> {
+ context.executables.encode(state, parameters, &mut context.command_buffer)?;
- LanguageModelGeneratorEncodedTask {
+ Ok(LanguageModelGeneratorEncodedTask {
key,
predicate_buffer: context
.context
.create_buffer(size_of::<u32>())
.expect("Failed to create predicate buffer"),
- }
+ })
}
}
diff --git a/crates/uzu/src/session/types/error.rs b/crates/uzu/src/session/types/error.rs
index 33c1bd827..af0d03cf8 100644
--- a/crates/uzu/src/session/types/error.rs
+++ b/crates/uzu/src/session/types/error.rs
@@ -14,6 +14,10 @@ pub enum Error {
UnableToLoadTokenizer,
#[error("Model is too large to fit into available RAM")]
NotEnoughMemory,
+ #[error("Command buffer failed: {0}")]
+ CommandBufferFailed(Box<dyn std::error::Error>),
+ #[error("Encode failed: {0}")]
+ EncodeFailed(Box<dyn std::error::Error>),
#[error("Unsupported context mode for model")]
UnsupportedContextModeForModel,
#[error("Unsupported speculator config for model")]
diff --git a/crates/uzu/src/tracer/trace_validator.rs b/crates/uzu/src/tracer/trace_validator.rs
index 7f24c91d5..164c62994 100644
--- a/crates/uzu/src/tracer/trace_validator.rs
+++ b/crates/uzu/src/tracer/trace_validator.rs
@@ -272,9 +272,11 @@ impl<B: Backend> TraceValidator<B> {
let mut command_buffer = ctx.context.create_command_buffer().expect("Failed to create command buffer");
- ctx.executables.encode(&mut state, &EncodingParameters::new(false, false, false), &mut command_buffer);
+ ctx.executables
+ .encode(&mut state, &EncodingParameters::new(false, false, false), &mut command_buffer)
+ .map_err(|e| Error::EncodeFailed(Box::new(e)))?;
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().map_err(|e| Error::CommandBufferFailed(Box::new(e)))?;
let traces = state.traces().clone();
let data_type = ctx.model_shape.activation_data_type();
diff --git a/crates/uzu/tests/attention_kernel_test.rs b/crates/uzu/tests/attention_kernel_test.rs
index 7f26857bd..d53cf4c76 100644
--- a/crates/uzu/tests/attention_kernel_test.rs
+++ b/crates/uzu/tests/attention_kernel_test.rs
@@ -352,7 +352,7 @@ fn run_single_pass_attention(
compute_encoder.end_encoding();
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let output_ptr = output_buffer.contents().as_ptr() as *const f32;
let output_slice = unsafe { std::slice::from_raw_parts(output_ptr, num_heads * seq_len * head_dim) };
@@ -424,7 +424,7 @@ fn run_single_pass_attention_with_is_causal(
compute_encoder.end_encoding();
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let output_ptr = output_buffer.contents().as_ptr() as *const f32;
let output_slice = unsafe { std::slice::from_raw_parts(output_ptr, num_heads * seq_len * head_dim) };
@@ -486,7 +486,7 @@ fn run_gemm_attention(
encode_result?;
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let output_ptr = output_buffer.contents().as_ptr() as *const f32;
let output_slice = unsafe { std::slice::from_raw_parts(output_ptr, num_heads * seq_len * head_dim) };
@@ -981,7 +981,7 @@ fn run_two_pass_attention(
compute_encoder.end_encoding();
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let output_ptr = output_buffer.contents().as_ptr() as *const f32;
let output_slice = unsafe { std::slice::from_raw_parts(output_ptr, num_heads * seq_len * head_dim) };
@@ -1215,7 +1215,7 @@ fn perf_two_pass_attention() {
// Time both host-side and GPU execution
let host_timer = Instant::now();
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;
// Get actual GPU execution time
diff --git a/crates/uzu/tests/kernel/full_precision_embedding_test.rs b/crates/uzu/tests/kernel/full_precision_embedding_test.rs
index 2202a09bf..ee634ba65 100644
--- a/crates/uzu/tests/kernel/full_precision_embedding_test.rs
+++ b/crates/uzu/tests/kernel/full_precision_embedding_test.rs
@@ -74,7 +74,7 @@ fn get_output<T: ArrayElement + Float, B: Backend>(input: &Input<T>) -> Vec<T> {
);
});
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
output_array.as_slice().to_vec()
}
diff --git a/crates/uzu/tests/kernel/tensor_add_bias_test.rs b/crates/uzu/tests/kernel/tensor_add_bias_test.rs
index 1baeb5116..7b25b0a4c 100644
--- a/crates/uzu/tests/kernel/tensor_add_bias_test.rs
+++ b/crates/uzu/tests/kernel/tensor_add_bias_test.rs
@@ -87,7 +87,7 @@ fn get_output<T: ArrayElement + Float, B: Backend>(
)
});
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
output_array.as_slice().to_vec()
}
diff --git a/crates/uzu/tests/kernel/tensor_add_swap_test.rs b/crates/uzu/tests/kernel/tensor_add_swap_test.rs
index 61a8ebdc7..d6c8b1952 100644
--- a/crates/uzu/tests/kernel/tensor_add_swap_test.rs
+++ b/crates/uzu/tests/kernel/tensor_add_swap_test.rs
@@ -56,7 +56,7 @@ fn get_output<T: ArrayElement + Float, B: Backend>(input: &Input<T>) -> (Vec<T>,
)
});
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
(skip_array.as_slice().to_vec(), main_array.as_slice().to_vec())
}
diff --git a/crates/uzu/tests/kernel/tensor_copy_test.rs b/crates/uzu/tests/kernel/tensor_copy_test.rs
index 3053e81a4..4870584f9 100644
--- a/crates/uzu/tests/kernel/tensor_copy_test.rs
+++ b/crates/uzu/tests/kernel/tensor_copy_test.rs
@@ -55,7 +55,7 @@ fn get_output<T: ArrayElement + Float, B: Backend>(input: &Input<T>) -> Vec<T> {
)
});
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
dst_array.as_slice().to_vec()
}
diff --git a/crates/uzu/tests/kernel/token_copy_test.rs b/crates/uzu/tests/kernel/token_copy_test.rs
index d4b64007d..fa47b5e77 100644
--- a/crates/uzu/tests/kernel/token_copy_test.rs
+++ b/crates/uzu/tests/kernel/token_copy_test.rs
@@ -23,7 +23,7 @@ fn test_token_copy_sampled_impl<B: Backend>(src_value: u32) {
kernel.encode(src_array.buffer().borrow().deref(), dst_array.buffer().borrow_mut().deref_mut(), encoder)
});
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let output = dst_array.as_slice::<u64>().to_vec();
assert_eq!(output[0], src_value as u64, "TokenCopySampled failed for backend {}", std::any::type_name::<B>());
@@ -43,7 +43,7 @@ fn test_token_copy_to_results_impl<B: Backend>(src_value: u32) {
kernel.encode(src_array.buffer().borrow().deref(), dst_array.buffer().borrow_mut().deref_mut(), encoder)
});
command_buffer.submit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
let output = dst_array.as_slice::<u32>().to_vec();
assert_eq!(output[0], src_value, "TokenCopyToResults failed for backend {}", std::any::type_name::<B>());
diff --git a/crates/uzu/tests/rms_norm_kernel_test.rs b/crates/uzu/tests/rms_norm_kernel_test.rs
index 156b4d4bf..bdc1eb6bd 100644
--- a/crates/uzu/tests/rms_norm_kernel_test.rs
+++ b/crates/uzu/tests/rms_norm_kernel_test.rs
@@ -329,7 +329,7 @@ fn test_rms_norm_basic_typed<InputT, ScaleT, OutputT>(
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
// Read results
let output_ptr = output_buffer.contents().as_ptr() as *const OutputT;
@@ -447,7 +447,7 @@ fn test_rms_norm_edge_cases_typed<InputT, ScaleT, OutputT>(
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let output_ptr = output_buffer.contents().as_ptr() as *const OutputT;
let output_data = unsafe { std::slice::from_raw_parts(output_ptr, 4) };
@@ -658,7 +658,7 @@ fn perf_rms_norm_with_size(
// Time both host-side and GPU execution
let host_timer = Instant::now();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;
// Get actual GPU execution time
@@ -829,7 +829,7 @@ fn qk_norm_test() {
compute_encoder.end_encoding();
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
}
// Verify Q heads were normalized (should have different values now)
@@ -892,7 +892,7 @@ fn qk_norm_test() {
compute_encoder.end_encoding();
command_buffer.commit();
- command_buffer.wait_until_completed();
+ command_buffer.wait_until_completed().unwrap();
}
// Verify K heads were normalized
diff --git a/crates/uzu/tests/sampling_test.rs b/crates/uzu/tests/sampling_test.rs
index f506a89aa..5650f6b69 100644
--- a/crates/uzu/tests/sampling_test.rs
+++ b/crates/uzu/tests/sampling_test.rs
@@ -129,7 +129,7 @@ fn test_argmax_sampling_with_strategy(strategy: ArgmaxStrategy) {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
// Check results
let result_ptr = output_buffer.contents().as_ptr() as *const u32;
@@ -262,7 +262,7 @@ fn test_topp_sampling_from_prob_exact_match(
.expect("encode");
compute_encoder.end_encoding();
cb_ref.commit();
- cb_ref.wait_until_completed();
+ cb_ref.wait_until_completed().unwrap();
let ptr = output_buf.contents().as_ptr() as *const u32;
let sampled_ids = unsafe { std::slice::from_raw_parts(ptr, batch_size) };
@@ -387,7 +387,7 @@ fn test_topp_sampling_statistical_large() {
compute_encoder.end_encoding();
cb_ref.commit();
- cb_ref.wait_until_completed();
+ cb_ref.wait_until_completed().unwrap();
let ptr = output_buf.contents().as_ptr() as *const u32;
let sample_ids = unsafe { std::slice::from_raw_parts(ptr, BATCH) };
@@ -491,7 +491,7 @@ fn perf_topp_128k_vocab() {
// Time both host-side and GPU execution
let host_timer = Instant::now();
cb_ref.commit();
- cb_ref.wait_until_completed();
+ cb_ref.wait_until_completed().unwrap();
let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;
// Get actual GPU execution time
@@ -590,7 +590,7 @@ fn perf_argmax_128k_vocab_with_strategy(strategy: ArgmaxStrategy) {
// Time both host-side and GPU execution
let host_timer = Instant::now();
cb_ref.commit();
- cb_ref.wait_until_completed();
+ cb_ref.wait_until_completed().unwrap();
let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;
// Get actual GPU execution time
@@ -713,7 +713,7 @@ fn test_categorical_sampling() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let result_ptr = output_buffer.contents().as_ptr() as *const u32;
let results = unsafe { std::slice::from_raw_parts(result_ptr, batch_size) };
@@ -855,7 +855,7 @@ fn test_categorical_sampling_statistical() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let result_ptr = output_buffer.contents().as_ptr() as *const u32;
let results = unsafe { std::slice::from_raw_parts(result_ptr, BATCH) };
@@ -962,7 +962,7 @@ fn perf_categorical_128k_vocab() {
let host_timer = Instant::now();
cb_ref.commit();
- cb_ref.wait_until_completed();
+ cb_ref.wait_until_completed().unwrap();
let host_elapsed_ms = host_timer.elapsed().as_secs_f64() * 1e3;
let gpu_elapsed_ms = cb.gpu_execution_time_ms();
@@ -1038,7 +1038,7 @@ fn test_temperature_gpu_cpu_match() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let gpu_ptr = processed_buffer.contents().as_ptr() as *const f32;
let gpu_results = unsafe { std::slice::from_raw_parts(gpu_ptr, logits.len()) };
@@ -1104,7 +1104,7 @@ fn test_topk_gpu_cpu_match() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let results_ptr = processed_buffer.contents().as_ptr() as *const f32;
let all_results = unsafe { std::slice::from_raw_parts(results_ptr, logits.len()) };
@@ -1169,7 +1169,7 @@ fn test_topp_gpu_cpu_match() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let results_ptr = processed_buffer.contents().as_ptr() as *const f32;
let all_results = unsafe { std::slice::from_raw_parts(results_ptr, logits.len()) };
@@ -1239,7 +1239,7 @@ fn test_minp_gpu_cpu_match() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let results_ptr = processed_buffer.contents().as_ptr() as *const f32;
let all_results = unsafe { std::slice::from_raw_parts(results_ptr, logits.len()) };
@@ -1344,7 +1344,7 @@ fn test_minp_sampling_exact_match(
.expect("encode");
compute_encoder.end_encoding();
cb_ref.commit();
- cb_ref.wait_until_completed();
+ cb_ref.wait_until_completed().unwrap();
let ptr = output_buf.contents().as_ptr() as *const u32;
let sampled_ids = unsafe { std::slice::from_raw_parts(ptr, batch_size) };
@@ -1433,7 +1433,7 @@ fn test_gumbel_gpu_cpu_match() {
compute_encoder.end_encoding();
command_buffer_ref.commit();
- command_buffer_ref.wait_until_completed();
+ command_buffer_ref.wait_until_completed().unwrap();
let result_ptr = gumbel_logits_buffer.contents().as_ptr() as *const f32;
let all_results = unsafe { std::slice::from_raw_parts(result_ptr, BATCH * VOCAB) };