{"record":{"id":"02fac4159961edd2","repo":"tracel-ai/burn","slug":"tensor-is-on-the-wrong-backend-expected-backend","errorCode":null,"errorMessage":"Tensor is on the wrong backend (expected {backend}).","messagePattern":"Tensor is on the wrong backend \\(expected (.+?)\\)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/macros.rs","lineNumber":396,"sourceCode":"    ($kind:ident, $inner_fn:ident, $tensor:expr, $device:expr, $to_device:ident, |$inner:ident, $device_ident:ident| $body:expr) => {\n        backend_matrix!(\n            float_to_device_arms,\n            $tensor,\n            $device,\n            $to_device,\n            |$inner, $device_ident| $body\n        )\n    };\n}\n\n/// Unwraps a `Vec<DispatchTensor>` for a known backend.\nmacro_rules! unwrap_vec {\n    ($Backend:ident, $vec:expr, $kind:ident) => {\n        $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                },","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/macros.rs#L378-L414","documentation":"The burn-dispatch `unwrap_vec!` macro converts dispatch tensors into backend-specific tensors. When a tensor's DispatchTensorKind variant does not match the backend being unwrapped, the library panics with 'Tensor is on the wrong backend'. This guards against mixing tensors created by different backends in one operation.","triggerScenarios":"Calling a dispatch-level tensor API (e.g. via Autodiff backend, ad_enabled, or memory_persistent_allocations paths) passing a vector of tensors where at least one tensor was created on a different backend than the one the macro was instantiated for (e.g. a Cpu tensor given to a Wgpu-dispatched op).","commonSituations":"Moving tensors between devices/backends and forgetting to convert them; passing a tensor from a non-autodiff backend into an autodiff-wrapped graph; storing tensors across backend switches in cached state.","solutions":["Ensure every tensor passed to the operation was created by the same backend as the operation's target backend","Call `.to_device(...)` / recreate the tensor on the target backend before the call","Check that you are not mixing an Autodiff-wrapped tensor with a primitive tensor where the unwrap expects the primitive variant"],"exampleFix":"// before\nlet out = my_op(vec![cpu_tensor]); // running under Wgpu backend\n// after\nlet out = my_op(vec![cpu_tensor.to_device(&wgpu_device)]); // convert to the active backend","handlingStrategy":"validation","validationCode":"fn assert_same_backend<B: Backend>(t: &burn::tensor::Tensor<B, D>) -> bool { /* ensure tensor's backend type parameter equals the op's backend */ true } // in Rust this is enforced statically via the B type parameter — keep all tensors as Tensor<B, _> for one B","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep one backend type alias (type B = Wgpu;) and use it for every tensor","Use `.to_device()` when moving between devices","Never mix tensors from different Backend implementations in one call"],"tags":["rust","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"}