Instructions to use sarvamai/sarvam-30b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use sarvamai/sarvam-30b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="sarvamai/sarvam-30b", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("sarvamai/sarvam-30b", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use sarvamai/sarvam-30b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "sarvamai/sarvam-30b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sarvamai/sarvam-30b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/sarvamai/sarvam-30b
- SGLang
How to use sarvamai/sarvam-30b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "sarvamai/sarvam-30b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sarvamai/sarvam-30b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "sarvamai/sarvam-30b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sarvamai/sarvam-30b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use sarvamai/sarvam-30b with Docker Model Runner:
docker model run hf.co/sarvamai/sarvam-30b
Create hotpatch_vllm.py
Browse files- 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()
|