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

By default, the macro automatically detects sub-modules and parameters as module types.

Any field not recognized as a module type is assumed to be a non-module and is skipped by the module system (not persistent, not visited).

§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 not parameters, not modules, and are not persistent. This is equivalent to the deprecated Ignored<T> wrapper.

§Requirements

The field must implement: Debug + Clone + Send.

§Example

#[derive(Module, Debug)]
pub struct MyModule<B: Backend, M, N: NonModuleTrait> {
    /// A normal parameter.
    weights: Param<Tensor<B, 2>>,
    /// A field configured at runtime.
    dropout_prob: f64,
    /// A field that is recomputed at runtime.
    cached_mask: Option<Tensor<B, 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,
}