Skip to content

Working streaming tokenizer #1210

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 1, 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
41 changes: 33 additions & 8 deletions pgml-extension/src/bindings/transformers/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,42 @@ def __init__(self, tokenizer, skip_prompt=False, timeout=None, **decode_kwargs):
self.next_tokens_are_prompt = True
self.stop_signal = None
self.text_queue = queue.Queue()
self.token_cache = []
self.text_index_cache = []

def put(self, value):
def put(self, values):
if self.skip_prompt and self.next_tokens_are_prompt:
self.next_tokens_are_prompt = False
return
# Can't batch this decode
decoded_values = []
for v in value:
decoded_values.append(self.tokenizer.decode(v, **self.decode_kwargs))
self.text_queue.put(decoded_values, self.timeout)
output = []
for i, v in enumerate(values):
if len(self.token_cache) <= i:
self.token_cache.append([])
self.text_index_cache.append(0)
token = v.tolist() # Returns a list or number
if type(token) == list:
self.token_cache[i].extend(token)
else:
self.token_cache[i].append(token)
text = self.tokenizer.decode(self.token_cache[i], **self.decode_kwargs)
if text.endswith("\n"):
output.append(text[self.text_index_cache[i] :])
self.token_cache[i] = []
self.text_index_cache[i] = 0
else:
printable_text = text[self.text_index_cache[i] : text.rfind(" ") + 1]
self.text_index_cache[i] += len(printable_text)
output.append(printable_text)
if any(output):
self.text_queue.put(output, self.timeout)

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)

def __iter__(self):
Expand All @@ -127,6 +150,7 @@ def __next__(self):
if value != self.stop_signal:
return value


class GGMLPipeline(object):
def __init__(self, model_name, **task):
import ctransformers
Expand Down Expand Up @@ -245,7 +269,8 @@ def stream(self, input, **kwargs):
generation_kwargs = None
if self.task == "conversational":
streamer = TextIteratorStreamer(
self.tokenizer, skip_prompt=True, skip_special_tokens=True
self.tokenizer,
skip_prompt=True,
)
if "chat_template" in kwargs:
input = self.tokenizer.apply_chat_template(
Expand All @@ -261,7 +286,7 @@ 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, skip_special_tokens=True)
streamer = TextIteratorStreamer(self.tokenizer)
input = self.tokenizer(input, return_tensors="pt", padding=True).to(
self.model.device
)
Expand Down
4 changes: 4 additions & 0 deletions pgml-sdks/pgml/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ async def migrate() -> None

Json = Any
DateTime = int
GeneralJsonIterator = Any
GeneralJsonAsyncIterator = Any
"#;

const ADDITIONAL_DEFAULTS_FOR_JAVASCRIPT: &[u8] = br#"
Expand All @@ -16,6 +18,8 @@ export function migrate(): Promise<void>;

export type Json = any;
export type DateTime = Date;
export type GeneralJsonIterator = any;
export type GeneralJsonAsyncIterator = any;

export function newCollection(name: string, database_url?: string): Collection;
export function newModel(name?: string, source?: string, parameters?: Json): Model;
Expand Down
1 change: 0 additions & 1 deletion pgml-sdks/pgml/python/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ async def test_open_source_ai_create_async():
],
temperature=0.85,
)
import json
assert len(results["choices"]) > 0


Expand Down
6 changes: 3 additions & 3 deletions pgml-sdks/pgml/src/languages/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ fn transform_stream_iterate_next(mut cx: FunctionContext) -> JsResult<JsPromise>
.expect("Error converting rust Json to JavaScript Object");
let d = cx.boolean(false);
o.set(&mut cx, "value", v)
.expect("Error setting object value in transform_sream_iterate_next");
.expect("Error setting object value in transform_stream_iterate_next");
o.set(&mut cx, "done", d)
.expect("Error setting object value in transform_sream_iterate_next");
.expect("Error setting object value in transform_stream_iterate_next");
} else {
let d = cx.boolean(true);
o.set(&mut cx, "done", d)
.expect("Error setting object value in transform_sream_iterate_next");
.expect("Error setting object value in transform_stream_iterate_next");
}
Ok(o)
})
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy