rahular commited on
Commit
8521c2a
·
verified ·
1 Parent(s): 3fab01c

Create hotpatch_vllm.py

Browse files
Files changed (1) hide show
  1. hotpatch_vllm.py +114 -0
hotpatch_vllm.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import sys
5
+ import subprocess
6
+ from pathlib import Path
7
+ from urllib.request import urlopen, Request
8
+
9
+
10
+ HF_BLOB_URL = "https://huggingface.co/sarvamai/sarvam2-30b/blob/main/sarvam.py"
11
+
12
+ NEW_LINES = [
13
+ ' "SarvamMoEForCausalLM": ("sarvam", "SarvamMoEForCausalLM"),\n',
14
+ ' "SarvamMLAForCausalLM": ("sarvam", "SarvamMLAForCausalLM"),\n',
15
+ ]
16
+
17
+
18
+ def run(cmd: list[str]) -> None:
19
+ print(f"+ {' '.join(cmd)}")
20
+ subprocess.check_call(cmd)
21
+
22
+
23
+ def pip_install_vllm() -> None:
24
+ run([sys.executable, "-m", "pip", "install", "vllm==0.15.0"])
25
+
26
+
27
+ def find_vllm_dir() -> Path:
28
+ import vllm # type: ignore
29
+
30
+ vllm_dir = Path(vllm.__file__).resolve().parent
31
+ print(f"Detected vLLM package dir: {vllm_dir}")
32
+ return vllm_dir
33
+
34
+
35
+ def patch_text_generation_models(registry_path: Path) -> None:
36
+ if not registry_path.exists():
37
+ raise FileNotFoundError(f"registry.py not found at: {registry_path}")
38
+
39
+ text = registry_path.read_text(encoding="utf-8")
40
+ lines = text.splitlines(keepends=True)
41
+
42
+ # Idempotency: if both keys already present, do nothing
43
+ if (
44
+ any('"SarvamMoEForCausalLM"' in l for l in lines)
45
+ and any('"SarvamMLAForCausalLM"' in l for l in lines)
46
+ ):
47
+ print("registry.py already contains Sarvam entries. Skipping patch.")
48
+ return
49
+
50
+ # Find the start of the _TEXT_GENERATION_MODELS dict
51
+ start_idx = None
52
+ for i, line in enumerate(lines):
53
+ if line.strip() == "_TEXT_GENERATION_MODELS = {":
54
+ start_idx = i
55
+ break
56
+
57
+ if start_idx is None:
58
+ raise RuntimeError(
59
+ "Could not find '_TEXT_GENERATION_MODELS = {' in registry.py. "
60
+ "vLLM version/layout may differ."
61
+ )
62
+
63
+ # Find the matching closing brace for that dict using brace depth
64
+ depth = 0
65
+ end_idx = None
66
+ for j in range(start_idx, len(lines)):
67
+ depth += lines[j].count("{")
68
+ depth -= lines[j].count("}")
69
+ if j > start_idx and depth == 0:
70
+ end_idx = j
71
+ break
72
+
73
+ if end_idx is None:
74
+ raise RuntimeError("Failed to find end of _TEXT_GENERATION_MODELS dict (brace matching).")
75
+
76
+ # Insert new entries just before the closing brace line
77
+ insert_at = end_idx
78
+ lines[insert_at:insert_at] = NEW_LINES
79
+
80
+ registry_path.write_text("".join(lines), encoding="utf-8")
81
+ print(f"Patched _TEXT_GENERATION_MODELS in: {registry_path}")
82
+
83
+
84
+ def download_sarvam_py(dst: Path) -> None:
85
+ # Use /raw/ to download file contents, not HTML
86
+ raw_url = HF_BLOB_URL.replace("/blob/", "/raw/")
87
+ print(f"Downloading sarvam.py from: {raw_url}")
88
+
89
+ req = Request(raw_url, headers={"User-Agent": "vllm-hotpatch-script"})
90
+ with urlopen(req) as resp:
91
+ data = resp.read()
92
+
93
+ dst.parent.mkdir(parents=True, exist_ok=True)
94
+ dst.write_bytes(data)
95
+ print(f"Wrote: {dst}")
96
+
97
+
98
+ def main() -> None:
99
+ pip_install_vllm()
100
+
101
+ vllm_dir = find_vllm_dir()
102
+ registry_path = vllm_dir / "model_executor" / "models" / "registry.py"
103
+ sarvam_path = vllm_dir / "model_executor" / "models" / "sarvam.py"
104
+
105
+ patch_text_generation_models(registry_path)
106
+ download_sarvam_py(sarvam_path)
107
+
108
+ print("\nDone.")
109
+ print(f"- Registry patched: {registry_path}")
110
+ print(f"- Sarvam module installed: {sarvam_path}")
111
+
112
+
113
+ if __name__ == "__main__":
114
+ main()