-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
267 lines (221 loc) · 8.82 KB
/
app.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import subprocess
import tempfile
from datetime import datetime
from pathlib import Path
import httpx
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi import FastAPI, HTTPException, Request
from sqlalchemy import Column, DateTime, Integer, Text, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from transformers import AutoConfig
engine = create_engine("sqlite:///model_state.db", echo=True)
with engine.connect():
pass
Base = declarative_base()
class ModelState(Base):
__tablename__ = "model_state"
id = Column(Integer, primary_key=True)
model_name = Column(Text, nullable=False)
last_accessed_at = Column(
DateTime(timezone=True), default=datetime.utcnow, nullable=False
)
deployment_id = Column(Text, unique=True, nullable=False)
url = Column(Text, unique=True, nullable=False)
deploy_method = Column(Text, nullable=False)
n_gpu = Column(Integer, nullable=False)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
app = FastAPI()
def try_to_get_deployment_id(model_name: str, deploy_method: str) -> str | None:
with Session() as session:
model_state: ModelState | None = (
session.query(ModelState)
.filter_by(
model_name=model_name,
deploy_method=deploy_method,
)
.first()
)
print("try_to_get_deployment_id", model_state)
if model_state is None:
return None
else:
return model_state.deployment_id
def try_to_get_url(deployment_id: str) -> str | None:
with Session() as session:
model_state: ModelState | None = (
session.query(ModelState).filter_by(deployment_id=deployment_id).first()
)
print("try_to_get_url", model_state)
assert model_state is not None
print("check", f"{model_state.url}/health")
health_response = httpx.get(f"{model_state.url}/health")
if health_response.status_code == 200:
return model_state.url
else:
return None
def update_last_accessed_at(deployment_id: str) -> None:
with Session() as session:
model_state: ModelState | None = (
session.query(ModelState).filter_by(deployment_id=deployment_id).first()
)
assert model_state is not None
model_state.last_accessed_at = datetime.utcnow()
session.commit()
@app.api_route("/{deploy_method}/v1/chat/completions", methods=["POST"])
async def chat_completions(deploy_method: str, request: Request):
print("deploy_method", deploy_method)
request_json = await request.json()
if "model" not in request_json:
raise HTTPException(status_code=400, detail='"model" attribute is not included')
model_name = request_json["model"]
print("model_name", model_name)
deployment_id_or_none: str | None = try_to_get_deployment_id(
model_name=model_name, deploy_method=deploy_method
)
print("deployment_id_or_none", deployment_id_or_none)
if deployment_id_or_none is None:
print("deploy", model_name)
n_gpu: int = detect_suitable_computing_resource_from_model_name(
model_name=model_name,
deploy_method=deploy_method,
)
deployment_id: str = deploy_model(
model_name=model_name, deploy_method=deploy_method, n_gpu=n_gpu
)
else:
deployment_id: str = deployment_id_or_none
print("deployment_id", deployment_id)
print("check!")
url_or_none: str | None = try_to_get_url(deployment_id=deployment_id)
if url_or_none is None:
print(model_name, "is not running yet")
raise HTTPException(status_code=503, detail="model is not running yet")
url: str = url_or_none
print(model_name, "is running")
update_last_accessed_at(deployment_id=deployment_id)
print("post", f"{url}/v1/chat/completions")
response = httpx.post(f"{url}/v1/chat/completions", json=request_json)
print("response", response)
return response.json()
@app.api_route("/v1/chat/completions", methods=["POST"])
async def chat_completions_wo_deploy_method_specification(request: Request):
return await chat_completions(deploy_method="vllm_fp16", request=request)
DELETE_UNUSED_MODEL_INTERVAL_SECONDS = int(
os.getenv("PLAYGROUND_API_DELETE_UNUSED_MOODEL_INTERVAL_SECONDS", 60 * 60)
)
def delete_unused_model() -> None:
now = datetime.utcnow()
with Session() as session:
model_state_list = session.query(ModelState).all()
for model_state in model_state_list:
if (
now - model_state.last_accessed_at
).total_seconds() > DELETE_UNUSED_MODEL_INTERVAL_SECONDS:
delete_deployment(deployment_id=model_state.deployment_id)
session.delete(model_state)
session.commit()
@app.on_event("startup")
def schedule_process() -> None:
scheduler = BackgroundScheduler()
scheduler.add_job(
delete_unused_model,
"interval",
seconds=min(60, DELETE_UNUSED_MODEL_INTERVAL_SECONDS),
)
scheduler.start()
@app.get("/running_model_list")
def running_model_list():
with Session() as session:
model_state_list = session.query(ModelState).all()
return model_state_list
def run(command: str, work_dir: str) -> None:
print(f"+ {command}")
subprocess.run(command, shell=True, cwd=work_dir, check=True)
def detect_model_size_in_giga_bytes(model_name: str) -> int:
if Path(model_name).exists():
params_file_list = list(Path(model_name).glob("**/*.safetensors")) or list(
Path(model_name).glob("**/*.bin")
)
if len(params_file_list) == 0:
raise HTTPException(status_code=400, detail="failed to detect model size")
total_file_size_in_bytes = 0
for params_file in params_file_list:
file_size_in_bytes = os.path.getsize(params_file)
total_file_size_in_bytes += file_size_in_bytes
else:
with tempfile.TemporaryDirectory() as tmp:
try:
run(
f"GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/{model_name}",
work_dir=tmp,
)
except Exception:
raise HTTPException(
status_code=400, detail=f'failed to git clone model "{model_name}"'
)
lfs_alias_list = list(Path(tmp).glob("**/*.safetensors")) or list(
Path(tmp).glob("**/*.bin")
)
if len(lfs_alias_list) == 0:
raise HTTPException(
status_code=400, detail="failed to detect model size"
)
total_file_size_in_bytes = 0
for lfs_alias in lfs_alias_list:
with open(lfs_alias) as lfs_alias_file:
for line in lfs_alias_file:
if line.startswith("size"):
file_size_in_bytes = int(line.split()[1])
total_file_size_in_bytes += file_size_in_bytes
return total_file_size_in_bytes / (1024**3)
def get_suitable_computing_resource(model_size_in_giga_bytes: float) -> int:
usage_ratio = 0.9
if model_size_in_giga_bytes < (24 * 6) * usage_ratio:
n_gpu = int(model_size_in_giga_bytes / 24 + 1)
else:
raise HTTPException(status_code=400, detail="model is too big")
if n_gpu > 1 and n_gpu % 2 == 1:
n_gpu += 1
assert n_gpu >= 1
return n_gpu
def detect_suitable_computing_resource_from_model_name(
model_name: str, deploy_method: str
) -> int:
model_size_in_giga_bytes = detect_model_size_in_giga_bytes(model_name=model_name)
n_gpu = get_suitable_computing_resource(
model_size_in_giga_bytes=model_size_in_giga_bytes
)
return n_gpu
def is_model_name_valid(model_name: str) -> bool:
if Path(model_name).exists():
return True
try:
AutoConfig.from_pretrained(model_name)
return True
except Exception as e:
print(e)
return False
def deploy_model(model_name: str, deploy_method: str, n_gpu: int) -> str:
if not is_model_name_valid(model_name=model_name):
raise HTTPException(status_code=404, detail="{model_name} is not found")
# TODO
# f"python3 deploy.py --model-name {model_name} --n-gpu {n_gpu}"
deployment_id = "hoge"
url = "http://localhost:8080"
with Session() as session:
new_model_state = ModelState(
model_name=model_name,
deployment_id=deployment_id,
url=url,
deploy_method=deploy_method,
n_gpu=n_gpu,
)
session.add(new_model_state)
session.commit()
return deployment_id
def delete_deployment(deployment_id: str) -> None:
print("delete deployment", deployment_id)
# TODO