cbensimon HF Staff commited on
Commit
8012ef2
·
1 Parent(s): 4e717d6

Load constants

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -15,6 +15,8 @@ import gradio as gr
15
  import spaces
16
  import torch
17
  import torch._inductor
 
 
18
  from torchvision.models import ResNet18_Weights, resnet18
19
 
20
 
@@ -33,24 +35,29 @@ def compile_model():
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)
 
15
  import spaces
16
  import torch
17
  import torch._inductor
18
+ from torch.export.pt2_archive._package import AOTICompiledModel
19
+ from torch.export.pt2_archive._package_weights import Weights
20
  from torchvision.models import ResNet18_Weights, resnet18
21
 
22
 
 
35
  model,
36
  example_inputs,
37
  )
38
+ artifacts = torch._inductor.aot_compile(exported_program.module(), *exported_program.example_inputs, options={
39
+ 'aot_inductor.package_constants_in_so': False,
40
+ 'aot_inductor.package_constants_on_disk': True,
41
+ 'aot_inductor.package': True,
42
+ 'max_autotune': True,
43
+ })
44
+ weights, = (artifact for artifact in artifacts if isinstance(artifact, Weights))
45
+ weights_: dict[str, torch.Tensor] = {}
46
+ for name in weights:
47
+ tensor, _properties = weights.get_weight(name)
48
+ tensor_ = torch.empty_like(tensor, device='cpu').pin_memory()
49
+ weights_[name] = tensor_.copy_(tensor).share_memory_()
50
+ return weights_
51
+
52
+ weights = compile_model()
53
+ del model
54
 
55
  @spaces.GPU
56
  def run_model():
57
+ compiled_model: AOTICompiledModel = torch._inductor.aoti_load_package(package_path)
58
+ compiled_model.load_constants(weights, check_full_update=True, user_managed=True)
59
  with torch.inference_mode():
60
  return str(compiled_model(example_inputs))
61
 
62
 
63
+ gr.Interface(run_model, [], 'text').launch(show_error=True)