PyTorch torch.inference_mode function
PyTorch torch reference manual
torch.inference_mode A context manager in PyTorch for inference mode. It is more torch.no_grad strict than the alternative, disabling not only gradient computation but also all tracking by the autograd engine.
This is more efficient than the alternative during model inference, no_grad further reducing memory usage and improving inference speed.
Function definition
torch.inference_mode(mode=True)
Parameters:
mode(bool, optional): If set toTrue(the default), inference mode is enabled; if set toFalse, inference mode is exited. Defaults toTrue.
Return Value:
- Returns a context manager in which gradients and autograd are disabled.
Usage examples
Example 1: Basic usage
Example
import
torch
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
,
requires_grad
=
True
)
Inside the inference_mode context
with
torch.
inference_mode
(
)
:
y
=
x *
2
(
"In inference_mode:"
,
y.
requires_grad
)
Inside the no_grad context
with
torch.
no_grad
(
)
:
z
=
x *
2
(
"Inside no_grad:"
,
z.
requires_grad
)
The output is:
在 inference_mode 中: False
在 no_grad 中: False
Example 2: Compare no_grad and inference_mode
Example
import
torch
Creating tensors
x
=
torch.
randn
(
100
,
100
)
In inference_mode
with
torch.
inference_mode
(
)
:
perform extensive computation
for
_
in
range
(
10
)
:
x
=
torch.
mm
(
x
,
x
)
Even after computation completes, tensors created in the context cannot be used for backpropagation
result
=
x.
sum
(
)
Check whether it can be converted to a tensor that requires gradients
(
"In inference_mode:"
,
result.
is_leaf
)
Do the same computation in no_grad
x2
=
torch.
randn
(
100
,
100
)
with
torch.
no_grad
(
)
:
for
_
in
range
(
10
)
:
x2
=
torch.
mm
(
x2
,
x2
)
result2
=
x2.
sum
(
)
(
"Inside no_grad:"
,
result2.
is_leaf
)
The output is:
在 inference_mode 中: False
在 no_grad 中: True
Example 3: Model inference
Example
import
torch
import
torch.
nn
as
nn
Define a simple model
model
=
nn.
Sequential
(
nn.
Linear
(
10
,
20
)
,
nn.
ReLU
(
)
,
nn.
Linear
(
20
,
5
)
)
model.
eval
(
)
Create input data
x
=
torch.
randn
(
100
,
10
)
Perform inference using inference_mode
with
torch.
inference_mode
(
)
:
output
=
model
(
x
)
(
"Output shape:"
,
output.
shape
)
(
"Output requires_grad:"
,
output.
requires_grad
)
You can also use it as a decorator
@
torch.
inference_mode
(
)
def
predict
(
x
)
:
return
model
(
x
)
result
=
predict
(
x
)
(
"Decorator style - output shape:"
,
result.
shape
)
The output is:
输出形状: torch.Size([100, 5])
输出 requires_grad: False
装饰器方式 - 输出形状: torch.Size([100, 5])
Example 4: Memory optimization comparison
Example
import
torch
import
torch.
nn
as
nn
model
=
nn.
Sequential
(
nn.
Linear
(
1000
,
1000
)
,
nn.
ReLU
(
)
,
nn.
Linear
(
1000
,
1000
)
,
nn.
ReLU
(
)
,
nn.
Linear
(
1000
,
10
)
)
Test memory usage in different modes
x
=
torch.
randn
(
50
,
1000
)
(
"Without any context manager:"
)
_
=
model
(
x
)
(
"nUsing no_grad:"
)
with
torch.
no_grad
(
)
:
_
=
model
(
x
)
(
"nUsing inference_mode:"
)
with
torch.
inference_mode
(
)
:
_
=
model
(
x
)
Use inference_mode It can further optimize memory because it completely disables the autograd engine.
Related functions
torch.no_grad(): Disables gradient computation but still retains some autograd functionality.torch.enable_grad(): Enables gradient computation.torch.is_inference_mode_enabled(): Checks whether inference mode is enabled.
Notes
inference_modecompared tono_gradStricter, disabling more functionality.- in
inference_modeTensors created in it are marked as non-leaf nodes and cannot be used for backpropagation. - It is recommended to use it during model inference and evaluation
inference_modefor the best performance. inference_modeIt cannot be used withno_gradin nested mode.