{"record":{"id":"bdb01b8ec67a2caf","repo":"tracel-ai/burn","slug":"failed-to-read-tensor-data-err","errorCode":null,"errorMessage":"Failed to read tensor data: {err}","messagePattern":"Failed to read tensor data: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/api/base.rs","lineNumber":2115,"sourceCode":"    }\n\n    /// Copies the tensor data to host memory and converts it to the dtype represented by `E`.\n    ///\n    /// The conversion is a no-op if the dtype is the same as the current dtype.\n    ///\n    /// See: [`Tensor::try_to_data_as`].\n    ///\n    /// # Returns\n    /// A `TensorData` with dtype `E::dtype()`.\n    ///\n    /// # Panics\n    ///\n    /// Panics if synchronous readback isn't supported, tensor execution or storage access fails,\n    /// or the data can't be converted to `E`.\n    #[track_caller]\n    pub fn to_data_as<E: Element>(&self) -> TensorData {\n        self.try_to_data_as::<E>()\n            .unwrap_or_else(|err| panic!(\"Failed to read tensor data: {err}\"))\n    }\n\n    /// Copies the tensor data to host memory and converts it to the dtype represented by `E`.\n    ///\n    /// By contract, this will yield the same result as\n    /// `tensor.try_to_data()?.try_cast_as::<E>()`.\n    ///\n    /// The conversion is a no-op if the dtype is the same as the current dtype.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if tensor execution or storage access fails, or the data can't be\n    /// converted to `E`.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the platform doesn't support synchronous readback.\n    pub fn try_to_data_as<E: Element>(&self) -> Result<TensorData, TensorReadError> {","sourceCodeStart":2097,"sourceCodeEnd":2133,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/api/base.rs#L2097-L2133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the fallible variant try_to_data_as::<E>() and handle the Result instead of panicking","Ensure the element type E matches or is castable from the tensor's dtype (or use to_data_dtype with an explicit DType)","Make sure the backend supports synchronous readback, or await/complete pending execution before reading","Call into_data()/to_data() first and then try_cast_as::<E>() to isolate the cast failure from the read failure"],"exampleFix":"// before\nlet data = tensor.to_data_as::<f32>(); // panics on read/cast failure\n// after\nlet data = tensor.try_to_data_as::<f32>()?; // handle Err explicitly","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match tensor.try_to_data_as::<E>() { Ok(d) => ..., Err(e) => ... }","preventionTips":["Prefer try_ variants in production code"],"tags":["tensor","readback","panic","burn","gpu"],"backgroundTag":"tensor-data-readback-failed","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}