tracel-ai/burn · error

Expected kind {kind:?}, got dtype {dtype:?}

Error message

Expected kind {kind:?}, got dtype {dtype:?}

What it means

Bridge-kind mismatch guard in `Tensor::from_bridge`: the BridgeTensor variant must match the target tensor kind K (and its dtype); e.g. wrapping an int BridgeTensor as `Tensor<B, D, Float>` hits the fallthrough and panics with the expected kind and the actual dtype. The failing input is the mis-typed BridgeTensor handed back by a custom backend op.

Source

Thrown at crates/burn-tensor/src/tensor/api/extension.rs:46

    ///
    /// This is the inverse of [`Tensor::into_bridge`] and is primarily intended
    /// for backend extensions, to wrap the output of a custom operation back into
    /// the high-level tensor API.
    ///
    /// # Panics
    ///
    /// Panics if the [`BridgeTensor`] variant does not match the tensor kind `K`
    /// (e.g. passing an int [`BridgeTensor`] when `K` is [`Float`](crate::Float).
    pub fn from_bridge(tensor: BridgeTensor) -> Self {
        let dtype = tensor.dtype();
        match (tensor.kind(), K::KIND) {
            (BridgeKind::Bool, Kind::Bool) if dtype.is_bool() => Self::new(tensor),
            (BridgeKind::Int, Kind::Int) if dtype.is_int() || dtype.is_uint() => Self::new(tensor),
            (BridgeKind::Float, Kind::Float) if dtype.is_float() => Self::new(tensor),
            (BridgeKind::QFloat, Kind::Float) if matches!(dtype, DType::QFloat(_)) => {
                Self::new(tensor)
            }
            (_, kind) => panic!("Expected kind {kind:?}, got dtype {dtype:?}"),
        }
    }

    /// Converts from a dispatch tensor into a tensor.
    ///
    /// # Panics
    /// Panis if the dispatch dtype does not match the tensor kind `K`.
    pub fn from_dispatch(tensor: DispatchTensor) -> Self {
        match (tensor.dtype(), K::KIND) {
            (DType::QFloat(_), Kind::Float) => Self::new(BridgeTensor::qfloat(tensor)),
            (dtype, Kind::Float) if dtype.is_float() => Self::new(BridgeTensor::float(tensor)),
            (dtype, Kind::Int) if dtype.is_int() || dtype.is_uint() => {
                Self::new(BridgeTensor::int(tensor))
            }
            (dtype, Kind::Bool) if dtype.is_bool() => Self::new(BridgeTensor::bool(tensor)),
            (dtype, kind) => panic!("Expected kind {kind:?}, got dtype {dtype:?}"),
        }
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Return the BridgeTensor with the kind/dtype matching the tensor type it will be wrapped into.
  2. Change the annotated tensor kind (Float/Int/Bool) on the receiving side to match the bridge value.
  3. Convert the bridge tensor's dtype before calling from_bridge.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/burn-tensor/src/tensor/api/extension.rs:46 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/d92b0e0f6cbc54c1. Report an issue: GitHub.