-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathengine.py
More file actions
188 lines (170 loc) · 5.95 KB
/
engine.py
File metadata and controls
188 lines (170 loc) · 5.95 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
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
import subprocess
import time
import requests
import openai
import asyncio
import aiohttp
import os
class SGlangEngine:
def __init__(
self,
model=os.getenv("MODEL_NAME"),
host=os.getenv("HOST", "0.0.0.0"),
port=int(os.getenv("PORT", 30000)),
):
self.model = model
self.host = host
self.port = port
self.base_url = f"http://{self.host}:{self.port}"
self.process = None
def start_server(self):
command = [
"python3",
"-m",
"sglang.launch_server",
"--host",
self.host,
"--port",
str(self.port),
]
# Dictionary of all possible options and their corresponding env var names
options = {
"MODEL_NAME": "--model-path",
"TOKENIZER_PATH": "--tokenizer-path",
"TOKENIZER_MODE": "--tokenizer-mode",
"LOAD_FORMAT": "--load-format",
"DTYPE": "--dtype",
"CONTEXT_LENGTH": "--context-length",
"QUANTIZATION": "--quantization",
"SERVED_MODEL_NAME": "--served-model-name",
"CHAT_TEMPLATE": "--chat-template",
"MEM_FRACTION_STATIC": "--mem-fraction-static",
"MAX_RUNNING_REQUESTS": "--max-running-requests",
"MAX_TOTAL_TOKENS": "--max-total-tokens",
"CHUNKED_PREFILL_SIZE": "--chunked-prefill-size",
"MAX_PREFILL_TOKENS": "--max-prefill-tokens",
"SCHEDULE_POLICY": "--schedule-policy",
"SCHEDULE_CONSERVATIVENESS": "--schedule-conservativeness",
"TENSOR_PARALLEL_SIZE": "--tensor-parallel-size",
"STREAM_INTERVAL": "--stream-interval",
"RANDOM_SEED": "--random-seed",
"LOG_LEVEL": "--log-level",
"LOG_LEVEL_HTTP": "--log-level-http",
"API_KEY": "--api-key",
"FILE_STORAGE_PATH": "--file-storage-path",
"DATA_PARALLEL_SIZE": "--data-parallel-size",
"LOAD_BALANCE_METHOD": "--load-balance-method",
"ATTENTION_BACKEND": "--attention-backend",
"SAMPLING_BACKEND": "--sampling-backend",
"TOOL_CALL_PARSER": "--tool-call-parser",
"REASONING_PARSER": "--reasoning-parser",
}
# Boolean flags
boolean_flags = [
"SKIP_TOKENIZER_INIT",
"TRUST_REMOTE_CODE",
"LOG_REQUESTS",
"SHOW_TIME_COST",
"DISABLE_RADIX_CACHE",
"DISABLE_CUDA_GRAPH",
"DISABLE_OUTLINES_DISK_CACHE",
"ENABLE_TORCH_COMPILE",
"ENABLE_P2P_CHECK",
"ENABLE_FLASHINFER_MLA",
"TRITON_ATTENTION_REDUCE_IN_FP32",
]
# Add options from environment variables only if they are set
for env_var, option in options.items():
value = os.getenv(env_var)
if value is not None and value != "":
command.extend([option, value])
# Add boolean flags only if they are set to true
for flag in boolean_flags:
if os.getenv(flag, "").lower() in ("true", "1", "yes"):
command.append(f"--{flag.lower().replace('_', '-')}")
self.process = subprocess.Popen(command, stdout=None, stderr=None)
print(f"Server started with PID: {self.process.pid}")
def wait_for_server(self, timeout=900, interval=5):
start_time = time.time()
while time.time() - start_time < timeout:
try:
response = requests.get(f"{self.base_url}/v1/models")
if response.status_code == 200:
print("Server is ready!")
return True
except requests.RequestException:
pass
time.sleep(interval)
raise TimeoutError("Server failed to start within the timeout period.")
def shutdown(self):
if self.process:
self.process.terminate()
self.process.wait()
print("Server shut down.")
class OpenAIRequest:
def __init__(self, base_url="http://0.0.0.0:30000/v1", api_key="EMPTY"):
self.client = openai.Client(base_url=base_url, api_key=api_key)
async def request_chat_completions(
self,
model="default",
messages=None,
max_tokens=100,
stream=False,
frequency_penalty=0.0,
n=1,
stop=None,
temperature=1.0,
top_p=1.0,
):
if messages is None:
messages = [
{"role": "system", "content": "You are a helpful AI assistant"},
{"role": "user", "content": "List 3 countries and their capitals."},
]
response = self.client.chat.completions.create(
model=model,
messages=messages,
max_tokens=max_tokens,
stream=stream,
frequency_penalty=frequency_penalty,
n=n,
stop=stop,
temperature=temperature,
top_p=top_p,
)
if stream:
async for chunk in response:
yield chunk.to_dict()
else:
yield response.to_dict()
async def request_completions(
self,
model="default",
prompt="The capital of France is",
max_tokens=100,
stream=False,
frequency_penalty=0.0,
n=1,
stop=None,
temperature=1.0,
top_p=1.0,
):
response = self.client.completions.create(
model=model,
prompt=prompt,
max_tokens=max_tokens,
stream=stream,
frequency_penalty=frequency_penalty,
n=n,
stop=stop,
temperature=temperature,
top_p=top_p,
)
if stream:
async for chunk in response:
yield chunk.to_dict()
else:
yield response.to_dict()
async def get_models(self):
response = await self.client.models.list()
return response