{"record":{"id":"8c378ee5b912fac8","repo":"tracel-ai/burn","slug":"should-be-bool-got-autodiff","errorCode":null,"errorMessage":"Should be bool, got autodiff","messagePattern":"Should be bool, got autodiff","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":80,"sourceCode":"        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 {\n            BackendTensor::Quantized(tensor) => tensor,\n            _ => unreachable!(),\n        }\n    }\n\n    #[cfg(feature = \"autodiff\")]\n    /// Returns the inner autodiff tensor primitive.\n    pub fn autodiff(self) -> FloatTensor<Autodiff<B>> {\n        match self {\n            BackendTensor::Autodiff(tensor) => tensor,\n            // NOTE: this is the panicking code reached in tensor.rs:74:18:\n            _ => unreachable!(),","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L62-L98","documentation":"`BackendTensor::bool()` extracts the inner `Bool` variant's primitive. When autodiff is enabled and the wrapper holds an `Autodiff(_)` variant instead, there is no bool primitive available, so the code panics with 'Should be bool, got autodiff'. The variant only exists when the `autodiff` feature is compiled in.","triggerScenarios":"Calling `BackendTensor::bool()` on a wrapper whose inner variant is `BackendTensor::Autodiff(_)` — typically when reading tensor results inside an autodiff-enabled session/graph where floats are wrapped in the autodiff variant.","commonSituations":"Running training or gradient code with the autodiff feature enabled and expecting plain bool primitives from ops; often after switching on the autodiff feature or migrating inference code to training code.","solutions":["Strip the autodiff wrapper (e.g. take the inner primitive via the autodiff API) and access the bool tensor through the appropriate method for that primitive.","Disable/avoid the autodiff path (run in a non-autodiff context) if only bool results are needed.","Match on all `BackendTensor` variants including Autodiff and handle the bool extraction correctly."],"exampleFix":"// before\nlet b = tensor.bool(); // panics: got autodiff\n// after\nlet b = match tensor {\n    BackendTensor::Bool(t) => t,\n    BackendTensor::Autodiff(t) => t.inner().bool(), // unwrap autodiff first\n    other => panic!(\"unexpected variant\"),\n};","handlingStrategy":"type-guard","validationCode":"if !matches!(tensor, BackendTensor::Bool(_)) { /* unwrap autodiff or convert before .bool() */ }","typeGuard":"fn is_bool_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Bool(_))\n}","tryCatchPattern":"let result = std::panic::catch_unwind(AssertUnwindSafe(|| tensor.clone().bool()));\nmatch result {\n    Ok(b) => use_bool(b),\n    Err(_) => eprintln!(\"tensor was autodiff-wrapped, not bool\"),\n}","preventionTips":["When autodiff feature is on, expect Autodiff variants and unwrap before dtype-specific access.","Separate inference (no autodiff) from training paths where bool results are consumed.","Match exhaustively on BackendTensor variants including the cfg-gated Autodiff one."],"tags":["rust","tensor","autodiff","dtype-mismatch","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"}