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
#![forbid(unsafe_code)]
//! Live numpy differential coverage for fsci_sparse elementwise +
//! reduction helpers: sparse_abs, sparse_power, sparse_sum,
//! sparse_row_sums, sparse_col_sums.
//!
//! Resolves [frankenscipy-xgtlt]. 1e-12 abs.

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::{
    CsrMatrix, Shape2D, sparse_abs, sparse_col_sums, sparse_power, sparse_row_sums, sparse_sum,
};
use serde::{Deserialize, Serialize};

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

#[derive(Debug, Clone, Serialize)]
struct PointCase {
    case_id: String,
    rows: usize,
    cols: usize,
    dense: Vec<f64>,
    /// Only used by sparse_power.
    p: f64,
}

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

#[derive(Debug, Clone, Deserialize)]
struct DenseArm {
    case_id: String,
    dense: Option<Vec<f64>>,
}

#[derive(Debug, Clone, Deserialize)]
struct SumsArm {
    case_id: String,
    total: Option<f64>,
    row_sums: Option<Vec<f64>>,
    col_sums: Option<Vec<f64>>,
}

#[derive(Debug, Clone, Deserialize)]
struct OracleResult {
    abs: Vec<DenseArm>,
    power: Vec<DenseArm>,
    sums: Vec<SumsArm>,
}

#[derive(Debug, Clone, Serialize)]
struct CaseDiff {
    case_id: String,
    op: 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 elementwise 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 elementwise diff log");
    fs::write(path, json).expect("write elementwise diff log");
}

fn dense_to_csr(rows: usize, cols: usize, dense: &[f64]) -> CsrMatrix {
    let mut data = Vec::new();
    let mut indices = Vec::new();
    let mut indptr = Vec::with_capacity(rows + 1);
    indptr.push(0);
    for r in 0..rows {
        for c in 0..cols {
            let v = dense[r * cols + c];
            if v != 0.0 {
                data.push(v);
                indices.push(c);
            }
        }
        indptr.push(data.len());
    }
    CsrMatrix::from_components(Shape2D::new(rows, cols), data, indices, indptr, true)
        .expect("dense_to_csr build")
}

fn dense_from_csr(csr: &CsrMatrix) -> Vec<f64> {
    let shape = csr.shape();
    let mut dense = vec![0.0_f64; shape.rows * shape.cols];
    let indptr = csr.indptr();
    let indices = csr.indices();
    let data = csr.data();
    for row in 0..shape.rows {
        for idx in indptr[row]..indptr[row + 1] {
            dense[row * shape.cols + indices[idx]] += data[idx];
        }
    }
    dense
}

fn generate_query() -> OracleQuery {
    let mat_3x3 = vec![1.0, 0.0, -2.0, 0.0, 3.0, 0.0, -4.0, 0.0, 5.0];
    let mat_4x5 = vec![
        1.0, 0.0, 0.0, 0.0, 0.5, 0.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.7, 0.0, 0.0, 0.0,
        -4.0, 0.0,
    ];
    let mat_2x4 = vec![1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0];

    let abs_cases = vec![
        PointCase {
            case_id: "abs_3x3_mixed".into(),
            rows: 3,
            cols: 3,
            dense: mat_3x3.clone(),
            p: 0.0,
        },
        PointCase {
            case_id: "abs_4x5".into(),
            rows: 4,
            cols: 5,
            dense: mat_4x5.clone(),
            p: 0.0,
        },
        PointCase {
            case_id: "abs_2x4".into(),
            rows: 2,
            cols: 4,
            dense: mat_2x4.clone(),
            p: 0.0,
        },
    ];

    let mat_3x3_pos = vec![1.0, 0.0, 4.0, 0.0, 9.0, 0.0, 16.0, 0.0, 25.0];
    let power_cases = vec![
        PointCase {
            case_id: "power_3x3_p2".into(),
            rows: 3,
            cols: 3,
            dense: mat_3x3_pos.clone(),
            p: 2.0,
        },
        PointCase {
            case_id: "power_3x3_p0.5".into(),
            rows: 3,
            cols: 3,
            dense: mat_3x3_pos.clone(),
            p: 0.5,
        },
        PointCase {
            case_id: "power_3x3_p3".into(),
            rows: 3,
            cols: 3,
            dense: mat_3x3_pos,
            p: 3.0,
        },
    ];

    let sums_cases = vec![
        PointCase {
            case_id: "sums_3x3".into(),
            rows: 3,
            cols: 3,
            dense: mat_3x3,
            p: 0.0,
        },
        PointCase {
            case_id: "sums_4x5".into(),
            rows: 4,
            cols: 5,
            dense: mat_4x5,
            p: 0.0,
        },
        PointCase {
            case_id: "sums_2x4".into(),
            rows: 2,
            cols: 4,
            dense: mat_2x4,
            p: 0.0,
        },
    ];

    OracleQuery {
        abs: abs_cases,
        power: power_cases,
        sums: sums_cases,
    }
}

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

def finite_or_none(arr):
    flat = []
    for v in np.asarray(arr, dtype=float).flatten().tolist():
        if not math.isfinite(float(v)):
            return None
        flat.append(float(v))
    return flat

q = json.load(sys.stdin)

abs_out = []
for c in q["abs"]:
    r = int(c["rows"]); cc = int(c["cols"])
    A = np.array(c["dense"], dtype=float).reshape(r, cc)
    abs_A = np.abs(A)
    abs_out.append({"case_id": c["case_id"], "dense": finite_or_none(abs_A)})

power_out = []
for c in q["power"]:
    r = int(c["rows"]); cc = int(c["cols"])
    A = np.array(c["dense"], dtype=float).reshape(r, cc)
    p = float(c["p"])
    # Match fsci semantics: power applied only to stored nonzeros
    # (zero^p stays zero). np.power preserves zeros for positive p,
    # so this is consistent.
    pow_A = np.where(A != 0, np.power(np.abs(A), p) * np.sign(A) ** 0 if False else A, 0.0)
    pow_A = np.where(A != 0, np.power(A, p), 0.0)
    power_out.append({"case_id": c["case_id"], "dense": finite_or_none(pow_A)})

sums_out = []
for c in q["sums"]:
    r = int(c["rows"]); cc = int(c["cols"])
    A = np.array(c["dense"], dtype=float).reshape(r, cc)
    total = float(np.sum(A))
    rs = [float(v) for v in np.sum(A, axis=1).tolist()]
    cs = [float(v) for v in np.sum(A, axis=0).tolist()]
    sums_out.append({"case_id": c["case_id"], "total": total,
                     "row_sums": rs, "col_sums": cs})

print(json.dumps({"abs": abs_out, "power": power_out, "sums": sums_out}))
"#;
    let query_json = serde_json::to_string(query).expect("serialize elementwise 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 elementwise oracle: {e}"
            );
            eprintln!("skipping elementwise oracle: python3 not available ({e})");
            return None;
        }
    };
    {
        let stdin = child.stdin.as_mut().expect("open elementwise 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(),
                "elementwise oracle stdin write failed: {err}; stderr: {stderr}"
            );
            eprintln!("skipping elementwise oracle: stdin write failed ({err})\n{stderr}");
            return None;
        }
    }
    let output = child
        .wait_with_output()
        .expect("wait for elementwise oracle");
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            std::env::var(REQUIRE_SCIPY_ENV).is_err(),
            "elementwise oracle failed: {stderr}"
        );
        eprintln!("skipping elementwise oracle: numpy not available\n{stderr}");
        return None;
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    Some(serde_json::from_str(&stdout).expect("parse elementwise oracle JSON"))
}

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

    let abs_map: HashMap<String, DenseArm> = oracle
        .abs
        .into_iter()
        .map(|d| (d.case_id.clone(), d))
        .collect();
    let power_map: HashMap<String, DenseArm> = oracle
        .power
        .into_iter()
        .map(|d| (d.case_id.clone(), d))
        .collect();
    let sums_map: HashMap<String, SumsArm> = oracle
        .sums
        .into_iter()
        .map(|d| (d.case_id.clone(), d))
        .collect();

    let start = Instant::now();
    let mut diffs = Vec::new();
    let mut max_overall = 0.0_f64;
    let mut ledger = CompareLedger::new(
        "diff_sparse_elementwise_reductions",
        &[
            "sparse_abs",
            "sparse_power",
            "sparse_sum",
            "sparse_row_sums",
            "sparse_col_sums",
        ],
    );

    for case in &query.abs {
        let scipy_arm = abs_map.get(&case.case_id).expect("validated oracle");
        let csr = dense_to_csr(case.rows, case.cols, &case.dense);
        let out = sparse_abs(&csr);
        let flat = dense_from_csr(&out);
        let Some((expected, flat)) = ledger.slices(
            "sparse_abs",
            &case.case_id,
            scipy_arm.dense.as_deref(),
            Some(flat.as_slice()),
        ) else {
            continue;
        };
        let abs_d = flat
            .iter()
            .zip(expected.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        max_overall = max_overall.max(abs_d);
        ledger.compared("sparse_abs", &case.case_id, abs_d <= ABS_TOL);
        diffs.push(CaseDiff {
            case_id: case.case_id.clone(),
            op: "sparse_abs".into(),
            abs_diff: abs_d,
            pass: abs_d <= ABS_TOL,
        });
    }

    for case in &query.power {
        let scipy_arm = power_map.get(&case.case_id).expect("validated oracle");
        let csr = dense_to_csr(case.rows, case.cols, &case.dense);
        let out = sparse_power(&csr, case.p);
        let flat = dense_from_csr(&out);
        let Some((expected, flat)) = ledger.slices(
            "sparse_power",
            &case.case_id,
            scipy_arm.dense.as_deref(),
            Some(flat.as_slice()),
        ) else {
            continue;
        };
        let abs_d = flat
            .iter()
            .zip(expected.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        max_overall = max_overall.max(abs_d);
        ledger.compared("sparse_power", &case.case_id, abs_d <= ABS_TOL);
        diffs.push(CaseDiff {
            case_id: case.case_id.clone(),
            op: "sparse_power".into(),
            abs_diff: abs_d,
            pass: abs_d <= ABS_TOL,
        });
    }

    for case in &query.sums {
        let scipy_arm = sums_map.get(&case.case_id).expect("validated oracle");
        let csr = dense_to_csr(case.rows, case.cols, &case.dense);
        let total = sparse_sum(&csr);
        let rs = sparse_row_sums(&csr);
        let cs = sparse_col_sums(&csr);
        // One slot per arm; NaN until that arm's case is compared.
        let mut arm_diffs = [f64::NAN; 3];
        if let Some((total_exp, total)) =
            ledger.pair("sparse_sum", &case.case_id, scipy_arm.total, Some(total))
        {
            let dt = (total - total_exp).abs();
            ledger.compared("sparse_sum", &case.case_id, dt <= ABS_TOL);
            arm_diffs[0] = dt;
        }
        let vec_arms = [
            (
                "sparse_row_sums",
                scipy_arm.row_sums.as_deref(),
                rs.as_slice(),
            ),
            (
                "sparse_col_sums",
                scipy_arm.col_sums.as_deref(),
                cs.as_slice(),
            ),
        ];
        for (slot, (arm, scipy, fsci)) in arm_diffs[1..].iter_mut().zip(vec_arms) {
            let Some((expected, got)) = ledger.slices(arm, &case.case_id, scipy, Some(fsci)) else {
                continue;
            };
            let d = got
                .iter()
                .zip(expected.iter())
                .map(|(a, b)| (a - b).abs())
                .fold(0.0_f64, f64::max);
            ledger.compared(arm, &case.case_id, d <= ABS_TOL);
            *slot = d;
        }
        if arm_diffs.iter().any(|d| d.is_nan()) {
            continue; // the ledger recorded why this case was not compared
        }
        let [dt, dr, dc] = arm_diffs;
        let abs_d = dt.max(dr).max(dc);
        max_overall = max_overall.max(abs_d);
        diffs.push(CaseDiff {
            case_id: case.case_id.clone(),
            op: "sums".into(),
            abs_diff: abs_d,
            pass: abs_d <= ABS_TOL,
        });
    }

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

    let log = DiffLog {
        test_id: "diff_sparse_elementwise_reductions".into(),
        category: "fsci_sparse abs/power/sums vs numpy".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!("{} mismatch: {} abs_diff={}", d.op, d.case_id, d.abs_diff);
        }
    }

    assert!(
        all_pass,
        "elementwise conformance failed: {} cases, max_diff={}",
        diffs.len(),
        max_overall
    );
    // Every arm is designed to compare every case of its family; the smallest family bounds all.
    ledger.finish(query.abs.len().min(query.power.len()).min(query.sums.len()));
}