tracel-ai/burn · error

Failed to read tensor data: {err}

Error message

Failed to read tensor data: {err}

What it means

Tensor::to_data_as::<E>() copies tensor data to the host and converts it to the element type E. It wraps the fallible try_to_data_as and panics on any error: the backend does not support synchronous readback, kernel execution or storage access failed, or the values cannot be cast to E. The panic message embeds the underlying ExecutionError/cast error.

Source

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

    }

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

    /// Copies the tensor data to host memory and converts it to the dtype represented by `E`.
    ///
    /// By contract, this will yield the same result as
    /// `tensor.try_to_data()?.try_cast_as::<E>()`.
    ///
    /// 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 `E`.
    ///
    /// # Panics
    ///
    /// Panics if the platform doesn't support synchronous readback.
    pub fn try_to_data_as<E: Element>(&self) -> Result<TensorData, TensorReadError> {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Use the fallible variant try_to_data_as::<E>() and handle the Result instead of panicking
  2. Ensure the element type E matches or is castable from the tensor's dtype (or use to_data_dtype with an explicit DType)
  3. Make sure the backend supports synchronous readback, or await/complete pending execution before reading
  4. Call into_data()/to_data() first and then try_cast_as::<E>() to isolate the cast failure from the read failure

Example fix

// before
let data = tensor.to_data_as::<f32>(); // panics on read/cast failure
// after
let data = tensor.try_to_data_as::<f32>()?; // handle Err explicitly
Defensive patterns

Strategy: try-catch

Try / catch

match tensor.try_to_data_as::<E>() { Ok(d) => ..., Err(e) => ... }

Prevention

When it happens

Trigger: Calling to_data_as::<f32>() on a tensor whose element type cannot be cast to f32; reading back a tensor on a backend without sync readback support (e.g. some async GPU backends); the tensor's computation graph failed (kernel error, OOM) so storage access fails.

Common situations: Fetching results from a GPU (WebGPU/CUDA) tensor at a point where execution hasn't completed or sync isn't supported; casting quantized/complex/bf16 data to an incompatible element type in tests; assertions in test code that assume readback always succeeds.

Related errors


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