-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathlmdeploy_oai_compatible.py
90 lines (68 loc) · 2.39 KB
/
lmdeploy_oai_compatible.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
# # Deploy a model with `lmdeploy`
#
# This script is used to deploy a model using [lmdeploy](https://github.com/InternLM/lmdeploy) with OpenAI compatible API.
import subprocess
import modal
from modal import App, Image, Secret, gpu
########## CONSTANTS ##########
# define model for serving and path to store in modal container
MODEL_NAME = "meta-llama/Llama-2-7b-hf"
MODEL_DIR = f"/models/{MODEL_NAME}"
SERVE_MODEL_NAME = "meta--llama-2-7b"
HF_SECRET = Secret.from_name("huggingface-secret")
SECONDS = 60 # for timeout
########## UTILS FUNCTIONS ##########
def download_hf_model(model_dir: str, model_name: str):
"""Retrieve model from HuggingFace Hub and save into
specified path within the modal container.
Args:
model_dir (str): Path to save model weights in container.
model_name (str): HuggingFace Model ID.
"""
import os
from huggingface_hub import snapshot_download # type: ignore
from transformers.utils import move_cache # type: ignore
os.makedirs(model_dir, exist_ok=True)
snapshot_download(
model_name,
local_dir=model_dir,
# consolidated.safetensors is prevent error here: https://github.com/vllm-project/vllm/pull/5005
ignore_patterns=["*.pt", "*.bin", "consolidated.safetensors"],
token=os.environ["HF_TOKEN"],
)
move_cache()
########## IMAGE DEFINITION ##########
# define image for modal environment
lmdeploy_image = (
Image.from_registry(
"openmmlab/lmdeploy:v0.4.2",
)
.pip_install(["lmdeploy[all]", "huggingface_hub", "hf-transfer"])
.env({"HF_HUB_ENABLE_HF_TRANSFER": "1"})
.run_function(
download_hf_model,
timeout=60 * SECONDS,
kwargs={"model_dir": MODEL_DIR, "model_name": MODEL_NAME},
secrets=[HF_SECRET],
)
)
########## APP SETUP ##########
app = App(f"lmdeploy-{SERVE_MODEL_NAME}")
NO_GPU = 1
TOKEN = "secret12345"
@app.function(
image=lmdeploy_image,
gpu=gpu.A10G(count=NO_GPU),
container_idle_timeout=20 * SECONDS,
# https://modal.com/docs/guide/concurrent-inputs
allow_concurrent_inputs=256, # max concurrent input into container
)
@modal.web_server(port=23333, startup_timeout=60 * SECONDS)
def serve():
cmd = f"""
lmdeploy serve api_server {MODEL_DIR} \
--model-name {SERVE_MODEL_NAME} \
--server-port 23333 \
--session-len 4092
"""
subprocess.Popen(cmd, shell=True)