Skip to main content

Module

Derive Macro Module 

Source
#[derive(Module)]
{
    // Attributes available to this derive:
    #[module]
}
Expand description

Derive macro for the Module trait.

§Sub-modules

Fields are treated as sub-modules unless their type is recognized as a constant or they are marked #[module(skip)]. Participating fields must implement Module and ModuleDisplay.

Constants are detected syntactically, including usize, f32, bool, String and combinations such as Option<usize> and Vec<f32>. Unlike 0.21, unknown concrete types are not automatically skipped. Use #[module(skip)] for custom config/state fields that report missing Module / ModuleDisplay implementations. Do not skip trainable children.

§Generics

Bounds are inferred on each field’s type: Wrapper<T> must implement Module and ModuleDisplay, but T need not implement them. Existing bounds, including custom forward traits, are preserved. Use #[module(skip)] for generic config/state fields; the same type parameter can also appear in a participating field.

Named and tuple structs support recursive children such as Vec<Self> and Option<Box<Self>>. Enums support variants containing a single unnamed field.

§Field Attributes

§#[module(skip)]

Explicitly marks a field to be ignored by the module derive.

Skipped fields are excluded from parameter traversal, checkpointing, device movement and training/validation transitions. They are still cloned and included in debug display. This is equivalent to the deprecated Ignored<T> wrapper.

§Requirements

The field must implement: Debug + Clone + Send. A Param field cannot be skipped.

§Example

ⓘ
#[derive(Module, Debug)]
pub struct MyModule<M, N: NonModuleTrait> {
    /// A normal parameter.
    weights: Param<Tensor<2>>,
    /// A field configured at runtime.
    dropout_prob: f64,
    /// A field that is recomputed at runtime.
    #[module(skip)]
    cached_mask: Option<Tensor<2>>,
    /// A field that contains some debug state.
    debug_state: String,
    /// Treated as a module (default for generics).
    inner: M,
    /// Hint required: this generic is NOT a module.
    #[module(skip)]
    other: N,
}