Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / ml / src / main / rust / examples / report_read_path.rs

report_read_path.rs92 lines · 3.0 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.
16use std::sync::Arc;
17
18use modelrunner::vulkan::context::{self, Context};
19use modelrunner::vulkan::imageprobe;
20
21/// Sweeps per path. Odd, so the median is a sample rather than an average of two.
22const RUNS: usize = 5;
23
24fn 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
35fn 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!(
60 "median storage buffer {b:.2} GB/s texel buffer {t:.2} GB/s {:.2}x",
61 t / b.max(1e-9)
62 );
63 println!(
64 "spread storage {:.2}-{:.2} texel {:.2}-{:.2}",
65 low(&ssbo),
66 high(&ssbo),
67 low(&texel),
68 high(&texel)
69 );
70 println!();
71 println!("The storage-buffer figure is itself a read-bandwidth ceiling over 256 MB, so it is");
72 println!(
73 "the denominator for every kernel number: a shader cannot beat it reading the same way."
74 );
75}
76
77/// The middle sample, or `None` when nothing succeeded. Sorts `values` in place.
78fn median(values: &mut [f64]) -> Option<f64> {
79 if values.is_empty() {
80 return None;
81 }
82 values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
83 values.get(values.len() / 2).copied()
84}
85
86fn low(values: &[f64]) -> f64 {
87 values.iter().copied().fold(f64::INFINITY, f64::min)
88}
89
90fn high(values: &[f64]) -> f64 {
91 values.iter().copied().fold(f64::NEG_INFINITY, f64::max)
92}