Skip to content

Added correct timeout #1219

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

Merged
merged 2 commits into from
Dec 5, 2023
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
18 changes: 11 additions & 7 deletions pgml-extension/src/bindings/transformers/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ def put(self, values):
self.text_index_cache[i] += len(printable_text)
output.append(printable_text)
if any(output):
self.text_queue.put(output, self.timeout)
self.text_queue.put(output)

def end(self):
self.next_tokens_are_prompt = True
output = []
for i, tokens in enumerate(self.token_cache):
text = self.tokenizer.decode(tokens, **self.decode_kwargs)
output.append(text[self.text_index_cache[i] :])
self.text_queue.put(output, self.timeout)
self.text_queue.put(self.stop_signal, self.timeout)
self.text_queue.put(output)
self.text_queue.put(self.stop_signal)

def __iter__(self):
return self
Expand Down Expand Up @@ -264,12 +264,13 @@ def __init__(self, model_name, **kwargs):
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token

def stream(self, input, **kwargs):
def stream(self, input, timeout=None, **kwargs):
streamer = None
generation_kwargs = None
if self.task == "conversational":
streamer = TextIteratorStreamer(
self.tokenizer,
timeout=timeout,
skip_prompt=True,
)
if "chat_template" in kwargs:
Expand All @@ -286,7 +287,10 @@ def stream(self, input, **kwargs):
input = self.tokenizer(input, return_tensors="pt").to(self.model.device)
generation_kwargs = dict(input, streamer=streamer, **kwargs)
else:
streamer = TextIteratorStreamer(self.tokenizer)
streamer = TextIteratorStreamer(
self.tokenizer,
timeout=timeout,
)
input = self.tokenizer(input, return_tensors="pt", padding=True).to(
self.model.device
)
Expand Down Expand Up @@ -355,7 +359,7 @@ def create_pipeline(task):
return pipe


def transform_using(pipeline, args, inputs, stream=False):
def transform_using(pipeline, args, inputs, stream=False, timeout=None):
args = orjson.loads(args)
inputs = orjson.loads(inputs)

Expand All @@ -364,7 +368,7 @@ def transform_using(pipeline, args, inputs, stream=False):
convert_eos_token(pipeline.tokenizer, args)

if stream:
return pipeline.stream(inputs, **args)
return pipeline.stream(inputs, timeout=timeout, **args)
return orjson.dumps(pipeline(inputs, **args), default=orjson_default).decode()


Expand Down