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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! The node type — pure metadata (ports, kind, dim, activation).

use flodl::Variable;
use serde::{Deserialize, Serialize};

/// Divide floor used by [`CombineOp::Divide`]: denominators smaller
/// in magnitude are treated as ±ε (bounded output, bounded gradients).
pub const DIVIDE_EPS: f64 = 1e-3;

/// `n / d` without the singularity: `n * (d / clamp(|d|, ε))`. Exact
/// `|d| ≥ ε`, saturating below it; differentiable with no NaN in backward.
pub fn safe_divide(n: &Variable, d: &Variable) -> flodl::tensor::Result<Variable> {
    let d_abs = d.abs()?;
    let clamped = d_abs.clamp_min(DIVIDE_EPS)?;
    let ratio = d.div(&clamped)?;
    n.mul(&ratio)
}

/// How multiple incoming tensors are combined before the node transforms them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CombineOp {
    /// Sum of incoming tensors.
    Add,
    /// Average of incoming tensors.
    Mean,
    /// Element-wise product of incoming tensors.
    Multiply,
    /// Element-wise subtraction: first - second - third - ...
    Subtract,
    /// Element-wise division: first / second / third - ...
    Divide,
    /// Element-wise maximum across incoming tensors.
    Max,
    /// Element-wise minimum across incoming tensors.
    Min,
}

impl CombineOp {
    /// Apply this combine operation to a slice of tensors.
    pub fn apply(&self, tensors: &[Variable]) -> flodl::tensor::Result<Variable> {
        match self {
            CombineOp::Add => {
                let mut result = tensors[0].clone();
                for t in &tensors[1..] {
                    result = result.add(t)?;
                }
                Ok(result)
            }
            CombineOp::Mean => {
                let sum = CombineOp::Add.apply(tensors)?;
                sum.mul_scalar(1.0 / tensors.len() as f64)
            }
            CombineOp::Multiply => {
                let mut result = tensors[0].clone();
                for t in &tensors[1..] {
                    result = result.mul(t)?;
                }
                Ok(result)
            }
            CombineOp::Subtract => {
                let mut result = tensors[0].clone();
                for t in &tensors[1..] {
                    result = result.sub(t)?;
                }
                Ok(result)
            }
            CombineOp::Divide => {
                // Divide-safe (see `safe_divide`): a zeroed denominator (dropout
                // mask, dead branch) yields a bounded value, never inf/NaN.
                let mut result = tensors[0].clone();
                for t in &tensors[1..] {
                    result = safe_divide(&result, t)?;
                }
                Ok(result)
            }
            CombineOp::Max => {
                let mut result = tensors[0].clone();
                for t in &tensors[1..] {
                    result = result.maximum(t)?;
                }
                Ok(result)
            }
            CombineOp::Min => {
                let mut result = tensors[0].clone();
                for t in &tensors[1..] {
                    result = result.minimum(t)?;
                }
                Ok(result)
            }
        }
    }
}

/// Activation applied after a node's linear transform.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum Activation {
    /// No activation -- pure linear.
    #[default]
    Identity,
    /// max(0, x) -- sparse, efficient.
    ReLU,
    /// x * Phi(x) -- smooth ReLU approximation.
    GeLU,
    /// x * sigmoid(x) -- self-gated, smooth.
    SiLU,
    /// Self-normalizing ELU -- preserves mean/variance.
    SELU,
    /// Hyperbolic tangent -- output in (-1, 1).
    Tanh,
    /// 1/(1+e^-x) -- output in (0, 1), for gating.
    Sigmoid,
    /// x * tanh(softplus(x)) -- smooth, non-monotonic.
    Mish,
    /// max(0.01x, x) -- leaky ReLU with slope 0.01.
    LeakyReLU,
    /// x if x>0, else e^x - 1 -- smooth negative branch.
    ELU,
    /// GeLU via tanh approximation -- faster than exact GeLU.
    GeluTanh,
    /// log(1 + e^x) -- smooth ReLU, always positive.
    Softplus,
    /// x * ReLU6(x+3)/6 -- efficient GeLU variant.
    HardSwish,
    /// ReLU6(x+3)/6 -- efficient Sigmoid approximation.
    HardSigmoid,
    /// sin(x) -- periodic, bounded in (-1, 1).
    Sin,
    /// cos(x) -- periodic, bounded in (-1, 1).
    Cos,
    /// softmax(x) over the feature dim -- rows sum to 1; a competitive mixer
    /// over channels (winner-take-most reweighting).
    Softmax,
    /// log(softmax(x)) over the feature dim -- numerically stable log
    /// Softmax. Pairs with NLL-style downstream losses; strictly negative.
    LogSoftmax,
}

impl Activation {
    /// Apply this activation to a tensor, propagating flodl errors.
    pub fn apply(&self, x: &Variable) -> flodl::tensor::Result<Variable> {
        match self {
            Activation::Identity => Ok(x.clone()),
            Activation::ReLU => x.relu(),
            Activation::GeLU => x.gelu(),
            Activation::SiLU => x.silu(),
            Activation::SELU => x.selu(),
            Activation::Tanh => x.tanh(),
            Activation::Sigmoid => x.sigmoid(),
            Activation::Mish => x.mish(),
            Activation::LeakyReLU => x.leaky_relu(0.01),
            Activation::ELU => x.elu(1.0),
            Activation::GeluTanh => x.gelu_tanh(),
            Activation::Softplus => x.softplus(1.0, 20.0),
            Activation::HardSwish => x.hardswish(),
            Activation::HardSigmoid => x.hardsigmoid(),
            Activation::Sin => x.sin(),
            Activation::Cos => x.cos(),
            // Dim -1 = feature dim: matches LayerNorm's axis convention,
            // node's normalization/activation both operate per-sample.
            Activation::Softmax => x.softmax(-1),
            Activation::LogSoftmax => x.log_softmax(-1),
        }
    }
}

/// Normalization after linear, before activation. Part of per-node NAS knobs.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum StandardizeOp {
    /// No normalization — the linear output passes straight to activation.
    #[default]
    Identity,
    /// Layer normalization over the feature dimension.
    LayerNorm,
    /// RMS normalization over the feature dim -- root-mean-square scaling, no
    /// centering and no params. Cheaper than LayerNorm; stateless.
    RmsNorm,
    /// Per-sample z-score over the feature dim. Deliberately the stateless form:
    /// affine params or running stats would break the replay contract.
    InstanceNorm,
    /// z-score within a FIXED 4-group split of the feature dim -- the middle
    /// ground between LayerNorm and InstanceNorm. Stateless; last group uneven.
    GroupNorm,
}

/// The label parse lives with the enum, so every new arm's label is added
/// one place and any tooling can reuse
impl TryFrom<&str> for StandardizeOp {
    /// The unknown-label message.
    type Error = String;
    /// Case-insensitive label → variant; an unknown label errors with its name.
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        Ok(match s.to_lowercase().as_str() {
            "identity" => StandardizeOp::Identity,
            "layernorm" => StandardizeOp::LayerNorm,
            "rmsnorm" | "rms_norm" => StandardizeOp::RmsNorm,
            "instancenorm" | "instance_norm" => StandardizeOp::InstanceNorm,
            "groupnorm" | "group_norm" => StandardizeOp::GroupNorm,
            other => return Err(format!("unknown standardize op '{other}'")),
        })
    }
}

impl StandardizeOp {
    /// Apply this standardize op. All variants are pure `f(x)` — stateless,
    /// no learnable params — so the replay contract holds.
    pub fn apply(&self, x: &Variable) -> flodl::tensor::Result<Variable> {
        match self {
            StandardizeOp::Identity => Ok(x.clone()),
            StandardizeOp::LayerNorm => {
                // z-score across feature dim: (x - mean) / sqrt(var + eps)
                let mean = x.mean_dim(-1, true)?; // [batch, 1]
                let centered = x.sub(&mean)?;
                let var = centered.mul(&centered)?.mean_dim(-1, true)?; // [batch, 1]
                let std = var.add_scalar(1e-5)?.sqrt()?; // [batch, 1]
                let normed = centered.div(&std)?;
                Ok(normed)
            }
            StandardizeOp::RmsNorm => {
                // x / sqrt(mean(x²) + eps) — no centering, scale-only
                let ms = x.mul(x)?.mean_dim(-1, true)?; // [batch, 1]
                let rms = ms.add_scalar(1e-5)?.sqrt()?;
                Ok(x.div(&rms)?)
            }
            StandardizeOp::InstanceNorm => {
                // Hand-rolled rather than flodl's nn::instance_norm, which carries
                // running stats (state) — this stays pure f(x).
                let mean = x.mean_dim(-1, true)?;
                let centered = x.sub(&mean)?;
                let var = centered.mul(&centered)?.mean_dim(-1, true)?;
                let std = var.add_scalar(1e-5)?.sqrt()?;
                Ok(centered.div(&std)?)
            }
            StandardizeOp::GroupNorm => {
                // Hand-rolled (flodl's nn module carries state): pad the feature
                // axis to a multiple of 4, z-score per (sample, group), unpad.
                let shape = x.data().shape();
                let feats = shape[shape.len() - 1] as usize;
                let group = 4usize;
                if feats <= group {
                    // Degenerate: as many groups as features ⇒ InstanceNorm.
                    let mean = x.mean_dim(-1, true)?;
                    let centered = x.sub(&mean)?;
                    let var = centered.mul(&centered)?.mean_dim(-1, true)?;
                    let std = var.add_scalar(1e-5)?.sqrt()?;
                    return Ok(centered.div(&std)?);
                }
                let pad = (group - feats % group) % group;
                let padded: flodl::Tensor = if pad > 0 {
                    // Zero-pad the feature axis so it divides evenly.
                    let flat = x.data().reshape(&[-1i64, feats as i64])?; // [batch, feats]
                    let rows = flat.shape()[0];
                    let zeros = flodl::Tensor::from_f32(
                        &vec![0.0_f32; (rows as usize) * pad],
                        &[rows, pad as i64],
                        x.data().device(),
                    )?;
                    flat.cat(&zeros, 1)? // [batch, feats + pad]
                } else {
                    x.data().reshape(&[-1i64, feats as i64])?
                };
                let padded_feats = (feats + pad) as i64;
                let g = padded.reshape(&[-1i64, group as i64, padded_feats / group as i64])?;
                let mean = g.mean_dim(-1, true)?; // [batch, group, 1]
                let centered = g.sub(&mean)?;
                let var = centered.mul(&centered)?.mean_dim(-1, true)?;
                let std = var.add_scalar(1e-5)?.sqrt()?;
                let normed = centered.div(&std)?; // [batch, group, feats/group]
                let normed = normed.reshape(&[-1i64, padded_feats])?;
                let flat_out = if pad > 0 {
                    // Slice the padding back off: keep the first `feats` cols.
                    normed.narrow(-1, 0, feats as i64)?
                } else {
                    normed
                };
                Ok(Variable::new(flat_out.reshape(&shape)?, false))
            }
        }
    }
}

/// A node in the computational graph: receives tensors, transforms them,
/// applies an activation, exposes outputs. Invariants live in `Topology::validate`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Node {
    pub id: usize,          //  unique id; also execution order (0 runs first)
    pub num_inputs: usize,  //  how many input ports this node has
    pub num_outputs: usize, //  how many output ports this node has
    pub kind: NodeKind,     // role: Input / Hidden / Output
    /// Feature-dim override for this layer's *output* (`None` = the graph's
    /// `hidden_dim`); the layer's *input* dim comes from its sources at build time.
    pub hidden_dim: Option<usize>,
    /// Activation applied after this node's linear transform.
    pub activation: Activation,
    /// Per-output-port activations: entry `i` REPLACES the node-level activation
    /// for port `i`'s wire, so one node's ports can carry different signals.
    #[serde(default)]
    pub port_activations: Option<Vec<Activation>>,
    /// Per-node combine override: how this node merges incoming tensors (`None`
    /// = the graph's `combine_op`); serde default keeps old JSON loadable.
    #[serde(default)]
    pub combine_op: Option<CombineOp>,
    /// Per-node standardize op: normalization applied after linear, before
    /// activation (`None` = inherit the graph's `standardize_op`).
    #[serde(default)]
    pub standardize: Option<StandardizeOp>,
    /// Per-output-port **feature masks** over the raw input features — the feature split.
    #[serde(default)]
    pub feature_masks: Option<Vec<Vec<u8>>>,
}

/// The role a node plays — where it sits in the forward pass, and its ports.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NodeKind {
    /// Start of the network: no inputs, feeds the rest.
    Input,
    /// Middle of the network: combine → pass
    Hidden,
    /// End of the network: its output becomes the network output.
    Output,
}

impl Node {
    ///  Create an input node: 0 inputs, `num_outputs` outputs.
    pub fn new_input(id: usize, num_outputs: usize) -> Self {
        Node {
            id,
            num_inputs: 0,
            num_outputs,
            kind: NodeKind::Input,
            hidden_dim: None,
            activation: Activation::Identity,
            port_activations: None,
            combine_op: None,
            standardize: None,
            feature_masks: None,
        }
    }

    ///  Create a hidden node.
    pub fn new_hidden(id: usize, num_inputs: usize, num_outputs: usize) -> Self {
        Node {
            id,
            num_inputs,
            num_outputs,
            kind: NodeKind::Hidden,
            hidden_dim: None,
            activation: Activation::Identity,
            port_activations: None,
            combine_op: None,
            standardize: None,
            feature_masks: None,
        }
    }

    ///  Create an output node.
    pub fn new_output(id: usize, num_inputs: usize, num_outputs: usize) -> Self {
        Node {
            id,
            num_inputs,
            num_outputs,
            kind: NodeKind::Output,
            hidden_dim: None,
            activation: Activation::Identity,
            port_activations: None,
            combine_op: None,
            standardize: None,
            feature_masks: None,
        }
    }

    /// Set activation (builder style).
    pub fn with_activation(mut self, activation: Activation) -> Self {
        self.activation = activation;
        self
    }

    /// Set combine-op override (builder style).
    pub fn with_combine_op(mut self, combine_op: CombineOp) -> Self {
        self.combine_op = Some(combine_op);
        self
    }

    /// Set per-node channel-width override (builder style).
    pub fn with_hidden_dim(mut self, hidden_dim: usize) -> Self {
        self.hidden_dim = Some(hidden_dim);
        self
    }

    /// Set per-port activations (builder style). Empty = inherit everywhere;
    /// otherwise it must be exactly `num_outputs` long — `validate()` enforces
    pub fn with_port_activations(mut self, ports: Vec<Activation>) -> Self {
        self.port_activations = if ports.is_empty() { None } else { Some(ports) };
        self
    }

    /// The activation carried by output port `i`: its override when set, else
    /// node-level one. Input-node ports are Identity (raw data fan-out).
    pub fn port_activation(&self, i: usize) -> Activation {
        self.port_activations
            .as_ref()
            .and_then(|v| v.get(i).copied())
            .unwrap_or(self.activation)
    }

    /// Set per-output-port feature masks (builder style).
    pub fn with_feature_masks(mut self, masks: Vec<Vec<u8>>) -> Self {
        self.feature_masks = if masks.is_empty() { None } else { Some(masks) };
        self
    }

    /// The feature mask carried by output port `i`, if this node has one. `None`
    /// means the port is unmasked and carries the shared mixed vector.
    pub fn port_feature_mask(&self, i: usize) -> Option<&[u8]> {
        self.feature_masks
            .as_ref()
            .and_then(|v| v.get(i))
            .map(|m| m.as_slice())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::topology::{Topology, TopologyOptions};
    use proptest::prelude::*;

    #[test]
    fn new_pool_ops_are_numerically_sound() {
        // [batch=2, features=4] input with known structure.
        let x = flodl::Tensor::from_f32(
            &[1.0, 2.0, 3.0, 4.0, -1.0, 0.0, 1.0, 2.0],
            &[2, 4],
            flodl::Device::CPU,
        )
        .unwrap();
        let x = flodl::Variable::new(x, false);

        // Softmax over features: rows sum to 1, all positive.
        let s = Activation::Softmax.apply(&x).unwrap();
        let sums = s.data().sum().unwrap().item().unwrap() as f32;
        assert!(
            (sums - 2.0).abs() < 1e-4,
            "softmax rows must sum to 1 each (got {sums})"
        );

        // LogSoftmax: log of softmax — strictly non-positive, exp sums to 1.
        let ls = Activation::LogSoftmax.apply(&x).unwrap();
        let recon = ls.data().exp().unwrap().sum().unwrap().item().unwrap() as f32;
        assert!(
            (recon - 2.0).abs() < 1e-4,
            "exp(log_softmax) must sum to 1 per row (got {recon})"
        );

        // RMSNorm: per-row RMS of output ≈ 1.
        let r = StandardizeOp::RmsNorm.apply(&x).unwrap();
        let ms = r
            .data()
            .mul(&r.data())
            .unwrap()
            .mean()
            .unwrap()
            .item()
            .unwrap() as f32;
        let rms = ms.sqrt();
        assert!(
            (rms - 1.0).abs() < 1e-3,
            "rmsnorm output RMS ≈ 1 (got {rms})"
        );

        // InstanceNorm (hand-rolled, stateless): per-row mean 0, std 1.
        let i = StandardizeOp::InstanceNorm.apply(&x).unwrap();
        let m = i.data().mean().unwrap().item().unwrap() as f32;
        assert!(m.abs() < 1e-5, "instancenorm per-row mean ≈ 0 (got {m})");

        // GroupNorm (fixed 4 groups): 12 features → 4 contiguous groups of 3, each
        // z-scored alone → every group shows the ±1.2247 sawtooth, not a ramp.
        let x12 = flodl::Tensor::from_f32(
            &[
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, -1.0, 0.0, 1.0,
                -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0,
            ],
            &[2, 12],
            flodl::Device::CPU,
        )
        .unwrap();
        let x12 = flodl::Variable::new(x12, false);
        let g = StandardizeOp::GroupNorm.apply(&x12).unwrap();
        assert_eq!(g.data().shape(), x12.data().shape());
        let gd = g.data().to_f32_vec().unwrap();
        let r0 = &gd[0..12]; // row 0
        for (gi, chunk) in r0.chunks(3).enumerate() {
            let mean: f32 = chunk.iter().sum::<f32>() / 3.0;
            assert!(
                mean.abs() < 1e-4,
                "groupnorm row-0 group-{gi} mean ≈ 0 (got {mean})"
            );
            let var: f32 = chunk.iter().map(|v| (v - mean) * (v - mean)).sum::<f32>() / 3.0;
            assert!(
                (var.sqrt() - 1.0).abs() < 1e-3,
                "groupnorm group-{gi} std ≈ 1"
            );
        }
        // Sawtooth, not ramp — group boundaries reset to ±√(3/2) ≈ ±1.2247:
        assert!(
            (r0[0] - r0[3]).abs() < 1e-4,
            "group 0 and 1 start identically"
        );
        assert!(
            (r0[2] - 1.2247).abs() < 1e-3,
            "3-element z-score ends at √(3/2) ≈ 1.2247"
        );
        // And it genuinely differs from InstanceNorm on the same input:
        let inst = StandardizeOp::InstanceNorm.apply(&x12).unwrap();
        let id = inst.data().to_f32_vec().unwrap();
        assert_ne!(r0.to_vec(), id[0..12].to_vec(), "GroupNorm ≠ InstanceNorm");

        // Degenerate feats ≤ 4: falls back to the InstanceNorm shape, avoiding
        // single-feature group that would z-score to 0/0.
        let g4 = StandardizeOp::GroupNorm.apply(&x).unwrap();
        let gd4 = g4.data().to_f32_vec().unwrap();
        let i4 = StandardizeOp::InstanceNorm.apply(&x).unwrap();
        let id4 = i4.data().to_f32_vec().unwrap();
        assert_eq!(
            gd4.to_vec(),
            id4.to_vec(),
            "feats=4 ⇒ InstanceNorm fallback"
        );

        // Non-divisible count: 10 feats → groups {3,3,3,1+pad}; the pad lands
        // in the last group, so groups 0-2 stay exact and the shape is unchanged.
        let x10 = flodl::Tensor::from_f32(
            &[
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0,
                14.0, 16.0, 18.0, 20.0,
            ],
            &[2, 10],
            flodl::Device::CPU,
        )
        .unwrap();
        let x10 = flodl::Variable::new(x10, false);
        let g10 = StandardizeOp::GroupNorm.apply(&x10).unwrap();
        assert_eq!(
            g10.data().shape(),
            x10.data().shape(),
            "uneven feature count"
        );
        let g10d = g10.data().to_f32_vec().unwrap();
        let r10 = &g10d[0..10]; // row 0: [1..=10]
        // Group 0 = {1, 2, 3} → z-score [-1.22, 0, 1.22].
        assert!((r10[0] + 1.2247).abs() < 1e-3, "uneven group 0 start");
        assert!(r10[1].abs() < 1e-4, "uneven group 0 middle");
        assert!((r10[2] - 1.2247).abs() < 1e-3, "uneven group 0 end");
        // Groups 1-2 are {4,5,6}/{7,8,9}: same sawtooth pattern.
        assert!((r10[3] - r10[0]).abs() < 1e-4);
        assert!((r10[6] - r10[0]).abs() < 1e-4);
    }

    /// Arbitrary valid node metadata (port counts, kind, id).
    fn node_strategy() -> impl Strategy<Value = Node> {
        (0usize..100, 0usize..8, 0usize..8, 0usize..3).prop_map(
            |(id, num_inputs, num_outputs, kind)| Node {
                id,
                num_inputs,
                num_outputs,
                kind: match kind {
                    0 => NodeKind::Input,
                    1 => NodeKind::Hidden,
                    _ => NodeKind::Output,
                },
                hidden_dim: None,
                activation: Activation::Identity,
                port_activations: None,
                combine_op: None,
                standardize: None,
                feature_masks: None,
            },
        )
    }

    #[test]
    fn test_new_node_inputs() {
        let node: Node = Node::new_input(1, 2);
        assert_eq!(node.num_inputs, 0);
        assert_eq!(node.num_outputs, 2);
        assert_eq!(node.hidden_dim, None);
        assert_eq!(node.activation, Activation::Identity);
    }

    #[test]
    fn test_new_node_hidden() {
        let node: Node = Node::new_hidden(1, 3, 2);
        assert_eq!(node.num_inputs, 3);
        assert_eq!(node.num_outputs, 2);
        assert_eq!(node.activation, Activation::Identity);
    }

    #[test]
    fn test_node_builders() {
        let node = Node::new_hidden(1, 3, 2)
            .with_activation(Activation::GeLU)
            .with_hidden_dim(32);
        assert_eq!(node.activation, Activation::GeLU);
        assert_eq!(node.hidden_dim, Some(32));
        // Chaining doesn't disturb the port counts / kind
        assert_eq!(node.num_inputs, 3);
        assert_eq!(node.num_outputs, 2);
        assert_eq!(node.kind, NodeKind::Hidden);
        // Order-independent: with_hidden_dim before with_activation
        let wide = Node::new_hidden(1, 3, 2)
            .with_hidden_dim(16)
            .with_activation(Activation::ReLU);
        assert_eq!(wide.hidden_dim, Some(16));
        assert_eq!(wide.activation, Activation::ReLU);
    }

    #[test]
    fn test_new_node_outputs() {
        let node: Node = Node::new_output(1, 3, 2);
        assert_eq!(node.num_inputs, 3);
        assert_eq!(node.num_outputs, 2);
        assert_eq!(node.hidden_dim, None);
    }

    #[test]
    fn test_activation_default_and_display() {
        assert_eq!(Activation::default(), Activation::Identity);
        assert_eq!(Activation::ReLU.to_string(), "relu");
        assert_eq!(Activation::GeLU.to_string(), "gelu");
        assert_eq!(Activation::SELU.to_string(), "selu");
        assert_eq!(Activation::LeakyReLU.to_string(), "leaky_relu");
        assert_eq!(Activation::ELU.to_string(), "elu");
        assert_eq!(Activation::GeluTanh.to_string(), "gelu_tanh");
        assert_eq!(Activation::Softplus.to_string(), "softplus");
        assert_eq!(Activation::HardSwish.to_string(), "hardswish");
        assert_eq!(Activation::HardSigmoid.to_string(), "hardsigmoid");
    }

    // ── property tests (proptest) ───────────────────────────────────────────

    proptest! {
        /// The builder methods only touch their target field: id, kind
        /// port counts must survive chaining, in either order.
        #[test]
        fn prop_node_builders_preserve_identity(
            node in node_strategy(),
            hidden_dim in 1usize..128,
            relu in any::<bool>(),
        ) {
            let activation = if relu { Activation::ReLU } else { Activation::GeLU };
            let built = node
                .clone()
                .with_hidden_dim(hidden_dim)
                .with_activation(activation);
            prop_assert_eq!(built.id, node.id);
            prop_assert_eq!(built.kind, node.kind);
            prop_assert_eq!(built.num_inputs, node.num_inputs);
            prop_assert_eq!(built.num_outputs, node.num_outputs);
            prop_assert_eq!(built.activation, activation);
            prop_assert_eq!(built.hidden_dim, Some(hidden_dim));
            // With hidden_dim 0 the graph rejects the node at validate() time.
            let bad = node.with_hidden_dim(0);
            prop_assert_eq!(bad.hidden_dim, Some(0));
        }

        /// A random hidden node's port counts always land inside the options
        /// ranges, and its id/kind follow the "append" contract.
        #[test]
        fn prop_random_hidden_node_respects_port_ranges(
            min_inputs in 1usize..5,
            min_outputs in 1usize..5,
            span_in in 0usize..4,
            span_out in 0usize..4,
        ) {
            let opts = TopologyOptions {
                topology_seed: 16,
                min_hidden_num_nodes: 2,
                max_hidden_num_nodes: 5,
                min_hidden_inputs_per_node: min_inputs,
                max_hidden_inputs_per_node: min_inputs + span_in,
                min_hidden_outputs_per_node: min_outputs,
                max_hidden_outputs_per_node: min_outputs + span_out,
                input_dim: Some(4),
                output_dim: Some(2),
                dropout_prob: 0.0,
                feature_split: None,
                weight_seed: None,
            };
            let mut graph = Topology::new(0, Some(opts));
            graph.create_random_hidden_node();
            let node = &graph.nodes[0];
            prop_assert!(node.id == 0);
            prop_assert!(node.kind == NodeKind::Hidden);
            prop_assert!(
                node.num_inputs >= min_inputs && node.num_inputs <= min_inputs + span_in,
                "num_inputs {} outside [{}, {}]",
                node.num_inputs,
                min_inputs,
                min_inputs + span_in
            );
            prop_assert!(
                node.num_outputs >= min_outputs && node.num_outputs <= min_outputs + span_out,
                "num_outputs {} outside [{}, {}]",
                node.num_outputs,
                min_outputs,
                min_outputs + span_out
            );
        }
    }
}