Skip to main content

incin_core/nn/
module.rs

1use crate::backend_authoring::VariableBackend;
2use crate::err::Result;
3use crate::shapes::{DynShape, Shape};
4use crate::tensor::device::Device;
5use crate::tensor::dtype::DType;
6use crate::tensor::transfer::ToDevice;
7use alloc::string::{String, ToString};
8use alloc::vec::Vec;
9
10/// Receives trainable parameter leaves without exposing backend handle maps.
11pub trait ParameterVisitor<B: VariableBackend> {
12    /// Visits one parameter tensor with its state path.
13    fn visit_param<S, K, Train>(
14        &mut self,
15        path: &crate::nn::StatePath,
16        param: &crate::nn::param::Param<S, B, K, Train>,
17    ) -> Result<()>
18    where
19        S: Shape,
20        K: DType,
21        Train: crate::nn::param::TrainState;
22}
23
24/// Traverses trainable parameter leaves using a typed visitor.
25pub trait VisitParameters<B: VariableBackend> {
26    /// Number of flat module slots occupied by this subtree.
27    fn flat_width() -> usize
28    where
29        Self: Sized,
30    {
31        1
32    }
33
34    /// Walks every parameter via the visitor.
35    fn visit_parameters<V: ParameterVisitor<B>>(
36        &self,
37        path: &crate::nn::StatePath,
38        visitor: &mut V,
39    ) -> Result<()>;
40
41    /// Visits this subtree at a flat positional index under `parent`.
42    fn visit_parameters_flat<V: ParameterVisitor<B>>(
43        &self,
44        parent: &crate::nn::StatePath,
45        base_index: usize,
46        visitor: &mut V,
47    ) -> Result<()>
48    where
49        Self: Sized,
50    {
51        self.visit_parameters(&parent.index(base_index), visitor)
52    }
53}
54
55/// Recursively switches a module (and every submodule reachable through
56/// `#[module]`-derived fields) between training and evaluation behavior -
57/// `#[module]` auto-implements this for derived modules.
58/// typed state visitors, walking every field and delegating to its `TrainMode`
59/// implementation, so calling `.eval()` on a top-level model
60/// propagates all the way down to every nested [`crate::nn::dropout::Dropout`]
61/// without the caller needing to reach into the tree by hand.
62///
63/// `set_training` defaults to a no-op, so any leaf layer with no
64/// training-dependent behavior (`Linear`, `ReLU`, `Conv2d`, ...) can opt in
65/// with a bare `impl TrainMode for X {}` - this is what makes it possible
66/// for [`Sequential`] to require `L1: TrainMode, L2: TrainMode` (see its own
67/// impl below) without forcing every existing layer type to implement real
68/// logic. Containers use explicit trait bounds and direct field calls.
69///
70/// ## What this currently affects
71///
72/// Only [`crate::nn::dropout::Dropout`] has training-dependent behavior
73/// today (`is_training` gates whether it randomly zeroes elements or acts as
74/// an identity function - see its own doc). **`BatchNorm2d` does not
75/// currently respond to this call** - its own `forward` always normalizes
76/// using the supplied running statistics regardless of mode (a deliberate,
77/// already-documented "inference-mode-only" scope carried forward from an
78/// earlier design decision - see the `_momentum` parameter's doc comment in
79/// `cpu/ops/norm.rs::batch_norm_impl`), so it opts into `TrainMode` as a
80/// harmless no-op via the macro's default rather than silently claiming a
81/// behavior change that isn't actually implemented. Reversing that
82/// BatchNorm scope decision is a separate, larger, cross-backend proposal,
83/// not part of this one.
84///
85/// ## Examples
86/// ```rust
87/// # extern crate incin_core as incin;
88/// # fn main() -> incin::prelude::Result<()> {
89/// # type DefaultBackend = incin_backends::cpu::CpuBackendImpl;
90/// use incin::prelude::*;
91///
92/// let mut model = seq!(
93///     Linear::<s![4, 4], DefaultBackend>::build(())?,
94///     Dropout::new(0.5)
95/// );
96/// let x = Tensor::<Dyn, DefaultBackend>::zeros(vec![1, 4])?;
97///
98/// model.eval();   // nested Dropout layers become identity functions
99/// let out = model.forward(x)?;
100/// model.train();  // back to normal (randomized) training behavior
101/// # Ok(()) }
102/// ```
103pub trait TrainMode {
104    /// Sets training mode on this module and recursively on every
105    /// submodule. `train()`/`eval()` are the ergonomic entry points - most
106    /// callers should use those instead of calling this directly. Defaults
107    /// to a no-op so leaf layers with no training-dependent behavior can
108    /// opt in with a bare `impl TrainMode for X {}`.
109    #[inline]
110    fn set_training(&mut self, _training: bool) {}
111
112    /// Switches to training mode (the default after construction).
113    #[inline]
114    fn train(&mut self) {
115        self.set_training(true);
116    }
117
118    /// Switches to evaluation/inference mode.
119    #[inline]
120    fn eval(&mut self) {
121        self.set_training(false);
122    }
123}
124
125/// Explicit field contract used by module traversal and diagnostics.
126pub trait ShapeInfo {
127    /// Returns a compact shape description when the field owns tensor-shaped state.
128    fn shape_info(&self) -> Option<String>;
129}
130
131impl<
132    S: Shape + DynShape,
133    B: crate::tensor::backend::VariableBackend,
134    K: DType,
135    Train: crate::nn::param::TrainState,
136> ShapeInfo for crate::nn::param::Param<S, B, K, Train>
137{
138    fn shape_info(&self) -> Option<String> {
139        Some(format!("{:?}", self.shape_dims()))
140    }
141}
142
143impl<S: Shape + DynShape, B: crate::tensor::backend::VariableBackend, K: DType> ShapeInfo
144    for crate::nn::param::Buffer<S, B, K>
145{
146    fn shape_info(&self) -> Option<String> {
147        Some(format!("{:?}", self.shape_dims()))
148    }
149}
150
151impl<T: ShapeInfo> ShapeInfo for Option<T> {
152    fn shape_info(&self) -> Option<String> {
153        self.as_ref().and_then(ShapeInfo::shape_info)
154    }
155}
156
157/// A generic Neural Network Layer or Module.
158/// Capable of taking an input and returning an output or error.
159///
160/// `Module` is the fundamental building block of neural networks in Incin.
161/// Any layer, from a simple ReLU to an entire ResNet architecture, implements `Module`.
162///
163/// ## Deriving Modules
164///
165/// While you can implement `Module` manually, the recommended approach is to use the `#[module]` attribute macro
166/// provided by `incin-macros`. This automatically derives typed visitors and structural boilerplate.
167///
168/// ```rust
169/// # extern crate incin_core as incin;
170/// use incin::backend_authoring::VariableBackend;
171/// use incin::prelude::*;
172///
173/// #[module]
174/// pub struct MyLayer<B: VariableBackend> {
175///     weight: Param<s![128, 128], B>,
176///     bias: Param<s![128], B>,
177/// }
178///
179/// impl<B: VariableBackend> Module<Tensor<s![1, 128], B>> for MyLayer<B> {
180///     type Output = Tensor<s![1, 128], B>;
181///     type Error = Error;
182///
183///     fn forward(&self, x: Tensor<s![1, 128], B>) -> Result<Self::Output> {
184///         // Custom logic here
185///         Ok(x)
186///     }
187/// }
188/// ```
189pub trait Module<Input> {
190    /// The output tensor type produced by this module's forward pass.
191    type Output;
192    /// The error type returned if the forward pass fails.
193    type Error;
194
195    /// Runs the forward pass of this module on the given input.
196    fn forward(&self, input: Input) -> core::result::Result<Self::Output, Self::Error>;
197}
198
199/// A sequential container for composing two modules.
200/// `Sequential` automatically implements `Module` if the inner modules are compatible.
201#[derive(Debug, Clone)]
202pub struct Sequential<L1, L2>(pub L1, pub L2);
203
204impl<I, L1, L2> Module<I> for Sequential<L1, L2>
205where
206    L1: Module<I>,
207    L2: Module<L1::Output, Error = L1::Error>,
208{
209    /// The output tensor type produced by this module's forward pass.
210    type Output = L2::Output;
211    /// The error type returned if the forward pass fails.
212    type Error = L1::Error;
213
214    #[inline]
215    /// Runs the forward pass of this module on the given input.
216    fn forward(&self, input: I) -> core::result::Result<Self::Output, Self::Error> {
217        let out1 = self.0.forward(input)?;
218        self.1.forward(out1)
219    }
220}
221
222impl<B: crate::tensor::backend::VariableBackend, NewD: Device, L1, L2>
223    crate::tensor::transfer::ToDevice<B, NewD> for Sequential<L1, L2>
224where
225    L1: ToDevice<B, NewD>,
226    L2: ToDevice<B, NewD>,
227{
228    /// The same `Sequential` with each inner module transferred to `NewD`.
229    type Output = Sequential<L1::Output, L2::Output>;
230    /// Transfers both inner modules to the new device.
231    fn to_device(self, arg: &NewD::Arg) -> Result<Self::Output> {
232        Ok(Sequential(self.0.to_device(arg)?, self.1.to_device(arg)?))
233    }
234}
235
236// Explicit bounds + direct calls for every leaf layer with no
237// training-dependent behavior
238// (`Linear`, `ReLU`, `Conv2d`, pooling layers, ...) implements `TrainMode`
239// via its default no-op body specifically so this bound is satisfiable for
240// real `seq!`-built chains, not just chains containing `Dropout`.
241impl<L1: TrainMode, L2: TrainMode> TrainMode for Sequential<L1, L2> {
242    /// Recursively sets training mode on both inner modules.
243    fn set_training(&mut self, training: bool) {
244        self.0.set_training(training);
245        self.1.set_training(training);
246    }
247}
248
249impl<B, L1, L2> crate::nn::VisitState<B> for Sequential<L1, L2>
250where
251    B: crate::tensor::backend::VariableBackend,
252    L1: crate::nn::VisitState<B>,
253    L2: crate::nn::VisitState<B>,
254{
255    fn flat_width() -> usize {
256        L1::flat_width() + L2::flat_width()
257    }
258
259    fn visit_state<V: crate::nn::StateVisitor<B>>(
260        &self,
261        path: &crate::nn::StatePath,
262        visitor: &mut V,
263    ) -> Result<()> {
264        self.visit_state_flat(path, 0, visitor)
265    }
266
267    fn visit_state_flat<V: crate::nn::StateVisitor<B>>(
268        &self,
269        parent: &crate::nn::StatePath,
270        base_index: usize,
271        visitor: &mut V,
272    ) -> Result<()> {
273        self.0.visit_state_flat(parent, base_index, visitor)?;
274        self.1
275            .visit_state_flat(parent, base_index + L1::flat_width(), visitor)
276    }
277}
278
279impl<B, L1, L2> crate::nn::VisitStateMut<B> for Sequential<L1, L2>
280where
281    B: crate::tensor::backend::VariableBackend,
282    L1: crate::nn::VisitStateMut<B>,
283    L2: crate::nn::VisitStateMut<B>,
284{
285    fn flat_width() -> usize {
286        L1::flat_width() + L2::flat_width()
287    }
288
289    fn visit_state_mut<V: crate::nn::StateMutVisitor<B>>(
290        &mut self,
291        path: &crate::nn::StatePath,
292        visitor: &mut V,
293    ) -> Result<()> {
294        self.visit_state_mut_flat(path, 0, visitor)
295    }
296
297    fn visit_state_mut_flat<V: crate::nn::StateMutVisitor<B>>(
298        &mut self,
299        parent: &crate::nn::StatePath,
300        base_index: usize,
301        visitor: &mut V,
302    ) -> Result<()> {
303        self.0.visit_state_mut_flat(parent, base_index, visitor)?;
304        self.1
305            .visit_state_mut_flat(parent, base_index + L1::flat_width(), visitor)
306    }
307}
308
309impl<B, L1, L2> crate::nn::VisitParameters<B> for Sequential<L1, L2>
310where
311    B: crate::tensor::backend::VariableBackend,
312    L1: crate::nn::VisitParameters<B>,
313    L2: crate::nn::VisitParameters<B>,
314{
315    fn flat_width() -> usize {
316        L1::flat_width() + L2::flat_width()
317    }
318
319    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
320        &self,
321        path: &crate::nn::StatePath,
322        visitor: &mut V,
323    ) -> Result<()> {
324        self.visit_parameters_flat(path, 0, visitor)
325    }
326
327    fn visit_parameters_flat<V: crate::nn::ParameterVisitor<B>>(
328        &self,
329        parent: &crate::nn::StatePath,
330        base_index: usize,
331        visitor: &mut V,
332    ) -> Result<()> {
333        self.0.visit_parameters_flat(parent, base_index, visitor)?;
334        self.1
335            .visit_parameters_flat(parent, base_index + L1::flat_width(), visitor)
336    }
337}
338
339impl<T, B: crate::tensor::backend::VariableBackend> crate::nn::VisitStateMut<B>
340    for core::marker::PhantomData<T>
341where
342    T: crate::tensor::dtype::DType,
343{
344    fn visit_state_mut<V: crate::nn::StateMutVisitor<B>>(
345        &mut self,
346        _path: &crate::nn::StatePath,
347        _visitor: &mut V,
348    ) -> Result<()> {
349        Ok(())
350    }
351}
352
353impl<L, B> crate::nn::VisitState<B> for Option<L>
354where
355    L: crate::nn::VisitState<B>,
356    B: crate::tensor::backend::VariableBackend,
357{
358    fn visit_state<V: crate::nn::StateVisitor<B>>(
359        &self,
360        path: &crate::nn::StatePath,
361        visitor: &mut V,
362    ) -> Result<()> {
363        if let Some(value) = self {
364            value.visit_state(path, visitor)?;
365        }
366        Ok(())
367    }
368}
369
370impl<L, B> crate::nn::VisitStateMut<B> for Option<L>
371where
372    L: crate::nn::VisitStateMut<B>,
373    B: crate::tensor::backend::VariableBackend,
374{
375    fn visit_state_mut<V: crate::nn::StateMutVisitor<B>>(
376        &mut self,
377        path: &crate::nn::StatePath,
378        visitor: &mut V,
379    ) -> Result<()> {
380        if let Some(value) = self {
381            value.visit_state_mut(path, visitor)?;
382        }
383        Ok(())
384    }
385}
386
387impl<L, B> crate::nn::VisitParameters<B> for Option<L>
388where
389    L: crate::nn::VisitParameters<B>,
390    B: crate::tensor::backend::VariableBackend,
391{
392    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
393        &self,
394        path: &crate::nn::StatePath,
395        visitor: &mut V,
396    ) -> Result<()> {
397        if let Some(value) = self {
398            value.visit_parameters(path, visitor)?;
399        }
400        Ok(())
401    }
402}
403
404/// Represents a node in the neural network layer structure metadata tree.
405#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
406pub struct LayerNode {
407    /// The display name of this layer node.
408    pub name: String,
409    /// The type name of this layer node.
410    pub type_name: String,
411    /// Human-readable string describing the layer's shape for display purposes.
412    pub shape_info: String,
413    /// Nested child layer nodes for hierarchical visualization.
414    pub children: Vec<LayerNode>,
415}
416
417/// A trait implemented by all Neural Network modules to report their structural architecture.
418pub trait NamedLayers {
419    /// Returns the layer hierarchy rooted at this module for visualization.
420    fn layer_structure(&self, prefix: &str) -> Vec<LayerNode>;
421
422    /// Returns a formatted human-readable architecture summary table.
423    fn summary(&self) -> String {
424        format_layer_summary(&self.layer_structure(""))
425    }
426
427    /// [`Self::summary`] plus a params/MACs/FLOPs totals footer at the given
428    /// batch size - see [`format_layer_summary_with_stats`]. Only callable
429    /// when `Self` also implements
430    /// [`crate::nn::stats::ComputeStats`] (every `#[module]`
431    /// struct does, automatically).
432    fn summary_with_stats(&self, batch: u64) -> String
433    where
434        Self: crate::nn::stats::ComputeStats,
435    {
436        format_layer_summary_with_stats(&self.layer_structure(""), self.stats(batch))
437    }
438}
439
440/// Formats a slice of `LayerNode` items into a human-readable printable tree table.
441pub fn format_layer_summary(nodes: &[LayerNode]) -> String {
442    let mut out = String::new();
443    out.push_str(
444        "================================================================================\n",
445    );
446    out.push_str(&format!(
447        "{:<32} {:<28} {:<16}\n",
448        "Layer (Name: Type)", "Shape / Spec", "Details"
449    ));
450    out.push_str(
451        "================================================================================\n",
452    );
453
454    fn print_node(node: &LayerNode, indent: usize, out: &mut String) {
455        let indent_str = "  ".repeat(indent);
456        let clean_type = clean_type_name(&node.type_name);
457        let name_type = if node.name.is_empty() {
458            format!("{}{}", indent_str, clean_type)
459        } else {
460            format!("{}{}: {}", indent_str, node.name, clean_type)
461        };
462        let shape = if node.shape_info.is_empty() {
463            "-"
464        } else {
465            &node.shape_info
466        };
467        out.push_str(&format!("{:<32} {:<28} {:<16}\n", name_type, shape, "-"));
468        for child in &node.children {
469            print_node(child, indent + 1, out);
470        }
471    }
472
473    for node in nodes {
474        print_node(node, 0, &mut out);
475    }
476    out.push_str(
477        "================================================================================\n",
478    );
479    out
480}
481
482/// [`format_layer_summary`] plus a totals footer (params / MACs / FLOPs at
483/// the given `stats`' batch size) - the "optional stats column" from
484/// `docs/growth/04-compile-time-stats.md`'s v1 acceptance criteria. This is
485/// a **totals row**, not a per-layer breakdown: threading per-node stats
486/// through `LayerNode` itself would mean adding a batch parameter to
487/// `NamedLayers::layer_structure`, a breaking signature change to a widely
488/// implemented trait that a "ships this week" v1 shouldn't take on. Extends
489/// [`format_layer_summary`] by composition rather than duplicating its tree
490/// printer.
491pub fn format_layer_summary_with_stats(
492    nodes: &[LayerNode],
493    stats: crate::nn::stats::ModelStats,
494) -> String {
495    let mut out = format_layer_summary(nodes);
496    out.push_str(&format!(
497        "Total params: {}    MACs: {}    FLOPs: {}\n",
498        stats.params, stats.macs, stats.flops
499    ));
500    out.push_str(
501        "================================================================================\n",
502    );
503    out
504}
505
506/// Cleans a fully qualified Rust type path into a simple name (e.g. `incin::nn::linear::Linear<...>` -> `Linear`).
507pub fn clean_type_name(raw: &str) -> String {
508    let without_generics = match raw.find('<') {
509        Some(idx) => &raw[..idx],
510        None => raw,
511    };
512    let name = match without_generics.rfind("::") {
513        Some(idx) => &without_generics[idx + 2..],
514        None => without_generics,
515    };
516    name.trim().to_string()
517}
518
519/// Recursively updates child node prefixes when a parent node is renamed.
520pub fn update_node_name_prefix(node: &mut LayerNode, new_name: &str) {
521    let old_name = node.name.clone();
522    node.name = new_name.to_string();
523    for child in &mut node.children {
524        let child_suffix = if old_name.is_empty() {
525            &child.name
526        } else if child.name.starts_with(&old_name) {
527            let prefix_len = old_name.len();
528            if child.name.len() > prefix_len && child.name.as_bytes()[prefix_len] == b'.' {
529                &child.name[prefix_len + 1..]
530            } else {
531                &child.name[prefix_len..]
532            }
533        } else {
534            &child.name
535        };
536        let new_child_name = if new_name.is_empty() {
537            child_suffix.to_string()
538        } else {
539            format!("{}.{}", new_name, child_suffix)
540        };
541        update_node_name_prefix(child, &new_child_name);
542    }
543}
544
545/// Flatten helper to assign sequential names (e.g. Linear1, ReLU1, Linear2) to a slice of child nodes.
546pub fn assign_sequential_names(nodes: &mut [LayerNode], prefix: &str) {
547    let mut type_counts = alloc::collections::BTreeMap::new();
548    for (i, node) in nodes.iter_mut().enumerate() {
549        let clean_type = clean_type_name(&node.type_name);
550        let count = type_counts.entry(clean_type.clone()).or_insert(0);
551        *count += 1;
552
553        let seq_name = if !clean_type.is_empty() && clean_type != "LayerNode" {
554            format!("{}{}", clean_type, count)
555        } else {
556            format!("{}", i)
557        };
558
559        let new_name = if prefix.is_empty() {
560            seq_name
561        } else {
562            format!("{}.{}", prefix, seq_name)
563        };
564
565        update_node_name_prefix(node, &new_name);
566    }
567}
568
569impl<L1: NamedLayers, L2: NamedLayers> NamedLayers for Sequential<L1, L2> {
570    /// Returns the layer hierarchy rooted at this module for visualization.
571    fn layer_structure(&self, prefix: &str) -> Vec<LayerNode> {
572        let mut nodes = Vec::new();
573        nodes.extend(self.0.layer_structure(""));
574        nodes.extend(self.1.layer_structure(""));
575
576        assign_sequential_names(&mut nodes, prefix);
577        nodes
578    }
579}
580
581impl<T: NamedLayers> NamedLayers for Option<T> {
582    /// Returns the layer hierarchy rooted at this module for visualization.
583    fn layer_structure(&self, prefix: &str) -> Vec<LayerNode> {
584        if let Some(layer) = self {
585            layer.layer_structure(prefix)
586        } else {
587            Vec::new()
588        }
589    }
590}
591
592impl<T: TrainMode> TrainMode for Option<T> {
593    fn set_training(&mut self, training: bool) {
594        if let Some(value) = self {
595            value.set_training(training);
596        }
597    }
598}
599
600impl<
601    S: Shape + DynShape,
602    B: crate::tensor::backend::VariableBackend,
603    K: DType,
604    Train: crate::nn::param::TrainState,
605> NamedLayers for crate::nn::param::Param<S, B, K, Train>
606{
607    fn layer_structure(&self, _prefix: &str) -> Vec<LayerNode> {
608        Vec::new()
609    }
610}
611
612impl<S: Shape + DynShape, B: crate::tensor::backend::VariableBackend, K: DType> NamedLayers
613    for crate::nn::param::Buffer<S, B, K>
614{
615    fn layer_structure(&self, _prefix: &str) -> Vec<LayerNode> {
616        Vec::new()
617    }
618}
619
620impl<
621    S: Shape + DynShape,
622    B: crate::tensor::backend::VariableBackend,
623    K: DType,
624    Train: crate::nn::param::TrainState,
625> TrainMode for crate::nn::param::Param<S, B, K, Train>
626{
627}
628
629impl<S: Shape + DynShape, B: crate::tensor::backend::VariableBackend, K: DType> TrainMode
630    for crate::nn::param::Buffer<S, B, K>
631{
632}
633
634/// A macro to easily build Sequential models with many layers.
635/// `seq!(L1, L2, L3)` expands to `Sequential(L1, Sequential(L2, L3))`.
636///
637/// Naming the *type* of that value (e.g. for a `#[module]` struct field)
638/// still requires hand-nesting `Sequential<L1, Sequential<L2, L3>>` - see
639/// `SeqTy!`, which generates that same nesting from the same
640/// flat layer list so it never has to be written out by hand.
641#[macro_export]
642macro_rules! seq {
643    ($l1:expr) => {
644        $l1
645    };
646    ($l1:expr, $($tail:expr),+ $(,)?) => {
647        $crate::nn::Sequential($l1, $crate::seq!($($tail),+))
648    };
649}
650
651/// Names the nested [`Sequential`] *type* that [`seq!`] would build a
652/// *value* of, from the same flat list of layer types.
653///
654/// `Sequential<L1, L2>` only composes two layers at a time, so a
655/// three-or-more-layer model's field type has to be hand-nested
656/// (`Sequential<A, Sequential<B, C>>`) even though `seq!(a, b, c)` already
657/// builds the matching value without that nesting spelled out. This macro
658/// mirrors `seq!`'s exact right-nesting rule at the type level:
659/// `SeqTy!(L1, L2, L3)` expands to `Sequential<L1, Sequential<L2, L3>>`,
660/// so a layer list only needs to be written once per meaning (the type via
661/// this macro, the value via `seq!`) instead of the type being re-derived by
662/// hand every time the layer list changes.
663///
664/// ## Examples
665/// ```rust
666/// # extern crate incin_core as incin;
667/// # fn main() -> incin::prelude::Result<()> {
668/// # type DefaultBackend = incin_backends::cpu::CpuBackendImpl;
669/// use incin::prelude::*;
670///
671/// type Net = SeqTy!(
672///     Linear<s![768, 256], DefaultBackend>,
673///     ReLU,
674///     Linear<s![256, 10], DefaultBackend>
675/// );
676///
677/// let net: Net = seq!(
678///     Linear::<s![768, 256], DefaultBackend>::build(())?,
679///     ReLU,
680///     Linear::<s![256, 10], DefaultBackend>::build(())?
681/// );
682/// # Ok(()) }
683/// ```
684#[macro_export]
685macro_rules! SeqTy {
686    ($l1:ty) => {
687        $l1
688    };
689    ($l1:ty, $($tail:ty),+ $(,)?) => {
690        $crate::nn::Sequential<$l1, $crate::SeqTy!($($tail),+)>
691    };
692}
693
694#[macro_export]
695#[doc(hidden)]
696macro_rules! seq_ty {
697    ($($tokens:tt)*) => {
698        $crate::SeqTy!($($tokens)*)
699    };
700}
701
702#[macro_export]
703#[doc(hidden)]
704macro_rules! seq_type {
705    ($($tokens:tt)*) => {
706        $crate::SeqTy!($($tokens)*)
707    };
708}