{"record":{"id":"94bbf3d15d2875b6","repo":"tracel-ai/burn","slug":"grid-sample-2d-numcast-from-x-to-retur","errorCode":null,"errorMessage":"grid_sample_2d: NumCast::from({x:?}) to {:?} returned None","messagePattern":"grid_sample_2d: NumCast::from\\((.+?)\\) to (.+?) returned None","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/burn-flex/src/ops/grid_sample.rs","lineNumber":105,"sourceCode":"    let g_stride_h = w_out * 2;\n\n    let o_stride_n = channels * h_out * w_out;\n    let o_stride_c = h_out * w_out;\n    let o_stride_h = w_out;\n\n    // Low-precision types (f16/bf16) are widened to f64 for all arithmetic so\n    // that coordinate math, weights, and accumulated samples keep full precision.\n    //\n    // The from_f64 unwrap is unreachable for any well-formed input: bilinear\n    // is a convex combination of finite samples so the result stays bounded by\n    // the sample envelope. The `half` crate's `NumCast` impl for f16/bf16\n    // forwards through `to_f32` and maps non-finite inputs to `Some(inf/nan)`;\n    // `num_traits`'s f32/f64 impls do the same. The message-bearing panic is a\n    // diagnostic hatch for future dtypes where this invariant does not hold.\n    let to_f64 = |x: T| -> f64 { ToElement::to_f64(&x) };\n    let from_f64 = |x: f64| -> T {\n        <T as NumCast>::from(x).unwrap_or_else(|| {\n            panic!(\n                \"grid_sample_2d: NumCast::from({x:?}) to {:?} returned None\",\n                T::dtype()\n            )\n        })\n    };\n\n    for b in 0..batch_size {\n        for y in 0..h_out {\n            for x in 0..w_out {\n                let g_idx = b * g_stride_n + y * g_stride_h + x * 2;\n                let sample_x = to_f64(grid_data[g_idx]);\n                let sample_y = to_f64(grid_data[g_idx + 1]);\n\n                let (px, py) = if align {\n                    let px = (sample_x + 1.0) * ((w_in - 1) as f64) / 2.0;\n                    let py = (sample_y + 1.0) * ((h_in - 1) as f64) / 2.0;\n                    (px, py)\n                } else {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/grid_sample.rs#L87-L123","documentation":"Inside grid_sample_2d_impl, from_f64 converts computed coordinates back to the element type T via NumCast::from, which returns None when the f64 value is not representable in T. The panic is a diagnostic hatch: for current float dtypes ToElement::to_f64 preserves non-finite values so from() always succeeds; if a future dtype breaks that invariant, this panic surfaces it.","triggerScenarios":"Effectively unreachable with F32/F64/F16/BF16 today; would trigger if a non-standard element type T were used where to_f64 loses representability (e.g. an integer T given a fractional/overflowing f64 coordinate).","commonSituations":"Custom element types added to the backend, or extreme NaN/inf handling differences in a future dtype; as a library user on stock float dtypes you should not hit this.","solutions":["Nothing to fix for stock float dtypes — this path cannot return None there","If you hit it with a custom dtype, ensure its NumCast impl and to_f64 conversion are lossless for non-finite values","Clamp grid coordinates to finite values before grid_sample when feeding unusual data","Report it to burn-flex maintainers with the dtype and input values if reachable"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure grid coordinates are finite before grid_sample_2d\nassert!(grid.to_data().value.iter().all(|v| v.is_finite() || v.is_nan()), \"non-finite handling differs per dtype\");","typeGuard":null,"tryCatchPattern":"// this panic is not catchable in Rust without catch_unwind\nlet result = std::panic::catch_unwind(AssertUnwindSafe(||\n    backend.grid_sample_2d(tensor.clone(), grid.clone(), options.clone())\n));","preventionTips":["Stick to stock float dtypes (F32/F64/F16/BF16) for grid_sample","Keep grid coordinates finite; sanitize inputs if upstream may emit inf"],"tags":["panic","numeric-conversion","grid-sample","rust"],"backgroundTag":"numcast-conversion-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"}