Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

dsl simd (#190)

uuuvn60bf4fdd43ac
diff --git a/crates/uzu/build/metal/ast.rs b/crates/uzu/build/metal/ast.rs
index 36b9d0c5f..91896d88f 100644
--- a/crates/uzu/build/metal/ast.rs
+++ b/crates/uzu/build/metal/ast.rs
@@ -111,6 +111,7 @@ pub enum MetalArgumentType {
Axis(Box<str>, Box<str>),
Groups(MetalGroupsType),
Threads(Box<str>),
+ Simd,
}
#[derive(Debug, Serialize, Deserialize)]
@@ -242,6 +243,8 @@ impl MetalArgument {
},
_ => bail!("unknown annotation: {annotation_key}"),
}
+ } else if self.c_type.as_ref() == "Simd" || self.c_type.as_ref() == "const Simd" {
+ Ok(MetalArgumentType::Simd)
} else if self.c_type.contains("device") && self.c_type.contains('*') && !self.c_type.contains('&') {
Ok(MetalArgumentType::Buffer)
} else if let ["const", "constant", c_type_scalar, "&"] =
@@ -338,6 +341,10 @@ impl MetalKernelInfo {
self.arguments.iter().any(|a| matches!(a.argument_type(), Ok(MetalArgumentType::Threads(_))))
}
+ pub fn has_simd(&self) -> bool {
+ self.arguments.iter().any(|a| matches!(a.argument_type(), Ok(MetalArgumentType::Simd)))
+ }
+
pub fn to_kernel(&self) -> Kernel {
let mut indirect_flag = false;
diff --git a/crates/uzu/build/metal/wrapper.rs b/crates/uzu/build/metal/wrapper.rs
index 95fa3a0f2..3c68014ed 100644
--- a/crates/uzu/build/metal/wrapper.rs
+++ b/crates/uzu/build/metal/wrapper.rs
@@ -134,6 +134,12 @@ fn kernel_wrappers(
wrapper_arguments.push("uint3 __dsl_thread_idx [[thread_position_in_threadgroup]]".into());
}
+ if kernel.has_simd() {
+ wrapper_arguments.push("uint __dsl_simd_lane_idx [[thread_index_in_simdgroup]]".into());
+ wrapper_arguments.push("uint __dsl_simd_group_idx [[simdgroup_index_in_threadgroup]]".into());
+ wrapper_arguments.push("uint __dsl_simd_group_size [[threads_per_simdgroup]]".into());
+ }
+
let wrapper_arguments = wrapper_arguments.join(", ");
let shared_definitions = kernel.arguments.iter().filter_map(|a| match a.argument_type() {
@@ -167,6 +173,7 @@ fn kernel_wrappers(
MetalArgumentType::Threads(_) => {
format!("__dsl_thread_idx.{}", thread_axis_letters.next().unwrap())
},
+ MetalArgumentType::Simd => "Simd { .lane_idx = __dsl_simd_lane_idx, .group_idx = __dsl_simd_group_idx, .group_size = __dsl_simd_group_size }".into(),
})
.collect::<Vec<_>>()
.join(", ")
diff --git a/crates/uzu/src/backends/metal/kernel/definitions.metal b/crates/uzu/src/backends/metal/kernel/definitions.metal
index 8af9c3ce4..12acb43c3 100644
--- a/crates/uzu/src/backends/metal/kernel/definitions.metal
+++ b/crates/uzu/src/backends/metal/kernel/definitions.metal
@@ -489,6 +489,12 @@ static T threadgroup_cooperative_reduce_min(
#define GROUPS(EXPR) DSL_META("dsl.groups", DSL_XSTR(EXPR))
#define THREADS(EXPR) DSL_META("dsl.threads", DSL_XSTR(EXPR))
+struct Simd {
+ uint lane_idx;
+ uint group_idx;
+ uint group_size;
+};
+
// MARK: - Generate Template Kernels
#define generateKernel( \