{"record":{"id":"c556ae627338e524","repo":"tracel-ai/burn","slug":"should-be-float-got-autodiff","errorCode":null,"errorMessage":"Should be float, got autodiff","messagePattern":"Should be float, got autodiff","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":45,"sourceCode":"    /// Bool tensor handle.\n    Bool(B::BoolTensorPrimitive),\n    /// Quantized tensor handle.\n    Quantized(B::QuantizedTensorPrimitive),\n    #[cfg(feature = \"autodiff\")]\n    /// Autodiff float tensor handle.\n    Autodiff(FloatTensor<Autodiff<B>>),\n}\n\nimpl<B: Backend> BackendTensor<B> {\n    /// Returns the inner float tensor primitive.\n    pub fn float(self) -> B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n    /// Returns the inner float tensor primitive.\n    pub fn as_float(&self) -> &B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner int tensor primitive.\n    pub fn int(self) -> B::IntTensorPrimitive {\n        match self {\n            BackendTensor::Int(tensor) => tensor,","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L27-L63","documentation":"BackendTensor::float() panics with this message when called on an Autodiff tensor (only compiled with the \"autodiff\" feature). The method returns a plain float primitive; an autodiff-wrapped tensor must be unwrapped via .inner() or accessed through autodiff-aware APIs, so passing it to .float() is a variant-handling bug.","triggerScenarios":"Calling BackendTensor::float() on a tensor holding BackendTensor::Autodiff(_) — an autodiff-wrapped primitive (feature \"autodiff\" enabled) reaching float-only code without being stripped first.","commonSituations":"Forgetting to call .inner() (which strips autodiff wrappers) before dtype accessors; mixing autodiff-enabled and non-autodiff backends where generic code assumes plain primitives.","solutions":["Strip the autodiff wrapper first with .inner() before calling .float().","Match on the BackendTensor variant and recurse into the inner tensor for the Autodiff case.","Use autodiff-aware accessors/ops when gradient tracking is required rather than extracting the raw float primitive.","Check backend selection: if autodiff shouldn't be active, build tensors with the base backend instead of the Autodiff wrapper."],"exampleFix":"// before\nlet f = tensor.float(); // panics: Autodiff\n// after\nlet f = match tensor {\n    BackendTensor::Autodiff(a) => BackendTensor::Float(a.inner()).float(),\n    t => t.float(),\n};","handlingStrategy":"type-guard","validationCode":"// before calling .float()\nif let BackendTensor::Autodiff(_) = &tensor {\n    // unwrap with .inner() first\n}","typeGuard":"fn is_autodiff_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Autodiff(_))\n}","tryCatchPattern":"// guard before extraction\nlet f = match tensor {\n    BackendTensor::Autodiff(a) => BackendTensor::Float(a.inner()).float(),\n    t => t.float(),\n};","preventionTips":["Call .inner() to strip autodiff wrappers before dtype accessors.","Keep a single unwrapping boundary between autodiff-wrapped and plain tensor code.","Enable the autodiff feature in tests so Autodiff-variant bugs surface in CI, not production."],"tags":["rust","dtype","autodiff","tensor","panic"],"backgroundTag":"tensor-dtype-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"}