Spaces:
Running
on
Zero
Running
on
Zero
Phase 1
Browse files- aoti_base_example.py +0 -37
- app.py +42 -11
aoti_base_example.py
DELETED
|
@@ -1,37 +0,0 @@
|
|
| 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
|
@@ -3,23 +3,54 @@
|
|
| 3 |
|
| 4 |
# Upgrade PyTorch
|
| 5 |
import os
|
| 6 |
-
os.system('pip install --upgrade torch torchvision')
|
| 7 |
|
| 8 |
# CUDA toolkit install
|
| 9 |
from utils.cuda_toolkit import install_cuda_toolkit; install_cuda_toolkit()
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@spaces.GPU
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def greet(name):
|
| 22 |
-
return "Hello " + name + "!!"
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Upgrade PyTorch
|
| 5 |
import os
|
| 6 |
+
os.system('pip install --upgrade --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu126 torch torchvision spaces')
|
| 7 |
|
| 8 |
# CUDA toolkit install
|
| 9 |
from utils.cuda_toolkit import install_cuda_toolkit; install_cuda_toolkit()
|
| 10 |
|
| 11 |
+
# Actual app.py
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
import spaces
|
| 16 |
+
import torch
|
| 17 |
+
import torch._inductor
|
| 18 |
+
from torchvision.models import ResNet18_Weights, resnet18
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
model = resnet18(weights=ResNet18_Weights.DEFAULT)
|
| 22 |
+
model.eval()
|
| 23 |
+
model.to('cuda')
|
| 24 |
+
|
| 25 |
+
package_path = os.path.join(os.getcwd(), 'resnet18.pt2')
|
| 26 |
+
inductor_configs = {'max_autotune': True}
|
| 27 |
+
example_inputs = (torch.randn(2, 3, 224, 224, device='cuda'),)
|
| 28 |
+
|
| 29 |
@spaces.GPU
|
| 30 |
+
def compile_model():
|
| 31 |
+
with torch.inference_mode():
|
| 32 |
+
exported_program = torch.export.export(
|
| 33 |
+
model,
|
| 34 |
+
example_inputs,
|
| 35 |
+
)
|
| 36 |
+
torch._inductor.aoti_compile_and_package(
|
| 37 |
+
exported_program,
|
| 38 |
+
package_path=package_path,
|
| 39 |
+
inductor_configs=inductor_configs
|
| 40 |
+
)
|
| 41 |
+
return "compiled"
|
| 42 |
|
| 43 |
+
@spaces.GPU
|
| 44 |
+
def run_model():
|
| 45 |
+
compiled_model = torch._inductor.aoti_load_package(package_path)
|
| 46 |
+
with torch.inference_mode():
|
| 47 |
+
return str(compiled_model(example_inputs))
|
| 48 |
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
gr.TabbedInterface([
|
| 51 |
+
gr.Interface(compile_model, [], "text", clear_btn=None, flagging_mode='never'),
|
| 52 |
+
gr.Interface(run_model, [], "text", clear_btn=None, flagging_mode='never'),
|
| 53 |
+
], [
|
| 54 |
+
"Compile",
|
| 55 |
+
"Run"
|
| 56 |
+
]).launch(show_error=True)
|