torch.autograd.is_inference_mode_enabled
function is_inference_mode_enabled(): booleanCheck if inference mode is currently enabled.
Returns whether the global inference mode is active. Inference mode is a stricter alternative to no_grad() that disables gradients and prevents tensors from being used in autograd later. This is useful for pure inference where you're certain the outputs won't be used for training.
Difference from is_grad_enabled():
- is_grad_enabled: only checks if gradients are enabled
- is_inference_mode_enabled: checks if inference mode is specifically active
Inference mode disables both gradients AND prevents later autograd use, while no_grad only disables gradient tracking but allows later autograd operations.
- Stricter than is_grad_enabled(): Both return false when enabled, but for different reasons
- Query-only: This function doesn't modify state, just returns current setting
- Affected by inference_mode(): Changed by torch.inference_mode() context
- Also affects gradients: Inference mode disables both gradients and inference status
- Not the same as no_grad: Inference mode prevents later autograd, no_grad doesn't
- For pure inference: Only use when you're absolutely sure outputs won't be trained
Returns
boolean– True if inference mode is currently active, false otherwiseExamples
// Check inference mode status
console.log(torch.is_inference_mode_enabled()); // false (default)
torch.inference_mode(() => {
console.log(torch.is_inference_mode_enabled()); // true
});
console.log(torch.is_inference_mode_enabled()); // false (restored)// Conditional behavior based on inference mode
function forward(model, input) {
if (torch.is_inference_mode_enabled()) {
// Pure inference: use fastest path
return model.forward_fast(input);
} else {
// Training/evaluation: need standard forward
return model.forward(input);
}
}// Verify mode transition
const in_inf_mode = torch.is_inference_mode_enabled();
console.assert(!in_inf_mode, 'Should not start in inference mode');
torch.inference_mode(() => {
console.assert(torch.is_inference_mode_enabled(), 'Should be in inference mode');
});See Also
- PyTorch torch.is_inference_mode_enabled()
- torch.inference_mode - Enable inference mode context
- torch.is_grad_enabled - Check if gradients enabled (different from inference mode)
- torch.no_grad - Disable gradients without preventing later autograd