tracel-ai/burn · error

deform_conv2d: unsupported dtype {:?}

Error message

deform_conv2d: unsupported dtype {:?}

What it means

deform_conv2d in the burn-flex backend computes by casting the inputs to f32, running the f32 deformable-convolution, then casting back to the original float dtype. The dispatch only recognizes F32/F64/F16/BF16; any other dtype panics. Because the op round-trips through f32, integer dtypes were never considered valid inputs.

Source

Thrown at crates/burn-flex/src/ops/module.rs:143

                cast_from_f32(result, f16::from_f32)
            }
            DType::BF16 => {
                use burn_std::bf16;
                let result = deform_conv::deform_conv2d_f32(
                    cast_to_f32(x, bf16::to_f32),
                    cast_to_f32(offset, bf16::to_f32),
                    cast_to_f32(weight, bf16::to_f32),
                    mask.map(|m| cast_to_f32(m, bf16::to_f32)),
                    bias.map(|b| cast_to_f32(b, bf16::to_f32)),
                    options.stride,
                    options.padding,
                    options.dilation,
                    options.weight_groups,
                    options.offset_groups,
                );
                cast_from_f32(result, bf16::from_f32)
            }
            dtype => panic!("deform_conv2d: unsupported dtype {:?}", dtype),
        }
    }

    fn deform_conv2d_backward(
        x: FloatTensor<Flex>,
        offset: FloatTensor<Flex>,
        weight: FloatTensor<Flex>,
        mask: Option<FloatTensor<Flex>>,
        bias: Option<FloatTensor<Flex>>,
        output_grad: FloatTensor<Flex>,
        options: DeformConvOptions<2>,
    ) -> DeformConv2dBackward<Flex> {
        let (x_grad, offset_grad, weight_grad, mask_grad, bias_grad) = match x.dtype() {
            DType::F32 => deform_conv::deform_conv2d_backward_f32(
                x,
                offset,
                weight,
                mask,

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast every input (x, offset, mask) to a float dtype before deform_conv2d, e.g. offset.cast(DType::F32).
  2. Verify each tensor's dtype with .dtype(); usually only one of the inputs (often offset/mask) is the culprit.
  3. Insert a .float()/cast step right after the subnetwork that produces the offsets so the grid stays float end-to-end.
  4. If integer offsets are by design, convert them in the model definition (e.g. grid_sample-style offsets computed in f32).

Example fix

// before
let out = deform_conv2d(x, int_offset, mask, weight, bias, options);
// panic: deform_conv2d: unsupported dtype I32

// after
let offset = int_offset.cast(burn::tensor::DType::F32);
let out = deform_conv2d(x, offset, mask, weight, bias, options);
Defensive patterns

Strategy: validation

Validate before calling

for t in [&x, &offset, &mask] {
    assert!(matches!(t.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16), "deform_conv2d needs float inputs, got {:?}", t.dtype());
}
let out = deform_conv2d(x, offset, mask, weight, bias, options);

Type guard

fn all_float(dtypes: [DType; 3]) -> bool {
    dtypes.iter().all(|d| matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16))
}

Try / catch

let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| deform_conv2d(x.clone(), offset.clone(), mask.clone(), w.clone(), b.clone(), opts.clone())))
    .unwrap_or_else(|_| deform_conv2d(x.cast(DType::F32), offset.cast(DType::F32), mask.cast(DType::F32), w, b, opts));

Prevention

When it happens

Trigger: Calling deform_conv2d with x, offset, or mask tensors whose dtype is not one of F32/F64/F16/BF16 (e.g. I32 offset grid, U8 mask); feeding integer coordinate tensors as the offset input.

Common situations: Building deformable attention/conv modules (e.g. DCN, Deformable DETR) where the sampling grid offsets are kept as integers; converting an ONNX deform-conv graph whose offset outputs are int; exporting pipelines that change dtypes silently.

Related errors


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