-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
241 lines (223 loc) · 7.67 KB
/
server.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
from pydantic import TypeAdapter
from transformers import AutoTokenizer, AutoModelForCausalLM, TorchAoConfig
import torch
import json
from entropixing.generate import generate, stream
from asyncio import Lock
from uvicorn import run
from fastapi import FastAPI, Response, Request
from fastapi.responses import StreamingResponse
from openai.types.model import Model
from openai.types.chat import ChatCompletionChunk, ChatCompletion, ChatCompletionMessage
from openai.types.chat.chat_completion_chunk import Choice, ChoiceDelta
from openai.types.chat.chat_completion import Choice as NostreamChoice
from openai.types.chat.completion_create_params import CompletionCreateParams
from uuid import uuid4
import time
from entropixing.utils import is_supported_model
if torch.backends.mps.is_available():
device = torch.device("mps")
elif torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
print(f"Default device: {device}")
torch.set_float32_matmul_precision("high")
torch.backends.cudnn.benchmark = True
adapter: TypeAdapter[CompletionCreateParams] = TypeAdapter(CompletionCreateParams)
def main():
from argparse import ArgumentParser
global dtype
global device
global weights
global tokenizer
global lock
lock = Lock()
parser = ArgumentParser()
parser.add_argument(
"--model", type=str, required=True, default="google/gemma-2-2b-jpn-it"
)
parser.add_argument(
"--dtype",
type=str,
choices=["float16", "bfloat16", "float32"],
default="bfloat16",
)
parser.add_argument("--device", type=str, default=device.type)
parser.add_argument("--port", type=int, default=8000)
parser.add_argument("--host", type=str, default="0.0.0.0")
parser.add_argument("--max_length", type=int, default=512)
parser.add_argument("--context_length", type=int)
parser.add_argument("--top_p", type=float, default=0.95)
parser.add_argument("--top_k", type=int, default=40)
parser.add_argument("--min_p", type=int, default=0)
parser.add_argument("--repetition_penalty", type=float, default=1.0)
parser.add_argument("--seed", type=int)
parser.add_argument("--quantize", action="store_true")
args = parser.parse_args()
device = torch.device(args.device)
print(f"Using device: {device}")
if not is_supported_model(args.model):
raise ValueError("Unsupported model")
dtype = getattr(torch, args.dtype)
weights = AutoModelForCausalLM.from_pretrained(
args.model,
device_map=device,
torch_dtype=dtype,
quantization_config=(
TorchAoConfig("int4_weight_only", ["self_attn"], group_size=64)
if args.quantize
else None
),
).eval()
tokenizer = AutoTokenizer.from_pretrained(args.model)
app = FastAPI()
@app.post("/chat/completions")
async def chat_completion(body: Request) -> Response:
j = adapter.validate_python(await body.json())
max_length = j.get("max_completion_tokens") or args.max_length
messages = list(j["messages"])
top_p = j.get("top_p", args.top_p)
top_k = j.get("top_logprobs", args.top_k)
min_p = args.min_p
repetition_penalty = j.get("frequency_penalty", args.repetition_penalty)
seed = j.get("seed", args.seed)
if j.get("stream"):
async def stream_generator():
async for chunk in gen(
messages,
max_length,
top_p,
top_k,
min_p,
repetition_penalty,
seed,
args.context_length,
):
if "text" in chunk:
yield f"data: {ChatCompletionChunk(
id=str(uuid4()),
choices=[Choice(delta=ChoiceDelta(content=chunk["text"]), index=0)],
created=time.time() // 1000,
model=j["model"],
object="chat.completion.chunk",
).model_dump_json()}\n\n"
else:
yield f"data: {ChatCompletionChunk(
id=str(uuid4()),
choices=[Choice(delta=ChoiceDelta(content="⌫"), index=0)],
created=time.time() // 1000,
model=j["model"],
object="chat.completion.chunk",
).model_dump_json()}\n\n"
yield f"data: {ChatCompletionChunk(
id=str(uuid4()),
choices=[
Choice(delta=ChoiceDelta(), finish_reason="stop", index=0)
],
created=time.time() // 1000,
model=j["model"],
object="chat.completion.chunk",
).model_dump_json()}\n\n"
return StreamingResponse(
content=stream_generator(), media_type="text/event-stream"
)
else:
text = await gen_no_stream(
messages,
max_length,
top_p,
top_k,
min_p,
repetition_penalty,
seed,
args.context_length,
)
return Response(
content=ChatCompletion(
id=str(uuid4()),
choices=[
NostreamChoice(
finish_reason="stop",
message=ChatCompletionMessage(
content=text, role="assistant"
),
index=0,
)
],
created=time.time() // 1000,
model=j["model"],
object="chat.completion",
).model_dump_json(),
media_type="application/json",
)
@app.get("/models")
async def models():
return Response(
content=json.dumps(
{
"data": [
json.loads(
Model(
id="entropix-any",
object="model",
created=1,
owned_by="someone",
).model_dump_json()
)
]
}
),
media_type="application/json",
)
run(app, host=args.host, port=args.port)
async def gen_no_stream(
conv,
max_length,
top_p,
top_k,
min_p,
repetition_penalty,
seed,
context_length,
):
text = ""
async for chunk in gen(
conv, max_length, top_p, top_k, min_p, repetition_penalty, seed, context_length
):
if "text" in chunk:
text += chunk["text"]
return text
async def gen(
conv,
max_length,
top_p,
top_k,
min_p,
repetition_penalty,
seed,
context_length,
):
inputs = tokenizer.apply_chat_template(
conv, return_tensors="pt", add_generation_prompt=True
)
async with lock:
it = generate(
weights,
inputs,
device,
dtype,
[tokenizer.eos_token_id],
max_length,
top_p,
top_k,
min_p,
repetition_penalty,
seed,
False,
context_length,
)
for token in stream(it, tokenizer):
yield token
if __name__ == "__main__":
main()