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

Generic type parameters (e.g., field: M) are assumed to be sub-modules by default. If a generic field represents some other runtime state or configuration, you can use the #[module(skip)] attribute to provide a hint.

§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 or a field using a generic explicitly bounded by Module 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,
}