{"record":{"id":"560e835682383c43","repo":"tracel-ai/burn","slug":"argmax-unsupported-dtype","errorCode":null,"errorMessage":"argmax: unsupported dtype {:?}","messagePattern":"argmax: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/reduce.rs","lineNumber":779,"sourceCode":"                f16::from_f32,\n            )\n            .1\n        }\n        DType::BF16 => {\n            extremum_dim_with_indices_half::<bf16, _>(\n                &tensor,\n                dim,\n                |a, b| !b.is_nan() && (a.is_nan() || a > b),\n                bf16::to_f32,\n                bf16::from_f32,\n            )\n            .1\n        }\n        DType::I8 => extremum_dim_with_indices::<i8, _>(&tensor, dim, |a, b| a > b).1,\n        DType::I16 => extremum_dim_with_indices::<i16, _>(&tensor, dim, |a, b| a > b).1,\n        DType::I32 => extremum_dim_with_indices::<i32, _>(&tensor, dim, |a, b| a > b).1,\n        DType::I64 => extremum_dim_with_indices::<i64, _>(&tensor, dim, |a, b| a > b).1,\n        _ => panic!(\"argmax: unsupported dtype {:?}\", tensor.dtype()),\n    }\n}\n\n/// Argmin along a dimension, returning indices as isize (INDEX_DTYPE).\npub fn argmin(tensor: FlexTensor, dim: usize) -> FlexTensor {\n    assert!(\n        tensor.layout().shape()[dim] > 0,\n        \"argmin: dimension {dim} has size 0\"\n    );\n    assert_dim_fits_isize(tensor.layout().shape()[dim], dim);\n    // f32 last-dim fast path: 2-pass SIMD for large rows, 1-pass scalar for small rows\n    if tensor.dtype() == DType::F32 && dim == tensor.layout().shape().num_dims() - 1 {\n        #[cfg(feature = \"simd\")]\n        if tensor.layout().shape()[dim] >= EXTREMUM_SIMD_ROW_THRESHOLD {\n            return extremum_indices_f32_last_simd(&tensor, dim, kernels::min_f32);\n        }\n        return extremum_indices_f32_last_scalar(&tensor, dim, |a, b| a < b);\n    }","sourceCodeStart":761,"sourceCodeEnd":797,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/reduce.rs#L761-L797","documentation":"The burn-flex `argmax` reduction panics when the tensor dtype has no argmax implementation. The match at reduce.rs:742-780 handles F32/F64/F16/BF16 and I8-I64 only; unsigned integers (U8/U16/U32/U64) and Bool fall into the `_` arm and panic with the dtype name. Note the asymmetry with `min`/`max`, which do support unsigned types.","triggerScenarios":"Calling `ops::reduce::argmax(tensor, dim)` on a U8/U16/U32/U64 or Bool tensor; also on any dtype not in the supported set. The dim bounds and size-0 cases are caught by earlier asserts, so this panic is purely dtype-driven.","commonSituations":"Argmax over an unsigned tensor loaded from a file/quantized pipeline (u8 images, u32 ids); argmax over a bool mask to find index of first/any true; switching backends where the other backend supported unsigned argmax but burn-flex does not.","solutions":["Cast unsigned input to a signed or float type first: `tensor.cast(DType::I64)` or `tensor.cast(DType::F32)` before argmax (values must fit; u64 above i64::MAX overflows).","If the tensor is Bool, cast to U8 (or I64) first, then argmax.","If you need unsigned argmax, add U8/U16/U32/U64 arms to the match in crates/burn-flex/src/ops/reduce.rs using `extremum_dim_with_indices::<u32, _>(...)` etc.","Verify the producing op's output dtype; if you intended floats, fix the cast upstream instead of casting at the argmax call."],"exampleFix":"// before\nlet idx = argmax(u8_tensor, 1); // panics: unsupported dtype U8\n// after\nlet idx = argmax(u8_tensor.cast(DType::I64), 1);","handlingStrategy":"validation","validationCode":"fn ensure_argmax_supported(dtype: DType) -> Result<(), String> {\n    match dtype {\n        DType::F32 | DType::F64 | DType::F16 | DType::BF16\n        | DType::I8 | DType::I16 | DType::I32 | DType::I64 => Ok(()),\n        other => Err(format!(\"argmax: unsupported dtype {other:?}; cast first\")),\n    }\n}","typeGuard":"fn is_argmax_supported(dtype: DType) -> bool {\n    matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16\n        | DType::I8 | DType::I16 | DType::I32 | DType::I64)\n}","tryCatchPattern":"let out = std::panic::catch_unwind(|| argmax(t.clone(), dim))\n    .ok()\n    .unwrap_or_else(|| argmax(t.cast(DType::I64), dim));","preventionTips":["Remember burn-flex argmax/argmin support only floats and signed ints — cast unsigned tensors to I64 first","For Bool tensors, cast to U8/I64 before argmax","Check dtype at the boundary where data is loaded (u8 images etc.)","Add dtype assertions in tests mirroring the backend's match arms"],"tags":["panic","dtype","burn","argmax","reduce-ops"],"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"}