-
Notifications
You must be signed in to change notification settings - Fork 823
Expand file tree
/
Copy pathserver.py
More file actions
667 lines (614 loc) · 27.2 KB
/
server.py
File metadata and controls
667 lines (614 loc) · 27.2 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import asyncio
from collections import defaultdict
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
from itertools import cycle
import json
import os
import socket
import time
from typing import Annotated, Any, AsyncGenerator, Literal, cast
import uuid
from fastapi import FastAPI, HTTPException, Request
from openai import AsyncOpenAI
from openai.types import Model, ModelDeleted
from openai.types.chat.chat_completion import ChatCompletion, Choice, ChoiceLogprobs
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from openai.types.chat.chat_completion_message_function_tool_call import (
ChatCompletionMessageFunctionToolCall,
Function,
)
from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
from openai.types.chat.chat_completion_token_logprob import ChatCompletionTokenLogprob
from openai.types.chat.chat_completion_tool_union_param import (
ChatCompletionToolUnionParam,
)
from openai.types.chat.completion_create_params import CompletionCreateParams
from openai.types.completion_usage import CompletionUsage
from pydantic import BaseModel, Field, SkipValidation
import tinker
from transformers.tokenization_utils_base import BatchEncoding
import uvicorn
from art.tinker.cookbook_v import renderers
from art.tinker.cookbook_v.tokenizer_utils import get_tokenizer
from art.tinker.prefix_cache import LRUTrieCache
from art.tinker.renderers import get_renderer_name
from art.types import Message, Tools
from mp_actors import close_proxy, move_to_child_process
class ModelList(BaseModel):
object: Literal["list"] = "list"
data: list[Model]
class ModelUpsert(BaseModel):
target: str
WireMessagesAndChoices = list[Choice | Message]
class MessagesAndChoicesWithLogprobsArgs(BaseModel):
messages_and_choices: WireMessagesAndChoices
models: list[str]
model_aliases: dict[str, str] = Field(default_factory=dict)
tools: Tools | None
class MessagesAndChoicesWithLogprobs(BaseModel):
messages_and_choices: WireMessagesAndChoices
usages: list[CompletionUsage]
def _normalize_qwen3_5_messages(
base_model: str, messages: list[ChatCompletionMessageParam]
) -> list[dict[str, Any]]:
normalized_messages = [cast(dict[str, Any], message) for message in messages]
if not base_model.startswith("Qwen/Qwen3.5"):
return normalized_messages
for i, message in enumerate(normalized_messages):
tool_calls = message.get("tool_calls")
if not isinstance(tool_calls, list):
continue
normalized_tool_calls: list[Any] = []
changed = False
for tool_call in tool_calls:
if not isinstance(tool_call, dict):
normalized_tool_calls.append(tool_call)
continue
function = tool_call.get("function")
if not isinstance(function, dict):
normalized_tool_calls.append(tool_call)
continue
arguments_json = function.get("arguments")
if not isinstance(arguments_json, str):
normalized_tool_calls.append(tool_call)
continue
try:
arguments = json.loads(arguments_json)
except json.JSONDecodeError:
normalized_tool_calls.append(tool_call)
continue
if not isinstance(arguments, dict):
normalized_tool_calls.append(tool_call)
continue
changed = True
normalized_tool_calls.append(
{**tool_call, "function": {**function, "arguments": arguments}}
)
if changed:
normalized_messages[i] = {**message, "tool_calls": normalized_tool_calls}
return normalized_messages
@dataclass
class OpenAICompatibleTinkerServer:
host: str | None = None
port: int | None = None
num_workers: int | None = None
max_concurrent_sampling_clients: int | None = None
_prefix_cache: LRUTrieCache = field(default_factory=LRUTrieCache)
_task: asyncio.Task[None] | None = None
_tenants: dict[str, "OpenAICompatibleTinkerServerTenant"] = field(
default_factory=dict
)
_workers: list["OpenAICompatibleTinkerServerWorker"] = field(default_factory=list)
@property
def models(self) -> dict[str, str]:
if "TINKER_API_KEY" not in os.environ:
raise ValueError("TINKER_API_KEY is not set")
return self._get_tenant(os.environ["TINKER_API_KEY"]).models
@models.setter
def models(self, models: dict[str, str]) -> None:
if "TINKER_API_KEY" not in os.environ:
raise ValueError("TINKER_API_KEY is not set")
self._get_tenant(os.environ["TINKER_API_KEY"]).models = models
async def start(self) -> tuple[str, int]:
host = self.host or "0.0.0.0"
port = self.port or get_free_port(host)
self._workers = [
move_to_child_process(
OpenAICompatibleTinkerServerWorker(),
process_name=f"openai-compatible-tinker-server-worker-{i}",
)
for i in range(self.num_workers or self._default_num_workers())
]
self._task = asyncio.create_task(self._run(host, port))
client = AsyncOpenAI(api_key="health-check", base_url=f"http://{host}:{port}/v1")
start = time.time()
while True:
timeout = float(os.environ.get("ART_SERVER_TIMEOUT", 300.0))
if time.time() - start > timeout:
raise TimeoutError(
f"Unable to reach OpenAI-compatible server within {timeout} seconds. You can increase this timeout by setting the ART_SERVER_TIMEOUT environment variable."
)
try:
await client.completions.create(model="", prompt="")
break # Server is ready
except Exception:
await asyncio.sleep(0.1)
return host, port
async def stop(self) -> None:
if self._task is not None:
self._task.cancel()
await self._task
self._task = None
for worker in self._workers:
close_proxy(worker)
def _get_request_tenant(
self, request: Request
) -> "OpenAICompatibleTinkerServerTenant":
auth = request.headers.get("authorization", "")
scheme, _, api_key = auth.partition(" ")
api_key = api_key.strip()
if scheme.lower() != "bearer" or not api_key:
raise HTTPException(
status_code=401,
detail="Missing or invalid Authorization header",
headers={"WWW-Authenticate": "Bearer"},
)
return self._get_tenant(api_key)
async def _run(self, host: str, port: int) -> None:
workers = cycle(self._workers)
app = FastAPI()
@app.get("/metrics")
async def metrics() -> str:
# Minimal Prometheus-style metrics to satisfy the health monitor
return "# Tinker service metrics\n"
@app.post("/v1/completions")
async def completions() -> dict:
# Minimal completions endpoint for health checks
return {"choices": [{"text": ""}]}
@app.post("/v1/messages_and_choices/with_logprobs")
async def messages_and_choices_with_logprobs(
request: Request, args: MessagesAndChoicesWithLogprobsArgs
) -> MessagesAndChoicesWithLogprobs:
tenant = self._get_request_tenant(request)
async def add_logprobs(model: str, alias: str | None) -> CompletionUsage:
worker = next(workers)
samplable_model = await tenant.get_samplable_model(model)
prompt_tokens_and_choice_offsets = (
await worker.messages_and_choices_prompt_tokens_and_choice_offsets(
base_model=samplable_model.base_model,
messages_and_choices=args.messages_and_choices,
tools=args.tools,
)
)
if prompt_tokens_and_choice_offsets is None:
return CompletionUsage(
completion_tokens=0, prompt_tokens=0, total_tokens=0
)
prompt_tokens, choice_offsets = prompt_tokens_and_choice_offsets
try:
async with samplable_model.sampling_client() as sampling_client:
sample_response = await sampling_client.sample_async(
prompt=tinker.ModelInput.from_ints(tokens=prompt_tokens),
num_samples=1,
sampling_params=tinker.SamplingParams(max_tokens=1),
include_prompt_logprobs=True,
)
assert sample_response.prompt_logprobs is not None
for choice in args.messages_and_choices:
if not isinstance(choice, Choice):
continue
if choice.logprobs is None:
continue
token_logprobs = (
choice.logprobs.content or choice.logprobs.refusal or []
)
offset = choice_offsets.pop(0)
for i, token_logprob in enumerate(token_logprobs):
assert token_logprob.model_extra is not None
if token_logprob.token.startswith("token_id:"):
assert (
int(token_logprob.token.split(":")[1])
== prompt_tokens[offset + i]
)
token_logprob.model_extra.setdefault(
"extra_logprobs", {}
)[alias or model] = sample_response.prompt_logprobs[
offset + i
]
return CompletionUsage(
completion_tokens=1,
prompt_tokens=len(prompt_tokens),
total_tokens=1 + len(prompt_tokens),
)
except tinker.APIStatusError as e:
error_body = e.body
if isinstance(error_body, dict) and "detail" in error_body:
detail = error_body["detail"] # ty:ignore[invalid-argument-type]
elif error_body is not None:
detail = error_body
else:
detail = str(e)
raise HTTPException(status_code=e.status_code, detail=detail) from e
usages = await asyncio.gather(
*[
add_logprobs(model, args.model_aliases.get(model))
for model in args.models
]
)
return MessagesAndChoicesWithLogprobs(
messages_and_choices=args.messages_and_choices,
usages=usages,
)
@app.get("/v1/models")
async def list_models(request: Request) -> ModelList:
tenant = self._get_request_tenant(request)
return ModelList(
object="list",
data=[
Model(
id=model,
created=tenant.model_timestamps.get(model, 0),
object="model",
owned_by="tinker",
)
for model in tenant.models
],
)
@app.get("/v1/models/{model}")
async def get_model(request: Request, model: str) -> Model:
tenant = self._get_request_tenant(request)
if model not in tenant.models:
raise HTTPException(
status_code=404,
detail=f"Model not found: {model}",
)
return Model(
id=model,
created=tenant.model_timestamps.get(model, 0),
object="model",
owned_by="tinker",
)
@app.put("/v1/models/{model}")
async def put_model(
request: Request,
model: str,
body: ModelUpsert,
) -> Model:
tenant = self._get_request_tenant(request)
tenant.models[model] = body.target
tenant.model_timestamps.setdefault(model, int(time.time()))
return Model(
id=model,
created=tenant.model_timestamps[model],
object="model",
owned_by="tinker",
)
@app.delete("/v1/models/{model}")
async def delete_model(request: Request, model: str) -> ModelDeleted:
tenant = self._get_request_tenant(request)
if model not in tenant.models:
raise HTTPException(
status_code=404,
detail=f"Model not found: {model}",
)
tenant.models.pop(model)
tenant.model_timestamps.pop(model, None)
return ModelDeleted(
id=model,
deleted=True,
object="model",
)
@app.post("/v1/chat/completions")
async def chat_completions(
request: Request, body: Annotated[CompletionCreateParams, SkipValidation]
) -> ChatCompletion:
worker = next(workers)
tenant = self._get_request_tenant(request)
samplable_model = await tenant.get_samplable_model(body["model"])
rendered_prompt_tokens = await worker.prompt_tokens(
base_model=samplable_model.base_model,
messages=list(body["messages"]),
tools=list(body.get("tools", [])) if "tools" in body else None,
)
prompt_tokens = rendered_prompt_tokens
prefix_entry = self._prefix_cache.lookup(rendered_prompt_tokens)
if prefix_entry is not None and prefix_entry.rendered_len <= len(
rendered_prompt_tokens
):
prompt_tokens = (
list(prefix_entry.raw_prefix)
+ rendered_prompt_tokens[prefix_entry.rendered_len :]
)
try:
async with samplable_model.sampling_client() as sampling_client:
sample_response = await sampling_client.sample_async(
prompt=tinker.ModelInput.from_ints(tokens=prompt_tokens),
num_samples=body.get("n") or 1,
sampling_params=tinker.SamplingParams(
max_tokens=body.get("max_completion_tokens")
or body.get("max_tokens"),
seed=body.get("seed"),
temperature=(
t if (t := body.get("temperature")) is not None else 1.0
),
top_k=body.get("top_k") or -1,
top_p=body.get("top_p") or 1.0,
),
)
except tinker.APIStatusError as e:
error_body = e.body
if isinstance(error_body, dict) and "detail" in error_body:
detail = error_body["detail"] # ty:ignore[invalid-argument-type]
elif error_body is not None:
detail = error_body
else:
detail = str(e)
raise HTTPException(status_code=e.status_code, detail=detail) from e
(
chat_completion,
token_discrepancies,
) = await worker.chat_completion_and_token_discrepancies(
base_model=samplable_model.base_model,
sample_response=sample_response,
model_name=body["model"],
prompt_tokens=len(prompt_tokens),
)
for rendered_response_tokens, raw_response_tokens in token_discrepancies:
self._prefix_cache.insert(
rendered_prompt_tokens + rendered_response_tokens,
prompt_tokens + raw_response_tokens,
)
return chat_completion
server_config = uvicorn.Config(
app,
host=host,
port=port,
log_level="error",
)
server = uvicorn.Server(server_config)
await server.serve()
def _default_num_workers(self) -> int:
try:
return max(1, len(os.sched_getaffinity(0))) # ty:ignore[unresolved-attribute]
except (AttributeError, OSError):
return os.cpu_count() or 1
def _get_tenant(self, api_key: str) -> "OpenAICompatibleTinkerServerTenant":
if api_key not in self._tenants:
self._tenants[api_key] = OpenAICompatibleTinkerServerTenant(
api_key, self.max_concurrent_sampling_clients or 32
)
return self._tenants[api_key]
@dataclass
class OpenAICompatibleTinkerServerSamplableModel:
base_model: str
_sampling_client: tinker.SamplingClient
_concurrent_sampling_client_semaphore: asyncio.Semaphore
_lock: asyncio.Lock = field(default_factory=asyncio.Lock)
_yields: int = 0
@asynccontextmanager
async def sampling_client(self) -> AsyncGenerator[tinker.SamplingClient, None]:
async with self._lock:
if self._yields == 0:
await self._concurrent_sampling_client_semaphore.acquire()
self._yields += 1
try:
yield self._sampling_client
finally:
async with self._lock:
self._yields -= 1
if self._yields == 0:
self._concurrent_sampling_client_semaphore.release()
class OpenAICompatibleTinkerServerTenant:
def __init__(self, api_key: str, max_concurrent_sampling_clients: int) -> None:
self.models: dict[str, str] = {}
self.model_timestamps: dict[str, int] = {}
self._service_client = tinker.ServiceClient(api_key=api_key)
self._rest_client = self._service_client.create_rest_client()
self._samplable_models: dict[
str, asyncio.Task[OpenAICompatibleTinkerServerSamplableModel]
] = dict()
self._concurrent_sampling_client_semaphores: defaultdict[
str, asyncio.Semaphore
] = defaultdict(lambda: asyncio.Semaphore(max_concurrent_sampling_clients))
async def get_samplable_model(
self, model: str
) -> OpenAICompatibleTinkerServerSamplableModel:
model_path_or_base_model = self.models.get(model, model)
if not model_path_or_base_model.startswith("tinker://"):
try:
get_renderer_name(model_path_or_base_model)
except ValueError:
raise HTTPException(
status_code=404,
detail=(
f"Model not found: {model_path_or_base_model}. "
"A model must be either a valid `tinker://...` path, supported base model, or registered model alias."
),
)
if (task := self._samplable_models.get(model_path_or_base_model)) and (
not task.done() or task.exception() is None
):
return await task
self._samplable_models[model_path_or_base_model] = asyncio.create_task(
self._load_samplable_model(model_path_or_base_model)
)
return await self._samplable_models[model_path_or_base_model]
async def _load_samplable_model(
self, model_path_or_base_model: str
) -> OpenAICompatibleTinkerServerSamplableModel:
is_model_path = model_path_or_base_model.startswith("tinker://")
sampling_client = await self._service_client.create_sampling_client_async(
model_path=model_path_or_base_model if is_model_path else None,
base_model=model_path_or_base_model if not is_model_path else None,
)
if is_model_path:
sampler_response = await self._rest_client.get_sampler_async(
sampling_client._sampling_session_id
)
base_model = sampler_response.base_model
else:
base_model = model_path_or_base_model
# on_queue_state_change = sampling_client.on_queue_state_change
# def patched_on_queue_state_change(
# queue_state: TinkerQueueState, queue_state_reason: str | None
# ) -> None:
# on_queue_state_change(queue_state, queue_state_reason)
# if queue_state == TinkerQueueState.PAUSED_RATE_LIMIT:
# # implicit upper-bound on the number of concurrent sampling clients found
# # do not allow this number of concurrent sampling clients again
# semaphore = self._concurrent_sampling_client_semaphores[base_model]
# semaphore._value = max(semaphore._value - 1, -4)
# sampling_client.on_queue_state_change = patched_on_queue_state_change
return OpenAICompatibleTinkerServerSamplableModel(
base_model=base_model,
_sampling_client=sampling_client,
_concurrent_sampling_client_semaphore=self._concurrent_sampling_client_semaphores[
base_model
],
)
@dataclass
class OpenAICompatibleTinkerServerWorker:
_renderers: dict[str, renderers.Renderer] = field(default_factory=dict)
async def prompt_tokens(
self,
base_model: str,
messages: list[ChatCompletionMessageParam],
tools: list[ChatCompletionToolUnionParam] | None,
) -> list[int]:
normalized_messages = _normalize_qwen3_5_messages(base_model, messages)
encoding = self._get_renderer(base_model).tokenizer.apply_chat_template(
cast(Any, normalized_messages),
tools=cast(Any, tools),
add_generation_prompt=True,
)
if isinstance(encoding, BatchEncoding):
return encoding.input_ids
else:
return encoding # type: ignore
async def messages_and_choices_prompt_tokens_and_choice_offsets(
self,
base_model: str,
messages_and_choices: WireMessagesAndChoices,
tools: Tools | None,
) -> tuple[list[int], list[int]] | None:
from art.preprocessing.tokenize import tokenize_trajectory
from art.trajectories import History, Trajectory
result = tokenize_trajectory(
tokenizer=self._get_renderer(base_model).tokenizer,
image_processor=None,
history=History(
messages_and_choices=messages_and_choices,
tools=tools,
),
advantage=0.0,
allow_training_without_logprobs=False,
trajectory=Trajectory(
messages_and_choices=messages_and_choices,
tools=tools,
),
)
return (result.token_ids, result.choice_offsets) if result is not None else None
async def chat_completion_and_token_discrepancies(
self,
base_model: str,
sample_response: tinker.SampleResponse,
model_name: str,
prompt_tokens: int,
) -> tuple[ChatCompletion, list[tuple[list[int], list[int]]]]:
renderer = self._get_renderer(base_model)
choices: list[Choice] = []
token_discrepancies: list[tuple[list[int], list[int]]] = []
for i, sequence in enumerate(sample_response.sequences):
assert sequence.logprobs is not None, "Logprobs are required"
assert len(sequence.tokens) == len(sequence.logprobs), (
"Tokens and logprobs must have the same length"
)
rendered_response_tokens = renderer.tokenizer.encode(
renderer.tokenizer.decode(sequence.tokens)
)
if rendered_response_tokens != sequence.tokens:
token_discrepancies.append((rendered_response_tokens, sequence.tokens))
message, _ = renderer.parse_response(sequence.tokens)
openai_message = renderer.to_openai_message(message)
tool_calls = (
[
ChatCompletionMessageFunctionToolCall(
type="function",
id=tool_call.get("id") or "",
function=Function(
name=tool_call["function"]["name"],
arguments=(
tool_call["function"]["arguments"]
if isinstance(tool_call["function"]["arguments"], str)
else json.dumps(tool_call["function"]["arguments"])
),
),
)
for tool_call in openai_message.get("tool_calls", [])
]
if openai_message.get("tool_calls")
else None
)
choices.append(
Choice(
finish_reason=sequence.stop_reason,
index=i,
message=ChatCompletionMessage(
content=openai_message.get("content") or None,
role="assistant",
tool_calls=tool_calls, # type: ignore
),
logprobs=ChoiceLogprobs(
content=[
ChatCompletionTokenLogprob(
token=f"token_id:{token}",
bytes=list(renderer.tokenizer.decode(token).encode()),
logprob=logprob,
top_logprobs=[],
)
for token, logprob in zip(
sequence.tokens, sequence.logprobs
)
]
),
)
)
completion_tokens = sum(
len(sequence.tokens) for sequence in sample_response.sequences
)
return (
ChatCompletion(
id=str(uuid.uuid4()),
choices=choices,
created=int(time.time()),
model=model_name,
object="chat.completion",
usage=CompletionUsage(
completion_tokens=completion_tokens,
prompt_tokens=prompt_tokens,
total_tokens=completion_tokens + prompt_tokens,
),
),
token_discrepancies,
)
def _get_renderer(self, base_model: str) -> renderers.Renderer:
if base_model not in self._renderers:
self._renderers[base_model] = renderers.get_renderer(
name=get_renderer_name(base_model),
tokenizer=get_tokenizer(base_model),
model_name=base_model,
)
return self._renderers[base_model]
def get_free_port(host: str | None = None) -> int:
"""
Returns the first free port >= 8000.
"""
port = 8000
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host or "", port))
return port
except OSError:
port += 1