Modern Apps git

← Modern-Apps.git

ml: record NLLB/MADLAD decode step at load (first switch 230 -> ~65 ms)

Commit
e2ecf55286d1b24a44c82d94fccf5345280717c2
Author
vayun-mathur <[email protected]>
When
2026-10-25 23:26 UTC (1 month ago)
Parents
805e4d4

Diff

library/ml/src/main/rust/examples/profile_net.rs
diff --git a/library/ml/src/main/rust/examples/profile_net.rs b/library/ml/src/main/rust/examples/profile_net.rsindex 72597a5256..6c33ca704d 100644--- a/library/ml/src/main/rust/examples/profile_net.rs+++ b/library/ml/src/main/rust/examples/profile_net.rs@@ -49,10 +49,12 @@ fn nllb_profile(path: Option<&str>) -> Result<(), String> {     // Decode-first construction when MODELRUNNER_DECODE_FIRST=1: the cold record (one-time     // driver cost, ~130-166 ms for the 280-op decode plan) and the max-size arena allocation     // happen in load, and later switches only re-record (~10-15 ms) with no arena growth.-    let first = if std::env::var("MODELRUNNER_DECODE_FIRST").as_deref() == Ok("1") {-        nllb::Mode::DecodeStep { src_len: SRC_LEN }-    } else {-        nllb::Mode::Encode { len: SRC_LEN }+    // Any other number is the src_len to record the decode plan at, to check the cold cost is per+    // kernel rather than per shape.+    let first = match std::env::var("MODELRUNNER_DECODE_FIRST").ok().and_then(|v| v.parse().ok()) {+        Some(1) => nllb::Mode::DecodeStep { src_len: SRC_LEN },+        Some(src_len) => nllb::Mode::DecodeStep { src_len },+        None => nllb::Mode::Encode { len: SRC_LEN },     };     let mut net = Reshaped::new(         context,
library/ml/src/main/rust/src/bridge_part10.rs
diff --git a/library/ml/src/main/rust/src/bridge_part10.rs b/library/ml/src/main/rust/src/bridge_part10.rsindex 08fa775e2b..c4c984baa3 100644--- a/library/ml/src/main/rust/src/bridge_part10.rs+++ b/library/ml/src/main/rust/src/bridge_part10.rs@@ -183,15 +183,15 @@ fn build_madlad<'l>(         return Err(format!("a tokenizer of {} pieces, not {}", parsed.len(), madlad::VOCAB));     } -    // The smallest legal encoder, immediately replaced: `Net::new` needs a plan and the real shapes-    // are not known until a sentence arrives. `Net::rebuild` only ever grows the arena.-    let net = Reshaped::streamed(+    // Decode step first, then the smallest encoder, for the reason `build_nllb` gives.+    let mut net = Reshaped::streamed(         context::shared()?,         weights.offsets(),         &weights,-        madlad::Mode::Encode { len: SMALLEST },+        madlad::Mode::DecodeStep { src_len: WARM_SRC_LEN },         madlad_plan,     )?;+    net.at(madlad::Mode::Encode { len: SMALLEST })?;     Ok(MadladHandle { net, weights, tokenizer }) } 
library/ml/src/main/rust/src/bridge_part3.rs
diff --git a/library/ml/src/main/rust/src/bridge_part3.rs b/library/ml/src/main/rust/src/bridge_part3.rsindex 0adb4e88fa..243d81e21d 100644--- a/library/ml/src/main/rust/src/bridge_part3.rs+++ b/library/ml/src/main/rust/src/bridge_part3.rs@@ -135,6 +135,11 @@ pub extern "system" fn Java_com_vayunmathur_library_ml_MlNative_createNllb<'l>(     .resolve::<jni::errors::LogErrorAndDefault>() } +/// The source length translation nets record their decode step at during load. Any length+/// warms the kernels; this one also sizes the arena for typical sentences, so the first+/// translation neither re-records cold nor grows it.+const WARM_SRC_LEN: u32 = 64;+ fn build_nllb<'l>(     env: &Env,     file: File,@@ -158,15 +163,18 @@ fn build_nllb<'l>(         return Err(format!("a tokenizer of {} pieces, not {}", parsed.len(), nllb::VOCAB));     } -    // The smallest legal encoder, immediately replaced: `Net::new` needs a plan and the real shapes-    // are not known until a sentence arrives. `Net::rebuild` only ever grows the arena.-    let net = Reshaped::streamed(+    // Recorded on a decode step first, then the smallest encoder: the first recording of each+    // kernel costs the driver ~1.7 ms per dispatch (~155 ms for the decode plan), which would+    // otherwise land on the first translation's switch into decoding. `Net::rebuild` only ever+    // grows the arena, so the real encoder shapes replace the placeholder cheaply.+    let mut net = Reshaped::streamed(         context::shared()?,         weights.offsets(),         &weights,-        nllb::Mode::Encode { len: SMALLEST },+        nllb::Mode::DecodeStep { src_len: WARM_SRC_LEN },         nllb_plan,     )?;+    net.at(nllb::Mode::Encode { len: SMALLEST })?;     Ok(NllbHandle { net, weights, tokenizer }) }