gras 2.0.1

A Genetic Programming framework for Neural Architecture Search in Rust
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
//! Population diversity — are the live nets still DIFFERENT from each other?
//!
//! A long race can end with every net doing the same thing without the logs ever
//! saying so: the elite table shows the best net, the rollup shows the population's
//! means, and neither notices that the population has collapsed onto one blueprint.
//! This module measures that directly.
//!
//! **The metric is STRUCTURAL and deterministic.** A net becomes a multiset
//! genes — one token per node (`N:<kind>:<combine>:<std>:<activation>:<width>`), one
//! per output port (the activation that port emits), one per Input port's
//! feature-mask width, and one per WIRE (`E:<from_kind>><to_kind>`) — and two nets
//! are compared by multiset Jaccard distance:
//!
//! ```text
//! Δ(a, b) = 1 − (Σ min(a_t, b_t)) / (Σ max(a_t, b_t))      // 0 = identical, 1 = disjoint
//! ```
//!
//! **No matrix is materialised.** Δ is symmetric, so only the upper triangle
//! walked (`i < j`) — one pair at a time, `O(1)` memory beyond the genome set —
//! the pair count is therefore `n(n−1)/2` (a 100-net population is 4,950 pairs,
//! 10,000).
//!
//! `mean pairwise Δ` is the mean of that distance over pairs of live nets,
//! starts high (founders share only the Input/Output skeleton) and falls
//! crossover and selection homogenise the population. `distinct` is the blunter
//! companion number: how many byte-different blueprints are alive
//!
//! **The census is the shape reading.** `nodes`, `wires` and `dim` carry each
//! trait's mean AND its spread across the live nets, because a collapsing
//! population keeps its mean while the spread falls to zero: the mean says what
//! population looks like, the spread says whether it still varies.
//!
//! **What it is not.** A PROXY for behavioural convergence: two nets can share
//! gene and still hold different weights, and two differently-wired nets can compute
//! nearly the same policy. The engine never runs the environment (the trainer owns
//! it), so a behavioural number — action agreement on a probe set, say — belongs
//! beside the trainer's own eval games, not here.

use crate::graph::node::{CombineOp, NodeKind, StandardizeOp};
use crate::graph::topology::Topology;
use crate::state::RaceState;
use std::collections::{HashMap, HashSet};

/// One reading of population diversity (`log_diversity` renders
pub(crate) struct DiversityReport {
    /// Live nets in the population.
    pub live: usize,
    /// How many of them hold a DIFFERENT blueprint (byte-identical topology JSON):
    /// `live` means every net is unique, `1` means one clone army.
    pub distinct: usize,
    /// Mean pairwise multiset-Jaccard distance over the sampled pairs (0..1).
    pub mean_delta: f32,
    /// Pairs actually compared (all pairs up to a cap, then a seeded sample).
    pub pairs: usize,
    /// Per-net node count: mean and spread across the live nets.
    pub nodes: Trait,
    /// Per-net wire count: mean and spread across the live nets.
    pub wires: Trait,
    /// Hidden-node width, pooled over every hidden node in the population (one
    /// value per node, not one mean per net): mean and spread.
    pub dim: Trait,
}

/// One structural trait's population reading: its mean and its spread.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Trait {
    /// The trait's mean across the live nets.
    pub mean: f32,
    /// The trait's population std — how much the nets still differ
    pub spread: f32,
}

/// Mean and population spread of `values`. An empty slice reads `0.0 ± 0.0`
/// (nothing alive to describe) rather than NaN.
pub(crate) fn trait_of(values: &[f32]) -> Trait {
    if values.is_empty() {
        return Trait {
            mean: 0.0,
            spread: 0.0,
        };
    }
    let mean = values.iter().sum::<f32>() / values.len() as f32;
    let var = values
        .iter()
        .map(|v| {
            let d = v - mean;
            d * d
        })
        .sum::<f32>()
        / values.len() as f32;
    Trait {
        mean,
        spread: var.sqrt(),
    }
}

/// One net's structural reading: the gene multiset the pair metric uses, plus the census legs
/// was read
#[derive(Clone)]
pub(crate) struct NetShape {
    /// The net's genes — exactly what [`delta`] compares.
    pub genome: HashMap<String, usize>,
    /// Nodes in the blueprint.
    pub nodes: usize,
    /// Wires in the blueprint.
    pub wires: usize,
    /// Mean hidden-node width; `0.0` for a net with no hidden node.
    pub dim: f32,
    /// Share of the input each Input port reads on average: `1.0` = no split, `1/ports` = a
    /// perfect partition; `None` = no readable Input node.
    pub mask: Option<f32>,
}

/// Read one net's structure: the pair metric's genes plus the census legs.
pub(crate) fn shape_of(topo: &Topology) -> NetShape {
    let hidden: Vec<f32> = topo
        .nodes
        .iter()
        .filter(|n| n.kind == NodeKind::Hidden)
        .filter_map(|n| n.hidden_dim.map(|d| d as f32))
        .collect();
    let mask = match topo.nodes.iter().find(|n| n.kind == NodeKind::Input) {
        None => None,
        Some(input) => match input.feature_masks.as_ref() {
            None => Some(1.0),
            Some(rows) => {
                let ports = rows.len();
                let width = rows.first().map_or(0, Vec::len);
                (ports > 0 && width > 0).then(|| {
                    let ones = rows.iter().flatten().filter(|b| **b != 0).count();
                    ones as f32 / (ports * width) as f32
                })
            }
        },
    };
    NetShape {
        genome: genes(topo),
        nodes: topo.nodes.len(),
        wires: topo.connections.len(),
        dim: trait_of(&hidden).mean,
        mask,
    }
}

/// Mean Δ from ONE net to a set of reference nets: `0.0` means the net is structurally identical
/// every reference, `1.0` that it shares nothing with any of them.
pub(crate) fn mean_delta_to<'a, I>(shape: &NetShape, reference: I) -> Option<f32>
where
    I: IntoIterator<Item = &'a NetShape>,
{
    let mut n = 0usize;
    let mut sum = 0.0f32;
    for other in reference {
        sum += delta(&shape.genome, &other.genome);
        n += 1;
    }
    if n == 0 {
        return None;
    }
    Some(sum / n as f32)
}

/// Below this many live nets every pair is compared exactly (`n(n−1)/2` pairs: 100 nets = 4,950,
/// 512 nets = 130,776).
const ALL_PAIRS_UP_TO: usize = 512;
/// Pairs drawn when sampling (enough to move slowly, cheap enough for every clock).
const SAMPLED_PAIRS: usize = 4096;

/// A net's structural genome: gene token → how many times it occurs.
pub(crate) fn genes(topo: &Topology) -> HashMap<String, usize> {
    let mut out: HashMap<String, usize> = HashMap::new();
    let mut bump = |token: String| *out.entry(token).or_insert(0) += 1;
    for node in &topo.nodes {
        // Effective ops, resolved exactly as the forward pass resolves them (`network.rs`: no
        // per-node override ⇒ `Add` / no standardisation), so two nets that express the same thing
        let combine = node.combine_op.unwrap_or(CombineOp::Add);
        let std = node.standardize.unwrap_or(StandardizeOp::Identity);
        bump(format!(
            "N:{:?}:{combine:?}:{std:?}:{:?}:{}",
            node.kind,
            node.activation,
            node.hidden_dim.unwrap_or(0)
        ));
        // One gene per EMITTED PORT: a port override replaces the node's activation
        // for that wire, so a net whose ports differ must not read as identical.
        for port in 0..node.num_outputs {
            let act = node
                .port_activations
                .as_ref()
                .and_then(|acts| acts.get(port).copied())
                .unwrap_or(node.activation);
            bump(format!("W:{act:?}"));
        }
        // The feature split is genome too: how many features each Input port keeps.
        if let Some(masks) = &node.feature_masks {
            for mask in masks {
                bump(format!(
                    "M:{}",
                    mask.iter().filter(|kept| **kept != 0).count()
                ));
            }
        }
    }
    // WIRES ARE GENOME TOO, one token per connection — keyed by the KINDS it joins, never by node
    // id: crossover and mutation renumber nodes, so the same graph with two ids swapped must read
    let kinds: HashMap<usize, NodeKind> = topo.nodes.iter().map(|n| (n.id, n.kind)).collect();
    for conn in &topo.connections {
        if let (Some(from), Some(to)) = (kinds.get(&conn.from.node), kinds.get(&conn.to.node)) {
            bump(format!("E:{from:?}>{to:?}"));
        }
    }
    out
}

/// Multiset Jaccard distance: `1 − |A ∩ B| / |A ∪ B|`, in `0..=1`.
pub(crate) fn delta(a: &HashMap<String, usize>, b: &HashMap<String, usize>) -> f32 {
    let mut inter = 0usize;
    let mut union = 0usize;
    for (token, count) in a {
        let other = b.get(token).copied().unwrap_or(0);
        inter += (*count).min(other);
        union += (*count).max(other);
    }
    for (token, count) in b {
        if !a.contains_key(token) {
            union += *count;
        }
    }
    if union == 0 {
        return 0.0;
    }
    1.0 - inter as f32 / union as f32
}

/// Measure the live population. `None` when there is nothing to compare (< 2 nets).
pub(crate) fn measure(state: &RaceState, run_seed: u64, clock: usize) -> Option<DiversityReport> {
    let hashes = state.live_hashes();
    let live = hashes.len();
    if live < 2 {
        return None;
    }
    let mut genomes: Vec<HashMap<String, usize>> = Vec::with_capacity(live);
    let mut blueprints: HashSet<&str> = HashSet::new();
    // Census legs: one value per NET (nodes, wires) and one per HIDDEN NODE (dim).
    let mut node_counts: Vec<f32> = Vec::with_capacity(live);
    let mut wire_counts: Vec<f32> = Vec::with_capacity(live);
    let mut widths: Vec<f32> = Vec::new();
    for hash in &hashes {
        let Some(net) = state.net(hash) else { continue };
        blueprints.insert(net.topology.as_str());
        // A blueprint that will not parse is a bug, not a reason to fail a run:
        // net simply does not enter the comparison (`live` still counts
        if let Ok(topo) = net.topology() {
            node_counts.push(topo.nodes.len() as f32);
            wire_counts.push(topo.connections.len() as f32);
            widths.extend(
                topo.nodes
                    .iter()
                    .filter(|n| n.kind == NodeKind::Hidden)
                    .filter_map(|n| n.hidden_dim.map(|d| d as f32)),
            );
            genomes.push(genes(&topo));
        }
    }
    if genomes.len() < 2 {
        return None;
    }
    // Pairs: every pair for a small population, a SEEDED sample above the cap —
    // seed depends on `(run_seed, clock)`, so a replay sees the same pairs.
    let mut rng = fastrand::Rng::with_seed(run_seed.wrapping_add(clock as u64));
    let mut pairs = 0usize;
    let mut sum = 0.0f32;
    if genomes.len() <= ALL_PAIRS_UP_TO {
        for i in 0..genomes.len() {
            for j in (i + 1)..genomes.len() {
                sum += delta(&genomes[i], &genomes[j]);
                pairs += 1;
            }
        }
    } else {
        for _ in 0..SAMPLED_PAIRS {
            let i = rng.usize(..genomes.len());
            let j = rng.usize(..genomes.len());
            if i == j {
                continue;
            }
            // Upper triangle: `Δ(a, b) == Δ(b, a)`, so the drawn order folds away.
            let (lo, hi) = if i < j { (i, j) } else { (j, i) };
            sum += delta(&genomes[lo], &genomes[hi]);
            pairs += 1;
        }
    }
    if pairs == 0 {
        return None;
    }
    Some(DiversityReport {
        live,
        distinct: blueprints.len(),
        mean_delta: sum / pairs as f32,
        pairs,
        nodes: trait_of(&node_counts),
        wires: trait_of(&wire_counts),
        dim: trait_of(&widths),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn genome(tokens: &[(&str, usize)]) -> HashMap<String, usize> {
        tokens.iter().map(|(t, n)| (t.to_string(), *n)).collect()
    }

    #[test]
    fn identical_genomes_have_zero_distance() {
        let a = genome(&[("N:Hidden:Add:None:Tanh:48", 2), ("W:Tanh", 3)]);
        assert_eq!(delta(&a, &a), 0.0, "a net is not different from itself");
    }

    #[test]
    fn disjoint_genomes_have_full_distance() {
        let a = genome(&[("W:Tanh", 1)]);
        let b = genome(&[("W:SiLU", 1)]);
        assert_eq!(delta(&a, &b), 1.0, "nothing in common ⇒ 1.0");
    }

    #[test]
    fn one_missing_gene_out_of_n_is_one_over_n() {
        let a = genome(&[("W:Tanh", 3), ("W:SiLU", 1)]);
        let b = genome(&[("W:Tanh", 3)]);
        assert!(
            (delta(&a, &b) - 0.25).abs() < 1e-6,
            "Δ = 1/|A|, got {}",
            delta(&a, &b)
        );
    }

    #[test]
    fn delta_is_symmetric_so_the_upper_triangle_suffices() {
        let a = genome(&[("N:Hidden:Add:None:Tanh:48", 2), ("W:Tanh", 3)]);
        let b = genome(&[("N:Hidden:Min:Identity:SiLU:16", 1), ("W:ReLU", 2)]);
        assert_eq!(
            delta(&a, &b),
            delta(&b, &a),
            "Δ is a distance: the pair's order cannot matter"
        );
    }

    #[test]
    fn trait_of_reports_mean_and_population_spread() {
        let t = trait_of(&[2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]);
        assert!((t.mean - 5.0).abs() < 1e-6, "mean, got {}", t.mean);
        assert!(
            (t.spread - 2.0).abs() < 1e-6,
            "population std, got {}",
            t.spread
        );
    }

    /// A re-route is a structural change; a RENUMBERING is not. One token per wire,
    /// keyed by kinds, is the only label that satisfies both.
    #[test]
    fn a_reroute_is_a_gene_but_renumbering_is_not() {
        use crate::graph::node::{Activation, Node};
        use crate::graph::topology::{Connection, Port};
        let node = |id: usize, kind: NodeKind, num_inputs: usize, num_outputs: usize| Node {
            id,
            num_inputs,
            num_outputs,
            kind,
            hidden_dim: (kind == NodeKind::Hidden).then_some(8),
            activation: if kind == NodeKind::Hidden {
                Activation::ReLU
            } else {
                Activation::Identity
            },
            port_activations: None,
            combine_op: None,
            standardize: None,
            feature_masks: None,
        };
        let build = |skip: bool| {
            let mut g = Topology::new(0, None);
            g.nodes.push(node(0, NodeKind::Input, 0, 4));
            g.nodes.push(node(1, NodeKind::Hidden, 1, 1));
            g.nodes.push(node(2, NodeKind::Output, 2, 0));
            g.connections.push(Connection {
                from: Port { node: 0, index: 0 },
                to: Port { node: 1, index: 0 },
            });
            g.connections.push(Connection {
                from: Port { node: 1, index: 0 },
                to: Port { node: 2, index: 0 },
            });
            if skip {
                // The SAME nodes, one extra wire: input straight to the output.
                g.connections.push(Connection {
                    from: Port { node: 0, index: 1 },
                    to: Port { node: 2, index: 1 },
                });
            }
            g
        };
        let direct = genes(&build(false));
        let skip = genes(&build(true));
        // Node genes agree; the only difference is the added `E:Input>Output`.
        assert_eq!(direct.get("E:Input>Hidden"), Some(&1));
        assert_eq!(direct.get("E:Input>Output"), None);
        assert_eq!(skip.get("E:Input>Output"), Some(&1));
        assert!(
            delta(&direct, &skip) > 0.0,
            "a re-route must not read as the same architecture"
        );
        // Renumber every node: the same wiring must still read as distance zero.
        let mut moved = build(false);
        for n in &mut moved.nodes {
            n.id += 10;
        }
        for c in &mut moved.connections {
            c.from.node += 10;
            c.to.node += 10;
        }
        assert_eq!(
            delta(&genes(&build(false)), &genes(&moved)),
            0.0,
            "node ids are not a gene"
        );
    }

    #[test]
    fn mean_delta_to_reads_the_distance_to_the_reference_set() {
        let shape = |tokens: &[(&str, usize)]| NetShape {
            genome: genome(tokens),
            nodes: 1,
            wires: 1,
            dim: 4.0,
            mask: None,
        };
        let straggler = shape(&[("W:Tanh", 1)]);
        assert_eq!(
            mean_delta_to(&straggler, &[]),
            None,
            "no reference ⇒ `na`, never a fake 0.0"
        );
        assert_eq!(
            mean_delta_to(&straggler, &[shape(&[("W:Tanh", 1)])]),
            Some(0.0),
            "identical to the only elite"
        );
        assert_eq!(
            mean_delta_to(&straggler, &[shape(&[("W:SiLU", 1)])]),
            Some(1.0),
            "nothing in common"
        );
        // Mean over the reference SET: identical to one elite, disjoint from the other.
        let mixed = mean_delta_to(
            &straggler,
            &[shape(&[("W:Tanh", 1)]), shape(&[("W:SiLU", 1)])],
        );
        assert_eq!(mixed, Some(0.5), "the two legs average, got {mixed:?}");
    }

    /// `None` and `Some(Identity)` are spelled differently and mean the same thing (`network.rs`:
    /// `None` ⇒ no standardisation).
    #[test]
    fn a_missing_standardize_op_is_the_identity_gene() {
        let cfg = crate::engine::config::RaceConfig::defaults();
        let mut spelled_none = crate::engine::population::initial_population(&cfg, 42)
            .into_iter()
            .next()
            .expect("the founders are never empty");
        let mut spelled_identity = spelled_none.clone();
        for node in &mut spelled_none.nodes {
            node.standardize = None;
        }
        for node in &mut spelled_identity.nodes {
            node.standardize = Some(StandardizeOp::Identity);
        }
        assert_eq!(
            genes(&spelled_none),
            genes(&spelled_identity),
            "the identity op and no override are the same gene"
        );
        // A REAL standardisation is a different gene (and so is the combine op).
        for node in &mut spelled_identity.nodes {
            node.standardize = Some(StandardizeOp::LayerNorm);
        }
        assert_ne!(genes(&spelled_none), genes(&spelled_identity));
    }

    #[test]
    fn shape_of_reads_the_same_blueprint_the_pair_metric_does() {
        let cfg = crate::engine::config::RaceConfig::defaults();
        let topos = crate::engine::population::initial_population(&cfg, 42);
        let topo = &topos[0];
        let shape = shape_of(topo);
        assert_eq!(shape.nodes, topo.nodes.len());
        assert_eq!(shape.wires, topo.connections.len());
        assert_eq!(
            shape.genome,
            genes(topo),
            "the shape carries the pair metric's own genes, not a second spelling"
        );
        let hidden: Vec<f32> = topo
            .nodes
            .iter()
            .filter(|n| n.kind == NodeKind::Hidden)
            .filter_map(|n| n.hidden_dim.map(|d| d as f32))
            .collect();
        assert_eq!(shape.dim, trait_of(&hidden).mean);
    }

    #[test]
    fn trait_of_on_nothing_is_zero_not_nan() {
        let t = trait_of(&[]);
        assert_eq!(
            (t.mean, t.spread),
            (0.0, 0.0),
            "no nets to describe ⇒ a readable zero, never NaN"
        );
    }

    #[test]
    fn counts_matter_not_just_presence() {
        // Same token set, different multiplicity: multiset Jaccard still separates them.
        let a = genome(&[("W:Tanh", 1)]);
        let b = genome(&[("W:Tanh", 3)]);
        assert!(
            (delta(&a, &b) - 2.0 / 3.0).abs() < 1e-6,
            "Δ = 1 − 1/3, got {}",
            delta(&a, &b)
        );
    }
}