tracel-ai/burn · error
an autodiff float primitive must have an enabled autodiff co
Error message
an autodiff float primitive must have an enabled autodiff context
What it means
In q_matmul_fq_arms, when the lhs float tensor is stored as an Autodiff primitive (DispatchTensorKind::Autodiff), the code requires the dispatch autodiff context to be DispatchAutodiffContext::Enabled so it can unwrap the gradation strategy. If the context is Disabled while the tensor is an autodiff primitive, the invariant is broken and burn panics: an autodiff-wrapped tensor cannot be used without its gradient-tracking context.
Source
Thrown at crates/burn-dispatch/src/ops/qtensor.rs:125
TensorPrimitive::QFloat(rhs.quantized()),
);
wrap_q_matmul_autodiff!($Backend, output, strategy)
})
}
#[cfg(not(feature = "autodiff"))]
DispatchAutodiffContext::Enabled(_) => {
panic!("autodiff context requires the `autodiff` feature")
}
}
}
)*
#[cfg(feature = "autodiff")]
(DispatchTensorKind::Autodiff(lhs), rhs) => match (*lhs, rhs) {
$(
#[cfg($cfg)]
(DispatchTensorKind::$Backend(lhs), DispatchTensorKind::$Backend(rhs)) => {
let DispatchAutodiffContext::Enabled(strategy) = $autodiff else {
panic!("an autodiff float primitive must have an enabled autodiff context")
};
with_autodiff_backend!($Backend, strategy, |B| {
let output = B::q_matmul(
TensorPrimitive::Float(lhs.autodiff()),
TensorPrimitive::QFloat(rhs.quantized()),
);
wrap_q_matmul_autodiff!($Backend, output, strategy)
})
}
)*
#[allow(unreachable_patterns)]
_ => panic!("q_matmul inputs are on different backends"),
},
#[allow(unreachable_patterns)]
_ => panic!("q_matmul inputs are on different backends"),
}
}};
}View on GitHub (pinned to d16f7ba2ed)
Solutions
- Enable the autodiff context (gradation) around the matmul so the context matches the tensor primitive
- Use the tensor's inner float value (detach / .inner()) when working outside a gradation context
- Ensure tensors produced by an Autodiff backend are only consumed within that same context
- Rebuild the tensor through the same backend/context chain that created it
Example fix
// before let out = dispatch::q_matmul(autodiff_lhs, q_rhs, DispatchAutodiffContext::Disabled); // after let out = dispatch::q_matmul(autodiff_lhs, q_rhs, DispatchAutodiffContext::Enabled(strategy));
Defensive patterns
Strategy: type-guard
Validate before calling
fn autodiff_tensor_in_enabled_ctx(t: &DispatchTensor, ctx: &DispatchAutodiffContext) -> bool {
!matches!(t.kind, DispatchTensorKind::Autodiff(_))
|| matches!(ctx, DispatchAutodiffContext::Enabled(_))
} Type guard
fn is_autodiff(t: &DispatchTensor) -> bool {
matches!(t.kind, DispatchTensorKind::Autodiff(_))
} Prevention
- Only consume Autodiff-kind tensors inside a gradation (Enabled) context
- Detach to the inner float tensor before crossing into a no-grad scope
- Never hand-construct DispatchTensors with a Disabled context for Autodiff kinds
- Centralize context creation so it always matches the tensors it operates on
When it happens
Trigger: Calling q_matmul where lhs.kind is DispatchTensorKind::Autodiff but the autodiff argument is DispatchAutodiffContext::Disabled — e.g. an autodiff tensor from a gradation-enabled scope used inside a context that was constructed with autodiff disabled.
Common situations: Using a tensor returned from an Autodiff backend scope inside a plain (non-grad) dispatch scope; constructing a DispatchTensor manually with mismatched autodiff context; caching autodiff tensors across contexts in a module or server that toggles gradation.
Related errors
- Autodiff float tensor is on the wrong backend (expected {bac
- Expected autodiff-wrapped float tensor for backend {backend}
- Autodiff should not wrap an autodiff device.
- an autodiff float primitive must have an enabled autodiff co
- autodiff context requires the `autodiff` feature
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/0a6380d39dfcde43.
Report an issue: GitHub.