Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support streaming in DeepSpeed MII truss #66

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions deepspeed-mii/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
spec_version: '2.0'
base_image:
image: baseten/truss-server-base:3.11-gpu-v0.7.15
model_name: Llama-2-chat 7B DeepSpeed MII
description: Generate text from a prompt with this seven billion parameter language model.
model_metadata:
Expand Down Expand Up @@ -29,8 +27,3 @@ system_packages:
- cuda-toolkit-12-2
runtime:
predict_concurrency: 256
hf_cache:
- repo_id: meta-llama/Llama-2-7b-chat-hf
allow_patterns:
- "*.json"
- "*.bin"
72 changes: 55 additions & 17 deletions deepspeed-mii/model/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import asyncio
from typing import Dict
import logging
import queue
import threading
from typing import Any, Dict

import mii
from huggingface_hub import login
Expand All @@ -14,27 +17,62 @@ def __init__(self, **kwargs) -> None:
self.repo_id = model_metadata["repo_id"]
self.max_length = int(model_metadata["max_length"])
self.tensor_parallel = int(model_metadata["tensor_parallel"])
self.is_live_reload = kwargs["config"]["live_reload"]

def load(self):
login(token=self.hf_access_token)
# need to create a new loop because `mii.serve` creates async client at the end,
# and `load` function being called from new thread
asyncio.set_event_loop(asyncio.new_event_loop())
mii.serve(
self.repo_id,
tensor_parallel=self.tensor_parallel,
max_length=self.max_length,
)
try:
mii.serve(
self.repo_id,
tensor_parallel=self.tensor_parallel,
max_length=self.max_length,
)
except Exception as e:
if self.is_live_reload:
# in live reload `mii.serve` fails after reload since server is running already
logging.info(
"An exception occurred while starting mii server: %s, ignoring the exception due to live reload enabled",
e,
)
else:
raise e

def predict(self, request: Dict):
# we need to create new asyncio loop because each request is being server from new thread
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
client = mii.client(self.repo_id)
response = client.generate(
request.pop("prompt"),
max_new_tokens=request.pop("max_length", DEFAULT_RESPONSE_MAX_LENGTH),
)
new_loop.close()

return "\n".join(response.response)
prompt = request.pop("prompt")
generate_args = {
"max_new_tokens": request.pop("max_length", DEFAULT_RESPONSE_MAX_LENGTH)
}

if request.pop("stream", False):
return self.stream(prompt, generate_args)
else:
# we need to create new asyncio loop because each request is being server from new thread
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
client = mii.client(self.repo_id)
response = client.generate(prompt, **generate_args)
new_loop.close()

return {"result": "\n".join(response.response)}

def stream(self, prompt: str, generate_args: Dict[str, Any]):
q = queue.Queue()

def generate():
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
client = mii.client(self.repo_id)
client.generate(prompt, streaming_fn=q.put, **generate_args)
q.put(None)

threading.Thread(target=generate).start()

while True:
item = q.get() # This will block until an item is available
if item is not None:
yield "".join(item.response)
else:
break
Loading