{"record":{"id":"2d130b7f33a67494","repo":"tracel-ai/burn","slug":"int-scalar-op-unsupported-dtype","errorCode":null,"errorMessage":"int_scalar_op: unsupported dtype {:?}","messagePattern":"int_scalar_op: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/binary.rs","lineNumber":414,"sourceCode":"        DType::I16 => scalar_op_typed(tensor, scalar as i16, |a: i16, b: i16| {\n            op(a as i64, b as i64) as i16\n        }),\n        DType::I8 => scalar_op_typed(tensor, scalar as i8, |a: i8, b: i8| {\n            op(a as i64, b as i64) as i8\n        }),\n        DType::U64 => scalar_op_typed(tensor, scalar as u64, |a: u64, b: u64| {\n            op(a as i64, b as i64) as u64\n        }),\n        DType::U32 => scalar_op_typed(tensor, scalar as u32, |a: u32, b: u32| {\n            op(a as i64, b as i64) as u32\n        }),\n        DType::U16 => scalar_op_typed(tensor, scalar as u16, |a: u16, b: u16| {\n            op(a as i64, b as i64) as u16\n        }),\n        DType::U8 => scalar_op_typed(tensor, scalar as u8, |a: u8, b: u8| {\n            op(a as i64, b as i64) as u8\n        }),\n        _ => panic!(\"int_scalar_op: unsupported dtype {:?}\", dtype),\n    }\n}\n\n// Tests kept here exercise flex-specific behavior of `binary_op` /\n// `scalar_op`: non-contiguous (transposed/narrowed/permuted) strides,\n// flex f16/bf16 half-precision storage paths, and broadcast patterns\n// that probe the flex layout system. Plain contiguous add/sub/mul/div\n// and scalar-op smoke tests have been dropped in favor of the\n// equivalent coverage in burn-backend-tests, which exercises every\n// backend. When adding new tests, keep them here only if they probe\n// flex-internal dispatch; otherwise add them to\n// crates/burn-backend-tests/tests/tensor/float/ops/.\n#[cfg(test)]\n#[allow(clippy::needless_range_loop)]\nmod tests {\n    use super::*;\n    use alloc::vec;\n    use burn_backend::{TensorData, Tolerance};","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/binary.rs#L396-L432","documentation":"int_scalar_op applies a scalar i64 op to integer tensors elementwise; the scalar is truncated to the target dtype (matching PyTorch). U64/U32/U16/U8 are handled via i64 arithmetic; other dtypes (floats, bool, or unlisted signed variants) panic. It backs int_add_scalar, int_sub_scalar, int_mul_scalar, int_div_scalar, int_remainder_scalar and bitwise_and_scalar.","triggerScenarios":"Calling any int_*_scalar op on a float tensor, a Bool tensor, or a signed dtype not covered by the match (e.g. passing through with a dtype the dispatch never listed). Common when a scalar bias is applied to float data using the int entry point by mistake.","commonSituations":"Applying integer offsets to float feature tensors; bool flags combined with scalar bit masks; migrating PyTorch code relying on implicit type promotion.","solutions":["For float tensors use scalar_op / float_add_scalar instead: float_add_scalar(tensor, s).","Cast Bool to an integer dtype first: tensor.cast(DType::U8) then apply the int scalar op.","Verify the tensor's dtype with tensor.dtype() and route to the correct scalar-op family."],"exampleFix":"// before\nlet y = int_add_scalar(x_f32, 3); // panics\n// after\nlet y = float_add_scalar(x_f32, 3.0);","handlingStrategy":"validation","validationCode":"if !matches!(tensor.dtype(), DType::U64 | DType::U32 | DType::U16 | DType::U8 | DType::I64 | DType::I32) {\n    tensor = tensor.cast(DType::I64);\n}\nlet y = int_add_scalar(tensor, s);","typeGuard":"fn is_supported_int(d: DType) -> bool {\n    matches!(d, DType::U64 | DType::U32 | DType::U16 | DType::U8 | DType::I64 | DType::I32)\n}","tryCatchPattern":"let y = std::panic::catch_unwind(|| int_add_scalar(tensor.clone(), s))\n    .unwrap_or_else(|_| int_add_scalar(tensor.cast(DType::I64), s));","preventionTips":["Pick int_*_scalar vs float scalar ops based on tensor.dtype(), not the scalar type.","Note the scalar is truncated to the target dtype (PyTorch-like) — watch precision.","Cast Bool tensors to int before scalar ops.","Centralize scalar application in one helper that validates dtype."],"tags":["panic","dtype","int","scalar-op","unsupported-dtype"],"backgroundTag":"unsupported-dtype","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"}