gresil 0.1.0-alpha.9

Spiking neural networks in no_std Rust, i16 fixed point, for microcontrollers
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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// SPDX-FileCopyrightText: 2026 philo-the-dendron
// SPDX-License-Identifier: AGPL-3.0-or-later
//! The trace cases and their writer, shared by `examples/trace.rs` (which
//! writes the files) and `tests/traces.rs` (which compares them). No
//! library code: a case is a network built through the public API plus a
//! drive, and a row is what `step()` returned and `neurons()` after it.
//!
//! One file per case in this directory, in the library's format,
//! `neuralos-trace v1` (`gresil::trace`'s module doc); the board
//! case keeps its spiking steps only, 147 rows of 10,000 steps. Where a
//! case keeps the noise, it is the neuron's own LFSR, seeded by id and
//! time, so every case is a pure function of this file.
//!
//! Two words, two meanings (PR B). A `regression` trace comes from this
//! code and is compared with itself across commits: a trace that moves is
//! red, and a deliberate move is regenerated by
//! `cargo run -p gresil --features unstable-stdp,unstable-freeze --example trace -- write`
//! in a commit whose
//! message says which cases moved and why. A `reference` vector is also
//! checked against an oracle that is not this code: the unbounded-integer
//! neuron equation in `tests/traces.rs`. The only oracle today is that
//! neuron model, so there is one reference vector, a bare neuron with no
//! synapse and no spike.
//!
//! The fixed network. `freeze` writes `frozen.rs` in this directory with
//! the library's freezer (`gresil::fixed::freeze`): every case
//! whose network has plasticity off, as the arrays a `FixedNetwork`
//! steps, and `for_each_frozen!` over them. `plasticity-on` is the one
//! case it cannot freeze, since a `FixedNetwork` has no plasticity. A
//! frozen network writes its rows through the library's row writer
//! (`gresil::trace::row`), a second writer beside `render`, and
//! `tests/traces.rs` holds both against the files.

use std::path::{Path, PathBuf};

#[cfg(feature = "unstable-freeze")]
use gresil::fixed::freeze;
use gresil::nir::{NirImport, NirImportOptions, Thousandths};
use gresil::trace::{self, Kind, Rows};
use gresil::{LIFNeuron, NetworkTopology, NeuronType, SpikingNeuralNetwork, VoltageResolution};

/// Where the files live: this directory, resolved from the crate root.
pub const DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/traces");
/// Every case steps at 1 ms, the firmware's `DT_US` and the crate's own
/// tests' step, but the two NIR witnesses, which step at 0.1 ms: the step
/// snnTorch's exporter assumes and `gresil-nir2json --sim-units` takes by
/// default (`witness`).
const DT_US: u32 = 1_000;

/// One case: a name, what it is, how long it runs, and how to build it.
pub struct Case {
    pub name: &'static str,
    pub kind: Kind,
    pub steps: u32,
    pub rows: Rows,
    pub build: fn() -> Run,
}

/// A built network and its drive: the input currents (pA per neuron) of a
/// given step.
pub struct Run {
    pub net: SpikingNeuralNetwork,
    pub drive: Box<dyn Fn(u32) -> Vec<i16>>,
}

/// The file of a case.
pub fn path(case: &Case) -> PathBuf {
    Path::new(DIR).join(format!("{}.trace", case.name))
}

/// A case's header line, the file's first, without its newline: what the
/// built network says of itself before its first step, written by the
/// library (`gresil::trace::header`).
fn header(case: &Case, net: &SpikingNeuralNetwork) -> String {
    trace::header(net, case.name, case.kind, case.steps, case.rows)
        .expect("every case is on one grid and named by the rule")
}

/// The frozen file, `frozen.rs` in this directory.
#[cfg(feature = "unstable-freeze")]
pub fn frozen_path() -> PathBuf {
    Path::new(DIR).join("frozen.rs")
}

/// The text of `frozen.rs` (module doc): every plasticity-off case, built,
/// as one module named after it (`-` becomes `_`) holding `N`, `S`,
/// `DT_US`, `STEPS`, `SPIKES_ONLY`, `HEADER` (the trace's first line),
/// `NEURONS` (one constructor call and one setter per parameter each, as
/// built, before the first step), `SYNAPSES` (`FixedSynapse::from_network`:
/// the CSR order) and `DRIVE` (runs of equal steps). Then
/// `for_each_frozen!`, which applies a caller's macro to every module, so
/// no list is kept by hand.
///
/// # Panics
///
/// When a case's drive is not one current per neuron, which no case here
/// is; and with every refusal of `freeze::module`, none of which a case
/// here trips: each is built and frozen before its first step, on one
/// grid, named by the rule, and each numbers its neurons from 0; the one
/// case with plasticity on is skipped.
#[cfg(feature = "unstable-freeze")]
pub fn freeze() -> String {
    let mut out = String::from(freeze::preamble());
    let mut frozen = Vec::new();
    for case in CASES {
        let Run { net, drive } = (case.build)();
        if net.plasticity_enabled() {
            continue;
        }
        let drive = runs(case.name, &*drive, case.steps, net.neurons().len());
        out.push('\n');
        out.push_str(&freeze::module(
            &net, case.name, case.kind, case.steps, case.rows, &drive,
        ));
        frozen.push(case.name);
    }
    out.push('\n');
    out.push_str(&freeze::tail(&frozen));
    out
}

/// A case's drive over its run, as runs of equal steps: `(count,
/// currents)`, one current per neuron.
#[cfg(feature = "unstable-freeze")]
fn runs(name: &str, drive: &dyn Fn(u32) -> Vec<i16>, steps: u32, n: usize) -> Vec<(u32, Vec<i16>)> {
    let mut runs: Vec<(u32, Vec<i16>)> = Vec::new();
    for step in 0..steps {
        let currents = drive(step);
        assert_eq!(
            currents.len(),
            n,
            "{name} step {step}: one current per neuron"
        );
        match runs.last_mut() {
            Some((count, last)) if *last == currents => *count += 1,
            _ => runs.push((1, currents)),
        }
    }
    runs
}

/// Build the case, run it, and return the file's text.
///
/// # Panics
///
/// When a step fails, which the public API only does for a bad
/// construction, and every case here constructs.
pub fn render(case: &Case) -> String {
    let Run { mut net, drive } = (case.build)();
    let mut out = header(case, &net);
    out.push('\n');
    for step in 0..case.steps {
        let time_us = net.time_us();
        let spikes = net.step(&drive(step)).expect("a built case steps");
        if matches!(case.rows, Rows::Spikes) && spikes.is_empty() {
            continue;
        }
        let ids: Vec<String> = spikes.iter().map(|s| s.neuron_id.to_string()).collect();
        let membranes: Vec<String> = net
            .neurons()
            .iter()
            .map(|n| n.membrane_potential().to_string())
            .collect();
        out.push_str(&format!(
            "{step} {time_us} | {} | {}\n",
            ids.join(" "),
            membranes.join(" ")
        ));
    }
    out
}

/// A neuron with the noise set. A topology builder gives its neurons 5 pA
/// and `LIFNeuron::new` 0; a case that wants silence builds its neurons
/// here and hands them to `from_neurons`.
fn neuron(id: u16, kind: NeuronType, res: VoltageResolution, noise_pa: u8) -> LIFNeuron {
    LIFNeuron::new(id, kind, res).with_noise_amplitude_pa(noise_pa)
}

/// A network from given neurons and the given edges, which its first step
/// sorts.
fn wired(neurons: Vec<LIFNeuron>, edges: &[(u16, u16, i16)]) -> SpikingNeuralNetwork {
    let mut net = SpikingNeuralNetwork::from_neurons(neurons, DT_US).expect("neurons given");
    for &(pre, post, w) in edges {
        net.add_synapse(pre, post, w)
            .expect("ids in range, no self edge");
    }
    net
}

/// The reference chain of the NIR fixtures (`tests/nir_fixtures/`,
/// the pinned reference's own emission).
const NIR_CHAIN: &[u8] = include_bytes!("../nir_fixtures/chain.json");
/// The two witnesses of D8, the spiking Linear edge, as
/// `gresil-nir2json --sim-units` converts them
/// (`tests/nir_fixtures/README.md`).
const SNNTORCH_TWO_LAYER: &[u8] = include_bytes!("../nir_fixtures/snntorch_two_layer_sim.json");
const TWO_LIF_NEURONS: &[u8] = include_bytes!("../nir_fixtures/two_lif_neurons_sim.json");

/// A regression case over every step.
const fn regression(name: &'static str, steps: u32, build: fn() -> Run) -> Case {
    Case {
        name,
        kind: Kind::Regression,
        steps,
        rows: Rows::All,
        build,
    }
}

/// Every case, in file order: the reference vector, the board, then the
/// regression traces.
pub const CASES: &[Case] = &[
    Case {
        name: "neuron-reference",
        kind: Kind::Reference,
        steps: 150,
        rows: Rows::All,
        build: neuron_reference,
    },
    Case {
        name: "one-neuron-board",
        kind: Kind::Regression,
        steps: 10_000,
        rows: Rows::Spikes,
        build: one_neuron_board,
    },
    regression("chain-3", 100, chain_3),
    regression("feedforward-8", 150, feedforward_8),
    regression("recurrent-transmission", 30, recurrent_transmission),
    regression("centi-mv-grid", 30, centi_mv_grid),
    regression("inhibitory", 100, inhibitory),
    regression("refractory", 40, refractory),
    #[cfg(feature = "unstable-stdp")]
    regression("plasticity-on", 150, plasticity_on),
    regression("plasticity-off", 150, plasticity_off),
    regression("saturation-floor-ceiling", 100, saturation_floor_ceiling),
    regression("nir-chain-fixture", 100, nir_chain_fixture),
    regression("ternary-weights", 100, ternary_weights),
    regression("snntorch-two-layer", 150, snntorch_two_layer),
    regression("two-lif-neurons", 150, two_lif_neurons),
];

/// The reference vector: one excitatory neuron on the centi-mV grid, no
/// noise, the threshold out of reach (so the vector is the integration
/// alone, and the oracle needs no spike, reset, refractory or adaptation
/// term). The drive walks four regimes: the board's 160 pA toward its
/// steady state, the largest current to the ceiling, the smallest to the
/// floor, then nothing, back toward rest.
fn neuron_reference() -> Run {
    let n = neuron(
        0,
        NeuronType::Excitatory,
        VoltageResolution::CentiMillivolt,
        0,
    )
    .with_threshold(i16::MAX);
    let net = wired(vec![n], &[]);
    Run {
        net,
        drive: Box::new(|step| {
            vec![match step {
                0..=39 => 160,
                40..=69 => i16::MAX,
                70..=99 => i16::MIN,
                _ => 0,
            }]
        }),
    }
}

/// The firmware's neuron (`firmware/esp32c3/src/main.rs`): excitatory,
/// centi-mV grid, the constructor's noise (0), 160 pA, one step per ms for
/// 10,000 steps. The network's step does what the firmware's `step` does
/// (adaptation decay, then integrate at `step × DT_US`), so the spike
/// rows fold to the board's record: 147 spikes, the first at step 55,
/// checksum `8cd1e63b` (`evidence/esp32c3-bringup/README.md`; with the
/// noise at 5, as before round 61, it was `0b78b456`).
fn one_neuron_board() -> Run {
    let net = wired(
        vec![LIFNeuron::new(
            0,
            NeuronType::Excitatory,
            VoltageResolution::CentiMillivolt,
        )],
        &[],
    );
    Run {
        net,
        drive: Box::new(|_| vec![160]),
    }
}

/// Three excitatory neurons in a line, centi-mV, no noise, divisor 1 and
/// 5,000-weight edges: a 5,000 pA pulse lifts a neuron from anywhere
/// above its reset to threshold in one step. Neuron 0 is driven at
/// 600 pA and fires every ten steps or so; 1 fires the step after 0, and
/// 2 the step after 1.
fn chain_3() -> Run {
    let ns = (0..3)
        .map(|id| {
            neuron(
                id,
                NeuronType::Excitatory,
                VoltageResolution::CentiMillivolt,
                0,
            )
        })
        .collect();
    let mut net = wired(ns, &[(0, 1, 5_000), (1, 2, 5_000)]);
    net.set_synaptic_input_divisor(1).expect("in 1 to 32,767");
    Run {
        net,
        drive: Box::new(|_| vec![600, 0, 0]),
    }
}

/// The `Feedforward` topology builder, layers 3-3-2, on the centi-mV
/// grid with the builder's noise, 5 pA (the builder's neurons cannot be
/// reached before the first step; the LFSR is deterministic). Divisor 1,
/// so the builder's 100-weight edges land as 100 pA pulses, +50 quanta
/// on an excitatory neuron. The first layer is driven to fire every ten
/// steps or so; the second is biased at 148 pA, where the grid's
/// truncation parks it about 40 quanta under threshold and one pulse
/// fires it; the third, the two inhibitory neurons (ids 6 and 7, the
/// 80/20 split), at 199 pA, the same idea on its own threshold. A spiked
/// neuron takes most of a hundred steps to climb back, so the deeper
/// layers fire twice in the window and the first fourteen times.
fn feedforward_8() -> Run {
    let mut net = SpikingNeuralNetwork::new_with_voltage_resolution(
        8,
        DT_US,
        NetworkTopology::Feedforward { layers: &[3, 3, 2] },
        VoltageResolution::CentiMillivolt,
    )
    .expect("8 neurons, 1 ms");
    net.build_topology().expect("layers sum to 8");
    net.set_synaptic_input_divisor(1).expect("in 1 to 32,767");
    Run {
        net,
        drive: Box::new(|_| vec![600, 600, 600, 148, 148, 148, 199, 199]),
    }
}

/// Session F's pin on the mV grid (`network.rs`,
/// `transmission_is_live_one_step_delayed_mv_strong_weight`): an
/// excitatory neuron fires at step 0 on 3,000 pA, and its 2,000-weight
/// edge moves the inhibitory neuron by +2 mV exactly one step later,
/// not on the spike's own step. Silent after step 0. Plasticity off: the
/// pin is transmission, here and in `centi-mv-grid`.
fn recurrent_transmission() -> Run {
    let net = wired(
        vec![
            neuron(0, NeuronType::Excitatory, VoltageResolution::Millivolt, 0),
            neuron(1, NeuronType::Inhibitory, VoltageResolution::Millivolt, 0),
        ],
        &[(0, 1, 2_000)],
    );
    Run {
        net,
        drive: Box::new(|step| {
            if step == 0 {
                vec![3_000, 0]
            } else {
                vec![0, 0]
            }
        }),
    }
}

/// The same pin on the centi-mV grid
/// (`transmission_is_live_one_step_delayed_centimv`): a 125-weight edge,
/// a +12 pA pulse, and the inhibitory neuron moves −7,000 → −6,988, sub-mV
/// motion the mV grid cannot show.
fn centi_mv_grid() -> Run {
    let net = wired(
        vec![
            neuron(
                0,
                NeuronType::Excitatory,
                VoltageResolution::CentiMillivolt,
                0,
            ),
            neuron(
                1,
                NeuronType::Inhibitory,
                VoltageResolution::CentiMillivolt,
                0,
            ),
        ],
        &[(0, 1, 125)],
    );
    Run {
        net,
        drive: Box::new(|step| {
            if step == 0 {
                vec![3_000, 0]
            } else {
                vec![0, 0]
            }
        }),
    }
}

/// An inhibitory neuron (0) with a −10,000-weight edge onto an excitatory
/// one (1), both driven at 250 pA, centi-mV, no noise, divisor 10. Alone,
/// neuron 1 would fire every twelve steps or so; each −1,000 pA pulse
/// knocks it down by about 500 quanta, and it fires three times in the
/// window against neuron 0's five.
fn inhibitory() -> Run {
    let net = wired(
        vec![
            neuron(
                0,
                NeuronType::Inhibitory,
                VoltageResolution::CentiMillivolt,
                0,
            ),
            neuron(
                1,
                NeuronType::Excitatory,
                VoltageResolution::CentiMillivolt,
                0,
            ),
        ],
        &[(0, 1, -10_000)],
    );
    Run {
        net,
        drive: Box::new(|_| vec![250, 250]),
    }
}

/// One excitatory neuron at 3,000 pA, centi-mV, no noise: a spike, two
/// steps frozen at the reset potential (2 ms refractory at 1 ms steps),
/// two steps of climb, a spike. The 2 pA of adaptation each spike adds
/// decays to 0 in the two frozen steps, so every climb is the same.
fn refractory() -> Run {
    let net = wired(
        vec![neuron(
            0,
            NeuronType::Excitatory,
            VoltageResolution::CentiMillivolt,
            0,
        )],
        &[],
    );
    Run {
        net,
        drive: Box::new(|_| vec![3_000]),
    }
}

/// Two excitatory neurons, centi-mV, no noise, one 1,000-weight edge at
/// divisor 1 (a +500-quanta pulse), 0 driven at 600 pA and 1 at 200 pA so
/// both fire and pair. With STDP on the weight drifts by a few units per
/// pairing, and each unit is visible in neuron 1's membrane on the pulse
/// steps; with it off the two traces part at the first pairing.
fn plastic(enabled: bool) -> Run {
    let ns = (0..2)
        .map(|id| {
            neuron(
                id,
                NeuronType::Excitatory,
                VoltageResolution::CentiMillivolt,
                0,
            )
        })
        .collect();
    let mut net = wired(ns, &[(0, 1, 1_000)]);
    #[cfg(feature = "unstable-stdp")]
    net.set_plasticity_enabled(enabled);
    #[cfg(not(feature = "unstable-stdp"))]
    assert!(!enabled, "no switch without `unstable-stdp`");
    net.set_synaptic_input_divisor(1).expect("in 1 to 32,767");
    Run {
        net,
        drive: Box::new(|_| vec![600, 200]),
    }
}

#[cfg(feature = "unstable-stdp")]
fn plasticity_on() -> Run {
    plastic(true)
}

fn plasticity_off() -> Run {
    plastic(false)
}

/// One excitatory neuron on the mV grid, no noise, threshold out of
/// reach: the largest current pins the membrane at the +50 mV ceiling,
/// nothing lets it fall back (6 mV a step at first, then the mV grid's
/// truncation slows it), the smallest pins it at the −100 mV floor, and
/// nothing again.
fn saturation_floor_ceiling() -> Run {
    let n =
        neuron(0, NeuronType::Excitatory, VoltageResolution::Millivolt, 0).with_threshold(i16::MAX);
    let net = wired(vec![n], &[]);
    Run {
        net,
        drive: Box::new(|step| {
            vec![match step {
                0..=19 => i16::MAX,
                20..=49 => 0,
                50..=69 => i16::MIN,
                _ => 0,
            }]
        }),
    }
}

/// The reference chain (Input 3 → Linear 1×3 → LIF → Output) imported
/// with the mV options at 1 ms and built by `build_network`, driven through
/// its encoder with `[4, 0, 0]` as the assembly gate's frozen pin is:
/// 9 spikes in 100 steps, the first at step 6
/// (`examples/nir_assembly_gate.rs`, gate 6).
fn nir_chain_fixture() -> Run {
    let g = NirImport::from_json(
        NIR_CHAIN,
        NirImportOptions::new(1_000, VoltageResolution::Millivolt),
    )
    .expect("the reference emission imports");
    let (net, enc, _report) = g.build_network().expect("the chain assembles");
    Run {
        net,
        drive: Box::new(move |_| enc.encode(&[&[4_000, 0, 0].map(Thousandths)])),
    }
}

/// Three excitatory neurons on the mV grid, no noise, edges 2,400, 900
/// and −1,500 snapped by `ternarize_weights` to ±γ with γ = 1,600 (the
/// mean magnitude), divisor 1: neuron 1 takes +8 mV per spike of 0,
/// neuron 2 takes +8 from 0 and −8 from 1. Neurons 1 and 2 are biased at
/// 300 pA so they fire on their own and the pulses move their timing.
fn ternary_weights() -> Run {
    let ns = (0..3)
        .map(|id| neuron(id, NeuronType::Excitatory, VoltageResolution::Millivolt, 0))
        .collect();
    let mut net = wired(ns, &[(0, 1, 2_400), (0, 2, 900), (1, 2, -1_500)]);
    let gamma = net.ternarize_weights();
    assert_eq!(gamma, 1_600, "the mean magnitude of 2,400, 900 and 1,500");
    net.set_synaptic_input_divisor(1).expect("in 1 to 32,767");
    Run {
        net,
        drive: Box::new(|_| vec![600, 300, 300]),
    }
}

/// A D8 witness in simulation units at 0.1 ms, as `--sim-units` imports
/// it (`NirImportOptions::sim_units`, the centi-mV grid a spiking Linear
/// edge requires), built by `build_network`: two neurons, lif1 → lif2 by
/// one synapse of 1,000 quanta a spike (a weight of 1.0 at true scale),
/// driven through the graph's own encoder with the one input feature
/// `drive(step)`.
fn witness(json: &'static [u8], drive: fn(u32) -> Thousandths) -> Run {
    // 100 µs is gresil-nir2json's `SIM_DT_US`, the `--sim-units`
    // default: the converter depends on this crate, so its constant is
    // out of reach here
    let g = NirImport::from_json(json, NirImportOptions::sim_units(100))
        .expect("the converted witness imports");
    let (net, enc, _report) = g.build_network().expect("the two-layer graph assembles");
    Run {
        net,
        drive: Box::new(move |step| enc.encode(&[&[drive(step)]])),
    }
}

/// The framework witness: snnTorch 1.0's own export of `Linear → Leaky →
/// Linear → Leaky`. Input feature 1 on steps 0–9 and 50–59, else 0, so
/// the edge shows: a driven step adds exactly one threshold and NIR fires
/// on `v > v_threshold`, so lif1 fires on every other driven step from
/// step 1; each spike reaches lif2 one step later, and lif2 fires when a
/// pulse finds it above rest. Both are silent between the bursts and
/// after them.
fn snntorch_two_layer() -> Run {
    witness(SNNTORCH_TWO_LAYER, |step| {
        Thousandths(i32::from(matches!(step, 0..=9 | 50..=59)) * 1_000)
    })
}

/// The reference witness: the NIR paper's own `two_lif_neurons`, the same
/// shape, input feature 1 every step. lif1's leak sits above its
/// threshold, so it fires from rest at step 0, then every 63 steps, where
/// the same equation in floating point takes 61, since the substrate
/// truncates each step's move to whole quanta
/// (`LIFNeuron::integrate_and_fire` § Semantics, Rounding); lif2
/// (threshold 20) stays below threshold, as the paper's model means it
/// to.
fn two_lif_neurons() -> Run {
    witness(TWO_LIF_NEURONS, |_| Thousandths(1_000))
}