tracel-ai/burn · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

BackendTensor::autodiff() was called on a tensor that is not the Autodiff variant; the fallback arm uses unreachable!(), which panics with 'internal error: entered unreachable code'. The author treated the non-autodiff case as impossible, but at runtime a plain Float/Int/Bool/Quantized tensor reached this function — e.g. calling autodiff() on a tensor from a non-gradient (inference) path.

Source

Thrown at crates/burn-dispatch/src/tensor.rs:98

            BackendTensor::Autodiff(_) => panic!("Should be bool, got autodiff"),
        }
    }

    /// Returns the inner quantized tensor primitive.
    pub fn quantized(self) -> B::QuantizedTensorPrimitive {
        match self {
            BackendTensor::Quantized(tensor) => tensor,
            _ => unreachable!(),
        }
    }

    #[cfg(feature = "autodiff")]
    /// Returns the inner autodiff tensor primitive.
    pub fn autodiff(self) -> FloatTensor<Autodiff<B>> {
        match self {
            BackendTensor::Autodiff(tensor) => tensor,
            // NOTE: this is the panicking code reached in tensor.rs:74:18:
            _ => unreachable!(),
        }
    }

    #[cfg(feature = "autodiff")]
    /// Returns the inner autodiff tensor primitive.
    pub fn as_autodiff(&self) -> &FloatTensor<Autodiff<B>> {
        match self {
            BackendTensor::Autodiff(tensor) => tensor,
            _ => unreachable!(),
        }
    }

    #[cfg(feature = "autodiff")]
    /// Returns the inner autodiff tensor primitive.
    pub fn autodiff_inner(self) -> B::FloatTensorPrimitive {
        match self {
            BackendTensor::Autodiff(tensor) => tensor.primitive,
            _ => unreachable!(),

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Ensure the tensor was created through the Autodiff backend (Autodiff::float / marked as requiring gradients) before calling autodiff()
  2. Only call autodiff() on training-path tensors; use the non-autodiff primitive in inference paths
  3. Match the enum yourself and produce a descriptive error instead of relying on the unreachable!() arm
  4. Verify the feature configuration: under feature = "autodiff" all tensors in the training graph must be Autodiff-wrapped

Example fix

// before
let inner = handle.autodiff(); // panics if not Autodiff variant
// after
let inner = match handle {
    BackendTensor::Autodiff(t) => t,
    other => panic!("expected autodiff tensor, got {:?}; create it via Autodiff backend", std::mem::discriminant(&other) != std::mem::discriminant(&handle)),
};
Defensive patterns

Strategy: type-guard

Validate before calling

// only call autodiff() when the handle is the Autodiff variant
if !matches!(handle, BackendTensor::Autodiff(_)) {
    panic!("autodiff() requires a training-mode (Autodiff) tensor");
}

Type guard

#[cfg(feature = "autodiff")]
fn is_autodiff<B: BackendTypes>(t: &BackendTensor<B>) -> bool {
    matches!(t, BackendTensor::Autodiff(_))
}

Prevention

When it happens

Trigger: Calling BackendTensor::autodiff() on a handle built from BackendTensor::Float/Int/Bool/Quantized; running under the autodiff feature but invoking autodiff() in inference mode (no_grad/valid) where handles are plain Float primitives.

Common situations: Mixing inference-mode tensors with training-mode code paths; passing a tensor created outside the Autodiff backend into gradient-requiring code; missing mark/convert to Autodiff before calling autodiff().

Related errors


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