PyTorch torch.is_inference_mode_enabled function
PyTorch torch reference manual
torch.is_inference_mode_enabled A function in PyTorch for checking whether inference mode is currently enabled. It returns a boolean indicating whether the current context is inference_mode an inference-mode context.
This is very useful when writing code that needs to execute different logic based on the inference mode state.
Function definition
torch.is_inference_mode_enabled()
Parameters:
- No parameters.
Return Value:
- Returns a boolean: True if inference mode is currently enabled,
True; otherwise it returnsFalse.
Usage examples
Example 1: Basic usage
Example
import
torch
By default, inference mode is disabled
(
"Default state:"
,
torch.
is_inference_mode_enabled
(
)
)
Inside the inference_mode context
with
torch.
inference_mode
(
)
:
(
"In inference_mode:"
,
torch.
is_inference_mode_enabled
(
)
)
Restored after exit
(
"After exit:"
,
torch.
is_inference_mode_enabled
(
)
)
The output is:
默认状态: False
在 inference_mode 中: True
退出后: False
Example 2: Compare is_grad_enabled and is_inference_mode_enabled
Example
import
torch
Inside no_grad
with
torch.
no_grad
(
)
:
(
"Inside no_grad:"
)
(
" is_grad_enabled:"
,
torch.
is_grad_enabled
(
)
)
(
" is_inference_mode_enabled:"
,
torch.
is_inference_mode_enabled
(
)
)
In inference_mode
with
torch.
inference_mode
(
)
:
(
"In inference_mode:"
)
(
" is_grad_enabled:"
,
torch.
is_grad_enabled
(
)
)
(
" is_inference_mode_enabled:"
,
torch.
is_inference_mode_enabled
(
)
)
Default state
(
"Default state:"
)
(
" is_grad_enabled:"
,
torch.
is_grad_enabled
(
)
)
(
" is_inference_mode_enabled:"
,
torch.
is_inference_mode_enabled
(
)
)
The output is:
在 no_grad 中:
is_grad_enabled: False
is_inference_mode_enabled: False
在 inference_mode 中:
is_grad_enabled: False
is_inference_mode_enabled: True
Default state:
is_grad_enabled: True
is_inference_mode_enabled: False
Example 3: Use in conditional statements
Example
import
torch
import
torch.
nn
as
nn
def
forward_pass
(
x
,
model
)
:
"""Apply different optimizations based on inference mode"""
if
torch.
is_inference_mode_enabled
(
)
:
(
"Optimize using inference mode"
)
Optimized computation in inference mode
return
model
(
x
)
elif
torch.
is_grad_enabled
(
)
:
(
"Training mode"
)
return
model
(
x
)
else
:
(
"eval mode"
)
return
model
(
x
)
model
=
nn.
Linear
(
10
,
5
)
x
=
torch.
randn
(
5
,
10
)
Test different states
(
"=== Training mode ==="
)
with
torch.
enable_grad
(
)
:
result
=
forward_pass
(
x
,
model
)
(
"n=== Inference mode ==="
)
with
torch.
inference_mode
(
)
:
result
=
forward_pass
(
x
,
model
)
(
"n=== eval mode ==="
)
with
torch.
no_grad
(
)
:
result
=
forward_pass
(
x
,
model
)
The output is:
=== 训练模式 ===
训练模式
=== 推理模式 ===
使用推理模式优化
=== eval 模式 ===
eval 模式
Example 4: Detect in a custom module
Example
import
torch
import
torch.
nn
as
nn
class
OptimizedLayer
(
nn.
Module
)
:
def
init
(
self
)
:
super
(
)
.
init
(
)
self
.
weight
=
nn.
Parameter
(
torch.
randn
(
20
,
10
)
)
def
forward
(
self
,
x
)
:
Detect inference mode and apply optimizations
if
torch.
is_inference_mode_enabled
(
)
:
Inference mode: use more efficient computation
return
torch.
mm
(
x
,
self
.
weight
)
elif
torch.
is_grad_enabled
(
)
:
Training mode: retain gradient computation
return
torch.
mm
(
x
,
self
.
weight
)
else
:
eval mode: gradients are not needed but optimizations can be used
with
torch.
no_grad
(
)
:
return
torch.
mm
(
x
,
self
.
weight
)
layer
=
OptimizedLayer
(
)
x
=
torch.
randn
(
5
,
10
)
(
"=== Training mode ==="
)
with
torch.
enable_grad
(
)
:
_
=
layer
(
x
)
(
"n=== Inference mode ==="
)
with
torch.
inference_mode
(
)
:
_
=
layer
(
x
)
(
"n=== eval mode ==="
)
with
torch.
no_grad
(
)
:
_
=
layer
(
x
)
The output is:
=== 训练模式 ===
训练模式
=== 推理模式 ===
推理模式:使用更高效的计算方式
=== eval 模式 ===
eval 模式
Related functions
torch.inference_mode(): A context manager that enables inference mode.torch.no_grad(): A context manager that disables gradient computation.torch.is_grad_enabled(): Checks whether gradient computation is enabled.
Notes
is_inference_mode_enabledis a read-only function and does not change any state.- It is specifically designed to detect
inference_moderather thanno_grad. - in
inference_modecontext,is_grad_enabledalso returnsFalse, but not vice versa. - When writing general-purpose code, you can use this function to distinguish between different running modes.