incin-core 0.1.0

Core tensor operations and static shape checking for incin
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
use crate::backend_authoring::VariableBackend;
use crate::err::Result;
use crate::shapes::{DynShape, Shape};
use crate::tensor::device::Device;
use crate::tensor::dtype::DType;
use crate::tensor::transfer::ToDevice;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

/// Receives trainable parameter leaves without exposing backend handle maps.
pub trait ParameterVisitor<B: VariableBackend> {
    /// Visits one parameter tensor with its state path.
    fn visit_param<S, K, Train>(
        &mut self,
        path: &crate::nn::StatePath,
        param: &crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: Shape,
        K: DType,
        Train: crate::nn::param::TrainState;
}

/// Traverses trainable parameter leaves using a typed visitor.
pub trait VisitParameters<B: VariableBackend> {
    /// Number of flat module slots occupied by this subtree.
    fn flat_width() -> usize
    where
        Self: Sized,
    {
        1
    }

    /// Walks every parameter via the visitor.
    fn visit_parameters<V: ParameterVisitor<B>>(
        &self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()>;

    /// Visits this subtree at a flat positional index under `parent`.
    fn visit_parameters_flat<V: ParameterVisitor<B>>(
        &self,
        parent: &crate::nn::StatePath,
        base_index: usize,
        visitor: &mut V,
    ) -> Result<()>
    where
        Self: Sized,
    {
        self.visit_parameters(&parent.index(base_index), visitor)
    }
}

/// Recursively switches a module (and every submodule reachable through
/// `#[module]`-derived fields) between training and evaluation behavior -
/// `#[module]` auto-implements this for derived modules.
/// typed state visitors, walking every field and delegating to its `TrainMode`
/// implementation, so calling `.eval()` on a top-level model
/// propagates all the way down to every nested [`crate::nn::dropout::Dropout`]
/// without the caller needing to reach into the tree by hand.
///
/// `set_training` defaults to a no-op, so any leaf layer with no
/// training-dependent behavior (`Linear`, `ReLU`, `Conv2d`, ...) can opt in
/// with a bare `impl TrainMode for X {}` - this is what makes it possible
/// for [`Sequential`] to require `L1: TrainMode, L2: TrainMode` (see its own
/// impl below) without forcing every existing layer type to implement real
/// logic. Containers use explicit trait bounds and direct field calls.
///
/// ## What this currently affects
///
/// Only [`crate::nn::dropout::Dropout`] has training-dependent behavior
/// today (`is_training` gates whether it randomly zeroes elements or acts as
/// an identity function - see its own doc). **`BatchNorm2d` does not
/// currently respond to this call** - its own `forward` always normalizes
/// using the supplied running statistics regardless of mode (a deliberate,
/// already-documented "inference-mode-only" scope carried forward from an
/// earlier design decision - see the `_momentum` parameter's doc comment in
/// `cpu/ops/norm.rs::batch_norm_impl`), so it opts into `TrainMode` as a
/// harmless no-op via the macro's default rather than silently claiming a
/// behavior change that isn't actually implemented. Reversing that
/// BatchNorm scope decision is a separate, larger, cross-backend proposal,
/// not part of this one.
///
/// ## Examples
/// ```rust
/// # extern crate incin_core as incin;
/// # fn main() -> incin::prelude::Result<()> {
/// # type DefaultBackend = incin_backends::cpu::CpuBackendImpl;
/// use incin::prelude::*;
///
/// let mut model = seq!(
///     Linear::<s![4, 4], DefaultBackend>::build(())?,
///     Dropout::new(0.5)
/// );
/// let x = Tensor::<Dyn, DefaultBackend>::zeros(vec![1, 4])?;
///
/// model.eval();   // nested Dropout layers become identity functions
/// let out = model.forward(x)?;
/// model.train();  // back to normal (randomized) training behavior
/// # Ok(()) }
/// ```
pub trait TrainMode {
    /// Sets training mode on this module and recursively on every
    /// submodule. `train()`/`eval()` are the ergonomic entry points - most
    /// callers should use those instead of calling this directly. Defaults
    /// to a no-op so leaf layers with no training-dependent behavior can
    /// opt in with a bare `impl TrainMode for X {}`.
    #[inline]
    fn set_training(&mut self, _training: bool) {}

    /// Switches to training mode (the default after construction).
    #[inline]
    fn train(&mut self) {
        self.set_training(true);
    }

    /// Switches to evaluation/inference mode.
    #[inline]
    fn eval(&mut self) {
        self.set_training(false);
    }
}

/// Explicit field contract used by module traversal and diagnostics.
pub trait ShapeInfo {
    /// Returns a compact shape description when the field owns tensor-shaped state.
    fn shape_info(&self) -> Option<String>;
}

impl<
    S: Shape + DynShape,
    B: crate::tensor::backend::VariableBackend,
    K: DType,
    Train: crate::nn::param::TrainState,
> ShapeInfo for crate::nn::param::Param<S, B, K, Train>
{
    fn shape_info(&self) -> Option<String> {
        Some(format!("{:?}", self.shape_dims()))
    }
}

impl<S: Shape + DynShape, B: crate::tensor::backend::VariableBackend, K: DType> ShapeInfo
    for crate::nn::param::Buffer<S, B, K>
{
    fn shape_info(&self) -> Option<String> {
        Some(format!("{:?}", self.shape_dims()))
    }
}

impl<T: ShapeInfo> ShapeInfo for Option<T> {
    fn shape_info(&self) -> Option<String> {
        self.as_ref().and_then(ShapeInfo::shape_info)
    }
}

/// A generic Neural Network Layer or Module.
/// Capable of taking an input and returning an output or error.
///
/// `Module` is the fundamental building block of neural networks in Incin.
/// Any layer, from a simple ReLU to an entire ResNet architecture, implements `Module`.
///
/// ## Deriving Modules
///
/// While you can implement `Module` manually, the recommended approach is to use the `#[module]` attribute macro
/// provided by `incin-macros`. This automatically derives typed visitors and structural boilerplate.
///
/// ```rust
/// # extern crate incin_core as incin;
/// use incin::backend_authoring::VariableBackend;
/// use incin::prelude::*;
///
/// #[module]
/// pub struct MyLayer<B: VariableBackend> {
///     weight: Param<s![128, 128], B>,
///     bias: Param<s![128], B>,
/// }
///
/// impl<B: VariableBackend> Module<Tensor<s![1, 128], B>> for MyLayer<B> {
///     type Output = Tensor<s![1, 128], B>;
///     type Error = Error;
///
///     fn forward(&self, x: Tensor<s![1, 128], B>) -> Result<Self::Output> {
///         // Custom logic here
///         Ok(x)
///     }
/// }
/// ```
pub trait Module<Input> {
    /// The output tensor type produced by this module's forward pass.
    type Output;
    /// The error type returned if the forward pass fails.
    type Error;

    /// Runs the forward pass of this module on the given input.
    fn forward(&self, input: Input) -> core::result::Result<Self::Output, Self::Error>;
}

/// A sequential container for composing two modules.
/// `Sequential` automatically implements `Module` if the inner modules are compatible.
#[derive(Debug, Clone)]
pub struct Sequential<L1, L2>(pub L1, pub L2);

impl<I, L1, L2> Module<I> for Sequential<L1, L2>
where
    L1: Module<I>,
    L2: Module<L1::Output, Error = L1::Error>,
{
    /// The output tensor type produced by this module's forward pass.
    type Output = L2::Output;
    /// The error type returned if the forward pass fails.
    type Error = L1::Error;

    #[inline]
    /// Runs the forward pass of this module on the given input.
    fn forward(&self, input: I) -> core::result::Result<Self::Output, Self::Error> {
        let out1 = self.0.forward(input)?;
        self.1.forward(out1)
    }
}

impl<B: crate::tensor::backend::VariableBackend, NewD: Device, L1, L2>
    crate::tensor::transfer::ToDevice<B, NewD> for Sequential<L1, L2>
where
    L1: ToDevice<B, NewD>,
    L2: ToDevice<B, NewD>,
{
    /// The same `Sequential` with each inner module transferred to `NewD`.
    type Output = Sequential<L1::Output, L2::Output>;
    /// Transfers both inner modules to the new device.
    fn to_device(self, arg: &NewD::Arg) -> Result<Self::Output> {
        Ok(Sequential(self.0.to_device(arg)?, self.1.to_device(arg)?))
    }
}

// Explicit bounds + direct calls for every leaf layer with no
// training-dependent behavior
// (`Linear`, `ReLU`, `Conv2d`, pooling layers, ...) implements `TrainMode`
// via its default no-op body specifically so this bound is satisfiable for
// real `seq!`-built chains, not just chains containing `Dropout`.
impl<L1: TrainMode, L2: TrainMode> TrainMode for Sequential<L1, L2> {
    /// Recursively sets training mode on both inner modules.
    fn set_training(&mut self, training: bool) {
        self.0.set_training(training);
        self.1.set_training(training);
    }
}

impl<B, L1, L2> crate::nn::VisitState<B> for Sequential<L1, L2>
where
    B: crate::tensor::backend::VariableBackend,
    L1: crate::nn::VisitState<B>,
    L2: crate::nn::VisitState<B>,
{
    fn flat_width() -> usize {
        L1::flat_width() + L2::flat_width()
    }

    fn visit_state<V: crate::nn::StateVisitor<B>>(
        &self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()> {
        self.visit_state_flat(path, 0, visitor)
    }

    fn visit_state_flat<V: crate::nn::StateVisitor<B>>(
        &self,
        parent: &crate::nn::StatePath,
        base_index: usize,
        visitor: &mut V,
    ) -> Result<()> {
        self.0.visit_state_flat(parent, base_index, visitor)?;
        self.1
            .visit_state_flat(parent, base_index + L1::flat_width(), visitor)
    }
}

impl<B, L1, L2> crate::nn::VisitStateMut<B> for Sequential<L1, L2>
where
    B: crate::tensor::backend::VariableBackend,
    L1: crate::nn::VisitStateMut<B>,
    L2: crate::nn::VisitStateMut<B>,
{
    fn flat_width() -> usize {
        L1::flat_width() + L2::flat_width()
    }

    fn visit_state_mut<V: crate::nn::StateMutVisitor<B>>(
        &mut self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()> {
        self.visit_state_mut_flat(path, 0, visitor)
    }

    fn visit_state_mut_flat<V: crate::nn::StateMutVisitor<B>>(
        &mut self,
        parent: &crate::nn::StatePath,
        base_index: usize,
        visitor: &mut V,
    ) -> Result<()> {
        self.0.visit_state_mut_flat(parent, base_index, visitor)?;
        self.1
            .visit_state_mut_flat(parent, base_index + L1::flat_width(), visitor)
    }
}

impl<B, L1, L2> crate::nn::VisitParameters<B> for Sequential<L1, L2>
where
    B: crate::tensor::backend::VariableBackend,
    L1: crate::nn::VisitParameters<B>,
    L2: crate::nn::VisitParameters<B>,
{
    fn flat_width() -> usize {
        L1::flat_width() + L2::flat_width()
    }

    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
        &self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()> {
        self.visit_parameters_flat(path, 0, visitor)
    }

    fn visit_parameters_flat<V: crate::nn::ParameterVisitor<B>>(
        &self,
        parent: &crate::nn::StatePath,
        base_index: usize,
        visitor: &mut V,
    ) -> Result<()> {
        self.0.visit_parameters_flat(parent, base_index, visitor)?;
        self.1
            .visit_parameters_flat(parent, base_index + L1::flat_width(), visitor)
    }
}

impl<T, B: crate::tensor::backend::VariableBackend> crate::nn::VisitStateMut<B>
    for core::marker::PhantomData<T>
where
    T: crate::tensor::dtype::DType,
{
    fn visit_state_mut<V: crate::nn::StateMutVisitor<B>>(
        &mut self,
        _path: &crate::nn::StatePath,
        _visitor: &mut V,
    ) -> Result<()> {
        Ok(())
    }
}

impl<L, B> crate::nn::VisitState<B> for Option<L>
where
    L: crate::nn::VisitState<B>,
    B: crate::tensor::backend::VariableBackend,
{
    fn visit_state<V: crate::nn::StateVisitor<B>>(
        &self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()> {
        if let Some(value) = self {
            value.visit_state(path, visitor)?;
        }
        Ok(())
    }
}

impl<L, B> crate::nn::VisitStateMut<B> for Option<L>
where
    L: crate::nn::VisitStateMut<B>,
    B: crate::tensor::backend::VariableBackend,
{
    fn visit_state_mut<V: crate::nn::StateMutVisitor<B>>(
        &mut self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()> {
        if let Some(value) = self {
            value.visit_state_mut(path, visitor)?;
        }
        Ok(())
    }
}

impl<L, B> crate::nn::VisitParameters<B> for Option<L>
where
    L: crate::nn::VisitParameters<B>,
    B: crate::tensor::backend::VariableBackend,
{
    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
        &self,
        path: &crate::nn::StatePath,
        visitor: &mut V,
    ) -> Result<()> {
        if let Some(value) = self {
            value.visit_parameters(path, visitor)?;
        }
        Ok(())
    }
}

/// Represents a node in the neural network layer structure metadata tree.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct LayerNode {
    /// The display name of this layer node.
    pub name: String,
    /// The type name of this layer node.
    pub type_name: String,
    /// Human-readable string describing the layer's shape for display purposes.
    pub shape_info: String,
    /// Nested child layer nodes for hierarchical visualization.
    pub children: Vec<LayerNode>,
}

/// A trait implemented by all Neural Network modules to report their structural architecture.
pub trait NamedLayers {
    /// Returns the layer hierarchy rooted at this module for visualization.
    fn layer_structure(&self, prefix: &str) -> Vec<LayerNode>;

    /// Returns a formatted human-readable architecture summary table.
    fn summary(&self) -> String {
        format_layer_summary(&self.layer_structure(""))
    }

    /// [`Self::summary`] plus a params/MACs/FLOPs totals footer at the given
    /// batch size - see [`format_layer_summary_with_stats`]. Only callable
    /// when `Self` also implements
    /// [`crate::nn::stats::ComputeStats`] (every `#[module]`
    /// struct does, automatically).
    fn summary_with_stats(&self, batch: u64) -> String
    where
        Self: crate::nn::stats::ComputeStats,
    {
        format_layer_summary_with_stats(&self.layer_structure(""), self.stats(batch))
    }
}

/// Formats a slice of `LayerNode` items into a human-readable printable tree table.
pub fn format_layer_summary(nodes: &[LayerNode]) -> String {
    let mut out = String::new();
    out.push_str(
        "================================================================================\n",
    );
    out.push_str(&format!(
        "{:<32} {:<28} {:<16}\n",
        "Layer (Name: Type)", "Shape / Spec", "Details"
    ));
    out.push_str(
        "================================================================================\n",
    );

    fn print_node(node: &LayerNode, indent: usize, out: &mut String) {
        let indent_str = "  ".repeat(indent);
        let clean_type = clean_type_name(&node.type_name);
        let name_type = if node.name.is_empty() {
            format!("{}{}", indent_str, clean_type)
        } else {
            format!("{}{}: {}", indent_str, node.name, clean_type)
        };
        let shape = if node.shape_info.is_empty() {
            "-"
        } else {
            &node.shape_info
        };
        out.push_str(&format!("{:<32} {:<28} {:<16}\n", name_type, shape, "-"));
        for child in &node.children {
            print_node(child, indent + 1, out);
        }
    }

    for node in nodes {
        print_node(node, 0, &mut out);
    }
    out.push_str(
        "================================================================================\n",
    );
    out
}

/// [`format_layer_summary`] plus a totals footer (params / MACs / FLOPs at
/// the given `stats`' batch size) - the "optional stats column" from
/// `docs/growth/04-compile-time-stats.md`'s v1 acceptance criteria. This is
/// a **totals row**, not a per-layer breakdown: threading per-node stats
/// through `LayerNode` itself would mean adding a batch parameter to
/// `NamedLayers::layer_structure`, a breaking signature change to a widely
/// implemented trait that a "ships this week" v1 shouldn't take on. Extends
/// [`format_layer_summary`] by composition rather than duplicating its tree
/// printer.
pub fn format_layer_summary_with_stats(
    nodes: &[LayerNode],
    stats: crate::nn::stats::ModelStats,
) -> String {
    let mut out = format_layer_summary(nodes);
    out.push_str(&format!(
        "Total params: {}    MACs: {}    FLOPs: {}\n",
        stats.params, stats.macs, stats.flops
    ));
    out.push_str(
        "================================================================================\n",
    );
    out
}

/// Cleans a fully qualified Rust type path into a simple name (e.g. `incin::nn::linear::Linear<...>` -> `Linear`).
pub fn clean_type_name(raw: &str) -> String {
    let without_generics = match raw.find('<') {
        Some(idx) => &raw[..idx],
        None => raw,
    };
    let name = match without_generics.rfind("::") {
        Some(idx) => &without_generics[idx + 2..],
        None => without_generics,
    };
    name.trim().to_string()
}

/// Recursively updates child node prefixes when a parent node is renamed.
pub fn update_node_name_prefix(node: &mut LayerNode, new_name: &str) {
    let old_name = node.name.clone();
    node.name = new_name.to_string();
    for child in &mut node.children {
        let child_suffix = if old_name.is_empty() {
            &child.name
        } else if child.name.starts_with(&old_name) {
            let prefix_len = old_name.len();
            if child.name.len() > prefix_len && child.name.as_bytes()[prefix_len] == b'.' {
                &child.name[prefix_len + 1..]
            } else {
                &child.name[prefix_len..]
            }
        } else {
            &child.name
        };
        let new_child_name = if new_name.is_empty() {
            child_suffix.to_string()
        } else {
            format!("{}.{}", new_name, child_suffix)
        };
        update_node_name_prefix(child, &new_child_name);
    }
}

/// Flatten helper to assign sequential names (e.g. Linear1, ReLU1, Linear2) to a slice of child nodes.
pub fn assign_sequential_names(nodes: &mut [LayerNode], prefix: &str) {
    let mut type_counts = alloc::collections::BTreeMap::new();
    for (i, node) in nodes.iter_mut().enumerate() {
        let clean_type = clean_type_name(&node.type_name);
        let count = type_counts.entry(clean_type.clone()).or_insert(0);
        *count += 1;

        let seq_name = if !clean_type.is_empty() && clean_type != "LayerNode" {
            format!("{}{}", clean_type, count)
        } else {
            format!("{}", i)
        };

        let new_name = if prefix.is_empty() {
            seq_name
        } else {
            format!("{}.{}", prefix, seq_name)
        };

        update_node_name_prefix(node, &new_name);
    }
}

impl<L1: NamedLayers, L2: NamedLayers> NamedLayers for Sequential<L1, L2> {
    /// Returns the layer hierarchy rooted at this module for visualization.
    fn layer_structure(&self, prefix: &str) -> Vec<LayerNode> {
        let mut nodes = Vec::new();
        nodes.extend(self.0.layer_structure(""));
        nodes.extend(self.1.layer_structure(""));

        assign_sequential_names(&mut nodes, prefix);
        nodes
    }
}

impl<T: NamedLayers> NamedLayers for Option<T> {
    /// Returns the layer hierarchy rooted at this module for visualization.
    fn layer_structure(&self, prefix: &str) -> Vec<LayerNode> {
        if let Some(layer) = self {
            layer.layer_structure(prefix)
        } else {
            Vec::new()
        }
    }
}

impl<T: TrainMode> TrainMode for Option<T> {
    fn set_training(&mut self, training: bool) {
        if let Some(value) = self {
            value.set_training(training);
        }
    }
}

impl<
    S: Shape + DynShape,
    B: crate::tensor::backend::VariableBackend,
    K: DType,
    Train: crate::nn::param::TrainState,
> NamedLayers for crate::nn::param::Param<S, B, K, Train>
{
    fn layer_structure(&self, _prefix: &str) -> Vec<LayerNode> {
        Vec::new()
    }
}

impl<S: Shape + DynShape, B: crate::tensor::backend::VariableBackend, K: DType> NamedLayers
    for crate::nn::param::Buffer<S, B, K>
{
    fn layer_structure(&self, _prefix: &str) -> Vec<LayerNode> {
        Vec::new()
    }
}

impl<
    S: Shape + DynShape,
    B: crate::tensor::backend::VariableBackend,
    K: DType,
    Train: crate::nn::param::TrainState,
> TrainMode for crate::nn::param::Param<S, B, K, Train>
{
}

impl<S: Shape + DynShape, B: crate::tensor::backend::VariableBackend, K: DType> TrainMode
    for crate::nn::param::Buffer<S, B, K>
{
}

/// A macro to easily build Sequential models with many layers.
/// `seq!(L1, L2, L3)` expands to `Sequential(L1, Sequential(L2, L3))`.
///
/// Naming the *type* of that value (e.g. for a `#[module]` struct field)
/// still requires hand-nesting `Sequential<L1, Sequential<L2, L3>>` - see
/// `SeqTy!`, which generates that same nesting from the same
/// flat layer list so it never has to be written out by hand.
#[macro_export]
macro_rules! seq {
    ($l1:expr) => {
        $l1
    };
    ($l1:expr, $($tail:expr),+ $(,)?) => {
        $crate::nn::Sequential($l1, $crate::seq!($($tail),+))
    };
}

/// Names the nested [`Sequential`] *type* that [`seq!`] would build a
/// *value* of, from the same flat list of layer types.
///
/// `Sequential<L1, L2>` only composes two layers at a time, so a
/// three-or-more-layer model's field type has to be hand-nested
/// (`Sequential<A, Sequential<B, C>>`) even though `seq!(a, b, c)` already
/// builds the matching value without that nesting spelled out. This macro
/// mirrors `seq!`'s exact right-nesting rule at the type level:
/// `SeqTy!(L1, L2, L3)` expands to `Sequential<L1, Sequential<L2, L3>>`,
/// so a layer list only needs to be written once per meaning (the type via
/// this macro, the value via `seq!`) instead of the type being re-derived by
/// hand every time the layer list changes.
///
/// ## Examples
/// ```rust
/// # extern crate incin_core as incin;
/// # fn main() -> incin::prelude::Result<()> {
/// # type DefaultBackend = incin_backends::cpu::CpuBackendImpl;
/// use incin::prelude::*;
///
/// type Net = SeqTy!(
///     Linear<s![768, 256], DefaultBackend>,
///     ReLU,
///     Linear<s![256, 10], DefaultBackend>
/// );
///
/// let net: Net = seq!(
///     Linear::<s![768, 256], DefaultBackend>::build(())?,
///     ReLU,
///     Linear::<s![256, 10], DefaultBackend>::build(())?
/// );
/// # Ok(()) }
/// ```
#[macro_export]
macro_rules! SeqTy {
    ($l1:ty) => {
        $l1
    };
    ($l1:ty, $($tail:ty),+ $(,)?) => {
        $crate::nn::Sequential<$l1, $crate::SeqTy!($($tail),+)>
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! seq_ty {
    ($($tokens:tt)*) => {
        $crate::SeqTy!($($tokens)*)
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! seq_type {
    ($($tokens:tt)*) => {
        $crate::SeqTy!($($tokens)*)
    };
}