tracel-ai/burn · error

The dynamic optimizer state should match the optimizer state

Error message

The dynamic optimizer state should match the optimizer state type.

What it means

Type-cast guard in `DynState::downcast`: the dynamically-erased optimizer state is being downcast to a concrete type `T` that does not match the type it was stored as, indicating optimizer state was loaded/created with a different optimizer or record layout than expected.

Source

Thrown at crates/burn-optim/src/optim/module/base.rs:69

}

impl DynState {
    /// Erase a concrete optimizer state of rank `rank`.
    pub fn create<T: Send + Sync + 'static>(state: T, rank: usize) -> Self {
        Self {
            state: Arc::new(state),
            rank,
        }
    }

    /// Recover the concrete state by value, moving it out when this is the only handle and
    /// cloning only when the underlying state is still shared. Panics if `T` does not match the
    /// stored type.
    pub fn downcast<T: Clone + Send + Sync + 'static>(self) -> T {
        let state = self
            .state
            .downcast::<T>()
            .expect("The dynamic optimizer state should match the optimizer state type.");
        Arc::try_unwrap(state).unwrap_or_else(|state| (*state).clone())
    }

    /// Borrow the concrete state without cloning. Panics if `T` does not match the stored type.
    pub fn downcast_ref<T: 'static>(&self) -> &T {
        self.state
            .downcast_ref::<T>()
            .expect("The dynamic optimizer state should match the optimizer state type.")
    }

    /// The rank of the parameter this state belongs to.
    pub fn rank(&self) -> usize {
        self.rank
    }
}

/// Dispatch a runtime `rank` to a body parameterized by a `const D: usize`.
macro_rules! dispatch_rank {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Ensure the same optimizer type is used when saving and loading state
  2. Verify the checkpoint record matches the current optimizer (e.g., Adam vs SGD)
  3. Check that the `rank`/state creation path uses the same `T` as the downcast site
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/burn-optim/src/optim/module/base.rs:69 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/6d5d806baafb87b0. Report an issue: GitHub.