{"record":{"id":"a86cb4e33b7cc17f","repo":"tracel-ai/burn","slug":"an-autodiff-float-primitive-must-have-an-enabled-a-a86cb4","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/ops/distributed.rs","lineNumber":126,"sourceCode":"            dispatch_distributed_devices_arms,\n            $device,\n            $devices,\n            |$inner_devices| $body\n        )\n    };\n}\n\nmacro_rules! dispatch_distributed_float_arms {\n    ($tensor:expr, |$inner:ident| $body:expr; $([$Backend:ident, $cfg:meta]),*) => {{\n        let autodiff = $tensor.autodiff;\n        match $tensor.kind {\n            #[cfg(feature = \"autodiff\")]\n            $crate::DispatchTensorKind::Autodiff(inner) => match *inner {\n                $(\n                    #[cfg($cfg)]\n                    $crate::DispatchTensorKind::$Backend($inner) => {\n                        let $crate::DispatchAutodiffContext::Enabled(checkpointing) = autodiff else {\n                            panic!(\"an autodiff float primitive must have an enabled autodiff context\")\n                        };\n                        with_autodiff_backend!($Backend, checkpointing, |B| {\n                            let $inner = $inner.autodiff();\n                            $crate::DispatchTensor {\n                                kind: $crate::DispatchTensorKind::Autodiff(alloc::boxed::Box::new(\n                                    $crate::DispatchTensorKind::$Backend(\n                                        $crate::BackendTensor::Autodiff($body),\n                                    ),\n                                )),\n                                autodiff,\n                            }\n                        })\n                    }\n                )*\n                #[allow(unreachable_patterns)]\n                other => panic!(\"Distributed operations are not supported for tensor kind {other:?}\"),\n            },\n            $(","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/ops/distributed.rs#L108-L144","documentation":"When dispatching a distributed float operation on an Autodiff tensor kind, the macro requires an enabled autodiff context (`DispatchAutodiffContext::Enabled`) carrying the checkpointing strategy. If the context is missing/disabled while the tensor is an autodiff float primitive, it panics.","triggerScenarios":"Calling a distributed operation on an `AutodiffTensor` while the dispatch autodiff context is `Disabled` — e.g. running a distributed op through the autodiff backend without the autodiff feature providing a context, or calling the raw dispatch API without passing an Enabled context.","commonSituations":"Manual dispatch-layer calls (custom ops/serde graph execution) that forget to thread the autodiff context; running autodiff tensors under a code path compiled without proper context setup.","solutions":["Ensure the autodiff context is Enabled (with a checkpointing/backward strategy) when dispatching ops on autodiff tensors","Call the operation through the normal Autodiff backend API instead of the raw dispatch function","Convert to the primitive tensor (`.primitive()`/detach) if gradients are not needed for that op"],"exampleFix":"// before\ndispatch_fn(tensor, DispatchAutodiffContext::Disabled);\n// after\ndispatch_fn(tensor, DispatchAutodiffContext::Enabled(strategy));","handlingStrategy":"validation","validationCode":"fn ensure_ad_enabled(ctx: &DispatchAutodiffContext) -> Result<(), &'static str> {\n    match ctx { DispatchAutodiffContext::Enabled(_) => Ok(()), DispatchAutodiffContext::Disabled => Err(\"need enabled autodiff context\") }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always thread the autodiff context from the backend into custom dispatch calls","Use the public Autodiff backend API instead of raw dispatch functions","Only pass Autodiff tensors when a backward/step context is active"],"tags":["rust","autodiff","distributed","panic","burn"],"backgroundTag":"missing-autodiff-context","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"}