tracel-ai/burn · error
grid_sample_2d: {:?} mode is not supported
Error message
grid_sample_2d: {:?} mode is not supported What it means
burn-flex's grid_sample_2d only implements Bilinear and Nearest interpolation modes. Any other InterpolateMode (e.g. bicubic, as offered by other backends or future modes) hits this panic, because there is no implementation for it in this backend.
Source
Thrown at crates/burn-flex/src/ops/grid_sample.rs:31
use burn_std::{Bytes, Shape, bf16, f16};
use num_traits::{Float, NumCast};
use crate::{FlexTensor, Layout};
/// Grid sample 2D (bilinear and nearest-neighbor interpolation).
///
/// Input tensor shape: [N, C, H_in, W_in]
/// Grid shape: [N, H_out, W_out, 2] (x, y normalized to [-1, 1])
/// Output shape: [N, C, H_out, W_out]
pub fn grid_sample_2d(
tensor: FlexTensor,
grid: FlexTensor,
options: GridSampleOptions,
) -> FlexTensor {
match options.mode {
InterpolateMode::Bilinear | InterpolateMode::Nearest => {}
other => panic!("grid_sample_2d: {:?} mode is not supported", other),
}
let tensor = tensor.to_contiguous();
let grid = grid.to_contiguous();
match tensor.dtype() {
DType::F32 => grid_sample_2d_impl::<f32>(tensor, grid, options),
DType::F64 => grid_sample_2d_impl::<f64>(tensor, grid, options),
DType::F16 => grid_sample_2d_impl::<f16>(tensor, grid, options),
DType::BF16 => grid_sample_2d_impl::<bf16>(tensor, grid, options),
_ => panic!("grid_sample_2d: unsupported dtype {:?}", tensor.dtype()),
}
}
fn grid_sample_2d_impl<T>(
tensor: FlexTensor,
grid: FlexTensor,
options: GridSampleOptions,View on GitHub (pinned to d16f7ba2ed)
Solutions
- Set the interpolation mode to InterpolateMode::Bilinear (most common for grid_sample)
- Use InterpolateMode::Nearest if exact nearest-neighbor semantics are acceptable
- Implement or request bicubic support upstream in burn-flex, or switch to a backend that supports it
- Add a runtime assertion/whitelist of modes before constructing the options
Example fix
// before let options = GridSampleOptions::new(InterpolateMode::Cubic, PaddingMode::Zeros, true); let out = backend.grid_sample_2d(tensor, grid, options); // panic // after let options = GridSampleOptions::new(InterpolateMode::Bilinear, PaddingMode::Zeros, true); let out = backend.grid_sample_2d(tensor, grid, options);
Defensive patterns
Strategy: validation
Validate before calling
// before calling grid_sample_2d
match options.mode {
burn::tensor::InterpolateMode::Bilinear | burn::tensor::InterpolateMode::Nearest => {},
other => panic!("burn-flex grid_sample_2d does not support {:?}", other),
} Type guard
fn grid_sample_mode_supported(m: &InterpolateMode) -> bool {
matches!(m, InterpolateMode::Bilinear | InterpolateMode::Nearest)
} Prevention
- Restrict configurable interpolation modes to Bilinear/Nearest when targeting burn-flex
- Map bicubic requests to bilinear with a warning at config load time
When it happens
Trigger: Calling grid_sample_2d with GridSampleOptions whose mode is anything other than InterpolateMode::Bilinear or InterpolateMode::Nearest.
Common situations: Config copied from a PyTorch/torchvision model that used mode='bicubic'; a shared options struct set for another backend that supports more modes; a version change where a new InterpolateMode variant became available but burn-flex has not implemented it.
Related errors
- grid_sample_2d: unsupported dtype {:?}
- grid_sample_2d: NumCast::from({x:?}) to {:?} returned None
- capture tensor operations must run inside CaptureDevice::cap
- Capture tensors do not support autodiff
- Autodiff should not wrap an autodiff tensor.
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/07ddd4aa5e7aa116.
Report an issue: GitHub.