tracel-ai/burn · error

linalg::svd: gradients are not implemented; detach the input

Error message

linalg::svd: gradients are not implemented; detach the input tensor first

What it means

Autodiff-support guard in `linalg::svd`: SVD has no implemented backward pass, so calling it on a tensor that requires gradients (tracked by autodiff) panics with instructions to detach. The failing input is a tracked tensor passed to svd; detaching breaks the graph edge, making the op legal but non-differentiable.

Source

Thrown at crates/burn-tensor/src/tensor/linalg/svd.rs:97

///     // A = U @ diag(S) @ Vt (within tolerance)
///     let recon = u.mul(s.unsqueeze_dim(0)).matmul(vt);
///     println!("{}", recon);
/// }
/// ```
pub fn svd<const D: usize, const D1: usize>(
    mut tensor: Tensor<D>,
    sweeps: usize,
) -> (Tensor<D>, Tensor<D1>, Tensor<D>) {
    let dims = tensor.dims();
    let original_dtype = tensor.dtype();
    let device = tensor.device();
    check!(TensorCheck::svd_input_tensor::<D, D1>(
        "linalg::svd",
        &dims,
        original_dtype
    ));
    if tensor.is_require_grad() {
        panic!("linalg::svd: gradients are not implemented; detach the input tensor first");
    }
    assert!(sweeps > 0, "linalg::svd: sweeps must be greater than zero");

    // Upcast f16/bf16 to f32 (same convention as `det`), cast back at the end.
    let needs_upcast = original_dtype == DType::F16 || original_dtype == DType::BF16;
    if needs_upcast {
        tensor = tensor.cast(FloatDType::F32);
    }

    // One-sided formulation requires m >= n; decompose A^T for wide matrices.
    let (n_rows, n_cols) = (dims[D - 2], dims[D - 1]);
    let (a, swap) = if n_rows >= n_cols {
        (tensor, false)
    } else {
        (tensor.transpose(), true)
    };
    let (m, n) = (n_rows.max(n_cols), n_rows.min(n_cols));

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Call `.detach()` on the tensor before svd when no gradient through the decomposition is needed.
  2. Compute SVD inside `no_grad`-equivalent contexts, or restructure training so SVD is on a non-differentiable branch.
  3. Implement or wait for an SVD backward (e.g. via the closed-form gradient involving U, S, V) if gradients through SVD are required.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at crates/burn-tensor/src/tensor/linalg/svd.rs:97 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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