tracel-ai/burn · error
The input tensor to linalg::det should have float dtype.
Error message
The input tensor to linalg::det should have float dtype.
What it means
linalg::det computes the determinant via LU decomposition and queries the dtype's floating-point info (machine epsilon) to build a singularity threshold. If the tensor's dtype is not a float (int, bool, etc.), finfo() returns None and the expect panics.
Source
Thrown at crates/burn-tensor/src/tensor/linalg/det.rs:163
let batched_range_tensor = range.expand(expand_dims);
let n_row_swaps = squeezed_pivots
.not_equal(batched_range_tensor)
.int()
.sum_dim(D1 - 1);
let odd_mask = n_row_swaps.clone().remainder_scalar(2).equal_scalar(1);
let p_det = n_row_swaps
.cast(working_float_dtype)
.ones_like()
.mask_fill(odd_mask, -1.0)
.squeeze_dim(D1 - 1);
// Compute the determinant of U
let u_diag = linalg::diag::<D, D1, _>(lu);
let mut u_det = u_diag.clone().prod_dim(D1 - 1).squeeze_dim(D1 - 1);
let eps = tensor
.dtype()
.finfo()
.expect("The input tensor to linalg::det should have float dtype.")
.epsilon;
let n = dims[D - 1]; // The input tensor contains n by n matrices
let threshold = u_diag.clone().abs().max_dim(D1 - 1) * (n as f64).sqrt() * eps;
let near_zero = u_diag.abs().lower_equal(threshold);
let singular_mask = near_zero.any_dim(D1 - 1).squeeze_dim::<D2>(D1 - 1);
u_det = u_det.mask_fill(singular_mask, 0.0);
let final_det = p_det * u_det;
// Cast back to original dtypes
if needs_upcast {
final_det.cast(original_dtype)
} else {
final_det
}
}
View on GitHub (pinned to d16f7ba2ed)
Solutions
- Cast the tensor to a float dtype before calling det: `tensor.float()`
- Create the input as a float tensor (`Tensor::<..., Float>` or with f32/f64 dtype)
- Validate `tensor.dtype()` is a float kind in calling code before invoking det
- Use f64 dtype for numerical stability of the LU-based determinant
Example fix
// before let d = int_tensor.det(); // after let d = int_tensor.float().det();
Defensive patterns
Strategy: type-guard
Validate before calling
if !matches!(tensor.dtype(), DType::F32 | DType::F64 | DType::BF16 | DType::F16) {
tensor = tensor.float();
}
let d = tensor.det(); Type guard
fn is_float_dtype(dt: DType) -> bool {
matches!(dt, DType::F32 | DType::F64 | DType::BF16 | DType::F16)
} Prevention
- Cast to .float() before linalg operations
- Type tensors as Float at construction when they feed numeric linear algebra
- Validate dtypes at API boundaries when loading external data
When it happens
Trigger: Calling `tensor.det()` on a tensor with an integer or bool dtype; creating a tensor with `Tensor::<D, Int>` or casting to int before computing the determinant.
Common situations: Passing integer adjacency/count matrices directly to det; forgetting `.float()` after loading integer image masks; switching backends/dtypes and silently ending up with an int tensor.
Related errors
- Unsupported type {:?}
- ctc_loss_backward: 2 * max_target_len + 1 = {} exceeds the k
- Quantization scheme is not valid for dtype {other:?}
- Not a valid DType for tensors.
- Can't store native sub-byte values
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/49316b5149d9797c.
Report an issue: GitHub.