Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.inference_mode function

PyTorch torch.inference_mode function


PyTorch torch reference manual 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:

Return Value:


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

   

print

(

"In inference_mode:"

,

y.

requires_grad

)

Inside the no_grad context

with

torch.

no_grad

(

)

:

    z

=

x *

2

   

print

(

"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

   

print

(

"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

(

)

   

print

(

"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

)

   

print

(

"Output shape:"

,

output.

shape

)

   

print

(

"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

)

print

(

"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

)

print

(

"Without any context manager:"

)

_

=

model

(

x

)

print

(

"nUsing no_grad:"

)

with

torch.

no_grad

(

)

:

    _

=

model

(

x

)

print

(

"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


Notes


PyTorch torch reference manual PyTorch torch reference manual