tracel-ai/burn · error

argmin: unsupported dtype {:?}

Error message

argmin: unsupported dtype {:?}

What it means

The burn-flex `argmin` reduction panics when the tensor dtype has no argmin implementation. Identical structure to `argmax` (reduce.rs:798-837): only F32/F64/F16/BF16 and I8-I64 are handled; unsigned integers and Bool reach the `_ => panic!` arm at line 835.

Source

Thrown at crates/burn-flex/src/ops/reduce.rs:835

                f16::from_f32,
            )
            .1
        }
        DType::BF16 => {
            extremum_dim_with_indices_half::<bf16, _>(
                &tensor,
                dim,
                |a, b| !b.is_nan() && (a.is_nan() || a < b),
                bf16::to_f32,
                bf16::from_f32,
            )
            .1
        }
        DType::I8 => extremum_dim_with_indices::<i8, _>(&tensor, dim, |a, b| a < b).1,
        DType::I16 => extremum_dim_with_indices::<i16, _>(&tensor, dim, |a, b| a < b).1,
        DType::I32 => extremum_dim_with_indices::<i32, _>(&tensor, dim, |a, b| a < b).1,
        DType::I64 => extremum_dim_with_indices::<i64, _>(&tensor, dim, |a, b| a < b).1,
        _ => panic!("argmin: unsupported dtype {:?}", tensor.dtype()),
    }
}

// ============================================================================
// Dimension reduction helpers
// ============================================================================

#[derive(Clone, Copy)]
enum ReduceOp {
    Sum,
    Prod,
}

/// Optimized f32 dimension reduction with SIMD.
fn reduce_dim_f32(tensor: &FlexTensor, dim: usize, op: ReduceOp) -> FlexTensor {
    let ndims = tensor.layout().shape().num_dims();

    assert!(

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast to a supported dtype before the call: `tensor.cast(DType::I64)` (watch for u64 values above i64::MAX) or `tensor.cast(DType::F32)`.
  2. For Bool tensors cast to U8 or I64 first.
  3. Add unsigned arms (`extremum_dim_with_indices::<u8, _>(...)` etc.) to the argmin match in crates/burn-flex/src/ops/reduce.rs if unsigned support is needed.
  4. Check the dtype of the tensor at production time; fix the upstream cast if an unexpected dtype is flowing in.

Example fix

// before
let idx = argmin(u32_tensor, 0); // panics: unsupported dtype U32
// after
let idx = argmin(u32_tensor.cast(DType::I64), 0);
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_argmin_supported(dtype: DType) -> Result<(), String> {
    match dtype {
        DType::F32 | DType::F64 | DType::F16 | DType::BF16
        | DType::I8 | DType::I16 | DType::I32 | DType::I64 => Ok(()),
        other => Err(format!("argmin: unsupported dtype {other:?}; cast first")),
    }
}

Type guard

fn is_argmin_supported(dtype: DType) -> bool {
    matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16
        | DType::I8 | DType::I16 | DType::I32 | DType::I64)
}

Try / catch

let out = std::panic::catch_unwind(|| argmin(t.clone(), dim))
    .ok()
    .unwrap_or_else(|| argmin(t.cast(DType::I64), dim));

Prevention

When it happens

Trigger: Calling `ops::reduce::argmin(tensor, dim)` on a U8/U16/U32/U64 or Bool tensor. Dim-bounds problems fail earlier with different messages, so this panic only fires on unhandled dtypes.

Common situations: Argmin over u8 image data (nearest-color / template matching); argmin over a bool mask; porting code from a backend that supported unsigned argmin to burn-flex.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/d940b8db69a2dbdc. Report an issue: GitHub.