Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

simplify the cpu kernel binding generator

eugene06093f27641c
diff --git a/crates/uzu-engine/build/cpu/compiler.rs b/crates/uzu-engine/build/cpu/compiler.rs
index a6e4f2198..23fbdc879 100644
--- a/crates/uzu-engine/build/cpu/compiler.rs
+++ b/crates/uzu-engine/build/cpu/compiler.rs
@@ -21,17 +21,17 @@ use crate::common::{
};
#[derive(PartialEq, Debug)]
-pub enum FunctionArgumentType {
+enum FunctionArgumentType {
Pointer(KernelBufferAccess),
Value(Type),
Specialization(Type),
}
#[derive(PartialEq, Debug)]
-pub struct FunctionArgument {
- pub name: Ident,
- pub conditional: Option<Expr>,
- pub ty: FunctionArgumentType,
+struct FunctionArgument {
+ name: Ident,
+ conditional: Option<Expr>,
+ ty: FunctionArgumentType,
}
impl FunctionArgument {
@@ -70,17 +70,16 @@ impl FunctionArgument {
&self,
gpu_type_paths: &GpuTypePaths,
) -> Option<KernelParameter> {
- Some(KernelParameter {
- name: self.name.to_string().into_boxed_str(),
- ty: match &self.ty {
- FunctionArgumentType::Specialization(ty) => {
- KernelParameterType::Value(canonicalize_type_text(ty, gpu_type_paths).into_boxed_str())
- },
- _ => {
- return None;
- },
- },
- })
+ let FunctionArgumentType::Specialization(ty) = &self.ty else {
+ return None;
+ };
+ Some(
+ FunctionParameter {
+ name: self.name.clone(),
+ ty: FunctionParameterType::Value(ty.clone()),
+ }
+ .to_kernel_parameter(gpu_type_paths),
+ )
}
}
@@ -113,15 +112,15 @@ fn canonicalize_type_text(
}
#[derive(PartialEq, Debug)]
-pub enum FunctionParameterType {
+enum FunctionParameterType {
Type,
Value(Type),
}
#[derive(PartialEq, Debug)]
-pub struct FunctionParameter {
- pub name: Ident,
- pub ty: FunctionParameterType,
+struct FunctionParameter {
+ name: Ident,
+ ty: FunctionParameterType,
}
impl FunctionParameter {
@@ -465,49 +464,39 @@ impl CpuCompiler {
quote! { self::#function_ident }
};
- let function_call_args = function_arguments.iter().map(|argument| {
- let argument_ident = &argument.name;
-
- match &argument.ty {
- FunctionArgumentType::Pointer(_) => {
- if argument.conditional.is_some() {
- quote! { #argument_ident.map(|p| p.as_ptr() as _) }
- } else {
- quote! { #argument_ident.as_ptr() as _ }
- }
- },
- FunctionArgumentType::Value(Type::Reference(_)) => quote! { &*#argument_ident },
- FunctionArgumentType::Value(_) | FunctionArgumentType::Specialization(_) => {
- quote! { #argument_ident }
- },
- }
- });
-
- let function_call_args_joined = function_call_args.fold(quote! {}, |a, b| {
- if !a.is_empty() && !b.is_empty() {
- quote! {#a , #b}
- } else {
- quote! {#a #b}
- }
- });
+ let function_call_args = function_arguments
+ .iter()
+ .map(|argument| {
+ let argument_ident = &argument.name;
+
+ match &argument.ty {
+ FunctionArgumentType::Pointer(_) => {
+ if argument.conditional.is_some() {
+ quote! { #argument_ident.map(|p| p.as_ptr() as _) }
+ } else {
+ quote! { #argument_ident.as_ptr() as _ }
+ }
+ },
+ FunctionArgumentType::Value(Type::Reference(_)) => quote! { &*#argument_ident },
+ FunctionArgumentType::Value(_) | FunctionArgumentType::Specialization(_) => {
+ quote! { #argument_ident }
+ },
+ }
+ })
+ .collect::<Vec<_>>();
quote! {
- command_buffer.push_command(move || #monomorphized_function(#function_call_args_joined));
+ command_buffer.push_command(move || #monomorphized_function(#(#function_call_args),*));
}
};
let encode_body = if !function_parameters.is_empty() {
- let mut parameter_idents = function_parameters.iter().map(|p| p.name.clone()).fold(quote! {}, |a, b| {
- if !a.is_empty() {
- quote! {#a , self.#b}
- } else {
- quote! {self.#b}
- }
- });
-
- if function_parameters.len() > 1 {
- parameter_idents = quote! { (#parameter_idents) };
- }
+ let parameter_fields = function_parameters.iter().map(|p| &p.name).collect::<Vec<_>>();
+ let parameter_idents = if function_parameters.len() > 1 {
+ quote! { (#(self.#parameter_fields),*) }
+ } else {
+ quote! { #(self.#parameter_fields),* }
+ };
let variant_value_strs: Vec<String> = function_variants
.iter()
@@ -554,27 +543,13 @@ impl CpuCompiler {
let (match_variants, generic_variants): (Vec<TokenStream>, Vec<TokenStream>) =
variants.into_iter().unzip();
- let mut match_variant = match_variants.iter().fold(quote! {}, |a, b| {
- if !a.is_empty() && !b.is_empty() {
- quote! {#a , #b}
- } else {
- quote! {#a #b}
- }
- });
-
- if match_variants.len() > 1 {
- match_variant = quote! { (#match_variant) };
- }
-
- let generic_variant = generic_variants.iter().fold(quote! {}, |a, b| {
- if !a.is_empty() && !b.is_empty() {
- quote! {#a , #b}
- } else {
- quote! {#a #b}
- }
- });
+ let match_variant = if match_variants.len() > 1 {
+ quote! { (#(#match_variants),*) }
+ } else {
+ quote! { #(#match_variants),* }
+ };
- let encode = make_encode(generic_variant);
+ let encode = make_encode(quote! { #(#generic_variants),* });
quote! { #match_variant => { #encode } }
})