tracel-ai/burn · error

grid_sample_2d: unsupported dtype {:?}

Error message

grid_sample_2d: unsupported dtype {:?}

What it means

grid_sample_2d dispatches to a generic implementation only for float dtypes (F32, F64, F16, BF16). Integer dtypes have no grid-sample meaning in this backend, so any other dtype panics with the unsupported dtype message.

Source

Thrown at crates/burn-flex/src/ops/grid_sample.rs:42

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,
) -> FlexTensor
where
    T: Float + Element + bytemuck::Pod,
{
    let t_shape = tensor.layout().shape();
    let g_shape = grid.layout().shape();

    assert_eq!(t_shape.num_dims(), 4, "grid_sample_2d: input must be 4D");
    assert_eq!(g_shape.num_dims(), 4, "grid_sample_2d: grid must be 4D");
    assert_eq!(g_shape[3], 2, "grid_sample_2d: grid last dim must be 2");
    assert_eq!(

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast the input tensor (and grid, if needed) to a float dtype: tensor.cast::<f32>()
  2. Normalize integer image data to [0,1] floats before grid sampling
  3. Add a dtype check/assert before the call to fail early with a clearer message
  4. Check the pipeline for an accidental int cast before grid_sample_2d

Example fix

// before
let out = backend.grid_sample_2d(image_u8, grid, options); // panic: unsupported dtype U8
// after
let img_f32 = image_u8.cast::<f32>() / 255f32;
let out = backend.grid_sample_2d(img_f32, grid, options);
Defensive patterns

Strategy: validation

Validate before calling

// before calling grid_sample_2d
match tensor.dtype() {
    DType::F32 | DType::F64 | DType::F16 | DType::BF16 => {},
    other => tensor = tensor.cast::<f32>(), // or reject
}

Type guard

fn is_float_dtype(d: DType) -> bool {
    matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16)
}

Prevention

When it happens

Trigger: Calling grid_sample_2d with a tensor whose DType is I64, I32, U8, etc. — e.g. a uint8 image tensor passed straight in.

Common situations: Passing a quantized/uint8 image tensor directly instead of normalizing to float first; porting code from frameworks where grid_sample implicitly casts; dtype drift after a pipeline refactor introduced an int tensor.

Related errors


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