{"record":{"id":"52ff16af65a19ca9","repo":"tracel-ai/burn","slug":"the-provided-tensors-are-not-on-the-same-backend","errorCode":null,"errorMessage":"The provided tensors are not on the same backend. Got backends {t:?} and {g:?}.","messagePattern":"The provided tensors are not on the same backend\\. Got backends (.+?) and (.+?)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/backend.rs","lineNumber":479,"sourceCode":"                    tensor.as_autodiff().grad_replace(grads, grad.float())\n                }\n                #[cfg(any(feature = \"flex\", default_backend))]\n                (DispatchTensorKind::Flex(tensor), DispatchTensorKind::Flex(grad)) => {\n                    tensor.as_autodiff().grad_replace(grads, grad.float())\n                }\n                #[cfg(feature = \"ndarray\")]\n                (DispatchTensorKind::NdArray(tensor), DispatchTensorKind::NdArray(grad)) => {\n                    tensor.as_autodiff().grad_replace(grads, grad.float())\n                }\n                #[cfg(feature = \"remote\")]\n                (DispatchTensorKind::Remote(tensor), DispatchTensorKind::Remote(grad)) => {\n                    tensor.as_autodiff().grad_replace(grads, grad.float())\n                }\n                (DispatchTensorKind::Autodiff(_), _) => {\n                    panic!(\"Autodiff should not wrap an autodiff tensor.\")\n                }\n                // TODO: distributed message?\n                (t, g) => panic!(\n                    \"The provided tensors are not on the same backend. Got backends {t:?} and {g:?}.\"\n                ),\n            },\n            _ => panic!(\"Requires autodiff tensor.\"),\n        }\n    }\n\n    fn inner(tensor: DispatchTensor) -> DispatchTensor {\n        let DispatchTensor { kind, autodiff } = tensor;\n        assert!(\n            matches!(autodiff, DispatchAutodiffContext::Enabled(_)),\n            \"Requires autodiff tensor.\"\n        );\n\n        let kind = match kind {\n            DispatchTensorKind::Autodiff(inner_kind) => match *inner_kind {\n                #[cfg(cube_backend)]\n                DispatchTensorKind::Cube(tensor) => DispatchTensorKind::Cube(","sourceCodeStart":461,"sourceCodeEnd":497,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/backend.rs#L461-L497","documentation":"Cross-backend consistency guard in the dispatch backend's `grad_replace`: when installing gradients, the tensor and the gradient tensor are matched by `DispatchTensorKind`; if both are dispatchable but their backends differ (e.g. an NdArray tensor with a CUDA gradient) the kind match falls through and this panic reports the two mismatched backend types. The failing input is the gradient tensor created on a different backend than its target.","triggerScenarios":"Thrown at crates/burn-dispatch/src/backend.rs:479 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Create the gradient on the same backend/device as the tensor it is attached to.","Call `.to_device()`/backend conversion on the gradient before `grad_replace`.","Audit code that mixes backends (e.g. moving tensors to CPU for grads) and keep grads on the autodiff backend of record."],"exampleFix":null,"handlingStrategy":"validation","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"}