{"record":{"id":"7f8a7341895619dc","repo":"tracel-ai/burn","slug":"backend-extension-input-is-on-the-wrong-backend","errorCode":null,"errorMessage":"backend extension input is on the wrong backend","messagePattern":"backend extension input is on the wrong backend","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-backend-extension/src/routing.rs","lineNumber":753,"sourceCode":"        }\n    }\n}\n\nfn extract_extension(\n    name: &syn::Ident,\n    ty: &syn::Type,\n    paths: &RoutingPaths,\n    backend: &syn::Ident,\n    autodiff: bool,\n) -> TokenStream {\n    let backend_root = &paths.backend_root;\n    let dispatch_root = &paths.dispatch_root;\n    let dispatch_kind = quote!(#dispatch_root::DispatchTensorKind);\n    let backend_tensor = quote!(#dispatch_root::BackendTensor);\n    let autodiff_trait = &paths.autodiff_trait;\n    let extension_trait = &paths.extension_trait;\n    let backend_alias = &paths.backend_alias;\n    let mismatch = quote!(panic!(\"backend extension input is on the wrong backend\"));\n    if autodiff {\n        let target = ir::with_backend(ty, quote!(#backend_alias));\n        let context = quote!(#dispatch_root::DispatchAutodiffContext);\n        quote! {\n            let #name = <#target as #extension_trait<#backend_alias>>::map_from_dispatch(#name, |__tensor| {\n                let __input_context = __tensor.autodiff;\n                match __tensor.kind {\n                    #dispatch_kind::Autodiff(inner) => {\n                        let #context::Enabled(_) = __input_context else {\n                            panic!(\"an autodiff float primitive must have an enabled autodiff context\")\n                        };\n                        let tensor = match *inner {\n                            #dispatch_kind::#backend(tensor) => tensor,\n                            #[allow(unreachable_patterns)]\n                            _ => #mismatch,\n                        };\n                        match tensor {\n                            #backend_tensor::Autodiff(tensor) => #backend_tensor::Float(tensor),","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-backend-extension/src/routing.rs#L735-L771","documentation":"Emitted by generated code from extract_extension when mapping a dispatch tensor into a backend-extension primitive via `map_from_dispatch`. Inside the mapping, the inner dispatch kind must be the specific backend's variant; the `_ =>` arm panics when the extension input tensor belongs to a different backend than the one the extension is instantiated for. Extensions are per-backend code, so there is no fallback conversion.","triggerScenarios":"Calling an extension op (e.g. a custom fused kernel or extended primitive routed through `DispatchAutodiffContext`) with a tensor whose inner `DispatchTensorKind` is not the extension backend's variant, in either the autodiff or non-autodiff path.","commonSituations":"Using a backend-specific extension (e.g. a CubeCL-only kernel) with tensors from the generic/default backend; wrapping a module for one backend but feeding it tensors from another after a refactor.","solutions":["Instantiate the extension with the same backend that produced the input tensor","Convert the tensor onto the extension's backend before calling the extension op","Check that the `#backend_alias` used in your route matches your tensor construction site"],"exampleFix":"// before\nlet t = <Wgpu>::from_data(data, &device);\nmy_ext_op::<CandleBackedExt>(t); // panics: extension input on wrong backend\n// after\nlet t = <Candle>::from_data(data, &device);\nmy_ext_op::<CandleBackedExt>(t);","handlingStrategy":"validation","validationCode":"if !matches!(&t.kind, DispatchTensorKind::ExtBackend(_) | DispatchTensorKind::Autodiff(_)) { panic!(\"extension requires tensors on its backend\"); }","typeGuard":"fn matches_extension_backend(t: &DispatchTensor) -> bool { matches!(&t.kind, DispatchTensorKind::ExtBackend(_)) }","tryCatchPattern":null,"preventionTips":["Instantiate extensions with the same backend generic as your tensors","Keep extension ops and tensor creation in the same crate/module boundary","Pin the backend with a type alias used everywhere"],"tags":["burn","extension","backend-mismatch"],"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"}