eshanized commited on
Commit
83b28fa
·
verified ·
1 Parent(s): e92cf18

add M31 v5 standalone inference runtime

Browse files
Files changed (1) hide show
  1. m31_runtime.py +31 -0
m31_runtime.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, json, torch
2
+ from huggingface_hub import hf_hub_download
3
+ from safetensors.torch import load_file
4
+ from tokenizers import Tokenizer
5
+ from modeling_m31 import M31Model
6
+ from generation_m31 import generate
7
+
8
+ REPO_ID = os.environ.get('M31_REPO_ID', 'eshanized/M31Tesla')
9
+ CHECKPOINTS = {
10
+ 'pretrain': 'experiments/M31-Python-Agent-220M-v5/checkpoints/pretrain/step-00000733',
11
+ 'sft': 'experiments/M31-Python-Agent-220M-v5/checkpoints/sft/step-00000123',
12
+ 'agent': 'experiments/M31-Python-Agent-220M-v5/checkpoints/agent/step-00000123',
13
+ 'repair': 'experiments/M31-Python-Agent-220M-v5/checkpoints/repair/step-00000123',
14
+ }
15
+
16
+ def load_stage(stage='repair', device=None):
17
+ if stage not in CHECKPOINTS:
18
+ raise ValueError(f'Unknown stage {stage!r}: {sorted(CHECKPOINTS)}')
19
+ device = device or ('cuda' if torch.cuda.is_available() else 'cpu')
20
+ prefix = CHECKPOINTS[stage]
21
+ config_path = hf_hub_download(REPO_ID, f'{prefix}/config.json')
22
+ weights_path = hf_hub_download(REPO_ID, f'{prefix}/model.safetensors')
23
+ tokenizer_path = hf_hub_download(REPO_ID, 'tokenizer.json')
24
+ with open(config_path, 'r', encoding='utf-8') as f:
25
+ config = json.load(f)
26
+ model = M31Model()
27
+ state = load_file(weights_path, device='cpu')
28
+ model.load_state_dict(state, strict=True)
29
+ model.to(device).eval()
30
+ tokenizer = Tokenizer.from_file(tokenizer_path)
31
+ return model, tokenizer, config