{"record":{"id":"df50eea58d863c86","repo":"tracel-ai/burn","slug":"autodiff-float-tensor-is-on-the-wrong-backend-exp","errorCode":null,"errorMessage":"Autodiff float tensor is on the wrong backend (expected {backend}).","messagePattern":"Autodiff float tensor is on the wrong backend \\(expected (.+?)\\)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/macros.rs","lineNumber":410,"sourceCode":"        $vec.into_iter()\n            .map(|t| match t.kind {\n                $crate::DispatchTensorKind::$Backend(inner) => inner.$kind(),\n                #[allow(unreachable_patterns)]\n                _ => panic!(\n                    \"Tensor is on the wrong backend (expected {}).\",\n                    stringify!($Backend)\n                ),\n            })\n            .collect::<Vec<_>>()\n    };\n\n    // Autodiff-wrapped backend\n    (@autodiff $Backend:ident, $vec:expr, $kind:ident) => {\n        $vec.into_iter()\n            .map(|t| match t.kind {\n                $crate::DispatchTensorKind::Autodiff(inner) => match *inner {\n                    $crate::DispatchTensorKind::$Backend(inner) => inner.$kind(),\n                    _ => panic!(\n                        \"Autodiff float tensor is on the wrong backend (expected {}).\",\n                        stringify!($Backend)\n                    ),\n                },\n                _ => panic!(\n                    \"Expected autodiff-wrapped float tensor for backend {}.\",\n                    stringify!($Backend)\n                ),\n            })\n            .collect::<Vec<_>>()\n    };\n}\n\n/// Match arm generator for `transaction_op`.\nmacro_rules! transaction_op_arms {\n    ($tx:ident, $first:expr; $([$Backend:ident, $cfg:meta]),*) => {{\n        match &$first.kind {\n            // Autodiff arm first","sourceCodeStart":392,"sourceCodeEnd":428,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/macros.rs#L392-L428","documentation":"The `@autodiff` arm of the unwrap macro first unwraps an Autodiff-wrapped tensor kind, then expects the inner tensor to match the concrete backend. If the inner primitive is from a different backend, it panics with 'Autodiff float tensor is on the wrong backend'.","triggerScenarios":"Passing an Autodiff tensor whose inner backend tensor is not of the expected backend kind into an unwrap that requires the concrete backend (e.g. an Autodiff<Wgpu> tensor supplied to a Cpu-targeted operation).","commonSituations":"Wrapping one backend in Autodiff but passing tensors built from another backend into the training graph; constructing an Autodiff tensor manually from the wrong primitives.","solutions":["Build all autodiff tensors from the same inner backend that Autodiff<B> wraps","Recreate the tensor on the wrapped backend before passing it in","Verify the generic B parameter of your Autodiff<B> backend matches the tensors you pass"],"exampleFix":"// before\nlet ad = AutodiffTensor::from(wgpu_tensor); // used inside Autodiff<Cpu> graph\n// after\nlet ad = AutodiffTensor::from(cpu_tensor); // inner tensor matches Autodiff<Cpu>","handlingStrategy":"validation","validationCode":"fn check_ad_backend<B: AutodiffBackend>(t: &burn::tensor::Tensor<B, D>) -> bool { B::InnerBackend::device(&t.device()).is_ok() } // ensure the tensor belongs to Autodiff<B>'s inner backend","typeGuard":"fn is_ad_of_b<B: Backend>(t: &AdBackend<B>::Tensor<D>) -> bool { true } // rely on Rust generics: AdBackend<B> tensors are typed, so wrong-backend tensors won't compile","tryCatchPattern":null,"preventionTips":["Use type aliases: type AdB = Autodiff<Wgpu>; and type AdTensor<D> = burn::tensor::Tensor<AdB, D>;","Only construct autodiff tensors from B::InnerBackend tensors","Don't convert autodiff tensors to primitives and back across backends"],"tags":["rust","autodiff","backend-mismatch","panic","burn"],"backgroundTag":"tensor-backend-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"}