tracel-ai/burn · error

not supported for sorting operations

Error message

not supported for sorting operations

What it means

A dtype-dispatch macro in crates/burn-backend/src/backend/ops/sort.rs matches the element dtype before invoking a sorting kernel; `Bool` and `QFloat` dtypes fall through to `unimplemented!("not supported for sorting operations")`. Sorting kernels only support numeric integer/float dtypes.

Source

Thrown at crates/burn-backend/src/backend/ops/sort.rs:34

        macro_rules! dispatch_index {
            ($index_ty:ty) => {
                match $data.dtype {
                    DType::F64 => $fn::<f64, $index_ty>($data, $($args),*),
                    DType::F32 | DType::Flex32 => {
                        $fn::<f32, $index_ty>($data, $($args),*)
                    }
                    DType::F16 => $fn::<f16, $index_ty>($data, $($args),*),
                    DType::BF16 => $fn::<bf16, $index_ty>($data, $($args),*),
                    DType::I64 => $fn::<i64, $index_ty>($data, $($args),*),
                    DType::I32 => $fn::<i32, $index_ty>($data, $($args),*),
                    DType::I16 => $fn::<i16, $index_ty>($data, $($args),*),
                    DType::I8 => $fn::<i8, $index_ty>($data, $($args),*),
                    DType::U64 => $fn::<u64, $index_ty>($data, $($args),*),
                    DType::U32 => $fn::<u32, $index_ty>($data, $($args),*),
                    DType::U16 => $fn::<u16, $index_ty>($data, $($args),*),
                    DType::U8 => $fn::<u8, $index_ty>($data, $($args),*),
                    DType::Bool(_) | DType::QFloat(_) => {
                        unimplemented!("not supported for sorting operations")
                    }
                }
            };
        }

        match $index_dtype {
            IntDType::I64 => dispatch_index!(i64),
            IntDType::I32 => dispatch_index!(i32),
            IntDType::I16 => dispatch_index!(i16),
            IntDType::I8 => dispatch_index!(i8),
            IntDType::U64 => dispatch_index!(u64),
            IntDType::U32 => dispatch_index!(u32),
            IntDType::U16 => dispatch_index!(u16),
            IntDType::U8 => dispatch_index!(u8),
        }
    }};

    // Dispatch only element dtype.

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast the Bool tensor to a numeric dtype (e.g. I32/U8/F32) before sorting.
  2. Dequantize QFloat tensors to F32/F16 before sorting operations.
  3. Reorder logic so masks are used to filter/select, not sorted.

Example fix

// before
let sorted = tensor.sort(dim); // tensor is Bool or QFloat -> panics

// after
let numeric = tensor.cast(DType::F32); // or dequantize QFloat
let sorted = numeric.sort(dim);
Defensive patterns

Strategy: validation

Validate before calling

fn sortable(dtype: DType) -> bool {
    !matches!(dtype, DType::Bool(_) | DType::QFloat(_))
}
assert!(sortable(tensor.dtype()), "cast or dequantize before sorting");

Type guard

fn is_sortable(dtype: DType) -> bool {
    matches!(dtype, DType::F32 | DType::F16 | DType::BF16 | DType::I8 | DType::I16 | DType::I32 | DType::I64 | DType::U8 | DType::U16 | DType::U32 | DType::U64)
}

Prevention

When it happens

Trigger: Calling sort/topk/argsort (the macro-generated dispatch with index dtype parameter) on a Bool or quantized (QFloat) tensor.

Common situations: Sorting a boolean mask tensor directly instead of converting it; applying topk/argsort to quantized model outputs without dequantizing first.

Related errors


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