-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhandler.py
More file actions
129 lines (100 loc) · 3.75 KB
/
Copy pathhandler.py
File metadata and controls
129 lines (100 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import runpod
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
MODEL_ID = os.environ.get("MODEL_NAME", "microsoft/Phi-3-mini-4k-instruct")
HF_CACHE_ROOT = "/runpod-volume/huggingface-cache/hub"
# Force offline mode to use only cached models
os.environ["HF_HUB_OFFLINE"] = "1"
os.environ["TRANSFORMERS_OFFLINE"] = "1"
def resolve_snapshot_path(model_id: str) -> str:
"""
Resolve the local snapshot path for a cached model.
Args:
model_id: The model name from Hugging Face (e.g., 'microsoft/Phi-3-mini-4k-instruct')
Returns:
The full path to the cached model snapshot
"""
if "/" not in model_id:
raise ValueError(f"MODEL_ID '{model_id}' is not in 'org/name' format")
org, name = model_id.split("/", 1)
model_root = os.path.join(HF_CACHE_ROOT, f"models--{org}--{name}")
refs_main = os.path.join(model_root, "refs", "main")
snapshots_dir = os.path.join(model_root, "snapshots")
print(f"[ModelStore] MODEL_ID: {model_id}")
print(f"[ModelStore] Model root: {model_root}")
# Try to read the snapshot hash from refs/main
if os.path.isfile(refs_main):
with open(refs_main, "r") as f:
snapshot_hash = f.read().strip()
candidate = os.path.join(snapshots_dir, snapshot_hash)
if os.path.isdir(candidate):
print(f"[ModelStore] Using snapshot from refs/main: {candidate}")
return candidate
# Fall back to first available snapshot
if not os.path.isdir(snapshots_dir):
raise RuntimeError(f"[ModelStore] snapshots directory not found: {snapshots_dir}")
versions = [
d for d in os.listdir(snapshots_dir) if os.path.isdir(os.path.join(snapshots_dir, d))
]
if not versions:
raise RuntimeError(f"[ModelStore] No snapshot subdirectories found under {snapshots_dir}")
versions.sort()
chosen = os.path.join(snapshots_dir, versions[0])
print(f"[ModelStore] Using first available snapshot: {chosen}")
return chosen
# Resolve and load the model at startup
LOCAL_MODEL_PATH = resolve_snapshot_path(MODEL_ID)
print(f"[ModelStore] Resolved local model path: {LOCAL_MODEL_PATH}")
tokenizer = AutoTokenizer.from_pretrained(
LOCAL_MODEL_PATH,
trust_remote_code=False,
local_files_only=True,
)
model = AutoModelForCausalLM.from_pretrained(
LOCAL_MODEL_PATH,
trust_remote_code=False,
torch_dtype="auto",
device_map="auto",
local_files_only=True,
attn_implementation="eager",
)
text_gen = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
print("[ModelStore] Model loaded from local snapshot")
def handler(job):
"""
Handler function that processes each inference request.
Args:
job: Runpod job object containing input data
Returns:
Dictionary with generated text or error information
"""
job_input = job.get("input", {}) or {}
prompt = job_input.get("prompt", "Hello!")
max_tokens = int(job_input.get("max_tokens", 256))
temperature = float(job_input.get("temperature", 0.7))
print(f"[Handler] Prompt: {prompt[:80]!r}")
print(f"[Handler] max_tokens={max_tokens}, temperature={temperature}")
try:
outputs = text_gen(
prompt,
max_new_tokens=max_tokens,
do_sample=True,
temperature=temperature,
)
generated = outputs[0]["generated_text"]
print(f"[Handler] Generated length: {len(generated)} chars")
return {
"status": "success",
"output": generated,
}
except Exception as e:
print(f"[Handler] Error during generation: {e}")
return {
"status": "error",
"error": str(e),
}
runpod.serverless.start({"handler": handler})