{"record":{"id":"af929747cef72e55","repo":"tracel-ai/burn","slug":"only-float-tensors-may-use-an-autodiff-primitive","errorCode":null,"errorMessage":"only float tensors may use an autodiff primitive","messagePattern":"only float tensors may use an autodiff primitive","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-backend-extension/src/dispatch.rs","lineNumber":215,"sourceCode":"            panic!(\"Autodiff should not wrap an autodiff tensor.\")\n        }),\n        fallback: quote!(panic!(\"unsupported dispatch backend\")),\n    });\n    let arms = routing::backend_kind_arms(\n        &paths,\n        &backends,\n        selected.as_ref(),\n        autodiff_match,\n        |_, backend, autodiff| {\n            backend_route_body(backend, operation, &float_inputs, autodiff, source)\n        },\n    );\n    let direct_arms = arms.concrete;\n    let autodiff_route = if source_is_non_float {\n        quote! {\n            #[cfg(feature = \"autodiff\")]\n            crate::DispatchTensorKind::Autodiff(_) => {\n                panic!(\"only float tensors may use an autodiff primitive\")\n            }\n        }\n    } else {\n        arms.autodiff.expect(\"autodiff backend arms should exist\")\n    };\n\n    quote! {\n        #selection\n        match #source_kind {\n            #(#direct_arms)*\n            #autodiff_route\n            #[allow(unreachable_patterns)]\n            _ => panic!(\"unsupported dispatch backend\"),\n        }\n    }\n}\n\nfn float_input_presence(","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-backend-extension/src/dispatch.rs#L197-L233","documentation":"The dispatch macro routes tensor kinds to backend arms, but autodiff primitives are only defined for float tensors. When the source tensor kind is non-float (int/bool), and the operation would use an autodiff primitive, the generated Autodiff arm panics because autodiff only wraps float backends.","triggerScenarios":"Invoking a dispatch-generated operation marked for autodiff with a DispatchTensor holding an int or bool kind; declaring a primitive as autodiff-capable but calling it from non-float tensor paths (expand_from_input / expand_from_candidates).","commonSituations":"Autodiff-wrapped operation tables mistakenly including int kernels; passing IntTensor into a generic dispatch function expecting FloatTensor; macro attribute errors when annotating primitives.","solutions":["Pass a Float tensor (DispatchTensorKind::Float) to autodiff-routed operations","Remove the autodiff routing from non-float primitives so they dispatch directly to the concrete backend","Split mixed-dtype operations into separate float and int dispatch calls","Check macro annotations so only float primitives get the autodiff flag"],"exampleFix":"// before\nlet idx: IntTensor<_> = ...;\ndispatch_op!(autodiff)(&idx); // panic\n// after\nlet x: FloatTensor<_> = ...;\ndispatch_op!(autodiff)(&x);","handlingStrategy":"type-guard","validationCode":"// Dispatch autodiff primitives only with float tensors\nfn dispatch_float_only<B: Backend>(x: &FloatTensor<B>) { /* dispatch */ }","typeGuard":"fn ensure_float<B: Backend, const D: usize>(t: &Tensor<B, D>) -> &Tensor<B, D>\nwhere B: FloatDTypeSupport { t } // rely on the type system: FloatTensor vs IntTensor types differ","tryCatchPattern":null,"preventionTips":["Keep int/bool tensors on concrete backend primitives, never autodiff routes","Split mixed-dtype pipelines into separate typed calls","Verify macro annotations: only float primitives get autodiff routing"],"tags":["rust","autodiff","dispatch-macro","dtype-mismatch"],"backgroundTag":"unsupported-dispatch-backend","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"}