tracel-ai/burn · error

todo!("Default implementation for grid_sample_2d with {:?} u

Error message

todo!("Default implementation for grid_sample_2d with {:?} unimplemented", options.mode)

What it means

The reference implementation of `grid_sample_2d` supports only the Bilinear interpolation mode; other `InterpolateMode` variants hit a `_ => todo!` arm and panic. `grid_sample` in burn is partially implemented: only bilinear sampling has a default (reference) kernel.

Source

Thrown at crates/burn-backend/src/backend/ops/modules/grid_sample.rs:33

///   A [x = -1, y = -1] means top-left, and [x = 1, y = 1] means bottom-right
/// * `options` - Grid sampling options
///
/// # Returns
///
/// A tensor with shape (N, C, H_out, W_out)
pub fn float_grid_sample_2d_ref<B: Backend>(
    tensor: FloatTensor<B>,
    grid: FloatTensor<B>,
    options: GridSampleOptions,
) -> FloatTensor<B> {
    match options.mode {
        InterpolateMode::Bilinear => float_grid_sample_2d_bilinear::<B>(
            tensor,
            grid,
            options.padding_mode,
            options.align_corners,
        ),
        _ => todo!(
            "Default implementation for grid_sample_2d with {:?} unimplemented",
            options.mode
        ),
    }
}

/// Bilinear grid sampling implementation.
fn float_grid_sample_2d_bilinear<B: Backend>(
    tensor: FloatTensor<B>,
    grid: FloatTensor<B>,
    padding_mode: GridSamplePaddingMode,
    align_corners: bool,
) -> FloatTensor<B> {
    let n = tensor.shape()[0];
    let c = tensor.shape()[1];
    let h_in = tensor.shape()[2];
    let w_in = tensor.shape()[3];
    let h_out = grid.shape()[1];

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Set `options.mode = InterpolateMode::Bilinear` before calling grid_sample.
  2. Pre-process the sampling in a supported way (implement nearest sampling manually with gather/index ops).
  3. Export/convert the source model with bilinear grid sampling.
  4. Update burn or file an issue upstream if you need another mode.

Example fix

// before
let options = GridSampleOptions::new(InterpolateMode::Nearest, padding, align);
let out = grid.grid_sample_2d(input, options);
// after
let options = GridSampleOptions::new(InterpolateMode::Bilinear, padding, align);
let out = grid.grid_sample_2d(input, options);
Defensive patterns

Strategy: validation

Validate before calling

use burn::tensor::module::grid_sample::{GridSampleOptions, InterpolateMode};
fn grid_sample_supported(o: &GridSampleOptions) -> bool {
    matches!(o.mode, InterpolateMode::Bilinear)
}

Type guard

fn is_bilinear(mode: &InterpolateMode) -> bool { matches!(mode, InterpolateMode::Bilinear) }

Prevention

When it happens

Trigger: Calling `Tensor::grid_sample` (or the module op) with `GridSampleOptions` whose `mode` is not Bilinear (e.g. Nearest or bicubic), or loading an ONNX model whose GridSample node specifies a non-bilinear mode.

Common situations: Porting ONNX/PyTorch GridSample models that use `mode='nearest'` or 'bicubic'; copy-pasting options from a PyTorch implementation using nearest-neighbor sampling; experimental features enabled that change interpolation mode.

Related errors


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