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

PyTorch torch.is_inference_mode_enabled function

PyTorch torch.is_inference_mode_enabled function


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

Return Value:


Usage examples

Example 1: Basic usage

Example

import

torch

By default, inference mode is disabled

print

(

"Default state:"

,

torch.

is_inference_mode_enabled

(

)

)

Inside the inference_mode context

with

torch.

inference_mode

(

)

:

   

print

(

"In inference_mode:"

,

torch.

is_inference_mode_enabled

(

)

)

Restored after exit

print

(

"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

(

)

:

   

print

(

"Inside no_grad:"

)

   

print

(

"  is_grad_enabled:"

,

torch.

is_grad_enabled

(

)

)

   

print

(

"  is_inference_mode_enabled:"

,

torch.

is_inference_mode_enabled

(

)

)

In inference_mode

with

torch.

inference_mode

(

)

:

   

print

(

"In inference_mode:"

)

   

print

(

"  is_grad_enabled:"

,

torch.

is_grad_enabled

(

)

)

   

print

(

"  is_inference_mode_enabled:"

,

torch.

is_inference_mode_enabled

(

)

)

Default state

print

(

"Default state:"

)

print

(

"  is_grad_enabled:"

,

torch.

is_grad_enabled

(

)

)

print

(

"  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

(

)

:

       

print

(

"Optimize using inference mode"

)

       

Optimized computation in inference mode

       

return

model

(

x

)

   

elif

torch.

is_grad_enabled

(

)

:

       

print

(

"Training mode"

)

       

return

model

(

x

)

   

else

:

       

print

(

"eval mode"

)

       

return

model

(

x

)

model

=

nn.

Linear

(

10

,

5

)

x

=

torch.

randn

(

5

,

10

)

Test different states

print

(

"=== Training mode ==="

)

with

torch.

enable_grad

(

)

:

    result

=

forward_pass

(

x

,

model

)

print

(

"n=== Inference mode ==="

)

with

torch.

inference_mode

(

)

:

    result

=

forward_pass

(

x

,

model

)

print

(

"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

)

print

(

"=== Training mode ==="

)

with

torch.

enable_grad

(

)

:

    _

=

layer

(

x

)

print

(

"n=== Inference mode ==="

)

with

torch.

inference_mode

(

)

:

    _

=

layer

(

x

)

print

(

"n=== eval mode ==="

)

with

torch.

no_grad

(

)

:

    _

=

layer

(

x

)

The output is:

=== 训练模式 ===

训练模式



=== 推理模式 ===

推理模式:使用更高效的计算方式



=== eval 模式 ===

eval 模式


Related functions


Notes


PyTorch torch reference manual PyTorch torch reference manual