rustyml 0.17.0

Machine learning models, neural networks, metrics, and data utilities 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
//! Integration tests for [`Shape`] and for the pure output-shape method of a layer
//!
//! 2 properties matter here. The first is purity: the answer is a function of the layer
//! configuration and of the shape the caller passes. It reads nothing a forward pass wrote. The
//! second is refusal: an input the layer cannot accept comes back as an error that names the
//! layer and the problem.
//!
//! The model build passes each output shape into the next layer before any data arrives.
//! These tests also check a stack by hand without tensors.

use crate::roster::{Case, LAYER_TYPE_COUNT, LayerVisitor, visit_every_layer};
use ndarray::{Array, ArrayD, IxDyn};
use rustyml::neural_network::layers::*;
use rustyml::neural_network::sequential::SequentialBuilder;
use rustyml::neural_network::traits::{Layer, UnaryLayer};
use rustyml::neural_network::{Ctx, Shape};
use std::collections::BTreeSet;

/// Every pooling layer answers before it has seen a single tensor
///
/// Windowed pooling applies its window to each spatial axis. Global pooling drops every
/// spatial axis. Neither layer has run a forward pass at this point, and the global layer
/// holds no input shape at all
#[test]
fn the_pooling_family_answers_before_any_forward_pass() {
    let pool_1d = MaxPooling1D::new(2).with_stride(2).unwrap();
    let computed = pool_1d
        .compute_output_shape(&Shape::with_free_batch(&[1, 8, 2]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 4, 2)");

    let pool_2d = MaxPooling2D::new((2, 2));
    let computed = pool_2d
        .compute_output_shape(&Shape::with_free_batch(&[1, 6, 6, 3]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 3, 3, 3)");

    let average_1d = AveragePooling1D::new(3);
    let computed = average_1d
        .compute_output_shape(&Shape::known(&[4, 6, 1]))
        .unwrap();
    assert_eq!(computed.to_string(), "(4, 2, 1)");

    // A global pooling layer keeps no input shape until a build gives it one, so its own
    // `output_shape()` still reads "Unknown" here
    let global = GlobalMaxPooling1D::new();
    assert_eq!(global.output_shape(), "Unknown");
    let computed = global
        .compute_output_shape(&Shape::with_free_batch(&[5, 9, 7]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 7)");
}

/// A layer answers for a shape that differs from the 1 its build took
///
/// A pooling layer built for a length of 8 answers correctly for a length of 20. Nothing in
/// the answer comes from the build shape. The layer configuration supplies the window and stride
#[test]
fn a_layer_answers_for_a_shape_it_was_not_built_for() {
    let mut layer = MaxPooling1D::new(2).with_stride(2).unwrap();
    layer.build(&Shape::known(&[1, 8, 2])).unwrap();
    assert_eq!(layer.output_shape(), "(None, 4, 2)");

    let computed = layer
        .compute_output_shape(&Shape::with_free_batch(&[64, 20, 2]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 10, 2)");
}

/// A forward pass changes no answer that `compute_output_shape` gives
///
/// The layer runs a forward pass on a shape it was not built for. The method still answers
/// from its argument, so both calls agree
#[test]
fn a_forward_pass_changes_no_answer() {
    let mut layer = Conv2D::new(4, (3, 3), (1, 1), ReLU::new()).unwrap();
    let asked = Shape::with_free_batch(&[2, 10, 10, 3]);

    let before = layer.compute_output_shape(&asked).unwrap();
    layer
        .forward_mut(&ArrayD::zeros(IxDyn(&[2, 8, 8, 3])), &mut Ctx::inference())
        .unwrap();
    let after = layer.compute_output_shape(&asked).unwrap();

    assert_eq!(before, after);
    assert_eq!(before.to_string(), "(None, 8, 8, 4)");
}

/// A free batch axis stays free, and a fixed batch extent passes straight through
#[test]
fn the_batch_axis_passes_through_as_it_arrives() {
    let layer = Conv2D::new(2, (2, 2), (1, 1), Linear::new()).unwrap();

    let free = layer
        .compute_output_shape(&Shape::with_free_batch(&[1, 4, 4, 1]))
        .unwrap();
    assert_eq!(free.axes()[0], None);
    assert_eq!(free.to_string(), "(None, 3, 3, 2)");

    let fixed = layer
        .compute_output_shape(&Shape::known(&[6, 4, 4, 1]))
        .unwrap();
    assert_eq!(fixed.axes()[0], Some(6));
    assert_eq!(fixed.to_string(), "(6, 3, 3, 2)");
}

/// A recurrent layer keeps the time axis free when it returns a sequence, and drops it when it
/// returns the final state
#[test]
fn a_recurrent_layer_keeps_a_free_time_axis() {
    let sequence = LSTM::new(2, Tanh::new())
        .unwrap()
        .with_return_sequences(true);
    let computed = sequence
        .compute_output_shape(&Shape::new(vec![None, None, Some(3)]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, None, 2)");

    let last = LSTM::new(2, Tanh::new()).unwrap();
    let computed = last
        .compute_output_shape(&Shape::new(vec![None, Some(7), Some(3)]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 2)");
}

/// A layer that learns its shape from data still answers before it sees any
///
/// `UpSampling2D` reports "Unknown" until its build, because it holds no input shape. Its
/// shape algebra needs no such state
#[test]
fn a_data_driven_layer_answers_with_no_data() {
    let layer = UpSampling2D::new((2, 3), Interpolation::Nearest).unwrap();
    assert_eq!(layer.output_shape(), "Unknown");

    let computed = layer
        .compute_output_shape(&Shape::with_free_batch(&[1, 5, 6, 2]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 10, 18, 2)");
}

/// A convolution refuses a rank its forward pass would refuse, and names the rank it wanted
#[test]
fn a_convolution_refuses_the_wrong_rank() {
    let layer = Conv2D::new(4, (3, 3), (1, 1), ReLU::new()).unwrap();
    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[2, 8, 3]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("Conv2D"), "{message}");
    assert!(message.contains("rank 4"), "{message}");
    assert!(message.contains("(None, 8, 3)"), "{message}");
}

/// A built dense layer refuses a last axis that its kernel cannot contract
///
/// The kernel is `(input_dim, units)`, and the build reads `input_dim` from the last axis. A
/// layer built for 7 features therefore holds a kernel that cannot accept 5 features
#[test]
fn a_dense_layer_refuses_the_wrong_feature_count() {
    let mut layer = Dense::new(4, Linear::new()).unwrap();
    layer.build(&Shape::with_free_batch(&[2, 7])).unwrap();
    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[2, 5]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("Dense"), "{message}");
    assert!(message.contains("7 elements on the last axis"), "{message}");
    assert!(message.contains("got 5"), "{message}");
}

/// A pooling layer refuses a window that does not fit the length it is given
#[test]
fn a_pooling_layer_refuses_a_window_that_does_not_fit() {
    let layer = MaxPooling1D::new(4);
    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[1, 3, 2]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("pool_size"), "{message}");
    assert!(
        message.contains("cannot exceed the corresponding input dimension"),
        "{message}"
    );
}

/// A global pooling layer refuses an input whose channel axis carries no fixed extent
///
/// Axis 2 holds the channel count that the answer reports. The shape algebra needs a fixed
/// extent on every axis after the batch axis. A free channel axis is therefore refused before
/// the layer computes anything
#[test]
fn a_global_pooling_layer_refuses_a_free_spatial_axis() {
    let layer = GlobalMaxPooling1D::new();
    let message = layer
        .compute_output_shape(&Shape::new(vec![None, Some(4), None]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("GlobalMaxPooling1D"), "{message}");
    assert!(message.contains("axis 2"), "{message}");
}

/// A built PReLU refuses an extent on an axis that holds 1 slope per position
///
/// The build draws 1 slope per position of every axis after the batch axis that `shared_axes`
/// leaves out. A layer built for 3 positions on axis 1 holds 3 slopes, and 5 positions reach
/// past them
#[test]
fn p_relu_refuses_an_axis_it_holds_slopes_for() {
    let mut layer = PReLU::new(0.25).unwrap();
    layer.build(&Shape::with_free_batch(&[2, 3, 4])).unwrap();
    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[2, 5, 4]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("PReLU"), "{message}");
    assert!(message.contains("axis 1"), "{message}");
    assert!(message.contains("shared_axes"), "{message}");
}

/// `RepeatVector` refuses anything that is not a batch of feature vectors
#[test]
fn repeat_vector_refuses_a_rank_it_cannot_repeat() {
    let layer = RepeatVector::new(3).unwrap();
    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[2, 4, 5]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("RepeatVector"), "{message}");
    assert!(message.contains("rank 2"), "{message}");
}

/// Reshape refuses an input whose element count does not match its target
#[test]
fn reshape_refuses_an_element_count_it_cannot_match() {
    let layer = Reshape::new(vec![2, 2]).unwrap();
    assert!(
        layer
            .compute_output_shape(&Shape::with_free_batch(&[2, 5]))
            .is_err()
    );

    // The same layer accepts the count its target names
    let computed = layer
        .compute_output_shape(&Shape::with_free_batch(&[2, 4]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 2, 2)");
}

/// A cropping layer refuses a border that would leave an axis with no position
#[test]
fn a_cropping_layer_refuses_a_border_that_removes_everything() {
    let layer = Cropping2D::new(2);
    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[1, 4, 8, 3]))
        .unwrap_err()
        .to_string();

    assert!(message.contains("Cropping2D"), "{message}");
    assert!(message.contains("at least 1 must remain"), "{message}");
}

/// 1 unbuilt convolution of every type that bounds its input, with a shape too small for it
///
/// Every entry carries a kernel of 3 taps on each spatial axis and an input of 2 positions on
/// each spatial axis. No complete window therefore fits under `Valid` padding
fn valid_convolutions_that_cannot_fit() -> Vec<(Box<dyn Layer>, Shape, &'static str)> {
    vec![
        (
            Box::new(Conv1D::new(1, 3, 1, Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 1]),
            "Conv1D",
        ),
        (
            Box::new(Conv2D::new(1, (3, 3), (1, 1), Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 2, 1]),
            "Conv2D",
        ),
        (
            Box::new(Conv3D::new(1, (3, 3, 3), (1, 1, 1), Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 2, 2, 1]),
            "Conv3D",
        ),
        (
            Box::new(DepthwiseConv1D::new(3, 1, Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 1]),
            "DepthwiseConv1D",
        ),
        (
            Box::new(DepthwiseConv2D::new((3, 3), (1, 1), Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 2, 1]),
            "DepthwiseConv2D",
        ),
        (
            Box::new(SeparableConv1D::new(1, 3, 1, 1, Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 1]),
            "SeparableConv1D",
        ),
        (
            Box::new(SeparableConv2D::new(1, (3, 3), (1, 1), 1, Linear::new()).unwrap()),
            Shape::with_free_batch(&[1, 2, 2, 1]),
            "SeparableConv2D",
        ),
    ]
}

/// Every `Valid` convolution refuses an input axis shorter than its effective kernel
///
/// A kernel that does not fit leaves no complete window, so the axis would carry 0 positions.
/// A 0 extent is not an answer. It passes through every later layer of the stack and turns up
/// far from the layer that made it. The refusal names the layer, the axis, and the effective
/// kernel extent that the axis must reach
#[test]
fn every_valid_convolution_refuses_a_kernel_larger_than_the_input() {
    for (layer, input, name) in valid_convolutions_that_cannot_fit() {
        let message = match layer.compute_output_shape_many(std::slice::from_ref(&input)) {
            Ok(answer) => panic!("{name} answered {answer}, and it must refuse the shape {input}"),
            Err(error) => error.to_string(),
        };

        assert!(message.contains(name), "{name}: {message}");
        assert!(
            message.contains("effective kernel extent, which is 3"),
            "{name}: {message}"
        );
        assert!(message.contains("is 2."), "{name}: {message}");
    }
}

/// The same refusal reaches the build, so no oversized layer ever holds a weight
///
/// [`Layer::build_many`] is the step that allocates. It applies the same shape algebra first, so
/// a layer that cannot run draws nothing at all
#[test]
fn every_valid_convolution_refuses_the_build_as_well() {
    for (mut layer, input, name) in valid_convolutions_that_cannot_fit() {
        let message = layer
            .build_many(std::slice::from_ref(&input))
            .unwrap_err()
            .to_string();

        assert!(message.contains(name), "{name}: {message}");
        assert!(
            message.contains("effective kernel extent, which is 3"),
            "{name}: {message}"
        );
        assert!(message.contains("is 2."), "{name}: {message}");
        assert_eq!(layer.param_count().total(), 0, "{name} drew a weight");
    }
}

/// `Same` and `Causal` padding accept the kernel that `Valid` refuses
///
/// The padding supplies the cells the window reaches past, so the same geometry runs. This is
/// why the rule belongs to `Valid` alone, and why no constructor can hold it
#[test]
fn same_and_causal_padding_accept_an_oversized_kernel() {
    let mut same = Conv2D::new(1, (3, 3), (1, 1), Linear::new())
        .unwrap()
        .with_padding(PaddingType::Same);
    same.build(&Shape::with_free_batch(&[1, 2, 2, 1])).unwrap();
    let computed = same
        .compute_output_shape(&Shape::with_free_batch(&[1, 2, 2, 1]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 2, 2, 1)");

    let mut causal = Conv1D::new(1, 3, 1, Linear::new())
        .unwrap()
        .with_padding(ConvPadding::Causal);
    causal.build(&Shape::with_free_batch(&[1, 2, 1])).unwrap();
    let computed = causal
        .compute_output_shape(&Shape::with_free_batch(&[1, 2, 1]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 2, 1)");
}

/// The rule measures the dilated extent, not the tap count
///
/// 2 taps spaced 3 apart span `(2 - 1) * 3 + 1 = 4` cells. An input of 3 positions holds the 2
/// taps and not the extent they span, so the layer refuses it
#[test]
fn the_valid_rule_reads_the_dilated_extent() {
    let layer = Conv2D::new(1, (2, 2), (1, 1), Linear::new())
        .unwrap()
        .with_dilation_rate((3, 3))
        .unwrap();

    let message = layer
        .compute_output_shape(&Shape::with_free_batch(&[1, 3, 3, 1]))
        .unwrap_err()
        .to_string();
    assert!(
        message.contains("effective kernel extent, which is 4"),
        "{message}"
    );

    // 4 positions hold the dilated extent exactly, and give 1 output position
    let computed = layer
        .compute_output_shape(&Shape::with_free_batch(&[1, 4, 4, 1]))
        .unwrap();
    assert_eq!(computed.to_string(), "(None, 1, 1, 1)");
}

/// A transposed convolution puts no lower bound on its input
///
/// It grows its input instead of shrinking it, so a 1-position axis under a 3-tap kernel is a
/// normal first decoder step. The rule above must not reach these 3 layers
#[test]
fn the_transposed_convolutions_accept_a_single_position() {
    let mut conv_1d = Conv1DTranspose::new(1, 3, 1, Linear::new()).unwrap();
    conv_1d.build(&Shape::with_free_batch(&[1, 1, 1])).unwrap();
    assert_eq!(
        conv_1d
            .compute_output_shape(&Shape::with_free_batch(&[1, 1, 1]))
            .unwrap()
            .to_string(),
        "(None, 3, 1)"
    );

    let mut conv_2d = Conv2DTranspose::new(1, (3, 3), (1, 1), Linear::new()).unwrap();
    conv_2d
        .build(&Shape::with_free_batch(&[1, 1, 1, 1]))
        .unwrap();
    assert_eq!(
        conv_2d
            .compute_output_shape(&Shape::with_free_batch(&[1, 1, 1, 1]))
            .unwrap()
            .to_string(),
        "(None, 3, 3, 1)"
    );

    let mut conv_3d = Conv3DTranspose::new(1, (3, 3, 3), (1, 1, 1), Linear::new()).unwrap();
    conv_3d
        .build(&Shape::with_free_batch(&[1, 1, 1, 1, 1]))
        .unwrap();
    assert_eq!(
        conv_3d
            .compute_output_shape(&Shape::with_free_batch(&[1, 1, 1, 1, 1]))
            .unwrap()
            .to_string(),
        "(None, 3, 3, 3, 1)"
    );
}

/// A model build refuses the oversized kernel, and the message names the layer position
///
/// The first convolution takes an 8 by 8 input down to 4 by 4, and the second one takes that
/// to 2 by 2. The third one cannot run, and the build says so before it allocates. Nothing
/// past that position is built
#[test]
fn a_model_build_refuses_an_oversized_valid_kernel() {
    let refused = SequentialBuilder::new()
        .add(Conv2D::new(2, (5, 5), (1, 1), ReLU::new()).unwrap())
        .add(Conv2D::new(2, (3, 3), (1, 1), ReLU::new()).unwrap())
        .add(Conv2D::new(2, (3, 3), (1, 1), ReLU::new()).unwrap())
        .add(Flatten::new())
        .build(&Shape::known(&[2, 8, 8, 1]));

    let message = match refused {
        Ok(_) => panic!("a 3 by 3 Valid kernel does not fit a 2 by 2 input"),
        Err(error) => error.to_string(),
    };
    assert!(message.contains("layer 2"), "{message}");
    assert!(message.contains("Conv2D"), "{message}");
    assert!(message.contains("(2, 2, 2, 2)"), "{message}");
    assert!(
        message.contains("effective kernel extent, which is 3"),
        "{message}"
    );
}

/// A stack of 4 layers reports its final shape before any tensor exists
///
/// This is what the pure method buys. The caller threads the output shape of each layer into
/// the next one. It learns the shape of the model output, with no forward pass anywhere
#[test]
fn a_whole_stack_answers_before_any_tensor_exists() {
    let layers: Vec<Box<dyn Layer>> = vec![
        Box::new(Conv2D::new(4, (3, 3), (1, 1), ReLU::new()).unwrap()),
        Box::new(MaxPooling2D::new((2, 2))),
        Box::new(Flatten::new()),
        Box::new(Dense::new(5, Linear::new()).unwrap()),
    ];

    let mut shape = Shape::with_free_batch(&[1, 8, 8, 1]);
    for (position, layer) in layers.iter().enumerate() {
        shape = layer
            .compute_output_shape_many(std::slice::from_ref(&shape))
            .unwrap_or_else(|error| panic!("layer {position} ({}): {error}", layer.layer_type()));
    }
    assert_eq!(shape.to_string(), "(None, 5)");
}

/// A bad stack names the layer that refuses, and its type
///
/// The pooling window is larger than what the convolution before it emits. A forward pass
/// would report this from inside the pooling kernel, with no layer named
#[test]
fn a_bad_stack_names_the_layer_that_refuses() {
    let layers: Vec<Box<dyn Layer>> = vec![
        Box::new(Conv2D::new(4, (3, 3), (1, 1), ReLU::new()).unwrap()),
        Box::new(MaxPooling2D::new((4, 4))),
    ];

    let mut shape = Shape::with_free_batch(&[1, 4, 4, 1]);
    let mut refused = None;
    for (position, layer) in layers.iter().enumerate() {
        match layer.compute_output_shape_many(std::slice::from_ref(&shape)) {
            Ok(next) => shape = next,
            Err(error) => {
                refused = Some(format!(
                    "layer {position} ({}): {error}",
                    layer.layer_type()
                ));
                break;
            }
        }
    }

    let report = refused.expect("the stack must be refused");
    assert!(report.starts_with("layer 1 (MaxPooling2D)"), "{report}");
}

/// `output_shape` runs the pure method against the shape the layer holds
///
/// The 3 layers below cover the 3 sources of that shape. The sources are a build the caller
/// ran, a build that `forward_mut` ran from the tensor, and nothing at all
#[test]
fn output_shape_reads_the_shape_the_layer_holds() {
    // The build supplies feature extents. The display reports a free batch axis
    let mut declared = AveragePooling1D::new(2);
    declared.build(&Shape::known(&[1, 6, 1])).unwrap();
    assert_eq!(declared.output_shape(), "(None, 3, 1)");

    // `forward_mut` builds from the tensor. The displayed shape keeps a free batch axis
    let mut learned = UpSampling2D::new(2, Interpolation::Nearest).unwrap();
    assert_eq!(learned.output_shape(), "Unknown");
    learned
        .forward_mut(
            &Array::zeros(IxDyn(&[1, 3, 3, 2])).into_dyn(),
            &mut Ctx::inference(),
        )
        .unwrap();
    assert_eq!(learned.output_shape(), "(None, 6, 6, 2)");

    // Held by neither, so the layer reports nothing
    let global = GlobalMaxPooling1D::new();
    assert_eq!(global.output_shape(), "Unknown");
}

/// The printed output shape matches exactly what the forward pass enforces
#[test]
fn a_built_layer_refuses_the_extents_its_summary_does_not_name() {
    use ndarray::Array4;
    let mut layer = rustyml::neural_network::layers::Conv2D::new(
        3,
        (2, 2),
        (1, 1),
        rustyml::neural_network::layers::Activation::Linear,
    )
    .unwrap();
    layer
        .build(&rustyml::neural_network::Shape::known(&[2, 4, 4, 2]))
        .unwrap();
    assert_eq!(layer.output_shape(), "(None, 3, 3, 3)");

    // A spatial extent other than the one the build declared is refused
    let wider = Array4::<f32>::ones((2, 6, 6, 2)).into_dyn();
    assert!(layer.forward(&wider, &mut Ctx::inference()).is_err());

    // A different batch size still passes, because the batch axis is never checked
    let bigger_batch = Array4::<f32>::ones((5, 4, 4, 2)).into_dyn();
    assert!(layer.forward(&bigger_batch, &mut Ctx::inference()).is_ok());
}

/// Every layer type answers `compute_output_shape` before its build
///
/// This is the contract that the method exists for. The answer reads the layer configuration
/// and the argument, and nothing that a build or a forward pass wrote, so an unbuilt layer
/// answers. That is what lets `SequentialBuilder::build` walk a stack, thread each output
/// shape into the next layer, and refuse a bad stack before any tensor exists
///
/// A layer that reads a field that only `build` fills breaks this. Such a layer reports the
/// unset value of that field and refuses every input, including its intended build shape
#[test]
fn every_layer_type_answers_before_its_build() {
    struct AnswersUnbuilt(
        /// The distinct layer types that answered before their build.
        BTreeSet<String>,
    );
    impl LayerVisitor for AnswersUnbuilt {
        fn visit<L: Layer + 'static>(&mut self, case: Case, make: &dyn Fn() -> L) {
            let layer = make();
            let name = layer.layer_type().to_string();
            assert!(
                layer.build_config().is_none(),
                "{name} must hold no build before its build"
            );

            let computed = layer
                .compute_output_shape_many(&case.inputs)
                .unwrap_or_else(|error| {
                    panic!(
                        "{name} refused the shapes {:?} before its build: {error}",
                        case.inputs
                    )
                });
            assert_eq!(computed.to_string(), case.output, "{name}");

            assert!(
                self.0.insert(name.clone()),
                "{name} reaches the roster more than once"
            );
        }
    }

    let mut visitor = AnswersUnbuilt(BTreeSet::new());
    visit_every_layer(&mut visitor);
    assert_eq!(visitor.0.len(), LAYER_TYPE_COUNT);
}

/// A build changes no answer that `compute_output_shape` already gave
///
/// The 2 halves of the contract meet here. Every layer of the roster answers for the shapes it
/// is about to be built for, then takes that build, then answers again. A layer that reads
/// build state gives 2 different answers, and a layer that reads the argument alone gives 1
#[test]
fn a_build_changes_no_answer() {
    struct SameAfterBuild;
    impl LayerVisitor for SameAfterBuild {
        fn visit<L: Layer + 'static>(&mut self, case: Case, make: &dyn Fn() -> L) {
            let mut layer = make();
            let name = layer.layer_type().to_string();
            let before = layer.compute_output_shape_many(&case.inputs).unwrap();

            layer.build_many(&case.inputs).unwrap_or_else(|error| {
                panic!("{name} refused to build for {:?}: {error}", case.inputs)
            });

            let after = layer
                .compute_output_shape_many(&case.inputs)
                .unwrap_or_else(|error| {
                    panic!("{name} refused {:?} after its build: {error}", case.inputs)
                });
            assert_eq!(before, after, "{name}");
            assert_eq!(after.to_string(), case.output, "{name}");
        }
    }

    visit_every_layer(&mut SameAfterBuild);
}

/// A model input with an empty feature axis is refused, and the refusal names the axis
///
/// An axis of 0 elements carries no data. Many layers pass an extent through unchanged. Without
/// this rule a whole stack builds and then produces empty tensors, and the emptiness only shows
/// itself in the output. The batch axis is exempt, because a batch of 0 is an empty dataset that
/// `fit` and `predict` already refuse when the data arrives
#[test]
fn a_model_refuses_an_input_shape_with_an_empty_feature_axis() {
    let error = SequentialBuilder::new()
        .add(Flatten::new())
        .build(&Shape::known(&[1, 0, 4]))
        .err()
        .expect("a 0 extent must not build");
    let text = error.to_string();
    assert!(
        text.contains("axis 1") && text.contains("0 elements"),
        "the refusal must name the empty axis, got {text}"
    );

    // The same stack with a free batch axis and no empty extent builds
    SequentialBuilder::new()
        .add(Flatten::new())
        .build(&Shape::with_free_batch(&[1, 3, 4]))
        .expect("a free batch axis is not an empty axis");
}