trioskosmos commited on
Commit
9cd6a8f
·
verified ·
1 Parent(s): 0f9c2a1

Upload ai/utils/export_model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ai/utils/export_model.py +34 -0
ai/utils/export_model.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ import torch
5
+
6
+ # Add project root to path
7
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
8
+
9
+ from ai.models.training_config import INPUT_SIZE, POLICY_SIZE
10
+ from ai.training.train import AlphaNet
11
+
12
+
13
+ def export_to_torchscript(model_path, output_path):
14
+ device = torch.device("cpu") # Export on CPU for cross-device compatibility
15
+ checkpoint = torch.load(model_path, map_location=device)
16
+ state_dict = (
17
+ checkpoint["model_state"] if isinstance(checkpoint, dict) and "model_state" in checkpoint else checkpoint
18
+ )
19
+
20
+ model = AlphaNet(policy_size=POLICY_SIZE).to(device)
21
+ model.load_state_dict(state_dict)
22
+ model.eval()
23
+
24
+ # Create dummy input for tracing
25
+ dummy_input = torch.randn(1, INPUT_SIZE)
26
+
27
+ # Trace the model
28
+ traced_model = torch.jit.trace(model, dummy_input)
29
+ traced_model.save(output_path)
30
+ print(f"Model successfully exported to {output_path}")
31
+
32
+
33
+ if __name__ == "__main__":
34
+ export_to_torchscript("ai/models/alphanet_best.pt", "ai/models/alphanet_traced.pt")