use gresil::nir::{
nir_export, nir_import, nir_scan, NirBuffers, NirError, NirGraphEncoder, NirImport,
NirImportOptions, NirLif, NirName, NirNode, NirNote, Thousandths, EXPORT_VERSION,
};
use gresil::{SpikingNeuralNetwork, VoltageResolution};
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{mpsc, Arc, Mutex};
use std::time::{Duration, Instant};
const CHAIN: &str = include_str!("nir_fixtures/chain.json");
const CHAIN_POP: &str = include_str!("nir_fixtures/chain_population.json");
const CHAIN_VRESET: &str = include_str!("nir_fixtures/chain_vreset_absent.json");
const AFFINE: &str = include_str!("nir_fixtures/neg_affine.json");
const UNKNOWN: &str = include_str!("nir_fixtures/neg_unknown_kind.json");
const TAU_ZERO: &str = include_str!("nir_fixtures/neg_tau_zero.json");
const THRESH_ZERO: &str = include_str!("nir_fixtures/neg_threshold_zero_quant.json");
const TAU_DT: &str = include_str!("nir_fixtures/neg_tau_below_dt.json");
const POT_RANGE: &str = include_str!("nir_fixtures/neg_potential_out_of_range.json");
const MISSING_F: &str = include_str!("nir_fixtures/neg_missing_field.json");
const ENDPOINT: &str = include_str!("nir_fixtures/neg_unknown_endpoint.json");
const DUP_EDGE: &str = include_str!("nir_fixtures/neg_duplicate_edge.json");
const RAGGED: &str = include_str!("nir_fixtures/neg_ragged_weight.json");
const PARAM_LEN: &str = include_str!("nir_fixtures/neg_param_length.json");
const MISSING_V: &str = include_str!("nir_fixtures/neg_missing_version.json");
const ESCAPED: &str = include_str!("nir_fixtures/neg_escaped_name.json");
fn import_owned(doc: &str) -> Result<NirImport<'_>, NirError> {
NirImport::from_json(
doc.as_bytes(),
NirImportOptions::new(1_000, VoltageResolution::Millivolt),
)
}
#[test]
fn reference_chain_imports_with_exact_quantization() {
let scan = nir_scan(CHAIN.as_bytes()).expect("reference emission scans");
assert_eq!(scan.version, "1.0.0");
assert_eq!(
(scan.node_count, scan.edge_count, scan.weight_cells),
(4, 3, 3)
);
let g = import_owned(CHAIN).expect("reference emission imports");
assert_eq!(
g.nodes.iter().map(|n| n.name).collect::<Vec<_>>(),
vec!["input", "linear", "lif", "output"],
"document order"
);
assert_eq!(g.edges, vec![(0, 1), (1, 2), (2, 3)]);
let lif = g.nodes[2].lif.expect("lif");
let rec = g.lifs[lif.offset];
assert_eq!(
(
rec.tau_us,
rec.resistance_mohm,
rec.leak_q,
rec.threshold_q,
rec.reset_q
),
(20_000, 100, -70, -55, -80)
);
assert_eq!(rec.capacitance_pf, 200, "C = tau/r = 200 pF");
assert!(!rec.v_reset_defaulted);
let lin = g.nodes[1].linear.expect("linear");
assert_eq!((lin.rows, lin.cols), (1, 3));
assert_eq!(g.weights, vec![16384, -32767, 8192]);
assert!((lin.scale - 1.0 / 32_767.0).abs() < 1e-20);
assert!(
lin.max_abs_err > 0.0 && lin.max_abs_err <= lin.scale / 2.0,
"scale is non-dyadic: bounded loss, recorded"
);
}
#[test]
fn absent_v_reset_is_reference_semantics_with_note() {
let scan = nir_scan(CHAIN_VRESET.as_bytes()).unwrap();
let mut nodes = vec![NirNode::BLANK; scan.node_count];
let mut edges = vec![(0u32, 0u32); scan.edge_count];
let mut weights = vec![0i16; scan.weight_cells];
let mut lifs = vec![NirLif::default(); scan.lif_neurons];
let mut scratch = vec![0f64; scan.weight_cells + 5 * scan.lif_neurons];
let mut bufs = NirBuffers {
nodes: &mut nodes,
edges: &mut edges,
weights: &mut weights,
lifs: &mut lifs,
scratch: &mut scratch,
};
let report = nir_import(
CHAIN_VRESET.as_bytes(),
NirImportOptions::new(1_000, VoltageResolution::Millivolt),
&mut bufs,
)
.expect("imports");
let lif = nodes[2].lif.expect("lif");
let rec = lifs[lif.offset];
assert!(rec.v_reset_defaulted);
assert_eq!(rec.reset_q, 0);
assert!(report.notes(NirNote::VResetDefaulted) >= 1);
assert!(
report.notes(NirNote::QuantizationLoss) >= 1,
"0.1 is not dyadic"
);
}
#[test]
fn reference_chain_assembles_and_fires() {
let g = import_owned(CHAIN).expect("imports");
let (mut net, enc) = g.build_chain_network().expect("canonical chain");
assert_eq!(net.neuron_count(), 1);
assert_eq!((enc.rows(), enc.cols()), (1, 3));
let spikes: usize = (0..100)
.map(|t| {
let i = net.step(&enc.encode(&[4, 0, 0])).expect("step");
let _ = t;
i.len()
})
.sum();
assert!(spikes > 0, "the imported chain must actually spike");
}
#[test]
fn reference_population_chain_imports_per_neuron() {
let scan = nir_scan(CHAIN_POP.as_bytes()).expect("scans");
assert_eq!(
(
scan.node_count,
scan.edge_count,
scan.weight_cells,
scan.lif_neurons
),
(4, 3, 6, 2)
);
let g = import_owned(CHAIN_POP).expect("imports");
let pop = g.nodes[2].lif.expect("lif population");
assert_eq!(pop.len, 2);
let n0 = g.lifs[pop.offset];
let n1 = g.lifs[pop.offset + 1];
assert_eq!(
(
n0.tau_us,
n0.resistance_mohm,
n0.leak_q,
n0.threshold_q,
n0.reset_q
),
(20_000, 100, -70, -55, -80)
);
assert_eq!(n0.capacitance_pf, 200);
assert_eq!(
(
n1.tau_us,
n1.resistance_mohm,
n1.leak_q,
n1.threshold_q,
n1.reset_q
),
(30_000, 200, -65, -50, -75)
);
assert_eq!(n1.capacitance_pf, 150);
let (mut net, enc) = g.build_chain_network().expect("canonical chain");
assert_eq!(net.neuron_count(), 2);
assert_eq!((enc.rows(), enc.cols()), (2, 3));
let spikes: usize = (0..100)
.map(|_| net.step(&enc.encode(&[400, 0, 0])).expect("step").len())
.sum();
assert!(spikes > 0, "the population chain fires");
let mut out = vec![0u8; 4096];
let n = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out).expect("exports");
let g2 = NirImport::from_json(&out[..n], g.opts).expect("re-imports");
assert_eq!(g2.lifs, g.lifs, "per-neuron quantized state identical");
}
#[test]
fn reference_chain_export_round_trips() {
let g = import_owned(CHAIN).expect("imports");
let mut out = vec![0u8; 4096];
let n = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out).expect("exports");
out.truncate(n);
assert!(std::str::from_utf8(&out).unwrap().contains(EXPORT_VERSION));
let mut out2 = vec![0u8; 4096];
let n2 = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out2).unwrap();
assert_eq!(out, out2[..n2]);
let g2 = NirImport::from_json(&out, g.opts).expect("re-imports");
assert_eq!(g2.weights, g.weights);
let pop1 = g.nodes[2].lif.unwrap();
let pop2 = g2.nodes[2].lif.unwrap();
let l1 = g.lifs[pop1.offset];
let l2 = g2.lifs[pop2.offset];
assert_eq!(
(l2.tau_us, l2.threshold_q, l2.leak_q, l2.reset_q),
(l1.tau_us, l1.threshold_q, l1.leak_q, l1.reset_q)
);
}
#[test]
#[allow(clippy::type_complexity)]
fn negative_fixtures_reject_with_the_named_error() {
let cases: &[(&str, &str, fn(&NirError) -> bool)] = &[
(
"affine",
AFFINE,
|e| matches!(e, NirError::UnsupportedNodeKind(k) if k == "Affine"),
),
(
"unknown kind",
UNKNOWN,
|e| matches!(e, NirError::UnsupportedNodeKind(k) if k == "CubaLIF"),
),
("tau zero", TAU_ZERO, |e| {
matches!(e, NirError::BadNumber("tau"))
}),
("threshold quantizes to 0", THRESH_ZERO, |e| {
matches!(e, NirError::ThresholdZero)
}),
("tau below dt", TAU_DT, |e| {
matches!(e, NirError::TauBelowDt)
}),
("potential out of range", POT_RANGE, |e| {
matches!(e, NirError::PotentialOutOfRange("v_leak"))
}),
("missing field", MISSING_F, |e| {
matches!(e, NirError::MissingField("v_threshold"))
}),
(
"unknown endpoint",
ENDPOINT,
|e| matches!(e, NirError::UnknownEdgeEndpoint(n) if n == "ghost"),
),
("duplicate edge", DUP_EDGE, |e| {
matches!(e, NirError::DuplicateEdge)
}),
("ragged weight", RAGGED, |e| {
matches!(e, NirError::BadShape("weight"))
}),
("param length mismatch (population)", PARAM_LEN, |e| {
matches!(e, NirError::BadShape("LIF param"))
}),
("missing version", MISSING_V, |e| {
matches!(e, NirError::MissingField("version"))
}),
("escaped name", ESCAPED, |e| {
matches!(e, NirError::EscapedOrNonAsciiString(_))
}),
];
assert_eq!(cases.len(), 13);
for (label, doc, check) in cases {
let err = import_owned(doc).expect_err(label);
assert!(check(&err), "{label}: got {err:?}");
}
}
#[test]
fn a_value_of_another_json_type_is_refused_as_json_at_its_byte() {
for (from, to) in [
("\"tau\":[0.02]", "\"tau\":\"0.02\""),
("\"type\":\"LIF\"", "\"type\":5"),
] {
let doc = CHAIN.replacen(from, to, 1);
assert_ne!(doc, CHAIN, "{to}: the edit matched");
let at = doc.find(to).expect("the edit") + to.find(':').expect("a key") + 1;
assert_eq!(
nir_scan(doc.as_bytes()).err(),
Some(NirError::Json(at)),
"{to}"
);
assert_eq!(import_owned(&doc).err(), Some(NirError::Json(at)), "{to}");
}
}
#[test]
fn the_scan_counts_the_arrays_of_a_node_the_import_refuses() {
assert_eq!(nir_scan(AFFINE.as_bytes()).map(|s| s.weight_cells), Ok(4));
assert_eq!(
import_owned(AFFINE).err(),
Some(NirError::UnsupportedNodeKind(NirName::new("Affine")))
);
let untyped = CHAIN.replacen(
"\"nodes\":{",
"\"nodes\":{\"stray\":{\"tau\":[0.02,0.02]},",
1,
);
assert_ne!(untyped, CHAIN, "the edit matched");
assert_eq!(nir_scan(untyped.as_bytes()).map(|s| s.lif_neurons), Ok(3));
assert_eq!(
import_owned(&untyped).err(),
Some(NirError::MissingField("type"))
);
}
#[test]
fn non_chain_topology_rejects_at_assembly_only() {
let doc = "{\"version\":\"1.0.0\",\"node\":{\"type\":\"NIRGraph\",\
\"edges\":[[\"a\",\"b\"]],\"nodes\":{\"a\":{\"type\":\"Input\",\"shape\":[1]},\
\"b\":{\"type\":\"Output\",\"shape\":[1]}}}}";
let g = import_owned(doc).expect("format layer imports any 4-kind graph");
assert!(
g.build_chain_network().is_err(),
"assembly rejects non-chain topologies loudly"
);
let scan = nir_scan(doc.as_bytes()).unwrap();
assert_eq!(scan.node_count, 2);
}
#[test]
fn a_weight_of_f64_s_largest_is_refused_at_import() {
let merge = include_str!("nir_fixtures/merge.json");
let sim = include_str!("nir_fixtures/two_lif_neurons_sim.json");
let in_merge = |w: &str| merge.replacen("[[0.25,0.0],", &format!("[[0.25,{w}],"), 1);
let in_sim = |w: &str| sim.replacen("\"weight\":[[1]]", &format!("\"weight\":[[{w}]]"), 1);
let sim_units = NirImportOptions::sim_units(100);
let mut out = [vec![0u8; 1 << 16], vec![0u8; 1 << 16], vec![0u8; 1 << 16]];
for w in ["1.7976931348623157e308", "-1.7976931348623157e308"] {
let (native, simulation) = (in_merge(w), in_sim(w));
assert!(native != merge && simulation != sim, "the edits matched");
assert_eq!(
NirImport::from_json(
native.as_bytes(),
NirImportOptions::new(1_000, VoltageResolution::Millivolt)
)
.err(),
Some(NirError::BadNumber("weight")),
"{w} in merge.json"
);
assert_eq!(
NirImport::from_json(simulation.as_bytes(), sim_units).err(),
Some(NirError::BadNumber("weight")),
"{w} in two_lif_neurons_sim.json"
);
}
let below = "1.7976931348623155e308";
for (doc, opts) in [
(
in_merge(below),
NirImportOptions::new(1_000, VoltageResolution::Millivolt),
),
(in_sim(below), sim_units),
] {
let path = import_path(doc.as_bytes(), opts, &mut out);
assert!(matches!(path, Ok(Some(_))), "one below: {path:?}");
}
}
#[test]
fn buffer_api_consumes_reference_emission() {
let scan = nir_scan(CHAIN.as_bytes()).unwrap();
let mut nodes = vec![NirNode::BLANK; scan.node_count];
let mut edges = vec![(0u32, 0u32); scan.edge_count];
let mut weights = vec![0i16; scan.weight_cells];
let mut lifs = vec![NirLif::default(); scan.lif_neurons];
let mut scratch = vec![0f64; scan.weight_cells + 5 * scan.lif_neurons];
{
let mut bufs = NirBuffers {
nodes: &mut nodes,
edges: &mut edges,
weights: &mut weights,
lifs: &mut lifs,
scratch: &mut scratch,
};
let report = nir_import(
CHAIN.as_bytes(),
NirImportOptions::new(1_000, VoltageResolution::Millivolt),
&mut bufs,
)
.expect("imports");
assert_eq!(report.weight_cells, 3);
assert!(
report.note_count() >= 1,
"QuantizationLoss noted (non-dyadic scale)"
);
}
assert_eq!(weights, vec![16384, -32767, 8192]);
}
const FUZZ_CASES: usize = 10_000;
const FUZZ_SEED: u64 = 0x9E37_79B9_7F4A_7C15;
const FLOOR_FROM: usize = 3_000;
fn fuzz_config() -> (usize, u64) {
let cases = std::env::var("NIR_FUZZ_CASES").map_or(FUZZ_CASES, |s| {
s.parse().expect("NIR_FUZZ_CASES is a case count")
});
let seed = std::env::var("NIR_FUZZ_SEED")
.map_or(FUZZ_SEED, |s| s.parse().expect("NIR_FUZZ_SEED is a u64"));
assert!(cases > 0, "NIR_FUZZ_CASES: at least one case");
assert!(seed != 0, "NIR_FUZZ_SEED: xorshift needs a nonzero seed");
(cases, seed)
}
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
let mut x = self.0;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.0 = x;
x.wrapping_mul(0x2545_F491_4F6C_DD1D)
}
fn below(&mut self, n: usize) -> usize {
if n == 0 {
0
} else {
(self.next() % n as u64) as usize
}
}
}
struct Seed {
name: String,
bytes: Vec<u8>,
opts: NirImportOptions,
spans: Spans,
}
fn fixture_paths() -> Vec<std::path::PathBuf> {
let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/nir_fixtures");
let mut paths: Vec<_> = std::fs::read_dir(dir)
.expect("the fixtures folder")
.map(|e| e.expect("a fixture").path())
.filter(|p| p.extension().is_some_and(|x| x == "json"))
.collect();
paths.sort();
paths
}
fn mutation_seeds(flight: &InFlight) -> Vec<Seed> {
let centi = NirImportOptions::new(1_000, VoltageResolution::CentiMillivolt);
let builds = |bytes: &[u8], opts: NirImportOptions| {
NirImport::from_json(bytes, opts).is_ok_and(|g| g.build_network().is_ok())
};
fixture_paths()
.iter()
.map(|p| {
let name = p
.file_name()
.expect("a name")
.to_string_lossy()
.into_owned();
let bytes = std::fs::read(p).expect("a fixture reads");
flight.begin(format!("the setup, {name}"), &bytes);
let opts = match NirImport::from_json(
&bytes,
NirImportOptions::new(1_000, VoltageResolution::Millivolt),
) {
Err(NirError::UnitsMismatch { .. }) => NirImportOptions::sim_units(100),
Ok(g) if g.build_network().is_err() && builds(&bytes, centi) => centi,
_ => NirImportOptions::new(1_000, VoltageResolution::Millivolt),
};
Seed {
name,
spans: spans(&bytes),
bytes,
opts,
}
})
.collect()
}
#[derive(Default)]
struct Spans {
numbers: Vec<(usize, usize)>,
strings: Vec<(usize, usize)>,
keys: Vec<(usize, usize)>,
elements: Vec<(usize, usize)>,
members: Vec<(usize, usize)>,
}
fn spans(json: &[u8]) -> Spans {
let mut w = SpanWalk {
b: json,
i: 0,
spans: Spans::default(),
};
w.value();
w.skip_ws();
assert_eq!(w.i, json.len(), "a fixture is one JSON value");
w.spans
}
struct SpanWalk<'a> {
b: &'a [u8],
i: usize,
spans: Spans,
}
impl SpanWalk<'_> {
fn skip_ws(&mut self) {
while self.b.get(self.i).is_some_and(u8::is_ascii_whitespace) {
self.i += 1;
}
}
fn string(&mut self) -> (usize, usize) {
let start = self.i;
self.i += 1;
while self.b[self.i] != b'"' {
self.i += if self.b[self.i] == b'\\' { 2 } else { 1 };
}
self.i += 1;
(start, self.i)
}
fn value(&mut self) -> (usize, usize) {
self.skip_ws();
let start = self.i;
match self.b[start] {
open @ (b'{' | b'[') => {
self.i += 1;
self.skip_ws();
if matches!(self.b[self.i], b'}' | b']') {
self.i += 1;
} else {
loop {
if open == b'{' {
self.skip_ws();
let key = self.string();
self.spans.keys.push(key);
self.skip_ws();
self.i += 1; let (_, end) = self.value();
self.spans.members.push((key.0, end));
} else {
let element = self.value();
self.spans.elements.push(element);
}
self.skip_ws();
self.i += 1; if matches!(self.b[self.i - 1], b'}' | b']') {
break;
}
}
}
}
b'"' => {
let string = self.string();
self.spans.strings.push(string);
}
b'-' | b'0'..=b'9' => {
while self
.b
.get(self.i)
.is_some_and(|&c| matches!(c, b'-' | b'+' | b'.' | b'e' | b'E' | b'0'..=b'9'))
{
self.i += 1;
}
self.spans.numbers.push((start, self.i));
}
_ => {
while self.b.get(self.i).is_some_and(u8::is_ascii_alphabetic) {
self.i += 1;
}
}
}
(start, self.i)
}
}
const EDITS: [&str; 13] = [
"flip a bit",
"cut the end",
"repeat a block",
"delete a block",
"change digits",
"a number to an edge value",
"a string to another",
"a string to a new name",
"a key to another",
"drop an element",
"repeat an element",
"drop a member",
"repeat a member",
];
const EDGE_NUMBERS: &str = "0 -0 1 -1 2 0.5 -0.5 0.1 1e-4 0.0001 9e-5 1e-5 1e-9 \
1e-30 1e-320 5e-324 1e9 1e30 1e308 -1e308 1.7976931348623157e308 1e400 \
3.2767 3.2768 -3.2767 -3.2768 32.767 32.768 -32.767 -32.768 \
32767 32768 -32768 -32769 65535 65536 4294967295 4294967296 \
9007199254740993 100 1000 0.02 0.001 -0.07 -0.08 -0.065";
fn edit(rng: &mut Rng, seed: &Seed, kind: usize) -> Vec<u8> {
let (b, s) = (&seed.bytes, &seed.spans);
let mut out = b.clone();
match kind {
0 => {
let i = rng.below(out.len());
out[i] ^= 1 << rng.below(8);
}
1 => out.truncate(rng.below(out.len())),
2 => {
let i = rng.below(out.len());
let len = 1 + rng.below(32.min(out.len() - i));
let block = out[i..i + len].to_vec();
let at = rng.below(out.len() + 1);
out.splice(at..at, block);
}
3 => {
let i = rng.below(out.len());
let len = 1 + rng.below(16.min(out.len() - i));
out.drain(i..i + len);
}
4 => {
let digits: Vec<usize> = (0..out.len())
.filter(|&i| out[i].is_ascii_digit())
.collect();
if !digits.is_empty() {
let i = digits[rng.below(digits.len())];
if rng.below(2) == 0 {
out[i] = b'0' + rng.below(10) as u8;
} else {
let more: Vec<u8> = (0..1 + rng.below(6))
.map(|_| b'0' + rng.below(10) as u8)
.collect();
out.splice(i..i, more);
}
}
}
5 => {
if let Some((a, z)) = pick(rng, &s.numbers) {
out = splice(b, (a, z), edge_number(rng, &b[a..z]).as_bytes());
}
}
6 => {
if let (Some(at), Some((c, d))) = (pick(rng, &s.strings), pick(rng, &s.strings)) {
out = splice(b, at, &b[c..d]);
}
}
7 => {
if let Some(at) = pick(rng, &s.strings) {
out = splice(b, at, b"\"zz\"");
}
}
8 => {
if let (Some(at), Some((c, d))) = (pick(rng, &s.keys), pick(rng, &s.keys)) {
out = splice(b, at, &b[c..d]);
}
}
9 => {
if let Some(at) = pick(rng, &s.elements) {
out = drop_item(b, at);
}
}
10 => {
if let Some(at) = pick(rng, &s.elements) {
out = repeat_item(b, at);
}
}
11 => {
if let Some(at) = pick(rng, &s.members) {
out = drop_item(b, at);
}
}
_ => {
if let Some(at) = pick(rng, &s.members) {
out = repeat_item(b, at);
}
}
}
out
}
fn pick(rng: &mut Rng, spans: &[(usize, usize)]) -> Option<(usize, usize)> {
(!spans.is_empty()).then(|| spans[rng.below(spans.len())])
}
fn edge_number(rng: &mut Rng, token: &[u8]) -> String {
if rng.below(2) == 0 {
let n = EDGE_NUMBERS.split_whitespace().count();
let value = EDGE_NUMBERS.split_whitespace().nth(rng.below(n));
return value.expect("a draw under the count").to_string();
}
let v: f64 = std::str::from_utf8(token)
.ok()
.and_then(|t| t.parse().ok())
.expect("a fixture's number parses");
let w = match rng.below(6) {
0 => -v,
1 => v * 10.0,
2 => v / 10.0,
3 => v * 1e3,
4 => v / 1e3,
_ => v * (1.0 + 1e-9),
};
format!("{w:e}")
}
fn splice(b: &[u8], (a, z): (usize, usize), with: &[u8]) -> Vec<u8> {
[&b[..a], with, &b[z..]].concat()
}
fn drop_item(b: &[u8], (a, z): (usize, usize)) -> Vec<u8> {
let after = z + b[z..]
.iter()
.take_while(|c| c.is_ascii_whitespace())
.count();
if b.get(after) == Some(&b',') {
return splice(b, (a, after + 1), b"");
}
let before = a - b[..a]
.iter()
.rev()
.take_while(|c| c.is_ascii_whitespace())
.count();
if before > 0 && b[before - 1] == b',' {
return splice(b, (before - 1, z), b"");
}
splice(b, (a, z), b"")
}
fn repeat_item(b: &[u8], (a, z): (usize, usize)) -> Vec<u8> {
splice(b, (z, z), &[b",".as_slice(), &b[a..z]].concat())
}
const STALL: Duration = Duration::from_secs(10);
fn budget(cases: usize) -> Duration {
Duration::from_millis(6 * cases as u64).max(Duration::from_secs(60))
}
#[derive(Default)]
struct InFlight {
begun: AtomicUsize,
cases: AtomicUsize,
step: Mutex<(String, Vec<u8>)>,
}
impl InFlight {
fn begin(&self, what: String, input: &[u8]) {
*self.step.lock().expect("the watchdog never holds it long") = (what, input.to_vec());
self.begun.fetch_add(1, Ordering::Relaxed);
}
fn begin_case(&self, what: String, input: &[u8]) {
self.cases.fetch_add(1, Ordering::Relaxed);
self.begin(what, input);
}
fn in_flight(&self) -> (String, Vec<u8>) {
self.step.lock().map(|s| s.clone()).unwrap_or_default()
}
}
fn under_watchdog(cases: usize, body: impl FnOnce(&InFlight) + Send + 'static) {
let (start, limit) = (Instant::now(), budget(cases));
let flight = Arc::new(InFlight::default());
let (done, finished) = mpsc::channel::<()>();
let worker = {
let flight = Arc::clone(&flight);
std::thread::spawn(move || {
body(&flight);
let _ = done.send(());
})
};
let (mut seen, mut since) = (0, Instant::now());
while let Err(mpsc::RecvTimeoutError::Timeout) =
finished.recv_timeout(Duration::from_millis(200))
{
let begun = flight.begun.load(Ordering::Relaxed);
if begun != seen {
(seen, since) = (begun, Instant::now());
} else if since.elapsed() > STALL {
let (what, bytes) = flight.in_flight();
panic!(
"{what} has run past {STALL:?}, a hang; its bytes: {}",
bytes.escape_ascii()
);
}
if start.elapsed() > limit {
let (what, bytes) = flight.in_flight();
panic!(
"the loop has run past {limit:?}, its budget for {cases} cases, and {} \
cases began; in flight, {what}; its bytes: {}",
flight.cases.load(Ordering::Relaxed),
bytes.escape_ascii()
);
}
}
if let Err(payload) = worker.join() {
panic::resume_unwind(payload);
}
}
fn import_path(
input: &[u8],
opts: NirImportOptions,
out: &mut [Vec<u8>; 3],
) -> Result<Option<bool>, String> {
let Ok(g) = NirImport::from_json(input, opts) else {
return Ok(None);
};
let built = match g.build_network() {
Ok((mut net, enc, _)) => {
steps(&mut net, &enc)?;
true
}
Err(_) => false,
};
let [e1, e2, e3] = out;
let n1 = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, e1)
.map_err(|e| format!("the import does not export: {e:?}"))?;
let g2 = NirImport::from_json(&e1[..n1], g.opts)
.map_err(|e| format!("its export is refused: {e:?}"))?;
same_records(&g, &g2)?;
let n2 = nir_export(&g2.nodes, &g2.edges, &g2.weights, &g2.lifs, g2.opts, e2)
.map_err(|e| format!("the re-import does not export: {e:?}"))?;
let g3 = NirImport::from_json(&e2[..n2], g.opts)
.map_err(|e| format!("the second export is refused: {e:?}"))?;
let n3 = nir_export(&g3.nodes, &g3.edges, &g3.weights, &g3.lifs, g3.opts, e3)
.map_err(|e| format!("the third import does not export: {e:?}"))?;
if e2[..n2] != e3[..n3] {
return Err("the second export and the third differ".to_string());
}
Ok(Some(built))
}
const STEPS: usize = 20;
fn steps(net: &mut SpikingNeuralNetwork, enc: &NirGraphEncoder) -> Result<(), String> {
let drive: Vec<Vec<Thousandths>> = (0..enc.input_count())
.map(|i| vec![Thousandths(1_000); enc.input_features(i)])
.collect();
let per_input: Vec<&[Thousandths]> = drive.iter().map(Vec::as_slice).collect();
let currents = enc.encode(&per_input);
for step in 1..=STEPS {
net.step(¤ts)
.map_err(|e| format!("step {step} of the network it builds: {e:?}"))?;
}
Ok(())
}
fn same_records(a: &NirImport<'_>, b: &NirImport<'_>) -> Result<(), String> {
let linear = |n: &NirNode<'_>| {
n.linear.map(|l| {
(
l.rows,
l.cols,
l.weight_offset,
l.scale.to_bits(),
l.zero_tensor,
)
})
};
let same_node = |x: &NirNode<'_>, y: &NirNode<'_>| {
(x.name, x.kind, x.shape, x.shape_len, x.lif)
== (y.name, y.kind, y.shape, y.shape_len, y.lif)
&& linear(x) == linear(y)
};
let lif = |l: &NirLif| {
(
l.tau_us,
l.resistance_mohm,
l.capacitance_pf,
l.capacitance_clamped,
l.leak_q,
l.threshold_q,
l.reset_q,
)
};
let nodes_hold = a.nodes.len() == b.nodes.len()
&& a.nodes.iter().zip(&b.nodes).all(|(x, y)| same_node(x, y));
if !nodes_hold {
return Err("the export re-imports other nodes".to_string());
}
if a.edges != b.edges {
return Err("the export re-imports other edges".to_string());
}
if a.weights != b.weights {
return Err("the export re-imports other weights".to_string());
}
if !a.lifs.iter().map(lif).eq(b.lifs.iter().map(lif)) {
return Err("the export re-imports other LIF records".to_string());
}
Ok(())
}
#[test]
fn mutated_fixtures_are_refused_or_round_trip() {
let (cases, seed) = fuzz_config();
under_watchdog(cases, move |flight| {
let seeds = mutation_seeds(flight);
let mut rng = Rng(seed);
let mut cuts = Rng(seed.wrapping_mul(0x2545_F491_4F6C_DD1D) | 1);
let mut out = [vec![0u8; 1 << 16], vec![0u8; 1 << 16], vec![0u8; 1 << 16]];
let (mut imported, mut built, mut unedited) = (0usize, 0usize, 0usize);
let (mut scratch_tried, mut scratch_held) = (0usize, 0usize);
let mut reach = vec![(0usize, 0usize); seeds.len()];
for case in 1..=cases {
let kind = rng.below(EDITS.len());
let at = rng.below(seeds.len());
let s = &seeds[at];
let input = edit(&mut rng, s, kind);
let what = format!("case {case} at seed {seed}, {} on {}", EDITS[kind], s.name);
flight.begin_case(what.clone(), &input);
let path = panic::catch_unwind(AssertUnwindSafe(|| {
let imports = import_path(&input, s.opts, &mut out)?;
let held = if imports.is_some() {
let g = NirImport::from_json(&input, s.opts).expect("it imported");
short_buffers(&input, &g, &mut out[0], &mut cuts)?
} else {
None
};
Ok::<_, String>((imports, held))
}))
.unwrap_or_else(|_| {
panic!(
"{what}: the import path or a short buffer panicked (above); its bytes: {}",
input.escape_ascii()
)
});
let (imports, held) = path.unwrap_or_else(|rule| {
panic!("{what}: {rule}; its bytes: {}", input.escape_ascii())
});
let edited = input != s.bytes;
unedited += usize::from(!edited);
if let (Some(b), true) = (imports, edited) {
imported += 1;
built += usize::from(b);
reach[at].0 += 1;
reach[at].1 += usize::from(b);
}
if let Some(h) = held {
scratch_tried += 1;
scratch_held += usize::from(h);
}
}
let per_fixture: Vec<String> = seeds
.iter()
.zip(&reach)
.map(|(s, (i, b))| format!("{} {i}/{b}", s.name))
.collect();
println!(
"{cases} cases at seed {seed}: {imported} imported, {built} built and stepped, not \
counting {unedited} cases that left the fixture as it was; {scratch_held} of \
{scratch_tried} short scratch buffers still imported, with the same records; per \
fixture, its edited cases imported/built: {}",
per_fixture.join(", ")
);
if cases >= FLOOR_FROM {
assert!(
imported * 1000 >= cases * 55,
"{imported} of {cases} cases imported, under the floor of 5.5 %"
);
assert!(
built * 1000 >= cases * 25,
"{built} of {cases} cases built a network, under the floor of 2.5 %"
);
for (s, (_, b)) in seeds.iter().zip(&reach) {
assert!(
*b > 0 || s.name.starts_with("neg_"),
"{} built no network in its edited cases, and is not a negative",
s.name
);
}
}
});
}
fn short_buffers(
input: &[u8],
g: &NirImport<'_>,
out: &mut [u8],
cuts: &mut Rng,
) -> Result<Option<bool>, String> {
let scan = nir_scan(input).map_err(|e| format!("an import's scan fails: {e:?}"))?;
let sizes = [
scan.node_count,
scan.edge_count,
scan.weight_cells,
scan.lif_neurons,
scan.weight_cells + 5 * scan.lif_neurons,
];
let names = ["nodes", "edges", "weights", "lifs", "scratch"];
let mut scratch_held = None;
for short in [None, Some(0), Some(1), Some(2), Some(3), Some(4)] {
if short.is_some_and(|i| sizes[i] == 0) {
continue;
}
let len = |i: usize| sizes[i] - usize::from(short == Some(i));
let mut nodes = vec![NirNode::BLANK; len(0)];
let mut edges = vec![(0u32, 0u32); len(1)];
let mut weights = vec![0i16; len(2)];
let mut lifs = vec![NirLif::default(); len(3)];
let mut scratch = vec![0f64; len(4)];
let r = nir_import(
input,
g.opts,
&mut NirBuffers {
nodes: &mut nodes,
edges: &mut edges,
weights: &mut weights,
lifs: &mut lifs,
scratch: &mut scratch,
},
)
.map(|_| ());
let same = nodes == g.nodes && edges == g.edges && weights == g.weights && lifs == g.lifs;
let held = match (short, r) {
(None | Some(4), Ok(())) => same,
(Some(_), Err(NirError::BufferOverflow)) => true,
_ => false,
};
if !held {
let which = short.map_or_else(
|| "the buffers the scan asks for".to_string(),
|i| format!("{} one short", names[i]),
);
let records = if same { "the same" } else { "other" };
return Err(format!(
"nir_import with {which} gave {r:?}, {records} records"
));
}
if short == Some(4) {
scratch_held = Some(r.is_ok());
}
}
let full = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, out)
.map_err(|e| format!("the import does not export: {e:?}"))?;
for cut in [full - 1, cuts.below(full)] {
let r = nir_export(
&g.nodes,
&g.edges,
&g.weights,
&g.lifs,
g.opts,
&mut out[..cut],
);
if r != Err(NirError::ExportTooSmall) {
return Err(format!(
"an export into {cut} of its {full} bytes gave {r:?}"
));
}
}
Ok(scratch_held)
}
#[test]
fn every_cut_of_a_fixture_is_refused_or_round_trips() {
let cuts: usize = fixture_paths()
.iter()
.map(|p| std::fs::metadata(p).expect("a fixture").len() as usize + 1)
.sum();
under_watchdog(cuts, move |flight| {
let seeds = mutation_seeds(flight);
let mut out = [vec![0u8; 1 << 16], vec![0u8; 1 << 16], vec![0u8; 1 << 16]];
let (mut imported, mut built, mut exports, mut short) = (0usize, 0usize, 0usize, 0usize);
for s in &seeds {
let n = s.bytes.len();
let whole_imports = NirImport::from_json(&s.bytes, s.opts).is_ok();
for len in 0..=n {
let input = &s.bytes[..len];
let what = format!("{} cut to {len} of its {n} bytes", s.name);
flight.begin_case(what.clone(), input);
let path =
panic::catch_unwind(AssertUnwindSafe(|| import_path(input, s.opts, &mut out)))
.unwrap_or_else(|_| panic!("{what}: the import path panicked (above)"))
.unwrap_or_else(|rule| panic!("{what}: {rule}"));
let only_whitespace = s.bytes[len..].iter().all(u8::is_ascii_whitespace);
assert_eq!(
path.is_some(),
whole_imports && only_whitespace,
"{what}: imported is {}",
path.is_some()
);
if let Some(b) = path {
imported += 1;
built += usize::from(b);
}
}
let Ok(g) = NirImport::from_json(&s.bytes, s.opts) else {
continue;
};
let full = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out[0])
.expect("an import exports");
exports += 1;
for cut in 0..full {
flight.begin(
format!("{} exported into {cut} of its {full} bytes", s.name),
&[],
);
let r = nir_export(
&g.nodes,
&g.edges,
&g.weights,
&g.lifs,
g.opts,
&mut out[0][..cut],
);
assert_eq!(
r,
Err(NirError::ExportTooSmall),
"{} exported into {cut} of its {full} bytes",
s.name
);
short += 1;
}
}
println!(
"{cuts} cuts of {} fixtures: {imported} imported, {built} built and stepped, each \
a fixture whole or without its trailing whitespace; {exports} fixtures exported \
into each of {short} short buffers, each refused",
seeds.len()
);
});
}