tracel-ai/burn · error

The provided tensors are not on the same backend. Got backen

Error message

The provided tensors are not on the same backend. Got backends {t:?} and {g:?}.

What it means

Cross-backend consistency guard in the dispatch backend's `grad_replace`: when installing gradients, the tensor and the gradient tensor are matched by `DispatchTensorKind`; if both are dispatchable but their backends differ (e.g. an NdArray tensor with a CUDA gradient) the kind match falls through and this panic reports the two mismatched backend types. The failing input is the gradient tensor created on a different backend than its target.

Source

Thrown at crates/burn-dispatch/src/backend.rs:479

                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                #[cfg(any(feature = "flex", default_backend))]
                (DispatchTensorKind::Flex(tensor), DispatchTensorKind::Flex(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                #[cfg(feature = "ndarray")]
                (DispatchTensorKind::NdArray(tensor), DispatchTensorKind::NdArray(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                #[cfg(feature = "remote")]
                (DispatchTensorKind::Remote(tensor), DispatchTensorKind::Remote(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                (DispatchTensorKind::Autodiff(_), _) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
                // TODO: distributed message?
                (t, g) => panic!(
                    "The provided tensors are not on the same backend. Got backends {t:?} and {g:?}."
                ),
            },
            _ => panic!("Requires autodiff tensor."),
        }
    }

    fn inner(tensor: DispatchTensor) -> DispatchTensor {
        let DispatchTensor { kind, autodiff } = tensor;
        assert!(
            matches!(autodiff, DispatchAutodiffContext::Enabled(_)),
            "Requires autodiff tensor."
        );

        let kind = match kind {
            DispatchTensorKind::Autodiff(inner_kind) => match *inner_kind {
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => DispatchTensorKind::Cube(

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Create the gradient on the same backend/device as the tensor it is attached to.
  2. Call `.to_device()`/backend conversion on the gradient before `grad_replace`.
  3. Audit code that mixes backends (e.g. moving tensors to CPU for grads) and keep grads on the autodiff backend of record.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/burn-dispatch/src/backend.rs:479 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/52ff16af65a19ca9. Report an issue: GitHub.