{"record":{"id":"6d5d806baafb87b0","repo":"tracel-ai/burn","slug":"the-dynamic-optimizer-state-should-match-the-optim","errorCode":null,"errorMessage":"The dynamic optimizer state should match the optimizer state type.","messagePattern":"The dynamic optimizer state should match the optimizer state type\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-optim/src/optim/module/base.rs","lineNumber":69,"sourceCode":"}\n\nimpl DynState {\n    /// Erase a concrete optimizer state of rank `rank`.\n    pub fn create<T: Send + Sync + 'static>(state: T, rank: usize) -> Self {\n        Self {\n            state: Arc::new(state),\n            rank,\n        }\n    }\n\n    /// Recover the concrete state by value, moving it out when this is the only handle and\n    /// cloning only when the underlying state is still shared. Panics if `T` does not match the\n    /// stored type.\n    pub fn downcast<T: Clone + Send + Sync + 'static>(self) -> T {\n        let state = self\n            .state\n            .downcast::<T>()\n            .expect(\"The dynamic optimizer state should match the optimizer state type.\");\n        Arc::try_unwrap(state).unwrap_or_else(|state| (*state).clone())\n    }\n\n    /// Borrow the concrete state without cloning. Panics if `T` does not match the stored type.\n    pub fn downcast_ref<T: 'static>(&self) -> &T {\n        self.state\n            .downcast_ref::<T>()\n            .expect(\"The dynamic optimizer state should match the optimizer state type.\")\n    }\n\n    /// The rank of the parameter this state belongs to.\n    pub fn rank(&self) -> usize {\n        self.rank\n    }\n}\n\n/// Dispatch a runtime `rank` to a body parameterized by a `const D: usize`.\nmacro_rules! dispatch_rank {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-optim/src/optim/module/base.rs#L51-L87","documentation":"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.","triggerScenarios":"Thrown at crates/burn-optim/src/optim/module/base.rs:69 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the same optimizer type is used when saving and loading state","Verify the checkpoint record matches the current optimizer (e.g., Adam vs SGD)","Check that the `rank`/state creation path uses the same `T` as the downcast site"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}