Spaces:
Sleeping
Sleeping
Test AOTI base example during app startup
Browse files- aoti_base_example.py +37 -0
- app.py +13 -0
aoti_base_example.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Modified from https://docs.pytorch.org/tutorials/recipes/torch_export_aoti_python.html
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import torch
|
| 7 |
+
import torch._inductor
|
| 8 |
+
from torchvision.models import ResNet18_Weights, resnet18
|
| 9 |
+
|
| 10 |
+
model = resnet18(weights=ResNet18_Weights.DEFAULT)
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
package_path = os.path.join(os.getcwd(), "resnet18.pt2")
|
| 14 |
+
inductor_configs = {'max_autotune': True}
|
| 15 |
+
device = "cuda"
|
| 16 |
+
|
| 17 |
+
# Compile
|
| 18 |
+
with torch.inference_mode():
|
| 19 |
+
model = model.to(device=device)
|
| 20 |
+
example_inputs = (torch.randn(2, 3, 224, 224, device=device),)
|
| 21 |
+
exported_program = torch.export.export(
|
| 22 |
+
model,
|
| 23 |
+
example_inputs,
|
| 24 |
+
)
|
| 25 |
+
torch._inductor.aoti_compile_and_package(
|
| 26 |
+
exported_program,
|
| 27 |
+
package_path=package_path,
|
| 28 |
+
inductor_configs=inductor_configs
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Load
|
| 32 |
+
compiled_model = torch._inductor.aoti_load_package(package_path)
|
| 33 |
+
example_inputs = (torch.randn(2, 3, 224, 224, device=device),)
|
| 34 |
+
|
| 35 |
+
# Run
|
| 36 |
+
with torch.inference_mode():
|
| 37 |
+
output = compiled_model(example_inputs)
|
app.py
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
def greet(name):
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
# Try CUDA toolkit install + AOTI base example
|
| 5 |
+
from utils.cuda_toolkit import install_cuda_toolkit; install_cuda_toolkit()
|
| 6 |
+
import spaces
|
| 7 |
+
@spaces.GPU
|
| 8 |
+
def run():
|
| 9 |
+
import aoti_base_example
|
| 10 |
+
run()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Base demo
|
| 14 |
import gradio as gr
|
| 15 |
|
| 16 |
def greet(name):
|