Modern Apps git

← Modern-Apps.git

library:ml: build the vulkan layer on the host, so shaders can run

Commit
7be9829e38c5abc3d1729093bbe23da06d30ab09
Author
vayun-mathur <[email protected]>
When
2026-08-28 16:57 UTC (3 months ago)
Parents
79b1773

Diff

library/ml/src/main/rust/Cargo.toml
diff --git a/library/ml/src/main/rust/Cargo.toml b/library/ml/src/main/rust/Cargo.tomlindex 2c9033ee19..868e8ce8b5 100644--- a/library/ml/src/main/rust/Cargo.toml+++ b/library/ml/src/main/rust/Cargo.toml@@ -11,8 +11,10 @@ # only the ~11 shaders these two nets need. # # Why `ash` and not `androidx.webgpu`: the same reasoning as `library/map` — read the-# header comment there. `features = ["loaded"]` dlopens `libvulkan.so` and links-# nothing, so the only native code shipped is our own.+# header comment there. `features = ["loaded"]` dlopens the platform's Vulkan loader and+# links nothing, so the only native code shipped is our own — and so the same code runs+# on a development host, which is where the shaders get exercised before a phone sees+# them. # # There is no graph interpreter: each net's forward pass is hardcoded Rust in `nets/`, # so a `.maml` file carries ordered tensors and no topology. See `src/weights.rs`.@@ -36,6 +38,6 @@ jni.workspace = true  # `default-features = false` drops the `linked` feature, so nothing links against # libvulkan at build time; `loaded` dlopens it, which is what an Android app must do-# anyway.-[target.'cfg(target_os = "android")'.dependencies]+# anyway. It is not android-gated: `loaded` finds the platform loader on the host too,+# which is the only way the shaders ever get executed before they reach a phone. ash = { version = "0.38", default-features = false, features = ["loaded"] }
library/ml/src/main/rust/src/lib.rs
diff --git a/library/ml/src/main/rust/src/lib.rs b/library/ml/src/main/rust/src/lib.rsindex ed18cf5dae..b889328537 100644--- a/library/ml/src/main/rust/src/lib.rs+++ b/library/ml/src/main/rust/src/lib.rs@@ -11,7 +11,7 @@ //! * [`nets`] — the two hardcoded forward passes, and the small compiler that packs //!   their activations into one arena and resolves every offset. //! * [`preprocess`] — bitmap to fp16 NCHW, and the fp16 conversions.-//! * `vulkan` — device, pipelines, the recorded command buffer. Android only.+//! * [`vulkan`] — device, pipelines, the recorded command buffer. //! //! # GPU only, by decision //!@@ -31,16 +31,21 @@ //! //! # Host builds //!-//! Everything except the Vulkan and JNI layers compiles and tests on the host, so-//! `cargo test` builds **both** networks in full, checks the `.maml` round trip, the-//! arena arithmetic and the preprocessing, with no device and no asset.+//! Everything except the JNI layer compiles and tests on the host, so `cargo test`+//! builds **both** networks in full, checks the `.maml` round trip, the arena arithmetic+//! and the preprocessing, with no device and no asset.+//!+//! [`vulkan`] builds on the host too — `ash`'s `loaded` feature finds whatever Vulkan+//! loader the platform has — which is the only way the shaders get executed anywhere but+//! a phone. Any device-backed test must therefore be `#[ignore]`d, so a host with no+//! Vulkan still passes `cargo test`. On an `x86_64-pc-windows-gnu` host, `libloading`+//! needs a `dlltool` on `PATH` to build; the NDK's `llvm-dlltool` works under that name.  pub mod nets; pub mod post; pub mod preprocess; pub mod weights; -#[cfg(target_os = "android")] pub mod vulkan;  #[cfg(target_os = "android")]
library/ml/src/main/rust/src/vulkan/context.rs
diff --git a/library/ml/src/main/rust/src/vulkan/context.rs b/library/ml/src/main/rust/src/vulkan/context.rsindex 7bdeeb1a4f..65e8c59bd5 100644--- a/library/ml/src/main/rust/src/vulkan/context.rs+++ b/library/ml/src/main/rust/src/vulkan/context.rs@@ -84,7 +84,7 @@ impl Context {     }      unsafe fn create() -> Result<Context, String> {-        let entry = Entry::load().map_err(|e| format!("no libvulkan.so: {e:?}"))?;+        let entry = Entry::load().map_err(|e| format!("no Vulkan loader: {e:?}"))?;          let app_name = CString::new("modelrunner").map_err(|e| format!("{e}"))?;         let app_info = vk::ApplicationInfo::default()
library/ml/src/main/rust/src/vulkan/mod.rs
diff --git a/library/ml/src/main/rust/src/vulkan/mod.rs b/library/ml/src/main/rust/src/vulkan/mod.rsindex b395fb9111..e7fcc58493 100644--- a/library/ml/src/main/rust/src/vulkan/mod.rs+++ b/library/ml/src/main/rust/src/vulkan/mod.rs@@ -1,4 +1,8 @@-//! The Vulkan compute layer. Android only.+//! The Vulkan compute layer.+//!+//! Built on every target, not just Android: `ash`'s `loaded` feature dlopens whatever+//! Vulkan loader the platform provides, so the shaders can be executed on a development+//! host instead of only on a phone. //! //! * [`context`] — instance, device, compute queue, and the fp16 feature chaining that //!   Vulkan 1.1 makes necessary.