fsci-conformance 0.3.0

Differential conformance testing harness for FrankenSciPy
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! P2C-004-H: Performance profiling tests for sparse matrix operations.
//!
//! Produces structured JSON artifacts at:
//!   fixtures/artifacts/FSCI-P2C-004/perf/
//!
//! Covers CSR construction, spmv, format conversion, arithmetic across
//! [100×100 5%, 1000×1000 1%, 10000×10000 0.1%] configurations.

use fsci_sparse::{
    CsrMatrix, FormatConvertible, Shape2D, add_csr, diags, eye, random, scale_csr, spmv_csr,
};
use serde::Serialize;
use std::time::Instant;

const CONFIGS: &[(usize, f64, &str)] = &[
    (100, 0.05, "100x100_d5"),
    (1_000, 0.01, "1000x1000_d1"),
    (10_000, 0.001, "10000x10000_d01"),
];
const SEED: u64 = 0xBEEF_CAFE;
const WARMUP_ITERS: usize = 3;
const BENCH_ITERS: usize = 20;

// ── Data structures ────────────────────────────────────────────────────────────

#[derive(Serialize)]
struct PerfReport {
    generated_at: String,
    operation_benchmarks: Vec<OperationBenchmark>,
    hotspot_ranking: Vec<HotspotEntry>,
    memory_notes: Vec<MemoryNote>,
    isomorphism_check: IsomorphismCheck,
}

#[derive(Serialize)]
struct OperationBenchmark {
    operation: String,
    matrix_desc: String,
    matrix_n: usize,
    nnz: usize,
    iterations: usize,
    median_ns: u128,
    p95_ns: u128,
    min_ns: u128,
    max_ns: u128,
    mean_ns: u128,
}

#[derive(Serialize)]
struct HotspotEntry {
    rank: usize,
    operation: String,
    matrix_desc: String,
    median_ns: u128,
    fraction_of_total: f64,
}

#[derive(Serialize)]
struct MemoryNote {
    operation: String,
    matrix_size: String,
    estimated_input_bytes: usize,
    notes: String,
}

#[derive(Serialize)]
struct IsomorphismCheck {
    all_operations_pass: bool,
    details: Vec<IsomorphismDetail>,
}

#[derive(Serialize)]
struct IsomorphismDetail {
    operation: String,
    passes: bool,
    note: String,
}

// ── Helpers ─────────────────────────────────────────────────────────────────────

fn make_random_csr(n: usize, density: f64) -> CsrMatrix {
    random(Shape2D::new(n, n), density, SEED)
        .expect("random coo")
        .to_csr()
        .expect("coo->csr")
}

fn make_vector(n: usize) -> Vec<f64> {
    (0..n).map(|i| (i as f64) * 0.01 - 0.5).collect()
}

fn time_operation<F: FnMut()>(mut f: F) -> Vec<u128> {
    for _ in 0..WARMUP_ITERS {
        f();
    }
    let mut timings = Vec::with_capacity(BENCH_ITERS);
    for _ in 0..BENCH_ITERS {
        let start = Instant::now();
        f();
        timings.push(start.elapsed().as_nanos());
    }
    timings.sort();
    timings
}

fn compute_stats(timings: &[u128]) -> (u128, u128, u128, u128, u128) {
    let n = timings.len();
    let median = timings[n / 2];
    let p95 = timings[(n as f64 * 0.95) as usize];
    let min_v = timings[0];
    let max_v = timings[n - 1];
    let mean = timings.iter().sum::<u128>() / n as u128;
    (median, p95, min_v, max_v, mean)
}

fn chrono_lite_now() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    format!("unix:{secs}")
}

// ── Main test ──────────────────────────────────────────────────────────────────

#[test]
fn perf_p2c004_full_profile() {
    let mut benchmarks: Vec<OperationBenchmark> = Vec::new();

    // CSR construction from COO
    for &(n, density, label) in CONFIGS {
        let coo = random(Shape2D::new(n, n), density, SEED).expect("random coo");
        let nnz = coo.nnz();
        let timings = time_operation(|| {
            let _ = coo.to_csr().expect("coo->csr");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "coo_to_csr".into(),
            matrix_desc: label.into(),
            matrix_n: n,
            nnz,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // spmv_csr
    for &(n, density, label) in CONFIGS {
        let csr = make_random_csr(n, density);
        let nnz = csr.nnz();
        let vec = make_vector(n);
        let timings = time_operation(|| {
            let _ = spmv_csr(&csr, &vec).expect("spmv");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "spmv_csr".into(),
            matrix_desc: label.into(),
            matrix_n: n,
            nnz,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // CSR -> CSC conversion
    for &(n, density, label) in CONFIGS {
        let csr = make_random_csr(n, density);
        let nnz = csr.nnz();
        let timings = time_operation(|| {
            let _ = csr.to_csc().expect("csr->csc");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "csr_to_csc".into(),
            matrix_desc: label.into(),
            matrix_n: n,
            nnz,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // add_csr
    for &(n, density, label) in CONFIGS {
        let a = make_random_csr(n, density);
        let b = random(Shape2D::new(n, n), density, SEED ^ 0xFF)
            .expect("random b")
            .to_csr()
            .expect("b csr");
        let nnz = a.nnz() + b.nnz();
        let timings = time_operation(|| {
            let _ = add_csr(&a, &b).expect("add");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "add_csr".into(),
            matrix_desc: label.into(),
            matrix_n: n,
            nnz,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // scale_csr
    for &(n, density, label) in CONFIGS {
        let a = make_random_csr(n, density);
        let nnz = a.nnz();
        let timings = time_operation(|| {
            let _ = scale_csr(&a, 2.5).expect("scale");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "scale_csr".into(),
            matrix_desc: label.into(),
            matrix_n: n,
            nnz,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // eye construction
    for &n in &[100usize, 1_000, 10_000] {
        let timings = time_operation(|| {
            let _ = eye(n).expect("eye");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "eye".into(),
            matrix_desc: format!("{n}x{n}"),
            matrix_n: n,
            nnz: n,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // diags (tridiagonal)
    for &n in &[100usize, 1_000, 10_000] {
        let sub = vec![-1.0; n.saturating_sub(1)];
        let main_d = vec![2.0; n];
        let sup = vec![-1.0; n.saturating_sub(1)];
        let timings = time_operation(|| {
            let _ = diags(
                &[sub.clone(), main_d.clone(), sup.clone()],
                &[-1, 0, 1],
                Some(Shape2D::new(n, n)),
            )
            .expect("tridiag");
        });
        let (median, p95, min_v, max_v, mean) = compute_stats(&timings);
        benchmarks.push(OperationBenchmark {
            operation: "diags_tridiag".into(),
            matrix_desc: format!("{n}x{n}"),
            matrix_n: n,
            nnz: 3 * n - 2,
            iterations: BENCH_ITERS,
            median_ns: median,
            p95_ns: p95,
            min_ns: min_v,
            max_ns: max_v,
            mean_ns: mean,
        });
    }

    // ── Hotspot ranking ────────────────────────────────────────────────────────
    // Rank at the largest config (10000x10000)
    let mut largest: Vec<_> = benchmarks.iter().filter(|b| b.matrix_n == 10_000).collect();
    largest.sort_by_key(|b| std::cmp::Reverse(b.median_ns));

    let total_ns: u128 = largest.iter().map(|b| b.median_ns).sum();
    let hotspot_ranking: Vec<HotspotEntry> = largest
        .iter()
        .enumerate()
        .map(|(i, b)| HotspotEntry {
            rank: i + 1,
            operation: b.operation.clone(),
            matrix_desc: b.matrix_desc.clone(),
            median_ns: b.median_ns,
            fraction_of_total: if total_ns > 0 {
                b.median_ns as f64 / total_ns as f64
            } else {
                0.0
            },
        })
        .collect();

    // ── Memory notes ───────────────────────────────────────────────────────────
    let memory_notes = vec![
        MemoryNote {
            operation: "spmv_csr".into(),
            matrix_size: "10000x10000_d01".into(),
            estimated_input_bytes: 10_000 * 10 * 16 + 10_000 * 8,
            notes: "CSR: ~10k nnz × (8B data + 8B index) + 10k indptr. Vector: 80KB.".into(),
        },
        MemoryNote {
            operation: "csr_to_csc".into(),
            matrix_size: "10000x10000_d01".into(),
            estimated_input_bytes: 10_000 * 10 * 16,
            notes: "Transpose allocates new indptr/indices/data arrays of same nnz.".into(),
        },
        MemoryNote {
            operation: "add_csr".into(),
            matrix_size: "10000x10000_d01".into(),
            estimated_input_bytes: 2 * 10_000 * 10 * 16,
            notes: "Two CSR inputs. Output nnz <= sum of input nnz.".into(),
        },
    ];

    // ── Isomorphism check ──────────────────────────────────────────────────────
    let mut iso_details = Vec::new();

    // spmv isomorphism: identity * v = v
    {
        let id = eye(100).expect("eye");
        let v = make_vector(100);
        let result = spmv_csr(&id, &v).expect("spmv");
        let max_err: f64 =
            result
                .iter()
                .zip(&v)
                .map(|(a, b)| (a - b).abs())
                .fold(0.0_f64, |a: f64, b: f64| {
                    if a.is_nan() || b.is_nan() {
                        f64::NAN
                    } else {
                        a.max(b)
                    }
                });
        iso_details.push(IsomorphismDetail {
            operation: "spmv_identity".into(),
            passes: max_err < 1e-12,
            note: format!("max_err={max_err:.2e}"),
        });
    }

    // format roundtrip isomorphism: csr -> csc -> csr preserves data
    {
        let csr = make_random_csr(100, 0.05);
        let roundtrip = csr.to_csc().expect("csr->csc").to_csr().expect("csc->csr");
        let v = make_vector(100);
        let orig = spmv_csr(&csr, &v).expect("spmv orig");
        let rt = spmv_csr(&roundtrip, &v).expect("spmv roundtrip");
        let max_err: f64 =
            orig.iter()
                .zip(&rt)
                .map(|(a, b)| (a - b).abs())
                .fold(0.0_f64, |a: f64, b: f64| {
                    if a.is_nan() || b.is_nan() {
                        f64::NAN
                    } else {
                        a.max(b)
                    }
                });
        iso_details.push(IsomorphismDetail {
            operation: "format_roundtrip".into(),
            passes: max_err < 1e-12,
            note: format!("spmv_diff={max_err:.2e}"),
        });
    }

    // add isomorphism: A + 0 = A
    {
        let a = make_random_csr(100, 0.05);
        let zero = random(Shape2D::new(100, 100), 0.0, 1)
            .expect("zero")
            .to_csr()
            .expect("zero csr");
        let sum = add_csr(&a, &zero).expect("add zero");
        let v = make_vector(100);
        let orig = spmv_csr(&a, &v).expect("spmv a");
        let sum_v = spmv_csr(&sum, &v).expect("spmv sum");
        let max_err: f64 =
            orig.iter()
                .zip(&sum_v)
                .map(|(a, b)| (a - b).abs())
                .fold(0.0_f64, |a: f64, b: f64| {
                    if a.is_nan() || b.is_nan() {
                        f64::NAN
                    } else {
                        a.max(b)
                    }
                });
        iso_details.push(IsomorphismDetail {
            operation: "add_zero_identity".into(),
            passes: max_err < 1e-12,
            note: format!("max_err={max_err:.2e}"),
        });
    }

    // scale isomorphism: scale(A, 1.0) = A
    {
        let a = make_random_csr(100, 0.05);
        let scaled = scale_csr(&a, 1.0).expect("scale 1.0");
        let v = make_vector(100);
        let orig = spmv_csr(&a, &v).expect("spmv a");
        let sc = spmv_csr(&scaled, &v).expect("spmv scaled");
        let max_err: f64 =
            orig.iter()
                .zip(&sc)
                .map(|(a, b)| (a - b).abs())
                .fold(0.0_f64, |a: f64, b: f64| {
                    if a.is_nan() || b.is_nan() {
                        f64::NAN
                    } else {
                        a.max(b)
                    }
                });
        iso_details.push(IsomorphismDetail {
            operation: "scale_identity".into(),
            passes: max_err < 1e-12,
            note: format!("max_err={max_err:.2e}"),
        });
    }

    let all_pass = iso_details.iter().all(|d| d.passes);

    let report = PerfReport {
        generated_at: chrono_lite_now(),
        operation_benchmarks: benchmarks,
        hotspot_ranking,
        memory_notes,
        isomorphism_check: IsomorphismCheck {
            all_operations_pass: all_pass,
            details: iso_details,
        },
    };

    // Write artifact
    let artifact_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("fixtures/artifacts/FSCI-P2C-004/perf");
    std::fs::create_dir_all(&artifact_dir).unwrap();

    let json = serde_json::to_string_pretty(&report).unwrap();
    std::fs::write(artifact_dir.join("perf_profile_report.json"), &json).unwrap();

    for d in &report.isomorphism_check.details {
        if d.passes {
            eprintln!("  PASS: {} — {}", d.operation, d.note);
        } else {
            eprintln!("  FAIL: {} — {}", d.operation, d.note);
        }
    }
    assert!(all_pass, "Behavior-isomorphism check failed");

    eprintln!("\n── Top-3 Hotspots (at 10000-size) ──");
    for h in report.hotspot_ranking.iter().take(3) {
        eprintln!(
            "  #{}: {} {} — {:.3}ms ({:.1}%)",
            h.rank,
            h.operation,
            h.matrix_desc,
            h.median_ns as f64 / 1_000_000.0,
            h.fraction_of_total * 100.0,
        );
    }
    eprintln!();
}