{"record":{"id":"f8d0874ca2685843","repo":"tracel-ai/burn","slug":"failed-to-read-tensor-data-synchronously-try-usin-f8d087","errorCode":null,"errorMessage":"Failed to read tensor data synchronously. Try using argwhere_async instead.","messagePattern":"Failed to read tensor data synchronously\\. Try using argwhere_async instead\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/api/bool.rs","lineNumber":273,"sourceCode":"    /// A tensor containing the indices of all non-zero elements of the given tensor. Each row in the\n    /// result contains the indices of a non-zero element.\n    ///\n    /// # Example\n    ///\n    /// ```rust\n    /// use burn_tensor::{Tensor, Bool};\n    ///\n    /// let device = Default::default();\n    /// let tensor = Tensor::<2, Bool>::from_bool(\n    ///     [[true, false, true], [false, true, false], [false, true, false]],\n    ///     &device,\n    /// );\n    /// let indices = tensor.argwhere();\n    /// println!(\"{indices}\"); // [[0, 0], [0, 2], [1, 1], [2, 1]]\n    /// ```\n    pub fn argwhere(self) -> Tensor<2, Int> {\n        try_read_sync(self.argwhere_async())\n            .expect(\"Failed to read tensor data synchronously. Try using argwhere_async instead.\")\n    }\n\n    /// Compute the indices of the elements that are true, grouped by element.\n    ///\n    /// # Returns\n    ///\n    /// A tensor containing the indices of all non-zero elements of the given tensor. Each row in the\n    /// result contains the indices of a non-zero element.\n    pub async fn argwhere_async(self) -> Tensor<2, Int> {\n        let out_dtype = self.device().settings().int_dtype;\n        let inner = Dispatch::bool_argwhere(self.primitive.into(), out_dtype).await;\n        Tensor::new(BridgeTensor::int(inner))\n    }\n\n    /// Creates a mask for the upper, lower triangle, or diagonal of a matrix, which can be used to\n    /// fill the specified area with a value.\n    fn tri_mask<S: Into<Shape>>(shape: S, tri_part: TriPart, offset: i64, device: &Device) -> Self {\n        let shape: Shape = shape.into();","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/api/bool.rs#L255-L291","documentation":"Tensor::argwhere() synchronously retrieves the coordinates of all true elements; like nonzero(), on async backends the blocking read fails and the expect panics. argwhere_async() is the supported alternative for GPU/async execution.","triggerScenarios":"Calling `.argwhere()` on a tensor on an async backend that cannot block (WGPU/WebGPU), especially inside async runtimes; porting CPU tensor code that used argwhere directly to a GPU backend.","commonSituations":"Mask coordinate extraction in image segmentation pipelines on WGPU; debugging code inside tokio::test with a GPU backend; position-index computation in loss functions on async devices.","solutions":["Use `tensor.argwhere_async()` and `.await` it in an async context","Use `nonzero_async()` if the extra grouping dimension of argwhere is not needed","Run on a synchronous CPU backend (NdArray) where blocking reads work","Restructure to read tensor data asynchronously via TensorData/into_data async APIs"],"exampleFix":"// before\nlet indices = tensor.argwhere();\n// after\nlet indices = tensor.argwhere_async().await;","handlingStrategy":"fallback","validationCode":"if backend_is_async() {\n    let indices = tensor.argwhere_async().await;\n}","typeGuard":null,"tryCatchPattern":"// prefer async read; argwhere() panics when sync read is unsupported\nlet indices = tensor.argwhere_async().await;","preventionTips":["Prefer argwhere_async on GPU backends","Restructure pipelines so mask coordinates are consumed after an async read","Keep CPU/sync reads confined to NdArray-backed tensors"],"tags":["async","sync-read","gpu","burn-tensor"],"backgroundTag":"blocking-async-read","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"}