A high-performance inference engine for AI models
wrap metal compilation with a feature
eugene780009cf8c87
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 36d4bcb6a..982dbfdfb 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -8,6 +8,7 @@],"rust-analyzer.check.workspace": false,"rust-analyzer.cargo.allTargets": true,+ "rust-analyzer.cargo.noDefaultFeatures": true,"rust-analyzer.testExplorer": true,"[rust]": {"editor.defaultFormatter": "rust-lang.rust-analyzer",diff --git a/uzu/Cargo.toml b/uzu/Cargo.tomlindex 676f508c9..482b2dc5b 100644--- a/uzu/Cargo.toml+++ b/uzu/Cargo.toml@@ -29,3 +29,7 @@ clap = { version = "4.5.39", features = ["derive"] }is_close = "0.1.3"rand = "0.9.1"serde_json5 = "0.2.1"++[features]+default = ["metal-shaders"]+metal-shaders = []diff --git a/uzu/build.rs b/uzu/build.rsindex c42b52d54..9b849063a 100644--- a/uzu/build.rs+++ b/uzu/build.rs@@ -1,7 +1,17 @@-use std::{collections::HashSet, env, fs, path::{Path, PathBuf}, process::Command, time::SystemTime};-use std::io::Write;+use std::{+ collections::HashSet,+ env, fs,+ io::Write,+ path::{Path, PathBuf},+ process::Command,+ time::SystemTime,+};fn main() {+ // Skip Metal shader compilation when the "metal-shaders" feature is disabled (e.g. during `cargo check --no-default-features`).+ if !cfg!(feature = "metal-shaders") {+ return;+ }// Watch for environment changes (e.g., custom API level) if usedprintln!("cargo:rerun-if-env-changed=MY_API_LEVEL");compile_metal_shaders();@@ -17,9 +27,9 @@ fn compile_metal_shaders() {let sdk = if target_os == "ios" {if target.contains("macabi") || target_env == "macabi" {"maccatalyst"- } else if target.contains("ios") && (- target.contains("86_64") || target_env == "sim"- ) {+ } else if target.contains("ios")+ && (target.contains("86_64") || target_env == "sim")+ {// Use iPhoneSimulator SDK for simulator targets (x86_64 or explicit 'sim')"iphonesimulator"} else {@@ -28,7 +38,9 @@ fn compile_metal_shaders() {} else if target_os == "macos" {"macosx"} else {- println!("cargo:warning=Not an Apple platform, skipping Metal shader compilation");+ println!(+ "cargo:warning=Not an Apple platform, skipping Metal shader compilation"+ );return;};@@ -50,22 +62,37 @@ fn compile_metal_shaders() {fs::create_dir_all(&metallib_dir).unwrap();// Require xcrun (should be present on macOS with Xcode)- if !Command::new("xcrun").arg("--version").status().map(|s| s.success()).unwrap_or(false) {- panic!("xcrun not found. Please install Xcode command-line tools to compile Metal shaders.");+ if !Command::new("xcrun")+ .arg("--version")+ .status()+ .map(|s| s.success())+ .unwrap_or(false)+ {+ panic!(+ "xcrun not found. Please install Xcode command-line tools to compile Metal shaders."+ );}// Platform-specific metallib path (separate for macos vs ios for caching)- let platform_suffix = if sdk == "macosx" || sdk == "maccatalyst" { "macos" } else { "ios" };- let metallib_path = metallib_dir.join(format!("shaders_{}.metallib", platform_suffix));+ let platform_suffix = if sdk == "macosx" || sdk == "maccatalyst" {+ "macos"+ } else {+ "ios"+ };+ let metallib_path =+ metallib_dir.join(format!("shaders_{}.metallib", platform_suffix));// Decide whether to compile (if metallib missing or any source changed)- let should_compile = needs_compilation(&metal_files, &metallib_path, &target);+ let should_compile =+ needs_compilation(&metal_files, &metallib_path, &target);if should_compile {compile_metal_files(&metal_files, &metallib_dir, sdk, &src_dir);// Record the target triple for which we built the metalliblet _ = fs::write(metallib_path.with_extension("target"), &target);} else {- println!("cargo:info=Metal shaders are up-to-date; skipping compilation");+ println!(+ "cargo:info=Metal shaders are up-to-date; skipping compilation"+ );}// Read the (new or existing) metallib bytes@@ -93,7 +120,8 @@ fn find_metal_files(dir: &Path) -> Vec<PathBuf> {let path = entry.path();if path.is_dir() {files.extend(find_metal_files(&path));- } else if path.extension().and_then(|s| s.to_str()) == Some("metal") {+ } else if path.extension().and_then(|s| s.to_str()) == Some("metal")+ {files.push(path);}}@@ -102,22 +130,35 @@ fn find_metal_files(dir: &Path) -> Vec<PathBuf> {}// Check timestamps and target to determine if recompilation is needed-fn needs_compilation(metal_files: &[PathBuf], metallib_path: &Path, target: &str) -> bool {+fn needs_compilation(+ metal_files: &[PathBuf],+ metallib_path: &Path,+ target: &str,+) -> bool {// If output metallib doesn't exist, need to compileif !metallib_path.exists() {return true;}// If the previously built target differs (target changed, e.g. arch or OS)- if let Ok(prev_target) = fs::read_to_string(metallib_path.with_extension("target")) {+ if let Ok(prev_target) =+ fs::read_to_string(metallib_path.with_extension("target"))+ {if prev_target != target {- println!("cargo:warning=Target changed from {} to {}, recompiling shaders", prev_target, target);+ println!(+ "cargo:warning=Target changed from {} to {}, recompiling shaders",+ prev_target, target+ );return true;}}// If any source file is newer than the metallib, recompile- let metallib_mtime = fs::metadata(metallib_path).and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH);+ let metallib_mtime = fs::metadata(metallib_path)+ .and_then(|m| m.modified())+ .unwrap_or(SystemTime::UNIX_EPOCH);for metal_file in metal_files {- if let Ok(src_mtime) = fs::metadata(metal_file).and_then(|m| m.modified()) {+ if let Ok(src_mtime) =+ fs::metadata(metal_file).and_then(|m| m.modified())+ {if src_mtime > metallib_mtime {return true;}@@ -129,7 +170,12 @@ fn needs_compilation(metal_files: &[PathBuf], metallib_path: &Path, target: &strfalse}-fn compile_metal_files(metal_files: &[PathBuf], out_dir: &Path, sdk: &str, src_dir: &Path) {+fn compile_metal_files(+ metal_files: &[PathBuf],+ out_dir: &Path,+ sdk: &str,+ src_dir: &Path,+) {// Create temp directory for .air fileslet air_dir = out_dir.join("air");fs::create_dir_all(&air_dir).unwrap();@@ -148,7 +194,7 @@ fn compile_metal_files(metal_files: &[PathBuf], out_dir: &Path, sdk: &str, src_dlet metal_opt_flag = match opt.as_str() {"0" => "-O0","1" => "-O1",- _ => "-O2", // treat levels 2,3,s,z as O2 for metal+ _ => "-O2", // treat levels 2,3,s,z as O2 for metal};// Platform-specific min version flags (Metal 3.1 requires macOS 14 / iOS 17)@@ -163,15 +209,21 @@ fn compile_metal_files(metal_files: &[PathBuf], out_dir: &Path, sdk: &str, src_dfor metal_file in metal_files {let name = metal_file.file_stem().unwrap().to_str().unwrap();// Name .air with platform prefix to avoid collisions- let prefix = if sdk == "macosx" || sdk == "maccatalyst" { "macos_" } else { "ios_" };+ let prefix = if sdk == "macosx" || sdk == "maccatalyst" {+ "macos_"+ } else {+ "ios_"+ };let air_path = air_dir.join(format!("{}{}.air", prefix, name));air_files.push(air_path.clone());// Invoke Metal compilerlet mut cmd = Command::new("xcrun");cmd.args(&["-sdk", sdk, "metal", metal_opt_flag]);- cmd.arg(format!("-std={}", "metal3.1")); // target Metal 3.1 shading language- for flag in &platform_flags { cmd.arg(flag); }+ cmd.arg(format!("-std={}", "metal3.1")); // target Metal 3.1 shading language+ for flag in &platform_flags {+ cmd.arg(flag);+ }for inc in &include_dirs {cmd.args(&["-I", inc.to_str().unwrap()]);}@@ -181,23 +233,49 @@ fn compile_metal_files(metal_files: &[PathBuf], out_dir: &Path, sdk: &str, src_dcmd.args(&["-frecord-sources", "-fpreserve-invariance"]);}}- cmd.args(&["-c", metal_file.to_str().unwrap(), "-o", air_path.to_str().unwrap()]);+ cmd.args(&[+ "-c",+ metal_file.to_str().unwrap(),+ "-o",+ air_path.to_str().unwrap(),+ ]);- println!("cargo:info=Running Metal compiler: {}", cmd.get_args().map(|a| a.to_string_lossy()).collect::<Vec<_>>().join(" "));+ println!(+ "cargo:info=Running Metal compiler: {}",+ cmd.get_args()+ .map(|a| a.to_string_lossy())+ .collect::<Vec<_>>()+ .join(" ")+ );if !cmd.status().expect("Failed to run metal compiler").success() {- panic!("Metal shader compilation failed for {}", metal_file.display());+ panic!(+ "Metal shader compilation failed for {}",+ metal_file.display()+ );}}// Link all .air files into a single .metallib library- let metallib_path = out_dir.join(if sdk == "macosx" || sdk == "maccatalyst" { "shaders_macos.metallib" } else { "shaders_ios.metallib" });+ let metallib_path =+ out_dir.join(if sdk == "macosx" || sdk == "maccatalyst" {+ "shaders_macos.metallib"+ } else {+ "shaders_ios.metallib"+ });let mut lib_cmd = Command::new("xcrun");lib_cmd.args(&["-sdk", sdk, "metallib"]);for air in &air_files {lib_cmd.arg(air);}lib_cmd.args(&["-o", metallib_path.to_str().unwrap()]);- println!("cargo:info=Linking Metal libraries: {}", lib_cmd.get_args().map(|a| a.to_string_lossy()).collect::<Vec<_>>().join(" "));+ println!(+ "cargo:info=Linking Metal libraries: {}",+ lib_cmd+ .get_args()+ .map(|a| a.to_string_lossy())+ .collect::<Vec<_>>()+ .join(" ")+ );if !lib_cmd.status().expect("Failed to run metallib linker").success() {panic!("Failed to link Metal shader libraries");}@@ -206,10 +284,16 @@ fn compile_metal_files(metal_files: &[PathBuf], out_dir: &Path, sdk: &str, src_dfor air in air_files {let _ = fs::remove_file(air);}- println!("cargo:info=Metal shaders compiled successfully to {}", metallib_path.display());+ println!(+ "cargo:info=Metal shaders compiled successfully to {}",+ metallib_path.display()+ );}-fn write_metallib_as_bytes(metallib_data: &[u8], out_path: &Path) {+fn write_metallib_as_bytes(+ metallib_data: &[u8],+ out_path: &Path,+) {let mut f = fs::File::create(out_path).expect("Cannot create metal_lib.rs");writeln!(f, "// AUTO-GENERATED Metal library byte array").unwrap();writeln!(f, "pub const METAL_LIBRARY_DATA: &[u8] = &[").unwrap();