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

Use LifoQueue for turbomind async_stream_infer #1179

Merged
merged 3 commits into from
Feb 27, 2024
Merged
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
52 changes: 37 additions & 15 deletions lmdeploy/turbomind/turbomind.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from configparser import ConfigParser
from contextlib import contextmanager
from queue import Queue
from queue import LifoQueue, Queue
from threading import Thread
from typing import Iterable, List, Optional, Union

Expand Down Expand Up @@ -507,6 +507,27 @@ def _func(device_id, enque_output):
t.start()
self.threads[device_id] = t

def _async_forward_callback(self, result, ctx, que: LifoQueue):
que.put((False, result))

def _async_forward_thread(self, inputs, que: LifoQueue):
instance_comm = self.tm_model.model_comm.create_instance_comm(
self.gpu_count)

def _func(device_id, enque_output):
with cuda_ctx(device_id):
output = self.model_insts[device_id].forward(
inputs, instance_comm)
if enque_output:
que.put((True, output))

for device_id in range(self.gpu_count):
t = Thread(target=_func,
args=(device_id, device_id == 0),
daemon=True)
t.start()
self.threads[device_id] = t

def _update_generation_config(self, config: EngineGenerationConfig,
**kwargs: dict):
if config is None:
Expand Down Expand Up @@ -699,8 +720,13 @@ async def async_stream_infer(self,
stream_output (bool): indicator for stream output
kwargs (dict): kwargs for backward compatibility
"""
# start forward thread
que = LifoQueue()
from functools import partial
_forward_callback = partial(self._async_forward_callback, que=que)
_forward_thread = partial(self._async_forward_thread, que=que)
if stream_output and not stop:
self.model_insts[0].register_callback(self._forward_callback)
self.model_insts[0].register_callback(_forward_callback)

gen_config = self._update_generation_config(gen_config, **kwargs)
inputs, input_lengths = self.prepare_inputs(
Expand All @@ -715,23 +741,17 @@ async def async_stream_infer(self,
gen_config=gen_config)

tm_inputs = _np_dict_to_tm_dict(inputs)
# start forward thread
self.que = Queue()
self._forward_thread(tm_inputs)
_forward_thread(tm_inputs)

seq_start = input_lengths + input_lengths.new_tensor(step)

prev_len = 0
# generator
while True:
# Thanks for https://github.com/frankxyy and his issue
# https://github.com/InternLM/lmdeploy/issues/832
while self.que.qsize() == 0:
await asyncio.sleep(0.002) # sleep(0) makes server unstable
while que.qsize() == 0: # let other requests in
await asyncio.sleep(0.002)

while self.que.qsize() > 1:
self.que.get()

finish, tm_outputs = self.que.get()
finish, tm_outputs = que.get()

outputs = _tm_dict_to_torch_dict(tm_outputs)

Expand All @@ -756,13 +776,15 @@ async def async_stream_infer(self,
outputs = (status, output[:-1].tolist(), len_)
else:
outputs = (status, output.tolist(), len_)
if outputs[-1] < prev_len and not finish:
continue
else:
prev_len = outputs[-1]
yield outputs

if finish:
for t in self.threads:
t.join()
while self.que.qsize() > 0:
self.que.get()
break

if stream_output and not stop:
Expand Down
Loading