{"record":{"id":"a9704fbf4e14359a","repo":"tracel-ai/burn","slug":"should-be-int-got-autodiff","errorCode":null,"errorMessage":"Should be int, got autodiff","messagePattern":"Should be int, got autodiff","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":68,"sourceCode":"        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,\n            BackendTensor::Float(_) => panic!(\"Should be int, got float\"),\n            BackendTensor::Bool(_) => panic!(\"Should be int, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be int, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be int, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner bool tensor primitive.\n    pub fn bool(self) -> B::BoolTensorPrimitive {\n        match self {\n            BackendTensor::Bool(tensor) => tensor,\n            BackendTensor::Float(_) => panic!(\"Should be bool, got float\"),\n            BackendTensor::Int(_) => panic!(\"Should be bool, got int\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be bool, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be bool, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner quantized tensor primitive.\n    pub fn quantized(self) -> B::QuantizedTensorPrimitive {\n        match self {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L50-L86","documentation":"BackendTensor::int() was called on an Autodiff-wrapped tensor handle. The Autodiff variant stores FloatTensor<Autodiff<B>> and cannot yield an int primitive, so the code panics. A gradient-tracking tensor reached an int-only code path — doubly wrong, since autodiff tensors are float by definition.","triggerScenarios":"With feature = \"autodiff\", calling int() on a training-graph tensor; passing an Autodiff backend tensor into code expecting raw int primitives without unwrapping; casting a float training tensor to int and keeping it wrapped.","commonSituations":"Mixing training (Autodiff) tensors into indexing/scatter logic that consumes int primitives; custom ops written against B receiving Autodiff<B> tensors; rounding indices from a grad-tracked computation and passing the wrapped result.","solutions":["Unwrap or detach first: call as_autodiff()/autodiff() and use inner()/valid() on the Autodiff backend to get the raw primitive, casting to int as needed","Keep autodiff tensors on the training path and int index tensors on the raw-backend path","If the site should support gradients, use float primitives and let the cast happen through autodiff-aware ops","Make code generic over B handle the Autodiff variant explicitly when the feature is enabled"],"exampleFix":"// before\nlet idx = train_handle.int(); // panics: Autodiff variant\n// after\n#[cfg(feature = \"autodiff\")]\nlet idx = train_handle.as_autodiff().inner(); // raw backend primitive; cast to int via backend ops","handlingStrategy":"type-guard","validationCode":"#[cfg(feature = \"autodiff\")]\nif matches!(handle, BackendTensor::Autodiff(_)) {\n    let raw = handle.as_autodiff().inner(); // unwrap before int/float paths\n}","typeGuard":"#[cfg(feature = \"autodiff\")]\nfn is_autodiff<B: BackendTypes>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Autodiff(_))\n}","tryCatchPattern":null,"preventionTips":["Unwrap autodiff tensors (inner()/valid()) before passing into int-consuming code","Keep grad-tracked tensors out of indexing/scatter paths; cast indices on the raw backend","Handle the Autodiff variant explicitly in generic code when the feature is on"],"tags":["rust","panic","dtype-mismatch","autodiff"],"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"}