File size: 984 Bytes
8c2e0d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Modified from https://docs.pytorch.org/tutorials/recipes/torch_export_aoti_python.html
"""

import os
import torch
import torch._inductor
from torchvision.models import ResNet18_Weights, resnet18

model = resnet18(weights=ResNet18_Weights.DEFAULT)
model.eval()

package_path = os.path.join(os.getcwd(), "resnet18.pt2")
inductor_configs = {'max_autotune': True}
device = "cuda"

# Compile
with torch.inference_mode():
    model = model.to(device=device)
    example_inputs = (torch.randn(2, 3, 224, 224, device=device),)
    exported_program = torch.export.export(
        model,
        example_inputs,
    )
    torch._inductor.aoti_compile_and_package(
        exported_program,
        package_path=package_path,
        inductor_configs=inductor_configs
    )

# Load
compiled_model = torch._inductor.aoti_load_package(package_path)
example_inputs = (torch.randn(2, 3, 224, 224, device=device),)

# Run
with torch.inference_mode():
    output = compiled_model(example_inputs)