{"record":{"id":"01d825ed2a241d42","repo":"tracel-ai/burn","slug":"scalar-op-unsupported-dtype","errorCode":null,"errorMessage":"scalar_op: unsupported dtype {:?}","messagePattern":"scalar_op: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/binary.rs","lineNumber":308,"sourceCode":"\n    match dtype {\n        DType::F32 => scalar_op_typed(tensor, scalar as f32, f32_op),\n        DType::F64 => scalar_op_typed(tensor, scalar, f64_op),\n        DType::F16 => {\n            let scalar_f16 = f16::from_f32(scalar as f32);\n            let s = scalar_f16.to_f32();\n            scalar_op_typed(tensor, scalar_f16, |a: f16, _| {\n                f16::from_f32(f32_op(a.to_f32(), s))\n            })\n        }\n        DType::BF16 => {\n            let scalar_bf16 = bf16::from_f32(scalar as f32);\n            let s = scalar_bf16.to_f32();\n            scalar_op_typed(tensor, scalar_bf16, |a: bf16, _| {\n                bf16::from_f32(f32_op(a.to_f32(), s))\n            })\n        }\n        _ => panic!(\"scalar_op: unsupported dtype {:?}\", dtype),\n    }\n}\n\npub(crate) fn scalar_op_typed<E, Op>(mut tensor: FlexTensor, scalar: E, op: Op) -> FlexTensor\nwhere\n    E: Element + bytemuck::Pod,\n    Op: Fn(E, E) -> E,\n{\n    // In-place fast path: unique, contiguous at offset 0\n    if tensor.is_unique()\n        && let Some((0, end)) = tensor.layout().contiguous_offsets()\n    {\n        let storage: &mut [E] = tensor.storage_mut();\n        for x in storage[..end].iter_mut() {\n            *x = op(*x, scalar);\n        }\n        return tensor;\n    }","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/binary.rs#L290-L326","documentation":"scalar_op applies an elementwise float op between a tensor and a scalar, dispatching on the tensor dtype; F32, F64, F16 and BF16 are handled (F16/BF16 by converting the scalar through half types) and all other dtypes panic. Used by float_add_scalar and float_sub_scalar, so scalar ops on non-float tensors abort here.","triggerScenarios":"Calling float_add_scalar/float_sub_scalar (or scalar_op directly, e.g. from tests test_scalar_f16/test_scalar_bf16 variants) on an integer, unsigned or bool tensor. Also occurs when a scalar-only adjustment is applied to a tensor that was cast to int earlier in the pipeline.","commonSituations":"Adding a bias/offset to a quantized (int8) tensor; test scaffolding reusing int fixtures; expectation of PyTorch-like result_type promotion that burn-flex does not implement.","solutions":["Cast the tensor to a float dtype first: tensor.cast(DType::F32) then apply the scalar op.","Use int_scalar_op / int_add_scalar instead when the tensor is genuinely integer — that is the integer counterpart.","Keep the pipeline float from the start so scalar adjustments land on float tensors."],"exampleFix":"// before\nlet y = float_add_scalar(x_i32, 0.5); // panics\n// after\nlet y = float_add_scalar(x_i32.cast(DType::F32), 0.5);","handlingStrategy":"type-guard","validationCode":"if !matches!(tensor.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16) {\n    tensor = tensor.cast(DType::F32);\n}\nlet y = float_add_scalar(tensor, s);","typeGuard":"fn is_float_dtype(d: DType) -> bool {\n    matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16)\n}","tryCatchPattern":"let y = std::panic::catch_unwind(|| float_add_scalar(tensor.clone(), s))\n    .unwrap_or_else(|_| float_add_scalar(tensor.cast(DType::F32), s));","preventionTips":["Use int_scalar_op family for integer tensors, scalar_op family for floats.","Track tensor dtype through the pipeline with debug asserts.","Dequantize int8 tensors before applying float scalars.","Write dtype-parametrized tests for scalar helpers."],"tags":["panic","dtype","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"}