PyTorch torch.compile function
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
)
(
output.
shape
)