PyTorch torch.compile Function
PyTorch torch Reference Manual
torch.compileis a function in PyTorch used to compile and optimize models, which can significantly improve the model's runtime performance.
Function Definition
torch.compile(model, *, mode="default", backend="inductor", fullgraph=False, dynamic=None, options=None, disable=False)
Usage Example
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)
# Use the compiled model for inference
x = torch.randn(1, 10)
output = compiled_model(x)
print(output.shape)
# 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)
# Use the compiled model for inference
x = torch.randn(1, 10)
output = compiled_model(x)
print(output.shape)
Other Extensions