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

PyTorch torch.compile function

PyTorch torch.compile function


PyTorch torch reference manual PyTorch torch reference manual

torch.compile is a function in PyTorch used to compile and optimize a model, which can significantly improve its runtime performance.

Function definition

torch.compile(model, *, mode="default", backend="inductor", fullgraph=False, dynamic=None, options=None, disable=False)


Usage examples

Example

import

torch

Define a simple model

class

SimpleModel

(

torch.

nn

.

Module

)

:

   

def

init

(

self

)

:

       

super

(

)

.

init

(

)

       

self

.

linear

=

torch.

nn

.

Linear

(

10

,

10

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

linear

(

x

)

model

=

SimpleModel

(

)

Compile the model

compiled_model

=

torch.

compile

(

model

)

Perform inference with the compiled model

x

=

torch.

randn

(

1

,

10

)

output

=

compiled_model

(

x

)

print

(

output.

shape

)


PyTorch torch reference manual PyTorch torch reference manual