tracel-ai/burn · error

Failed to read tensor data as {dtype:?}: {err}

Error message

Failed to read tensor data as {dtype:?}: {err}

What it means

Tensor::to_data_dtype(dtype) copies tensor data to the host and converts it to the given DType, panicking with this message if try_to_data_dtype fails. Failure modes are the same as other readbacks (unsupported sync readback, execution/storage error) plus dtype conversion failure. The panic message includes the requested dtype and the underlying error.

Source

Thrown at crates/burn-tensor/src/tensor/api/base.rs:2170

    }

    /// Copies the tensor data to host memory and converts it to `dtype`.
    ///
    /// The conversion is a no-op if the dtype is the same as the current dtype.
    ///
    /// See: [`Tensor::try_to_data_dtype`].
    ///
    /// # Returns
    /// A `TensorData` with the requested `dtype`.
    ///
    /// # Panics
    ///
    /// Panics if synchronous readback isn't supported, tensor execution or storage access fails,
    /// or the data can't be converted to `dtype`.
    #[track_caller]
    pub fn to_data_dtype(&self, dtype: DType) -> TensorData {
        self.try_to_data_dtype(dtype)
            .unwrap_or_else(|err| panic!("Failed to read tensor data as {dtype:?}: {err}"))
    }

    /// Copies the tensor data to host memory and converts it to `dtype`.
    ///
    /// By contract, this will yield the same result as
    /// `tensor.try_to_data()?.try_cast(dtype)`.
    ///
    /// The conversion is a no-op if the dtype is the same as the current dtype.
    ///
    /// # Errors
    ///
    /// Returns an error if tensor execution or storage access fails, or the data can't be
    /// converted to `dtype`.
    ///
    /// # Panics
    ///
    /// Panics if the platform doesn't support synchronous readback.
    pub fn try_to_data_dtype(&self, dtype: DType) -> Result<TensorData, TensorReadError> {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Use try_to_data_dtype(dtype) and handle the Result to avoid the panic
  2. Choose a supported target DType; fall back to manual conversion via to_data() + TensorData::try_cast(dtype)
  3. Confirm the source tensor's dtype supports conversion to the requested one (e.g. numeric-to-numeric)
  4. Verify backend support for sync readback or restructure so the read happens after execution completes

Example fix

// before
let data = tensor.to_data_dtype(DType::F16); // panics if cast/read fails
// after
let data = tensor.try_to_data_dtype(DType::F16)?;
Defensive patterns

Strategy: try-catch

Try / catch

match tensor.try_to_data_dtype(dtype) { Ok(d) => ..., Err(e) => ... }

Prevention

When it happens

Trigger: Requesting a dtype conversion the backend or Element implementation cannot perform (e.g. converting bool or complex data to an int/float dtype); reading a tensor on a backend lacking synchronous readback; an execution error (failed kernel, OOM) surfacing during the read.

Common situations: Downcasting model outputs to f16/bf16 or upcasting int8 quantized tensors to f32 where the cast is unsupported; test harnesses converting tensors to a comparison dtype; GPU pipelines reading intermediates before execution completes.

Related errors


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