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

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

dump_conv_shapes.rs112 lines · 4.5 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, Op,
11 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,
23 file: &str,
24 graph_id: u32,
25 build: &dyn Fn(&Weights) -> Result<Plan, String>| {
26 let path = format!("{dir}/{file}");
27 match std::fs::read(&path) {
28 Ok(bytes) => match Weights::parse(&bytes, graph_id) {
29 Ok(w) => match build(&w) {
30 Ok(plan) => plans.push((name.to_string(), plan)),
31 Err(e) => println!("{name}: build failed: {e}"),
32 },
33 Err(e) => println!("{name}: parse failed: {e}"),
34 },
35 Err(e) => println!("{name}: cannot read {path}: {e}"),
36 }
37 };
38 open("selfie", "selfie_segmentation.maml", graph::SELFIE, &|w| selfie::build(w));
39 open("modnet", "modnet.maml", graph::MODNET, &|w| modnet::build(w));
40 open("u2netp", "u2netp.maml", graph::U2NETP, &|w| u2netp::build(w));
41 open("scrfd", "scrfd_500m.maml", graph::SCRFD, &|w| {
42 scrfd::build(w, scrfd::LONG_SIDE, scrfd::LONG_SIDE)
43 });
44 open("mbf", "w600k_mbf.maml", graph::MOBILEFACENET, &|w| mobilefacenet::build(w));
45 open("det", "ppocr_det.maml", graph::PPOCR_DET, &|w| {
46 ppocr_det::build(w, ppocr_det::LONG_SIDE, ppocr_det::LONG_SIDE)
47 });
48 open("rec", "ppocr_rec.maml", graph::PPOCR_REC, &|w| ppocr_rec::build(w, REC_WIDTH));
49 open("clip_image", "clip/tinyclip.maml", graph::TINYCLIP, &|w| {
50 tinyclip::build(w, tinyclip::Mode::Image)
51 });
52 open("maia", "maia3-5m.maml", graph::MAIA, &|w| maia::build(w));
53
54 // (net, step, out_c, out_h, out_w, in_c, kh, kw, stride, dil, group, act, gmac)
55 type ConvRow = (String, usize, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, f64);
56 let mut rows: Vec<ConvRow> = Vec::new();
57 for (name, plan) in &plans {
58 for (step, op) in plan.ops.iter().enumerate() {
59 let Op::Dispatch { kind: Kind::Conv, push, .. } = op else { continue };
60 let macs = push.out_c as u64
61 * push.out_h as u64
62 * push.out_w as u64
63 * (push.in_c as u64 / push.group as u64)
64 * push.kh as u64
65 * push.kw as u64;
66 rows.push((
67 name.to_string(),
68 step,
69 push.out_c,
70 push.out_h,
71 push.out_w,
72 push.in_c,
73 push.kh,
74 push.kw,
75 push.stride_h,
76 push.dil_h,
77 push.group,
78 push.act,
79 macs as f64 / 1e9,
80 ));
81 }
82 }
83 rows.sort_by(|a, b| b.12.total_cmp(&a.12));
84 println!(
85 " {:<10} {:>4} {:>9} {:>11} {:>9} {:>3} {:>2} {:>2} {:>3} {:>7}",
86 "net", "step", "out_cxhxw", "in_c", "kxk", "s", "d", "g", "act", "GMAC"
87 );
88 let mut total = 0.0;
89 for (name, step, oc, oh, ow, ic, kh, kw, s, d, g, act, gmac) in &rows {
90 println!(
91 " {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}",
92 );
93 total += gmac;
94 }
95 println!("total untiled Conv GMAC: {total:.2}");
96 // Per-net dense (group==1) vs grouped/depthwise GMAC, for ranking the work.
97 // rows entries: (name, step, oc, oh, ow, ic, kh, kw, s, d, g, act, gmac)
98 let mut per_net: std::collections::BTreeMap<String, (f64, f64)> =
99 std::collections::BTreeMap::new();
100 for r in &rows {
101 let entry = per_net.entry(r.0.clone()).or_default();
102 if r.10 == 1 {
103 entry.0 += r.12;
104 } else {
105 entry.1 += r.12;
106 }
107 }
108 println!("per net (dense, grouped) GMAC:");
109 for (name, (dense, grouped)) in &per_net {
110 println!(" {name:<10} dense {dense:>7.3} grouped {grouped:>7.3}");
111 }
112}