{"record":{"id":"3c73591bd67b7841","repo":"tracel-ai/burn","slug":"an-autodiff-float-primitive-must-have-an-enabled-a-3c7359","errorCode":null,"errorMessage":"an autodiff float primitive must have an enabled autodiff context","messagePattern":"an autodiff float primitive must have an enabled autodiff context","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":388,"sourceCode":"\n    fn shape(&self) -> Shape {\n        self.kind.shape()\n    }\n\n    fn can_mut(&self) -> bool {\n        self.kind.can_mut()\n    }\n\n    type Device = DispatchDevice;\n\n    fn device(&self) -> Self::Device {\n        #[allow(unused_mut)]\n        let mut device = self.kind.device();\n\n        #[cfg(feature = \"autodiff\")]\n        match (&self.kind, self.autodiff) {\n            (DispatchTensorKind::Autodiff(_), DispatchAutodiffContext::Disabled) => {\n                panic!(\"an autodiff float primitive must have an enabled autodiff context\")\n            }\n            (DispatchTensorKind::Autodiff(_), DispatchAutodiffContext::Enabled(strategy)) => {\n                let DispatchDevice::Autodiff(device) = &mut device else {\n                    unreachable!(\"autodiff primitive must report an autodiff device\")\n                };\n                device.checkpointing = strategy;\n            }\n            (_, DispatchAutodiffContext::Enabled(strategy)) => {\n                if self.dtype().is_float() {\n                    panic!(\"an enabled float tensor must use an autodiff primitive\")\n                }\n                device = DispatchDevice::autodiff(device);\n                let DispatchDevice::Autodiff(device) = &mut device else {\n                    unreachable!()\n                };\n                device.checkpointing = strategy;\n            }\n            (_, DispatchAutodiffContext::Disabled) => {}","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L370-L406","documentation":"During `device()` resolution, a tensor whose kind is `DispatchTensorKind::Autodiff` must carry `DispatchAutodiffContext::Enabled`. If the autodiff context is `Disabled` while the kind is autodiff, the internal invariant is broken and the code panics. This keeps the kind and the autodiff context consistent so device extraction can find the autodiff device.","triggerScenarios":"Constructing or transforming a `DispatchTensor` so that `kind` is `Autodiff(_)` while `autodiff` is `DispatchAutodiffContext::Disabled`, then calling `device()` (e.g. via `assert_enabled_float`).","commonSituations":"Manual construction of DispatchTensors, custom backends/wrappers that set the kind and autodiff context independently, or code paths that disable autodiff without retagging the tensor kind.","solutions":["Ensure the autodiff context is Enabled (with a strategy) whenever the tensor kind is Autodiff.","Fix the constructor/transform that produced the inconsistent kind/context pair.","If autodiff is intentionally disabled, rebuild the tensor with a non-autodiff kind."],"exampleFix":"// before\nDispatchTensor { kind: DispatchTensorKind::Autodiff(p), autodiff: DispatchAutodiffContext::Disabled }\n// after\nDispatchTensor { kind: DispatchTensorKind::Autodiff(p), autodiff: DispatchAutodiffContext::Enabled(strategy) }","handlingStrategy":"validation","validationCode":"assert!(matches!(tensor.autodiff, DispatchAutodiffContext::Enabled(_)), \"autodiff kind requires enabled context\");","typeGuard":"fn has_enabled_autodiff(t: &DispatchTensor) -> bool {\n    !matches!(t.kind, DispatchTensorKind::Autodiff(_))\n        || matches!(t.autodiff, DispatchAutodiffContext::Enabled(_))\n}","tryCatchPattern":"let result = std::panic::catch_unwind(AssertUnwindSafe(|| tensor.device()));\nmatch result {\n    Ok(d) => use_device(d),\n    Err(_) => eprintln!(\"autodiff tensor had disabled context\"),\n}","preventionTips":["Always construct autodiff-kind tensors through constructors that set the Enabled context.","Never manually set DispatchAutodiffContext::Disabled on autodiff-kind tensors.","Add invariant assertions when building DispatchTensors in custom glue code."],"tags":["rust","tensor","autodiff","invariant-violation","panic"],"backgroundTag":"autodiff-context-mismatch","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"}