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

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

dump_conv_shapes.rs107 lines · 4.4 KiBRaw
1//! Conv geometry inventory for the vision nets, on the host (no device needed).
2//!
3//! ```text
4//! cargo run --offline -p modelrunner --example dump_conv_shapes -- path/to/assets
5//! ```
6//!
7//! Prints every `Kind::Conv` dispatch's geometry and MACs, hottest first, so the
8//! tiled spatial-conv work can pick tile sizes against the real shapes.
9use modelrunner::nets::{
10 maia, mobilefacenet, modnet, ppocr_det, ppocr_rec, scrfd, selfie, tinyclip, u2netp, Kind,
11 Op, Plan,
12};
13use modelrunner::post::ocr::REC_WIDTH;
14use modelrunner::weights::{graph, Weights};
15
16fn main() {
17 let dir = std::env::args()
18 .nth(1)
19 .or_else(|| std::env::var("MODELRUNNER_VISION").ok())
20 .unwrap_or_else(|| ".".into());
21 let mut plans: Vec<(String, Plan)> = Vec::new();
22 let mut open = |name: &str, file: &str, graph_id: u32, build: &dyn Fn(&Weights) -> Result<Plan, String>| {
23 let path = format!("{dir}/{file}");
24 match std::fs::read(&path) {
25 Ok(bytes) => match Weights::parse(&bytes, graph_id) {
26 Ok(w) => match build(&w) {
27 Ok(plan) => plans.push((name.to_string(), plan)),
28 Err(e) => println!("{name}: build failed: {e}"),
29 },
30 Err(e) => println!("{name}: parse failed: {e}"),
31 },
32 Err(e) => println!("{name}: cannot read {path}: {e}"),
33 }
34 };
35 open("selfie", "selfie_segmentation.maml", graph::SELFIE, &|w| selfie::build(w));
36 open("modnet", "modnet.maml", graph::MODNET, &|w| modnet::build(w));
37 open("u2netp", "u2netp.maml", graph::U2NETP, &|w| u2netp::build(w));
38 open("scrfd", "scrfd_500m.maml", graph::SCRFD, &|w| {
39 scrfd::build(w, scrfd::LONG_SIDE, scrfd::LONG_SIDE)
40 });
41 open("mbf", "w600k_mbf.maml", graph::MOBILEFACENET, &|w| mobilefacenet::build(w));
42 open("det", "ppocr_det.maml", graph::PPOCR_DET, &|w| {
43 ppocr_det::build(w, ppocr_det::LONG_SIDE, ppocr_det::LONG_SIDE)
44 });
45 open("rec", "ppocr_rec.maml", graph::PPOCR_REC, &|w| ppocr_rec::build(w, REC_WIDTH));
46 open("clip_image", "clip/tinyclip.maml", graph::TINYCLIP, &|w| {
47 tinyclip::build(w, tinyclip::Mode::Image)
48 });
49 open("maia", "maia3-5m.maml", graph::MAIA, &|w| maia::build(w));
50
51 // (net, step, out_c, out_h, out_w, in_c, kh, kw, stride, dil, group, act, gmac)
52 let mut rows: Vec<(String, usize, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, f64)> =
53 Vec::new();
54 for (name, plan) in &plans {
55 for (step, op) in plan.ops.iter().enumerate() {
56 let Op::Dispatch { kind: Kind::Conv, push, .. } = op else { continue };
57 let macs = push.out_c as u64 * push.out_h as u64 * push.out_w as u64
58 * (push.in_c as u64 / push.group as u64)
59 * push.kh as u64
60 * push.kw as u64;
61 rows.push((
62 name.to_string(),
63 step,
64 push.out_c,
65 push.out_h,
66 push.out_w,
67 push.in_c,
68 push.kh,
69 push.kw,
70 push.stride_h,
71 push.dil_h,
72 push.group,
73 push.act,
74 macs as f64 / 1e9,
75 ));
76 }
77 }
78 rows.sort_by(|a, b| b.12.total_cmp(&a.12));
79 println!(
80 " {:<10} {:>4} {:>9} {:>11} {:>9} {:>3} {:>2} {:>2} {:>3} {:>7}",
81 "net", "step", "out_cxhxw", "in_c", "kxk", "s", "d", "g", "act", "GMAC"
82 );
83 let mut total = 0.0;
84 for (name, step, oc, oh, ow, ic, kh, kw, s, d, g, act, gmac) in &rows {
85 println!(
86 " {name:<10} {step:>4} {oc:>4}x{oh:<4}x{ow:<4} {ic:>6} {kh:>2}x{kw:<2} {s:>2} {d:>2} {g:>4} {act:>3} {gmac:>7.3}",
87 );
88 total += gmac;
89 }
90 println!("total untiled Conv GMAC: {total:.2}");
91 // Per-net dense (group==1) vs grouped/depthwise GMAC, for ranking the work.
92 // rows entries: (name, step, oc, oh, ow, ic, kh, kw, s, d, g, act, gmac)
93 let mut per_net: std::collections::BTreeMap<String, (f64, f64)> =
94 std::collections::BTreeMap::new();
95 for r in &rows {
96 let entry = per_net.entry(r.0.clone()).or_default();
97 if r.10 == 1 {
98 entry.0 += r.12;
99 } else {
100 entry.1 += r.12;
101 }
102 }
103 println!("per net (dense, grouped) GMAC:");
104 for (name, (dense, grouped)) in &per_net {
105 println!(" {name:<10} dense {dense:>7.3} grouped {grouped:>7.3}");
106 }
107}