hyperlattice 0.6.0

A small Rust linear algebra library with hyperreal-backed scalar, vector, and matrix types.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#[cfg(feature = "hyperreal-dispatch-trace")]
mod enabled {
    use std::collections::BTreeMap;
    use std::fs;
    use std::hint::black_box;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Mutex, OnceLock};

    static ROWS: OnceLock<Mutex<BTreeMap<String, hyperreal::dispatch_trace::TraceSnapshot>>> =
        OnceLock::new();
    static MATRIX_PROFILE_ROWS: OnceLock<Mutex<Vec<MatrixProfileRow>>> = OnceLock::new();
    static TRACE_RUN: AtomicBool = AtomicBool::new(false);
    static TRACE_FILTER: OnceLock<Vec<String>> = OnceLock::new();

    #[derive(Clone, Debug, Default)]
    struct ScalarOpProfile {
        adds: u64,
        subs: u64,
        muls: u64,
        divs: u64,
        inverses: u64,
        signed_product_sums: u64,
    }

    #[derive(Clone, Debug)]
    struct MatrixProfileRow {
        matrix: String,
        kernel: String,
        input: String,
        calls: u64,
        scalar_ops: ScalarOpProfile,
        rational_stats: hyperreal::dispatch_trace::RationalTraceStats,
        constructor_events: u64,
    }

    fn rows() -> &'static Mutex<BTreeMap<String, hyperreal::dispatch_trace::TraceSnapshot>> {
        ROWS.get_or_init(|| Mutex::new(BTreeMap::new()))
    }

    fn matrix_profile_rows() -> &'static Mutex<Vec<MatrixProfileRow>> {
        MATRIX_PROFILE_ROWS.get_or_init(|| Mutex::new(Vec::new()))
    }

    fn should_capture(name: &str) -> bool {
        let filters = TRACE_FILTER.get_or_init(Vec::new);
        if filters.is_empty() {
            return true;
        }

        filters.iter().any(|filter| name.contains(filter))
    }

    pub fn begin_trace_run(filter: Option<&str>) {
        rows()
            .lock()
            .expect("dispatch trace rows lock poisoned")
            .clear();
        matrix_profile_rows()
            .lock()
            .expect("matrix profile rows lock poisoned")
            .clear();
        if let Some(raw_filter) = filter {
            let filters = raw_filter
                .split(',')
                .map(str::trim)
                .filter(|filter| !filter.is_empty())
                .map(str::to_owned)
                .collect::<Vec<String>>();
            let _ = TRACE_FILTER.set(filters);
        } else {
            let _ = TRACE_FILTER.set(Vec::new());
        }
        TRACE_RUN.store(true, Ordering::Relaxed);
    }

    pub fn trace_row(name: impl Into<String>, sample: impl FnOnce()) {
        if !TRACE_RUN.load(Ordering::Relaxed) {
            return;
        }

        let name = name.into();
        if !should_capture(&name) {
            return;
        }

        hyperreal::dispatch_trace::reset();
        hyperreal::dispatch_trace::with_recording(|| {
            sample();
        });
        let trace = hyperreal::dispatch_trace::take_trace();
        if trace.dispatch.is_empty() {
            return;
        }

        rows()
            .lock()
            .expect("dispatch trace rows lock poisoned")
            .insert(name.into(), trace);
    }

    pub fn trace_cases<T>(name: impl Into<String>, cases: &[T], mut sample: impl FnMut(&T)) {
        let name = name.into();
        if !should_capture(&name) {
            return;
        }

        trace_row(name, || {
            for case in cases {
                sample(black_box(case));
            }
        });
    }

    fn scalar_op_profile(counts: &[hyperreal::dispatch_trace::DispatchCount]) -> ScalarOpProfile {
        let mut profile = ScalarOpProfile::default();
        for count in counts
            .iter()
            .filter(|count| count.layer == "hyperlattice")
        {
            match (count.operation, count.path) {
                ("scalar_op", path) if path.starts_with("add-") => profile.adds += count.count,
                ("scalar_op", path) if path.starts_with("sub-") => profile.subs += count.count,
                ("scalar_op", path) if path.starts_with("mul-") => profile.muls += count.count,
                ("scalar_op", path) if path.starts_with("div-") => profile.divs += count.count,
                ("scalar_fast_path", "add-cached") => profile.adds += count.count,
                ("scalar_fast_path", "sub-cached") => profile.subs += count.count,
                ("scalar_fast_path", "mul-cached") => profile.muls += count.count,
                ("scalar_fast_path", "dot3-active" | "dot3-same-active") => {
                    profile.muls += count.count * 3;
                    profile.adds += count.count * 2;
                    profile.signed_product_sums += count.count;
                }
                ("scalar_fast_path", "dot4-active" | "dot4-same-active") => {
                    profile.muls += count.count * 4;
                    profile.adds += count.count * 3;
                    profile.signed_product_sums += count.count;
                }
                ("scalar_fast_path", "linear-combination3-specialized") => {
                    profile.muls += count.count * 3;
                    profile.adds += count.count * 2;
                }
                ("scalar_fast_path", "linear-combination3-active") => {
                    profile.muls += count.count * 3;
                    profile.adds += count.count * 2;
                    profile.signed_product_sums += count.count;
                }
                ("scalar_fast_path", "linear-combination4-specialized") => {
                    profile.muls += count.count * 4;
                    profile.adds += count.count * 3;
                }
                ("scalar_fast_path", "linear-combination4-active") => {
                    profile.muls += count.count * 4;
                    profile.adds += count.count * 3;
                    profile.signed_product_sums += count.count;
                }
                ("scalar_fast_path", "affine-combination3-specialized") => {
                    profile.muls += count.count * 3;
                    profile.adds += count.count * 3;
                }
                ("scalar_fast_path", "affine-combination4-specialized") => {
                    profile.muls += count.count * 4;
                    profile.adds += count.count * 4;
                }
                ("scalar_method", "inverse-owned" | "inverse-ref") => {
                    profile.inverses += count.count;
                }
                ("scalar_fast_path", path) if path.starts_with("signed-product-sum2-") => {
                    profile.signed_product_sums += count.count;
                    match path {
                        "signed-product-sum2-single-term" => profile.muls += count.count,
                        "signed-product-sum2-sparse-two" => {
                            profile.muls += count.count * 2;
                            profile.adds += count.count;
                        }
                        _ => {}
                    }
                }
                _ => {}
            }
        }
        profile
    }

    fn constructor_events(counts: &[hyperreal::dispatch_trace::DispatchCount]) -> u64 {
        counts
            .iter()
            .filter(|count| count.operation.contains("constructor"))
            .map(|count| count.count)
            .sum()
    }

    pub fn trace_matrix_profile_row(
        matrix: &'static str,
        kernel: &'static str,
        input: &'static str,
        calls: usize,
        sample: impl FnOnce(),
    ) {
        if !TRACE_RUN.load(Ordering::Relaxed) {
            return;
        }

        let name = format!("matrix_profile_row::{matrix}/{kernel}/{input}");
        if !should_capture(&name) {
            return;
        }

        hyperreal::dispatch_trace::reset();
        hyperreal::dispatch_trace::with_recording(|| {
            sample();
        });
        let trace = hyperreal::dispatch_trace::take_trace();
        let row = MatrixProfileRow {
            matrix: matrix.to_owned(),
            kernel: kernel.to_owned(),
            input: input.to_owned(),
            calls: calls as u64,
            scalar_ops: scalar_op_profile(&trace.dispatch),
            rational_stats: trace.rational,
            constructor_events: constructor_events(&trace.dispatch),
        };

        matrix_profile_rows()
            .lock()
            .expect("matrix profile rows lock poisoned")
            .push(row);
    }

    fn per_call(value: u64, calls: u64) -> String {
        if calls == 0 {
            return "n/a".to_owned();
        }
        let value = value as f64 / calls as f64;
        if (value - value.round()).abs() < 0.005 {
            format!("{value:.0}")
        } else if value >= 10.0 {
            format!("{value:.2}")
        } else {
            format!("{value:.3}")
        }
    }

    fn common_factor_distribution(
        stats: hyperreal::dispatch_trace::RationalTraceStats,
        calls: u64,
    ) -> String {
        let buckets = stats.common_factors;
        format!(
            "none={}, pow2={}, <=8b={}, <=64b={}, >64b={}",
            per_call(buckets.none, calls),
            per_call(buckets.power_of_two, calls),
            per_call(buckets.small, calls),
            per_call(buckets.medium, calls),
            per_call(buckets.large, calls)
        )
    }

    fn input_rank(input: &str) -> u8 {
        match input {
            "from-f64" => 0,
            "rational" => 1,
            _ => 2,
        }
    }

    pub fn write_report() {
        let rows = rows().lock().expect("dispatch trace rows lock poisoned");
        let matrix_rows = matrix_profile_rows()
            .lock()
            .expect("matrix profile rows lock poisoned");
        if rows.is_empty() && matrix_rows.is_empty() {
            return;
        }

        let mut out = String::new();
        out.push_str("# Hyperreal Dispatch Trace\n\n");
        out.push_str("Generated by running `cargo bench --bench mathbench --features hyperreal-dispatch-trace -- --write-dispatch-trace-md`. Each row is sampled outside Criterion's measured loop. Use the default benchmark build for timing comparisons; the trace feature intentionally compiles diagnostic hooks into hyperreal.\n\n");

        if !matrix_rows.is_empty() {
            out.push_str("## Matrix Kernel Profile\n\n");
            out.push_str("Per-call values are one unmeasured sample pass divided by the sampled calls. `dot3`/`dot4` and `linear`/`affine` fast paths are expanded into their scalar add/mul counts. `Signed product sums` counts the short determinant/cofactor polynomial hooks that delay exact-rational canonicalization. Common-factor buckets are rational reduction events per call; `pow2` is the dyadic shift-only path.\n\n");
            out.push_str("| Matrix | Kernel | Input | Calls | Real +/call | Real -/call | Real */call | Real div/call | Real inv/call | Signed product sums/call | Rational reductions/call | GCDs/call | Temps/ctors/call | Peak operand bits | Common factors/call |\n");
            out.push_str("| --- | --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | --- |\n");
            let mut rows = matrix_rows.clone();
            rows.sort_by(|left, right| {
                (
                    left.matrix.as_str(),
                    left.kernel.as_str(),
                    input_rank(left.input.as_str()),
                )
                    .cmp(&(
                        right.matrix.as_str(),
                        right.kernel.as_str(),
                        input_rank(right.input.as_str()),
                    ))
            });
            for row in rows {
                let temp_events = row.constructor_events + row.rational_stats.temporary_rationals;
                out.push_str(&format!(
                    "| `{}` | `{}` | `{}` | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} |\n",
                    row.matrix,
                    row.kernel,
                    row.input,
                    row.calls,
                    per_call(row.scalar_ops.adds, row.calls),
                    per_call(row.scalar_ops.subs, row.calls),
                    per_call(row.scalar_ops.muls, row.calls),
                    per_call(row.scalar_ops.divs, row.calls),
                    per_call(row.scalar_ops.inverses, row.calls),
                    per_call(row.scalar_ops.signed_product_sums, row.calls),
                    per_call(row.rational_stats.reductions, row.calls),
                    per_call(row.rational_stats.gcds, row.calls),
                    per_call(temp_events, row.calls),
                    row.rational_stats.peak_operand_bits,
                    common_factor_distribution(row.rational_stats, row.calls),
                ));
            }
            out.push('\n');
        }

        if !rows.is_empty() {
            out.push_str("## Correlation Summary\n\n");
            out.push_str("This table groups raw cross-stack labels into Yap-aligned diagnostic buckets. It is a report aid, not a correctness certificate; raw paths below remain the detailed source.\n\n");
            out.push_str("| Benchmark Row | Dispatch | Predicate | Linear Algebra | Object Facts | Scalar Facts | Detailed Facts | Unknown Facts | Rational Kinds | Sign/Zero Queries | Exact Reducers | Approximation | Approx Starts | Approx Cache | Refinement | Predicate Stages | Cache | Fallback/Abort | Rational Temps | Rational Reductions | Rational GCDs |\n");
            out.push_str("| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |\n");
            for (row, trace) in rows.iter() {
                let summary = trace.correlation_summary();
                out.push_str(&format!(
                    "| `{}` | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} |\n",
                    row,
                    summary.dispatch_events,
                    summary.predicate_events,
                    summary.linear_algebra_events,
                    summary.object_fact_events,
                    summary.scalar_fact_events,
                    summary.detailed_fact_events,
                    summary.unknown_fact_events,
                    summary.exact_rational_kind_events,
                    summary.sign_or_zero_query_events,
                    summary.exact_reducer_events,
                    summary.approximation_events,
                    summary.approximation_start_events,
                    summary.approximation_cache_events,
                    summary.refinement_events,
                    summary.predicate_decision_stage_events,
                    summary.cache_events,
                    summary.fallback_or_abort_events,
                    summary.rational_temporaries,
                    summary.rational_reductions,
                    summary.rational_gcds
                ));
            }
            out.push('\n');

            out.push_str("## Dispatch Counts\n\n");
            out.push_str("| Benchmark Row | Layer | Operation | Path | Count |\n");
            out.push_str("| --- | --- | --- | --- | ---: |\n");
            for (row, trace) in rows.iter() {
                for count in &trace.dispatch {
                    out.push_str(&format!(
                        "| `{}` | `{}` | `{}` | `{}` | {} |\n",
                        row, count.layer, count.operation, count.path, count.count
                    ));
                }
            }
        }

        if let Err(error) = fs::write("dispatch_trace.md", out) {
            eprintln!("failed to update dispatch_trace.md: {error}");
        }
    }
}

#[cfg(not(feature = "hyperreal-dispatch-trace"))]
mod enabled {
    pub fn begin_trace_run(_filter: Option<&str>) {}

    pub fn trace_row(name: impl Into<String>, sample: impl FnOnce()) {
        let _ = name;
        let _ = sample;
    }

    pub fn trace_cases<T>(name: impl Into<String>, cases: &[T], sample: impl FnMut(&T)) {
        let _ = name;
        let _ = cases;
        let _ = sample;
    }

    pub fn trace_matrix_profile_row(
        matrix: &'static str,
        kernel: &'static str,
        input: &'static str,
        calls: usize,
        sample: impl FnOnce(),
    ) {
        let _ = matrix;
        let _ = kernel;
        let _ = input;
        let _ = calls;
        let _ = sample;
    }

    pub fn write_report() {}
}

use enabled::{
    trace_cases as trace_dispatch_cases, trace_matrix_profile_row, trace_row as trace_dispatch_row,
};

fn begin_dispatch_trace_run(filter: Option<&str>) {
    enabled::begin_trace_run(filter);
}

fn write_dispatch_trace_report() {
    enabled::write_report();
}