Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / examples / report_read_path.rs
report_read_path.rs81 lines · 2.9 KiBRaw
| 1 | //! Is a texel-buffer read faster than a storage-buffer read on this device? |
| 2 | //! |
| 3 | //! ```text |
| 4 | //! cargo run --offline --release -p modelrunner --example report_read_path |
| 5 | //! ``` |
| 6 | //! |
| 7 | //! Runs [`modelrunner::vulkan::imageprobe::compare`] several times and reports the median of |
| 8 | //! each path. That function is otherwise reachable only through the JNI entry point |
| 9 | //! `MlNative.imageProbeGemma4`, which needs an app, a JVM and a loaded model; the probe itself |
| 10 | //! needs none of those - it allocates its own 256 MB and synthesises everything else - so this |
| 11 | //! exposes it as a plain binary that runs over `adb shell`. |
| 12 | //! |
| 13 | //! Repeated rather than run once because the first sweep on a cold device is not the steady |
| 14 | //! state, and a single sample cannot show whether a difference between the two paths is larger |
| 15 | //! than the run-to-run spread. |
| 16 | use std::sync::Arc; |
| 17 | |
| 18 | use modelrunner::vulkan::context::{self, Context}; |
| 19 | use modelrunner::vulkan::imageprobe; |
| 20 | |
| 21 | /// Sweeps per path. Odd, so the median is a sample rather than an average of two. |
| 22 | const RUNS: usize = 5; |
| 23 | |
| 24 | fn main() { |
| 25 | let context = match context::shared() { |
| 26 | Ok(context) => context, |
| 27 | Err(why) => { |
| 28 | println!("no usable Vulkan device: {why}"); |
| 29 | return; |
| 30 | } |
| 31 | }; |
| 32 | report(&context); |
| 33 | } |
| 34 | |
| 35 | fn report(context: &Arc<Context>) { |
| 36 | let mut ssbo = Vec::with_capacity(RUNS); |
| 37 | let mut texel = Vec::with_capacity(RUNS); |
| 38 | |
| 39 | println!(" run storage GB/s texel GB/s ratio"); |
| 40 | for run in 1..=RUNS { |
| 41 | match imageprobe::compare(context) { |
| 42 | Ok((buffer, view)) => { |
| 43 | let (b, t) = (buffer / 1e9, view / 1e9); |
| 44 | println!(" {run:3} {b:12.2} {t:10.2} {:6.2}x", t / b.max(1e-9)); |
| 45 | ssbo.push(b); |
| 46 | texel.push(t); |
| 47 | } |
| 48 | Err(why) => println!(" {run:3} probe failed: {why}"), |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | let (Some(b), Some(t)) = (median(&mut ssbo), median(&mut texel)) else { |
| 53 | println!(); |
| 54 | println!("no successful runs"); |
| 55 | return; |
| 56 | }; |
| 57 | |
| 58 | println!(); |
| 59 | println!("median storage buffer {b:.2} GB/s texel buffer {t:.2} GB/s {:.2}x", t / b.max(1e-9)); |
| 60 | println!("spread storage {:.2}-{:.2} texel {:.2}-{:.2}", low(&ssbo), high(&ssbo), low(&texel), high(&texel)); |
| 61 | println!(); |
| 62 | println!("The storage-buffer figure is itself a read-bandwidth ceiling over 256 MB, so it is"); |
| 63 | println!("the denominator for every kernel number: a shader cannot beat it reading the same way."); |
| 64 | } |
| 65 | |
| 66 | /// The middle sample, or `None` when nothing succeeded. Sorts `values` in place. |
| 67 | fn median(values: &mut [f64]) -> Option<f64> { |
| 68 | if values.is_empty() { |
| 69 | return None; |
| 70 | } |
| 71 | values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); |
| 72 | values.get(values.len() / 2).copied() |
| 73 | } |
| 74 | |
| 75 | fn low(values: &[f64]) -> f64 { |
| 76 | values.iter().copied().fold(f64::INFINITY, f64::min) |
| 77 | } |
| 78 | |
| 79 | fn high(values: &[f64]) -> f64 { |
| 80 | values.iter().copied().fold(f64::NEG_INFINITY, f64::max) |
| 81 | } |