Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

cleanup metal dead code

eugene4ebc5b820f9b
diff --git a/crates/uzu-engine/build/metal/ast.rs b/crates/uzu-engine/build/metal/ast.rs
index e7dc1f33e..7ca11b905 100644
--- a/crates/uzu-engine/build/metal/ast.rs
+++ b/crates/uzu-engine/build/metal/ast.rs
@@ -417,16 +417,10 @@ fn parse_argument_type(
bail!("cannot parse c type: {}", c_type);
}
-#[derive(Debug)]
-pub enum MetalTemplateParameterType {
- Type,
- Value(Box<str>),
-}
-
#[derive(Debug)]
pub struct MetalTemplateParameter {
pub name: Box<str>,
- pub ty: MetalTemplateParameterType,
+ pub ty: KernelParameterType,
pub variants: Box<[Box<str>]>,
}
@@ -434,10 +428,7 @@ impl MetalTemplateParameter {
fn to_parameter(&self) -> KernelParameter {
KernelParameter {
name: self.name.clone(),
- ty: match &self.ty {
- MetalTemplateParameterType::Type => KernelParameterType::Type,
- MetalTemplateParameterType::Value(ty) => KernelParameterType::Value(ty.clone()),
- },
+ ty: self.ty.clone(),
}
}
}
@@ -616,8 +607,8 @@ impl MetalKernelInfo {
Ok(MetalTemplateParameter {
name,
ty: match ty {
- None => MetalTemplateParameterType::Type,
- Some(ntt) => MetalTemplateParameterType::Value(MetalArgument::scalar_type_to_rust(
+ None => KernelParameterType::Type,
+ Some(ntt) => KernelParameterType::Value(MetalArgument::scalar_type_to_rust(
ntt.desugared_qual_type.unwrap_or(ntt.qual_type).as_ref(),
)?),
},
diff --git a/crates/uzu-engine/build/metal/bindgen/mod.rs b/crates/uzu-engine/build/metal/bindgen/mod.rs
index d2c4bbdf2..d88b7765b 100644
--- a/crates/uzu-engine/build/metal/bindgen/mod.rs
+++ b/crates/uzu-engine/build/metal/bindgen/mod.rs
@@ -5,7 +5,7 @@ mod specialize;
mod trait_wiring;
mod variants;
-use anyhow::Result;
+use anyhow::{Context, Result};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
@@ -23,7 +23,7 @@ pub fn bindgen(
library_const: &proc_macro2::Ident,
num_shards: usize,
library_compressed: bool,
-) -> Result<(TokenStream, Option<TokenStream>)> {
+) -> Result<TokenStream> {
let kernel_name = kernel.name.as_ref();
let trait_name = format_ident!("{}Kernel", kernel_name);
let struct_name = format_ident!("{}MetalKernel", kernel_name);
@@ -34,7 +34,7 @@ pub fn bindgen(
let mut host_expression_rewriter =
HostExpressionRewriter::new(&variant_binds, gpu_type_paths, specialize_emission.argument_names(), kernel_name);
let argument_emissions = arguments::parse(kernel, gpu_type_paths, &mut host_expression_rewriter)?;
- let trait_wiring = trait_wiring::build(kernel, &trait_name, &struct_name);
+ let trait_wiring = trait_wiring::build(kernel, &trait_name);
let dispatch_emission = dispatch::parse(kernel, &mut host_expression_rewriter)?;
let referenced_parameter_names = host_expression_rewriter.finish();
@@ -132,17 +132,20 @@ pub fn bindgen(
}
};
- Ok((kernel_tokens, trait_wiring.associated_type))
+ Ok(kernel_tokens)
}
pub fn bindgen_global(kernels: &[(impl AsRef<std::path::Path>, &[Kernel])]) -> Result<TokenStream> {
- let includes = kernels.iter().map(|(path, _kernels)| {
- let path = path.as_ref().to_str().expect("bindings path is not utf-8");
-
- quote! {
- include!(#path);
- }
- });
+ let includes = kernels
+ .iter()
+ .map(|(path, _kernels)| {
+ let path = path.as_ref().to_str().context("bindings path is not UTF-8")?;
+
+ anyhow::Ok(quote! {
+ include!(#path);
+ })
+ })
+ .collect::<Result<Vec<_>>>()?;
let associated_types = kernels.iter().flat_map(|(_path, kernels)| kernels.iter()).map(|kernel| {
let trait_name = format_ident!("{}Kernel", kernel.name.as_ref());
diff --git a/crates/uzu-engine/build/metal/bindgen/trait_wiring.rs b/crates/uzu-engine/build/metal/bindgen/trait_wiring.rs
index ba730fe97..48f610154 100644
--- a/crates/uzu-engine/build/metal/bindgen/trait_wiring.rs
+++ b/crates/uzu-engine/build/metal/bindgen/trait_wiring.rs
@@ -7,27 +7,23 @@ use super::super::ast::MetalKernelInfo;
pub struct TraitWiring {
pub trait_implementation_for: TokenStream,
pub associate_backend: TokenStream,
- pub associated_type: Option<TokenStream>,
pub method_visibility: TokenStream,
}
pub fn build(
kernel: &MetalKernelInfo,
trait_name: &Ident,
- struct_name: &Ident,
) -> TraitWiring {
if kernel.public {
TraitWiring {
trait_implementation_for: quote! { crate::backends::common::kernel::#trait_name for },
associate_backend: quote! { type Backend = crate::backends::metal::Metal; },
- associated_type: Some(quote! { type #trait_name = #struct_name; }),
method_visibility: quote! {},
}
} else {
TraitWiring {
trait_implementation_for: quote! {},
associate_backend: quote! {},
- associated_type: None,
method_visibility: quote! { pub(crate) },
}
}
diff --git a/crates/uzu-engine/build/metal/bindgen/variants.rs b/crates/uzu-engine/build/metal/bindgen/variants.rs
index 535b98f68..1c367b18b 100644
--- a/crates/uzu-engine/build/metal/bindgen/variants.rs
+++ b/crates/uzu-engine/build/metal/bindgen/variants.rs
@@ -5,8 +5,8 @@ use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::{Ident, Type};
-use super::super::ast::{MetalKernelInfo, MetalTemplateParameterType};
-use crate::common::gpu_type_paths::GpuTypePaths;
+use super::super::ast::MetalKernelInfo;
+use crate::common::{gpu_type_paths::GpuTypePaths, kernel::KernelParameterType};
pub struct VariantBind {
pub parameter_name: Ident,
@@ -27,8 +27,8 @@ pub fn parse(
let parameter_name = Ident::new(variant.name.as_ref(), Span::call_site());
let field_name = format_ident!("{}", variant.name.as_ref().to_ascii_lowercase());
let parsed_type = match &variant.ty {
- MetalTemplateParameterType::Type => None,
- MetalTemplateParameterType::Value(type_text) => Some(gpu_type_paths.resolve_type(type_text)?),
+ KernelParameterType::Type => None,
+ KernelParameterType::Value(type_text) => Some(gpu_type_paths.resolve_type(type_text)?),
};
Ok(VariantBind {
parameter_name,
diff --git a/crates/uzu-engine/build/metal/compiler.rs b/crates/uzu-engine/build/metal/compiler.rs
index 002fc0744..2abca4dea 100644
--- a/crates/uzu-engine/build/metal/compiler.rs
+++ b/crates/uzu-engine/build/metal/compiler.rs
@@ -13,7 +13,6 @@ use walkdir::WalkDir;
use xxhash_rust::xxh3::xxh3_64;
use super::{
- ast::MetalKernelInfo,
bindgen::bindgen_global,
toolchain::MetalToolchain,
wrapper::{KernelWrappers, VariantWrapper, wrappers},
@@ -73,8 +72,6 @@ struct Cached {
dependency_hashes: HashMap<Box<str>, [u8; blake3::OUT_LEN]>,
public_kernels: Box<[Kernel]>,
has_kernels: bool,
- num_variants: usize,
- num_shards: usize,
}
#[derive(Debug)]
@@ -175,23 +172,17 @@ impl MetalCompiler {
for path in cached.dependency_hashes.keys() {
self.emit_rerun_if_changed_for_dependency(path);
}
- let sharding = if cached.has_kernels {
- format!(" ({} variants / {} shards)", cached.num_variants, cached.num_shards)
- } else {
- Default::default()
- };
- debug_log!("compile cached: {source_path_relative_str}{sharding}");
+ debug_log!("compile cached: {source_path_relative_str}");
return Ok((kernel_path, cached.public_kernels, cached.has_kernels));
}
- let (metal_kernel_infos, dependencies) = self
+ let (kernel_infos, dependencies) = self
.toolchain
.analyze(&source_path)
.with_context(|| format!("cannot analyze {source_path_relative_str}"))?;
- let kernel_infos: Vec<MetalKernelInfo> = metal_kernel_infos.collect();
-
let dependency_hashes = dependencies
+ .into_iter()
.map(|path| {
self.emit_rerun_if_changed_for_dependency(&path);
Ok((
@@ -274,7 +265,6 @@ impl MetalCompiler {
self.metallib_compressed,
)
.with_context(|| format!("cannot generate bindings for {}", kernel.name))
- .map(|(tokens, _associated_type)| tokens)
})
.collect::<anyhow::Result<Vec<_>>>()?;
@@ -295,8 +285,6 @@ impl MetalCompiler {
dependency_hashes,
public_kernels: public_kernels.clone(),
has_kernels,
- num_variants,
- num_shards,
};
fs::write(&cached_file, serde_json::to_vec_pretty(&cached).context("cannot serialize cache")?)
.context("cannot write cache file")?;
diff --git a/crates/uzu-engine/build/metal/toolchain.rs b/crates/uzu-engine/build/metal/toolchain.rs
index a7a0f0f7b..1dbf815dd 100644
--- a/crates/uzu-engine/build/metal/toolchain.rs
+++ b/crates/uzu-engine/build/metal/toolchain.rs
@@ -13,7 +13,7 @@ use tempfile::NamedTempFile;
use super::ast::{MetalAstKind, MetalAstNode, MetalKernelInfo};
#[derive(Debug)]
-pub enum MetalSdk {
+enum MetalSdk {
MacOSX,
MacCatalyst,
IPhoneOS,
@@ -21,11 +21,11 @@ pub enum MetalSdk {
}
impl MetalSdk {
- pub fn from_parts(
- target: &str,
- target_os: &str,
- target_env: &str,
- ) -> anyhow::Result<Self> {
+ fn from_env() -> anyhow::Result<Self> {
+ let target = env::var("TARGET").context("missing TARGET")?;
+ let target_os = env::var("CARGO_CFG_TARGET_OS").context("missing CARGO_CFG_TARGET_OS")?;
+ let target_env = env::var("CARGO_CFG_TARGET_ENV").context("missing CARGO_CFG_TARGET_ENV")?;
+
if target_os == "ios" {
if target.contains("macabi") || target_env == "macabi" {
Ok(Self::MacCatalyst)
@@ -41,17 +41,7 @@ impl MetalSdk {
}
}
- pub fn from_env() -> anyhow::Result<Self> {
- let target = env::var("TARGET").context("missing TARGET")?;
- let target_os = env::var("CARGO_CFG_TARGET_OS").context("missing CARGO_CFG_TARGET_OS")?;
- let target_env = env::var("CARGO_CFG_TARGET_ENV").context("missing CARGO_CFG_TARGET_ENV")?;
-
- let sdk = Self::from_parts(&target, &target_os, &target_env)?;
-
- Ok(sdk)
- }
-
- pub fn to_str(&self) -> &'static str {
+ fn to_str(&self) -> &'static str {
match self {
Self::MacOSX | Self::MacCatalyst => "macosx",
Self::IPhoneOS => "iphoneos",
@@ -59,7 +49,7 @@ impl MetalSdk {
}
}
- pub fn os(&self) -> &'static str {
+ fn os(&self) -> &'static str {
match self {
Self::MacOSX | Self::MacCatalyst => "macosx",
Self::IPhoneOS | Self::IPhoneSimulator => "ios",
@@ -67,29 +57,12 @@ impl MetalSdk {
}
}
-#[derive(Debug)]
-pub enum MetalStd {
- Metal4_0,
-}
-
-impl MetalStd {
- pub fn to_str(&self) -> &'static str {
- match self {
- Self::Metal4_0 => "metal4.0",
- }
- }
-
- pub fn min_os(&self) -> &'static str {
- match self {
- Self::Metal4_0 => "26.4",
- }
- }
-}
+const METAL_STD: &str = "metal4.0";
+const METAL_MIN_OS: &str = "26.4";
#[derive(Debug)]
pub struct MetalToolchain {
sdk: MetalSdk,
- std: MetalStd,
opt_flags: Box<[OsString]>,
extra_options: Box<[OsString]>,
modules_cache_path: PathBuf,
@@ -103,7 +76,6 @@ impl MetalToolchain {
include_dir: PathBuf,
) -> anyhow::Result<Self> {
let sdk = MetalSdk::from_env().context("cannot get sdk")?;
- let std = MetalStd::Metal4_0;
let opt_level_flags = match env::var("OPT_LEVEL").context("missing OPT_LEVEL")?.as_str() {
"0" => vec![OsString::from("-O1")], // matmul kernels compiled with -O0 are broken and require a reboot to unfreeze the os
@@ -122,14 +94,14 @@ impl MetalToolchain {
let opt_flags = [opt_level_flags, debug_flags].concat().into_boxed_slice();
let extra_options: Box<[OsString]> =
- Box::new([OsString::from(format!("-m{}-version-min={}", sdk.os(), std.min_os()))]);
+ Box::new([OsString::from(format!("-m{}-version-min={METAL_MIN_OS}", sdk.os()))]);
let cache_key = {
let mut hasher = blake3::Hasher::new();
hasher.update(sdk.to_str().as_bytes());
hasher.update(b"\0");
- hasher.update(std.to_str().as_bytes());
+ hasher.update(METAL_STD.as_bytes());
hasher.update(b"\0");
for flag in opt_flags.iter().chain(extra_options.iter()) {
hasher.update(flag.as_encoded_bytes());
@@ -156,7 +128,6 @@ impl MetalToolchain {
Ok(Self {
sdk,
- std,
opt_flags,
extra_options,
modules_cache_path,
@@ -188,16 +159,13 @@ impl MetalToolchain {
pub fn analyze(
&self,
path: impl AsRef<Path>,
- ) -> anyhow::Result<(impl Iterator<Item = MetalKernelInfo>, impl Iterator<Item = Box<str>>)> {
+ ) -> anyhow::Result<(Vec<MetalKernelInfo>, Vec<Box<str>>)> {
let path = path.as_ref();
let depfile_path = NamedTempFile::new().context("cannot create temporary file")?;
let mut cmd = self.xcrun();
- cmd.arg("metal")
- .args(["-x", "metal"])
- .arg(format!("-std={}", self.std.to_str()))
- .args(self.extra_options.as_ref());
+ cmd.arg("metal").args(["-x", "metal"]).arg(format!("-std={METAL_STD}")).args(self.extra_options.as_ref());
self.add_common_args(&mut cmd);
@@ -243,10 +211,9 @@ impl MetalToolchain {
.iter()
.flat_map(|(_, d)| d)
.map(|f| f.as_ref().into())
- .collect::<Vec<_>>()
- .into_iter();
+ .collect::<Vec<_>>();
- Ok((kernel_infos.into_iter(), dependencies))
+ Ok((kernel_infos, dependencies))
}
pub fn compile(
@@ -258,7 +225,7 @@ impl MetalToolchain {
let mut cmd = self.xcrun();
cmd.arg("metal")
.args(["-x", "metal"])
- .arg(format!("-std={}", self.std.to_str()))
+ .arg(format!("-std={METAL_STD}"))
.args(self.extra_options.as_ref())
.args(self.opt_flags.as_ref());