pub struct Pool { /* private fields */ }Expand description
Persistent thread pool: shared job slot, epoch dispatch, caller participation.
Implementations§
Source§impl Pool
impl Pool
Sourcepub fn new(n_workers: usize) -> Self
pub fn new(n_workers: usize) -> Self
Examples found in repository?
13fn main() {
14 for nt in [2usize, 4, 6, 8, 10] {
15 let pool = Pool::new(nt);
16 let noop: &(dyn Fn(usize, usize) + Sync) = &|_w, _n| {};
17 // Warm: first dispatch spawns/parks the workers.
18 for _ in 0..100 {
19 pool.run(noop);
20 }
21 let iters = 20_000;
22 let t0 = Instant::now();
23 for _ in 0..iters {
24 pool.run(noop);
25 }
26 let el = t0.elapsed().as_secs_f64();
27 let per = el / iters as f64 * 1e6;
28 println!(
29 "threads={nt:2} {per:7.2} us/dispatch -> {:6.2} ms/token at 200 matvecs",
30 per * 200.0 / 1e3
31 );
32 }
33}More examples
21fn main() {
22 let mut args = std::env::args().skip(1);
23 let path = args
24 .next()
25 .expect("usage: matvec_bw <model.cmf> [sweep|one <tensor>]");
26 let mode = args.next().unwrap_or_else(|| "sweep".to_string());
27 let model = Arc::new(cortiq_core::CmfModel::open(&path).expect("open model"));
28
29 // Real LM activations carry a few heavy channels (>8·rms); measured
30 // mean on this model is ~3.7. NOUT models that distribution.
31 let nout: usize = std::env::var("NOUT")
32 .ok()
33 .and_then(|v| v.parse().ok())
34 .unwrap_or(4);
35 let mk_x = |cols: usize| -> Vec<f32> {
36 let mut x: Vec<f32> = (0..cols).map(|i| ((i % 17) as f32 - 8.0) / 8.0).collect();
37 for k in 0..nout {
38 x[k * 37 % cols] = 40.0;
39 }
40 x
41 };
42
43 if mode == "one" {
44 let name = args
45 .next()
46 .unwrap_or_else(|| "model.embed_tokens.weight".to_string());
47 let entry = model.tensor(&name).expect("tensor not found");
48 let (rows, cols) = (entry.shape[0], entry.shape[1]);
49 let nbytes = entry.nbytes as f64;
50 println!(
51 "tensor {name}: {rows}x{cols} {:?} = {:.1} MB, NOUT={nout}",
52 entry.dtype,
53 nbytes / 1e6
54 );
55 let t = QTensor::from_model(&model, &name).expect("wrap");
56 let x = mk_x(cols);
57 let mut out = vec![0f32; rows];
58 t.matvec(&x, &mut out, None);
59 for nt in [1usize, 2, 4, 6, 8, 10] {
60 let pool = if nt == 1 { None } else { Some(Pool::new(nt)) };
61 let iters = 8;
62 t.matvec(&x, &mut out, pool.as_ref());
63 let t0 = Instant::now();
64 for _ in 0..iters {
65 t.matvec(&x, &mut out, pool.as_ref());
66 }
67 let el = t0.elapsed().as_secs_f64();
68 println!(
69 "threads={nt:2} {:6.2} ms/matvec {:6.1} GB/s (sink {:.3})",
70 el / iters as f64 * 1e3,
71 nbytes * iters as f64 / el / 1e9,
72 out[0]
73 );
74 }
75 return;
76 }
77
78 // sweep: every 2-D q8_2f tensor once = one decode's worth of weights.
79 let names: Vec<String> = model
80 .tensors
81 .iter()
82 .filter(|t| t.dtype == TensorDtype::Q8_2f && t.shape.len() == 2)
83 .map(|t| t.name.clone())
84 .collect();
85 let total_bytes: f64 = model
86 .tensors
87 .iter()
88 .filter(|t| t.dtype == TensorDtype::Q8_2f && t.shape.len() == 2)
89 .map(|t| t.nbytes as f64)
90 .sum();
91 println!(
92 "sweep: {} q8_2f tensors, {:.2} GB total (= weights streamed per decode token), NOUT={nout}",
93 names.len(),
94 total_bytes / 1e9
95 );
96
97 let tensors: Vec<(QTensor, Vec<f32>, Vec<f32>)> = names
98 .iter()
99 .map(|n| {
100 let e = model.tensor(n).unwrap();
101 let (rows, cols) = (e.shape[0], e.shape[1]);
102 (
103 QTensor::from_model(&model, n).expect("wrap"),
104 mk_x(cols),
105 vec![0f32; rows],
106 )
107 })
108 .collect();
109 let mut tensors = tensors;
110
111 // Whole-model residency pass BEFORE any timing: the first touch of a
112 // 4.2 GB mmap faults ~260k pages, which would otherwise be charged to
113 // whichever thread count happens to run first. REVERSE=1 flips the
114 // order as a check that no first-touch cost is left in the table.
115 for _ in 0..2 {
116 for (t, x, out) in tensors.iter_mut() {
117 t.matvec(x, out, None);
118 }
119 }
120 let mut counts = vec![1usize, 2, 4, 6, 8, 10];
121 if std::env::var("REVERSE").is_ok() {
122 counts.reverse();
123 }
124 for nt in counts {
125 let pool = if nt == 1 { None } else { Some(Pool::new(nt)) };
126 // one warm pass, then two measured
127 for (t, x, out) in tensors.iter_mut() {
128 t.matvec(x, out, pool.as_ref());
129 }
130 let iters = 2;
131 let t0 = Instant::now();
132 for _ in 0..iters {
133 for (t, x, out) in tensors.iter_mut() {
134 t.matvec(x, out, pool.as_ref());
135 }
136 }
137 let el = t0.elapsed().as_secs_f64();
138 let per_tok = el / iters as f64;
139 println!(
140 "threads={nt:2} {:7.1} ms/sweep {:6.1} GB/s -> weight-path-only ceiling {:5.1} tok/s",
141 per_tok * 1e3,
142 total_bytes * iters as f64 / el / 1e9,
143 1.0 / per_tok
144 );
145 }
146}Sourcepub fn with_spin(n_workers: usize, spin_budget: usize) -> Self
pub fn with_spin(n_workers: usize, spin_budget: usize) -> Self
Explicit spin budget (tests pin it without touching the env).
Sourcepub fn effective_threads() -> usize
pub fn effective_threads() -> usize
The thread count from_env would use RIGHT NOW: forced (C ABI)
CMF_THREADS > big-core topology > available_parallelism−1. ≤1 means the model runs serial (no pool). Introspection (
execution_mode, status endpoints) must report THIS, not available_parallelism.
Sourcepub fn from_env() -> Option<Arc<Self>>
pub fn from_env() -> Option<Arc<Self>>
Pool sized from CMF_THREADS (see module docs). None = serial.
Without the env, heterogeneous ARM defaults to its BIG cores.
Sourcepub fn run_rows(&self, rows: usize, f: &(dyn Fn(usize, usize) + Sync))
pub fn run_rows(&self, rows: usize, f: &(dyn Fn(usize, usize) + Sync))
Run f(row_start, row_end) over 0..rows, self-balancing.
One dispatch, but workers pull row-ranges from a shared cursor instead of each taking a fixed 1/n slice. On a heterogeneous CPU (Apple Silicon: 4 P-cores + 6 E-cores here) a static split makes every matvec end at the SLOWEST core’s pace while the fast ones idle at the barrier; pulling by grain lets a P-core take several chunks for each one an E-core takes, so skew collapses to a single grain. Row ranges stay disjoint and each row’s dot is computed exactly as in the serial path → bit-identical output.
Sourcepub fn run_many(&self, parts: &[(usize, &(dyn Fn(usize, usize) + Sync))])
pub fn run_many(&self, parts: &[(usize, &(dyn Fn(usize, usize) + Sync))])
Multi-matrix job: one dispatch serves SEVERAL row spaces
(roadmap §3 P0 — «одна внешняя публикация job на слой»). Parts
are laid out back-to-back in a virtual row space and pulled by
grain from one shared cursor, so QKV or gate+up cost a single
barrier instead of one each. Each part’s f(start, end) sees its
OWN row indices — per-row math and outputs are bit-identical to
separate run_rows calls.
Sourcepub fn run(&self, f: &(dyn Fn(usize, usize) + Sync))
pub fn run(&self, f: &(dyn Fn(usize, usize) + Sync))
Run f(worker_idx, n_participants) on every worker AND the
calling thread (worker_idx = n_workers() for the caller);
returns when all participants have finished.
Examples found in repository?
13fn main() {
14 for nt in [2usize, 4, 6, 8, 10] {
15 let pool = Pool::new(nt);
16 let noop: &(dyn Fn(usize, usize) + Sync) = &|_w, _n| {};
17 // Warm: first dispatch spawns/parks the workers.
18 for _ in 0..100 {
19 pool.run(noop);
20 }
21 let iters = 20_000;
22 let t0 = Instant::now();
23 for _ in 0..iters {
24 pool.run(noop);
25 }
26 let el = t0.elapsed().as_secs_f64();
27 let per = el / iters as f64 * 1e6;
28 println!(
29 "threads={nt:2} {per:7.2} us/dispatch -> {:6.2} ms/token at 200 matvecs",
30 per * 200.0 / 1e3
31 );
32 }
33}