2022-10-08 04:30:12 -06:00
|
|
|
import asyncio
|
2022-10-17 06:59:00 -06:00
|
|
|
import os
|
2023-02-07 07:38:22 -07:00
|
|
|
import torch
|
2022-10-17 06:59:00 -06:00
|
|
|
|
2022-10-08 04:30:12 -06:00
|
|
|
from grpc import aio
|
2023-01-05 04:01:23 -07:00
|
|
|
from loguru import logger
|
2022-10-08 04:30:12 -06:00
|
|
|
|
|
|
|
from grpc_reflection.v1alpha import reflection
|
|
|
|
from pathlib import Path
|
2023-01-31 10:53:56 -07:00
|
|
|
from typing import List, Optional
|
2022-10-08 04:30:12 -06:00
|
|
|
|
2023-03-07 10:52:22 -07:00
|
|
|
from text_generation_server.cache import Cache
|
|
|
|
from text_generation_server.interceptor import ExceptionInterceptor
|
|
|
|
from text_generation_server.models import Model, get_model
|
|
|
|
from text_generation_server.pb import generate_pb2_grpc, generate_pb2
|
|
|
|
from text_generation_server.tracing import UDSOpenTelemetryAioServerInterceptor
|
2022-10-08 04:30:12 -06:00
|
|
|
|
|
|
|
|
2022-10-11 08:50:54 -06:00
|
|
|
class TextGenerationService(generate_pb2_grpc.TextGenerationServiceServicer):
|
2022-10-28 11:24:00 -06:00
|
|
|
def __init__(self, model: Model, cache: Cache, server_urls: List[str]):
|
2022-10-08 04:30:12 -06:00
|
|
|
self.cache = cache
|
|
|
|
self.model = model
|
|
|
|
self.server_urls = server_urls
|
2023-02-07 07:38:22 -07:00
|
|
|
# For some reason, inference_mode does not work well with GLOO which we use on CPU
|
|
|
|
if model.device.type == "cuda":
|
|
|
|
# Force inference mode for the lifetime of TextGenerationService
|
|
|
|
self._inference_mode_raii_guard = torch._C._InferenceMode(True)
|
2022-10-08 04:30:12 -06:00
|
|
|
|
2023-04-21 07:36:29 -06:00
|
|
|
async def Info(self, request, context):
|
|
|
|
return self.model.info
|
|
|
|
|
2023-04-26 12:23:54 -06:00
|
|
|
async def Health(self, request, context):
|
|
|
|
if self.model.device.type == "cuda":
|
|
|
|
torch.zeros((2, 2)).cuda()
|
|
|
|
return generate_pb2.HealthResponse()
|
|
|
|
|
2022-10-08 04:30:12 -06:00
|
|
|
async def ServiceDiscovery(self, request, context):
|
|
|
|
return generate_pb2.ServiceDiscoveryResponse(urls=self.server_urls)
|
|
|
|
|
|
|
|
async def ClearCache(self, request, context):
|
2023-03-28 03:29:35 -06:00
|
|
|
if request.HasField("id"):
|
|
|
|
self.cache.delete(request.id)
|
|
|
|
else:
|
|
|
|
self.cache.clear()
|
2022-10-11 08:50:54 -06:00
|
|
|
return generate_pb2.ClearCacheResponse()
|
2022-10-08 04:30:12 -06:00
|
|
|
|
2023-04-24 09:59:00 -06:00
|
|
|
async def FilterBatch(self, request, context):
|
|
|
|
batch = self.cache.pop(request.batch_id)
|
|
|
|
if batch is None:
|
|
|
|
raise ValueError(f"Batch ID {request.batch_id} not found in cache.")
|
2023-05-24 11:19:57 -06:00
|
|
|
filtered_batch = batch.filter(request.request_ids)
|
2023-04-24 09:59:00 -06:00
|
|
|
self.cache.set(filtered_batch)
|
|
|
|
|
|
|
|
return generate_pb2.FilterBatchResponse(batch=filtered_batch.to_pb())
|
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
async def Prefill(self, request, context):
|
2022-11-04 11:03:04 -06:00
|
|
|
batch = self.model.batch_type.from_pb(
|
2023-05-26 04:30:27 -06:00
|
|
|
request.batch, self.model.tokenizer, self.model.dtype, self.model.device
|
2022-11-04 11:03:04 -06:00
|
|
|
)
|
2022-10-11 08:50:54 -06:00
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
generations, next_batch = self.model.generate_token(batch)
|
2022-10-11 08:50:54 -06:00
|
|
|
self.cache.set(next_batch)
|
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
return generate_pb2.PrefillResponse(
|
|
|
|
generations=[generation.to_pb() for generation in generations],
|
2022-10-11 08:50:54 -06:00
|
|
|
batch=next_batch.to_pb() if next_batch else None,
|
2022-10-08 04:30:12 -06:00
|
|
|
)
|
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
async def Decode(self, request, context):
|
2022-10-11 08:50:54 -06:00
|
|
|
if len(request.batches) == 0:
|
|
|
|
raise ValueError("Must provide at least one batch")
|
|
|
|
|
|
|
|
batches = []
|
|
|
|
for batch_pb in request.batches:
|
|
|
|
batch = self.cache.pop(batch_pb.id)
|
|
|
|
if batch is None:
|
|
|
|
raise ValueError(f"Batch ID {batch_pb.id} not found in cache.")
|
2023-04-24 09:59:00 -06:00
|
|
|
batches.append(batch)
|
2023-04-20 03:07:40 -06:00
|
|
|
|
|
|
|
if len(batches) == 0:
|
|
|
|
raise ValueError("All batches are empty")
|
2022-10-11 08:50:54 -06:00
|
|
|
|
|
|
|
if len(batches) > 1:
|
2022-11-04 11:03:04 -06:00
|
|
|
batch = self.model.batch_type.concatenate(batches)
|
2022-10-11 08:50:54 -06:00
|
|
|
else:
|
|
|
|
batch = batches[0]
|
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
generations, next_batch = self.model.generate_token(batch)
|
2022-10-11 08:50:54 -06:00
|
|
|
self.cache.set(next_batch)
|
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
return generate_pb2.DecodeResponse(
|
|
|
|
generations=[generation.to_pb() for generation in generations],
|
2022-10-11 08:50:54 -06:00
|
|
|
batch=next_batch.to_pb() if next_batch else None,
|
|
|
|
)
|
|
|
|
|
2022-10-08 04:30:12 -06:00
|
|
|
|
2022-10-18 07:19:03 -06:00
|
|
|
def serve(
|
2023-02-03 04:43:37 -07:00
|
|
|
model_id: str,
|
2023-01-31 10:53:56 -07:00
|
|
|
revision: Optional[str],
|
2022-10-18 07:19:03 -06:00
|
|
|
sharded: bool,
|
2023-05-12 06:46:41 -06:00
|
|
|
quantize: Optional[str],
|
2023-05-23 12:40:39 -06:00
|
|
|
trust_remote_code: bool,
|
2022-10-18 07:19:03 -06:00
|
|
|
uds_path: Path,
|
|
|
|
):
|
2022-10-08 04:30:12 -06:00
|
|
|
async def serve_inner(
|
2023-02-03 04:43:37 -07:00
|
|
|
model_id: str,
|
2023-01-31 10:53:56 -07:00
|
|
|
revision: Optional[str],
|
2022-10-08 04:30:12 -06:00
|
|
|
sharded: bool = False,
|
2023-05-12 06:46:41 -06:00
|
|
|
quantize: Optional[str] = None,
|
2023-05-23 12:40:39 -06:00
|
|
|
trust_remote_code: bool = False,
|
2022-10-08 04:30:12 -06:00
|
|
|
):
|
2022-10-18 07:19:03 -06:00
|
|
|
unix_socket_template = "unix://{}-{}"
|
2022-10-08 04:30:12 -06:00
|
|
|
if sharded:
|
|
|
|
server_urls = [
|
2022-10-18 07:19:03 -06:00
|
|
|
unix_socket_template.format(uds_path, rank)
|
2022-10-28 11:24:00 -06:00
|
|
|
for rank in range(int(os.environ["WORLD_SIZE"]))
|
2022-10-08 04:30:12 -06:00
|
|
|
]
|
2022-10-28 11:24:00 -06:00
|
|
|
local_url = server_urls[int(os.environ["RANK"])]
|
2022-10-08 04:30:12 -06:00
|
|
|
else:
|
2022-10-18 07:19:03 -06:00
|
|
|
local_url = unix_socket_template.format(uds_path, 0)
|
2022-10-08 04:30:12 -06:00
|
|
|
server_urls = [local_url]
|
|
|
|
|
2023-02-07 07:38:22 -07:00
|
|
|
try:
|
2023-05-23 12:40:39 -06:00
|
|
|
model = get_model(model_id, revision, sharded, quantize, trust_remote_code)
|
2023-02-07 07:38:22 -07:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Error when initializing model")
|
|
|
|
raise
|
2022-10-28 11:24:00 -06:00
|
|
|
|
2023-02-13 05:02:45 -07:00
|
|
|
server = aio.server(
|
|
|
|
interceptors=[
|
|
|
|
ExceptionInterceptor(),
|
|
|
|
UDSOpenTelemetryAioServerInterceptor(),
|
|
|
|
]
|
|
|
|
)
|
2022-10-11 08:50:54 -06:00
|
|
|
generate_pb2_grpc.add_TextGenerationServiceServicer_to_server(
|
|
|
|
TextGenerationService(model, Cache(), server_urls), server
|
2022-10-08 04:30:12 -06:00
|
|
|
)
|
|
|
|
SERVICE_NAMES = (
|
2022-10-11 08:50:54 -06:00
|
|
|
generate_pb2.DESCRIPTOR.services_by_name["TextGenerationService"].full_name,
|
2022-10-08 04:30:12 -06:00
|
|
|
reflection.SERVICE_NAME,
|
|
|
|
)
|
|
|
|
reflection.enable_server_reflection(SERVICE_NAMES, server)
|
|
|
|
server.add_insecure_port(local_url)
|
2023-02-07 07:38:22 -07:00
|
|
|
|
2022-10-08 04:30:12 -06:00
|
|
|
await server.start()
|
2023-02-07 07:38:22 -07:00
|
|
|
|
2023-01-05 04:01:23 -07:00
|
|
|
logger.info("Server started at {}".format(local_url))
|
2023-02-07 07:38:22 -07:00
|
|
|
|
2022-10-18 07:19:03 -06:00
|
|
|
try:
|
|
|
|
await server.wait_for_termination()
|
|
|
|
except KeyboardInterrupt:
|
2023-01-05 04:01:23 -07:00
|
|
|
logger.info("Signal received. Shutting down")
|
2022-10-18 07:19:03 -06:00
|
|
|
await server.stop(0)
|
2022-10-08 04:30:12 -06:00
|
|
|
|
2023-05-23 12:40:39 -06:00
|
|
|
asyncio.run(serve_inner(model_id, revision, sharded, quantize, trust_remote_code))
|