tracel-ai/burn · error

todo!("grid_sample_2d with {:?} mode is not implemented", op

Error message

todo!("grid_sample_2d with {:?} mode is not implemented", options.mode)

What it means

grid_sample_2d in the ndarray backend (crates/burn-ndarray/src/ops/grid_sample.rs:31) only implements InterpolateMode::Bilinear; any other interpolation mode (e.g. Nearest, Bicubic) hits a todo! panic. The grid itself is prepared, but the mode gate runs before any computation.

Source

Thrown at crates/burn-ndarray/src/ops/grid_sample.rs:31

///
/// # Arguments
///
/// * `tensor` - The tensor being sampled from, must be contiguous with shape (N, C, H_in, W_in)
/// * `grid` - A tensor of locations, with shape (N, H_out, W_out, 2). Values are [-1, 1].
///   A [x = -1, y = -1] means top-left, and [x = 1, y = 1] means bottom-right
/// * `options` - Grid sampling options (mode, padding_mode, align_corners)
///
/// # Returns
///
/// A tensor with shape (N, C, H_out, W_out)
pub(crate) fn grid_sample_2d<E: FloatNdArrayElement>(
    tensor: SharedArray<E>,
    grid: SharedArray<E>,
    options: GridSampleOptions,
) -> SharedArray<E> {
    match options.mode {
        InterpolateMode::Bilinear => (),
        _ => todo!(
            "grid_sample_2d with {:?} mode is not implemented",
            options.mode
        ),
    }

    let tensor = tensor.into_dimensionality::<ndarray::Ix4>().unwrap();
    let grid = grid.into_dimensionality::<ndarray::Ix4>().unwrap();

    let (batch_size, channels, height_in, width_in) = tensor.dim();
    let (b, height_out, width_out, d) = grid.dim();
    assert!(batch_size == b);
    assert!(2 == d);

    let mut output = Array4::zeros((batch_size, channels, height_out, width_out));
    let unsafe_shared_out = UnsafeSharedRef::new(&mut output);

    let sample_count = batch_size * channels * height_out * width_out;
    let strides = (

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Switch GridSampleOptions to InterpolateMode::Bilinear if acceptable for your model.
  2. Use a backend with full grid_sample support (e.g. cubecl/CUDA) for this op.
  3. Implement nearest mode in burn-ndarray's grid_sample and upstream a PR.

Example fix

// before
let opts = GridSampleOptions::new(InterpolateMode::Nearest, PaddingMode::Zeros);
let out = grid.grid_sample(opts);
// after
let opts = GridSampleOptions::new(InterpolateMode::Bilinear, PaddingMode::Zeros);
let out = grid.grid_sample(opts);
Defensive patterns

Strategy: validation

Validate before calling

assert!(matches!(options.mode, InterpolateMode::Bilinear), "ndarray backend grid_sample only supports Bilinear");

Prevention

When it happens

Trigger: Calling Tensor::grid_sample (grid_sample_2d) with GridSampleOptions whose mode is anything other than Bilinear while using the NdArray backend.

Common situations: Porting a model that uses nearest-neighbor grid sampling (common in segmentation/stylization models) to CPU/ndarray; code that works on CUDA/WGPU backends panics on ndarray.

Related errors


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