tracel-ai/burn · error

autodiff context requires the `autodiff` feature

Error message

autodiff context requires the `autodiff` feature

What it means

In the Qtensor dispatch macro, a `DispatchAutodiffContext::Enabled` was provided while the `autodiff` cargo feature is disabled. Since the compiled code cannot handle autodiff contexts without the feature, it panics that the autodiff context requires the `autodiff` feature.

Source

Thrown at crates/burn-dispatch/src/ops/qtensor.rs:41

                    kind: DispatchTensorKind::$Backend(BackendTensor::Float(output)),
                    autodiff: DispatchAutodiffContext::Disabled,
                }),
                #[cfg(feature = "autodiff")]
                DispatchAutodiffContext::Enabled(strategy) => {
                    with_autodiff_backend!($Backend, strategy, |AD| {
                        TensorPrimitive::Float(DispatchTensor {
                            kind: DispatchTensorKind::Autodiff(alloc::boxed::Box::new(
                                DispatchTensorKind::$Backend(BackendTensor::Autodiff(
                                    <AD as burn_backend::AutodiffBackend>::from_inner(output),
                                )),
                            )),
                            autodiff: DispatchAutodiffContext::Enabled(strategy),
                        })
                    })
                }
                #[cfg(not(feature = "autodiff"))]
                DispatchAutodiffContext::Enabled(_) => {
                    panic!("autodiff context requires the `autodiff` feature")
                }
            },
        }
    }};
}

#[cfg(feature = "autodiff")]
macro_rules! wrap_q_matmul_autodiff {
    ($Backend:ident, $output:expr, $strategy:expr) => {{
        match $output {
            TensorPrimitive::QFloat(output) => TensorPrimitive::QFloat(DispatchTensor {
                kind: DispatchTensorKind::$Backend(BackendTensor::Quantized(output)),
                autodiff: DispatchAutodiffContext::Enabled($strategy),
            }),
            TensorPrimitive::Float(output) => TensorPrimitive::Float(DispatchTensor {
                kind: DispatchTensorKind::Autodiff(alloc::boxed::Box::new(
                    DispatchTensorKind::$Backend(BackendTensor::Autodiff(output)),
                )),

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Rebuild with the `autodiff` feature enabled (e.g. `cargo build --features autodiff`)
  2. Or pass `DispatchAutodiffContext::Disabled` when autodiff is not needed
  3. Avoid routing autodiff contexts into quantized-tensor code paths in inference-only builds

Example fix

// before
cargo build --features qtensor
DispatchAutodiffContext::Enabled(strategy) // panics
// after
cargo build --features qtensor,autodiff
// or pass DispatchAutodiffContext::Disabled
Defensive patterns

Strategy: validation

Validate before calling

#[cfg(not(feature = "autodiff"))]
compile_error!("this crate requires the `autodiff` feature when autodiff contexts are used");
// or at runtime: only construct Enabled contexts when cfg(feature = "autodiff")

Type guard

fn allows_ad_ctx() -> bool { cfg!(feature = "autodiff") }

Prevention

When it happens

Trigger: Calling a quantized tensor dispatch API with an Enabled autodiff context in a build without `--features autodiff` (the `#[cfg(not(feature = "autodiff")]` arm).

Common situations: Library consumers enabling quantization features but not `autodiff`; calling through a generic wrapper that always constructs an Enabled context.

Related errors


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