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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#![forbid(unsafe_code)]
//! Live SciPy differential coverage for fsci_sparse's iterative
//! solvers on well-conditioned SPD/symmetric systems:
//!   - `cg`       vs `scipy.sparse.linalg.cg`
//!   - `gmres`    vs `scipy.sparse.linalg.gmres`
//!   - `bicgstab` vs `scipy.sparse.linalg.bicgstab`
//!   - `minres`   vs `scipy.sparse.linalg.minres`
//!   - `lsmr`     vs `scipy.sparse.linalg.lsmr`
//!   - `bicg`     vs `scipy.sparse.linalg.bicg`
//!   - `cgs`      vs `scipy.sparse.linalg.cgs`
//!   - `lgmres`   vs `scipy.sparse.linalg.lgmres`
//!
//! Resolves [frankenscipy-x6tbi]. SPD systems have unique solutions
//! so a vector-element comparison is well-defined. Both implementations
//! solve to ~1e-8 internally; tolerance 1e-6 abs covers the residual
//! floor on probe systems.
//!
//! `bicg`, `cgs` and `lgmres` were added by the frankenscipy-6pdfn
//! delegating-stub compliance sweep. All three carry a "Matches
//! `scipy.sparse.linalg.<name>`" docstring claim on reachable public API
//! and previously had **zero** live-SciPy coverage backing it.
//!
//! They are covered here on the symmetric systems only. The nonsymmetric
//! column those three actually exist for is deliberately NOT here: fsci's
//! `bicg`, `cgs` and `bicgstab` all abort early on a nonsymmetric operator
//! because their breakdown gates are `f64::EPSILON * 1e6`, where SciPy
//! uses `eps**2` — replaying the recurrences on the 8x8 convection-diffusion
//! operator trips `bicg`/`cgs` at iteration 19 (min|rho| = 2.163e-10 vs
//! gate 2.220e-10) and `bicgstab` at iteration 13 (min|t·t| = 4.933e-11),
//! while SciPy converges all three. That is tracked as frankenscipy-9y533,
//! and the nonsymmetric column lands there as its red-before/green-after
//! probe. Adding it here would ship a red test.

use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use fsci_conformance::{ArmCounts, CompareLedger};
use fsci_sparse::{
    CooMatrix, FormatConvertible, IterativeSolveOptions, LgmresOptions, Shape2D, bicg, bicgstab,
    cg, cgs, gmres, lgmres, lsmr, minres,
};
use serde::{Deserialize, Serialize};

const PACKET_ID: &str = "FSCI-P2C-004";
const ABS_TOL: f64 = 1.0e-6;
const REQUIRE_SCIPY_ENV: &str = "FSCI_REQUIRE_SCIPY_ORACLE";

#[derive(Debug, Clone, Serialize)]
struct PointCase {
    case_id: String,
    solver: String,
    n: usize,
    /// COO triplets (row, col, value).
    triplets: Vec<(usize, usize, f64)>,
    b: Vec<f64>,
}

#[derive(Debug, Clone, Serialize)]
struct OracleQuery {
    points: Vec<PointCase>,
}

#[derive(Debug, Clone, Deserialize)]
struct PointArm {
    case_id: String,
    /// Solution vector.
    x: Option<Vec<f64>>,
    /// Why the oracle produced no solution, when it produced none.
    #[serde(default)]
    why: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
struct OracleResult {
    points: Vec<PointArm>,
}

#[derive(Debug, Clone, Serialize)]
struct CaseDiff {
    case_id: String,
    solver: String,
    abs_diff: f64,
    pass: bool,
}

#[derive(Debug, Clone, Serialize)]
struct DiffLog {
    test_id: String,
    category: String,
    case_count: usize,
    compared: BTreeMap<String, ArmCounts>,
    max_abs_diff: f64,
    pass: bool,
    timestamp_ms: u128,
    duration_ns: u128,
    cases: Vec<CaseDiff>,
}

fn output_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("fixtures/artifacts/{PACKET_ID}/diff"))
}

fn ensure_output_dir() {
    fs::create_dir_all(output_dir()).expect("create iter_solvers diff output dir");
}

fn timestamp_ms() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_millis())
}

fn emit_log(log: &DiffLog) {
    ensure_output_dir();
    let path = output_dir().join(format!("{}.json", log.test_id));
    let json = serde_json::to_string_pretty(log).expect("serialize iter_solvers diff log");
    fs::write(path, json).expect("write iter_solvers diff log");
}

/// Build a small SPD system from triplets + rhs.
fn system_4x4_tridiag_spd() -> (Vec<(usize, usize, f64)>, Vec<f64>) {
    // A = [[4,1,0,0],[1,4,1,0],[0,1,4,1],[0,0,1,4]]
    let trips = vec![
        (0, 0, 4.0),
        (0, 1, 1.0),
        (1, 0, 1.0),
        (1, 1, 4.0),
        (1, 2, 1.0),
        (2, 1, 1.0),
        (2, 2, 4.0),
        (2, 3, 1.0),
        (3, 2, 1.0),
        (3, 3, 4.0),
    ];
    let b = vec![1.0, 2.0, 3.0, 4.0];
    (trips, b)
}

fn system_5x5_diag_spd() -> (Vec<(usize, usize, f64)>, Vec<f64>) {
    // Strict diagonal dominance: A = diag(2,3,4,5,6)
    let trips = (0..5).map(|i| (i, i, (i as f64) + 2.0)).collect();
    let b = vec![1.0, -1.0, 2.0, 3.0, 0.5];
    (trips, b)
}

fn system_6x6_pentadiag_spd() -> (Vec<(usize, usize, f64)>, Vec<f64>) {
    // Symmetric pentadiagonal SPD: A = diag*5 + offdiag*1
    let n = 6;
    let mut trips = Vec::new();
    for i in 0..n {
        trips.push((i, i, 5.0));
        if i + 1 < n {
            trips.push((i, i + 1, 1.0));
            trips.push((i + 1, i, 1.0));
        }
        if i + 2 < n {
            trips.push((i, i + 2, 0.5));
            trips.push((i + 2, i, 0.5));
        }
    }
    let b = (1..=n).map(|i| i as f64).collect();
    (trips, b)
}

/// Symmetric **indefinite** system: the 8x8 Dirichlet five-point Laplacian
/// shifted by 3.7, which puts 28 of its 64 eigenvalues below zero.
///
/// Every other system here is SPD, so before this case MINRES was only ever
/// exercised on operators CG could have handled — the one matrix class it
/// exists for went untested, which is how a GMRES delegate passed this harness.
/// CG and BiCGSTAB are deliberately not run on it: CG has no contract on an
/// indefinite operator.
fn system_64x64_shifted_laplacian_indefinite() -> (Vec<(usize, usize, f64)>, Vec<f64>) {
    const SIDE: usize = 8;
    const DIAGONAL: f64 = 4.001 - 3.7;
    let n = SIDE * SIDE;
    let mut trips = Vec::new();
    for row in 0..SIDE {
        for col in 0..SIDE {
            let index = row * SIDE + col;
            if row > 0 {
                trips.push((index, index - SIDE, -1.0));
            }
            if col > 0 {
                trips.push((index, index - 1, -1.0));
            }
            trips.push((index, index, DIAGONAL));
            if col + 1 < SIDE {
                trips.push((index, index + 1, -1.0));
            }
            if row + 1 < SIDE {
                trips.push((index, index + SIDE, -1.0));
            }
        }
    }
    let b = (0..n).map(|i| 1.0 + 0.1 * (i % 7) as f64).collect();
    (trips, b)
}

fn generate_query() -> OracleQuery {
    let mut points = Vec::new();
    let systems: &[(&str, fn() -> (Vec<(usize, usize, f64)>, Vec<f64>), usize)] = &[
        ("4x4_tridiag_spd", system_4x4_tridiag_spd, 4),
        ("5x5_diag_spd", system_5x5_diag_spd, 5),
        ("6x6_pentadiag_spd", system_6x6_pentadiag_spd, 6),
    ];
    let solvers = [
        "cg", "gmres", "bicgstab", "minres", "lsmr", "bicg", "cgs", "lgmres",
    ];
    for (label, fac, n) in systems {
        let (trips, b) = fac();
        for solver in solvers {
            points.push(PointCase {
                case_id: format!("{solver}_{label}"),
                solver: solver.into(),
                n: *n,
                triplets: trips.clone(),
                b: b.clone(),
            });
        }
    }

    let (indefinite_trips, indefinite_b) = system_64x64_shifted_laplacian_indefinite();
    for solver in ["minres", "gmres"] {
        points.push(PointCase {
            case_id: format!("{solver}_64x64_shifted_laplacian_indefinite"),
            solver: solver.into(),
            n: 64,
            triplets: indefinite_trips.clone(),
            b: indefinite_b.clone(),
        });
    }

    OracleQuery { points }
}

fn scipy_oracle_or_skip(query: &OracleQuery) -> Option<OracleResult> {
    let script = r#"
import json
import math
import sys
import numpy as np
import scipy.sparse as sp
import scipy.sparse.linalg as spl

def finite_vec_or_none(arr):
    out = []
    for v in np.asarray(arr).tolist():
        try:
            v = float(v)
        except Exception:
            return None
        if not math.isfinite(v):
            return None
        out.append(v)
    return out

SOLVERS = {
    "cg":       spl.cg,
    "gmres":    spl.gmres,
    "bicgstab": spl.bicgstab,
    "minres":   spl.minres,
    "lsmr":     spl.lsmr,
    "bicg":     spl.bicg,
    "cgs":      spl.cgs,
    "lgmres":   spl.lgmres,
}

q = json.load(sys.stdin)
points = []
for case in q["points"]:
    cid = case["case_id"]; solver = case["solver"]
    n = case["n"]
    triplets = case["triplets"]
    if triplets:
        r = np.array([t[0] for t in triplets], dtype=int)
        c = np.array([t[1] for t in triplets], dtype=int)
        v = np.array([t[2] for t in triplets], dtype=float)
    else:
        r = np.zeros(0, dtype=int); c = np.zeros(0, dtype=int); v = np.zeros(0, dtype=float)
    b = np.array(case["b"], dtype=float)
    try:
        A = sp.csr_matrix((v, (r, c)), shape=(n, n))
        fn = SOLVERS[solver]
        if solver == "lsmr":
            x, istop, *_ = fn(A, b, atol=1e-10, btol=1e-10, maxiter=500)
            if istop in (0, 1, 2):
                points.append({"case_id": cid, "x": finite_vec_or_none(x)})
            else:
                points.append({"case_id": cid, "x": None, "why": f"istop={istop}"})
            continue
        # `minres` has no `atol` parameter (signature is rtol/shift/maxiter/M).
        # Passing one raises TypeError, which this block used to swallow into a
        # null arm — so every minres case was silently skipped and the minres
        # column of this harness tested nothing at all.
        kwargs = {"rtol": 1e-10, "maxiter": 500}
        if solver != "minres":
            kwargs["atol"] = 0.0
        x, info = fn(A, b, **kwargs)
        if info == 0:
            points.append({"case_id": cid, "x": finite_vec_or_none(x)})
        else:
            points.append({"case_id": cid, "x": None, "why": f"info={info}"})
    except Exception as exc:
        points.append({"case_id": cid, "x": None, "why": f"{type(exc).__name__}: {exc}"})
print(json.dumps({"points": points}))
"#;
    let query_json = serde_json::to_string(query).expect("serialize iter_solvers query");
    let mut child = match fsci_conformance::scipy_oracle_command()
        .arg("-c")
        .arg(script)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            assert!(
                std::env::var(REQUIRE_SCIPY_ENV).is_err(),
                "failed to spawn python3 for iter_solvers oracle: {e}"
            );
            eprintln!("skipping iter_solvers oracle: python3 not available ({e})");
            return None;
        }
    };
    {
        let stdin = child
            .stdin
            .as_mut()
            .expect("open iter_solvers oracle stdin");
        if let Err(err) = stdin.write_all(query_json.as_bytes()) {
            let output = child.wait_with_output().expect("wait for failed oracle");
            let stderr = String::from_utf8_lossy(&output.stderr);
            assert!(
                std::env::var(REQUIRE_SCIPY_ENV).is_err(),
                "iter_solvers oracle stdin write failed: {err}; stderr: {stderr}"
            );
            eprintln!("skipping iter_solvers oracle: stdin write failed ({err})\n{stderr}");
            return None;
        }
    }
    let output = child
        .wait_with_output()
        .expect("wait for iter_solvers oracle");
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            std::env::var(REQUIRE_SCIPY_ENV).is_err(),
            "iter_solvers oracle failed: {stderr}"
        );
        eprintln!("skipping iter_solvers oracle: scipy not available\n{stderr}");
        return None;
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    Some(serde_json::from_str(&stdout).expect("parse iter_solvers oracle JSON"))
}

fn fsci_solve(case: &PointCase) -> Option<Vec<f64>> {
    let r: Vec<usize> = case.triplets.iter().map(|t| t.0).collect();
    let c: Vec<usize> = case.triplets.iter().map(|t| t.1).collect();
    let d: Vec<f64> = case.triplets.iter().map(|t| t.2).collect();
    let coo = CooMatrix::from_triplets(Shape2D::new(case.n, case.n), d, r, c, false).ok()?;
    let csr = coo.to_csr().ok()?;
    let opts = IterativeSolveOptions {
        tol: 1.0e-10,
        max_iter: Some(500),
        ..Default::default()
    };
    let result = match case.solver.as_str() {
        "cg" => cg(&csr, &case.b, None, opts).ok()?,
        "gmres" => gmres(&csr, &case.b, None, opts).ok()?,
        "bicgstab" => bicgstab(&csr, &case.b, None, opts).ok()?,
        "minres" => minres(&csr, &case.b, None, opts).ok()?,
        "lsmr" => lsmr(&csr, &case.b, opts).ok()?,
        "bicg" => bicg(&csr, &case.b, None, opts).ok()?,
        "cgs" => cgs(&csr, &case.b, None, opts).ok()?,
        // `LgmresOptions` carries scipy's own defaults for the two knobs
        // that are unique to LGMRES (inner_m=30, outer_k=3), so the oracle
        // call is left at its defaults too and the arms stay comparable.
        "lgmres" => lgmres(
            &csr,
            &case.b,
            None,
            LgmresOptions {
                tol: opts.tol,
                max_iter: opts.max_iter,
                ..Default::default()
            },
        )
        .ok()?,
        _ => return None,
    };
    if !result.converged {
        return None;
    }
    Some(result.solution)
}

#[test]
fn diff_sparse_iterative_solvers() {
    let query = generate_query();
    let Some(oracle) = scipy_oracle_or_skip(&query) else {
        return;
    };
    assert_eq!(oracle.points.len(), query.points.len());

    let pmap: HashMap<String, PointArm> = oracle
        .points
        .into_iter()
        .map(|r| (r.case_id.clone(), r))
        .collect();

    let start = Instant::now();
    let mut diffs = Vec::new();
    let mut max_overall = 0.0_f64;

    // A case that neither arm can solve used to `continue` silently, so a
    // harness that compared nothing still reported "pass". Skips are now
    // recorded and asserted on below.
    let mut skipped: Vec<String> = Vec::new();
    let solvers = [
        "cg", "gmres", "bicgstab", "minres", "lsmr", "bicg", "cgs", "lgmres",
    ];
    let mut ledger = CompareLedger::new("diff_sparse_iterative_solvers", &solvers);

    for case in &query.points {
        let scipy_arm = pmap.get(&case.case_id).expect("validated oracle");
        let fsci_x = fsci_solve(case);
        if scipy_arm.x.is_none() {
            skipped.push(format!(
                "{} (scipy arm: {})",
                case.case_id,
                scipy_arm.why.as_deref().unwrap_or("no solution")
            ));
        } else if fsci_x.is_none() {
            skipped.push(format!("{} (fsci arm did not converge)", case.case_id));
        }
        let Some((scipy_x, fsci_x)) = ledger.slices(
            &case.solver,
            &case.case_id,
            scipy_arm.x.as_deref(),
            fsci_x.as_deref(),
        ) else {
            continue;
        };
        let abs_d = fsci_x
            .iter()
            .zip(scipy_x.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        max_overall = max_overall.max(abs_d);
        ledger.compared(&case.solver, &case.case_id, abs_d <= ABS_TOL);
        diffs.push(CaseDiff {
            case_id: case.case_id.clone(),
            solver: case.solver.clone(),
            abs_diff: abs_d,
            pass: abs_d <= ABS_TOL,
        });
    }

    // Every solver named in the query must have been genuinely compared at
    // least once. Without this, a solver whose oracle call raises — as
    // `minres` did for years, on an unsupported `atol` kwarg — contributes
    // zero cases and the harness reports a green "pass" over an empty column.
    for solver in solvers {
        let compared = diffs.iter().filter(|d| d.solver == solver).count();
        assert!(
            compared > 0,
            "iter_solvers compared zero cases for `{solver}`; this harness cannot \
             pass by testing nothing. Skipped: {skipped:?}"
        );
    }
    if !skipped.is_empty() {
        eprintln!(
            "iter_solvers skipped {} case(s): {skipped:?}",
            skipped.len()
        );
    }

    let all_pass = diffs.iter().all(|d| d.pass);

    let log = DiffLog {
        test_id: "diff_sparse_iterative_solvers".into(),
        category: "scipy.sparse.linalg cg/gmres/bicgstab/minres/lsmr/bicg/cgs/lgmres".into(),
        case_count: diffs.len(),
        compared: ledger.counts().clone(),
        max_abs_diff: max_overall,
        pass: all_pass,
        timestamp_ms: timestamp_ms(),
        duration_ns: start.elapsed().as_nanos(),
        cases: diffs.clone(),
    };
    emit_log(&log);

    for d in &diffs {
        if !d.pass {
            eprintln!(
                "iter_solvers {} mismatch: {} abs_diff={}",
                d.solver, d.case_id, d.abs_diff
            );
        }
    }

    assert!(
        all_pass,
        "scipy.sparse iterative-solver conformance failed: {} cases, max_diff={}",
        diffs.len(),
        max_overall
    );
    // Solvers run on different system sets (minres/gmres also get the indefinite one);
    // each must compare every case it is designed for, so the minimum bounds all arms.
    let per_solver = solvers
        .iter()
        .map(|s| query.points.iter().filter(|c| c.solver == *s).count())
        .min()
        .unwrap_or(0);
    ledger.finish(per_solver);
}