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

WIP: attempting to fix multibyte generation issues when streaming #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
95 changes: 93 additions & 2 deletions llama_cpp/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,8 @@ def _create_completion(

finish_reason = "length"
multibyte_fix = 0
buffered_tokens = []

for token in self.generate(
prompt_tokens,
top_k=top_k,
Expand Down Expand Up @@ -991,6 +993,24 @@ def _create_completion(
remaining_text = self.detokenize(remaining_tokens)
remaining_length = len(remaining_text)

if buffered_tokens:
# Handle case where we still have multibyte tokens to flush from prev cycle
yield {
"id": completion_id,
"object": "text_completion",
"created": created,
"model": model_name,
"choices": [
{
"text": self.detokenize(buffered_tokens).decode("utf-8", errors="ignore"),
"index": 0,
"logprobs": None,
"finish_reason": None,
}
],
}
buffered_tokens = []

# We want to avoid yielding any characters from
# the generated text if they are part of a stop
# sequence.
Expand All @@ -1004,7 +1024,7 @@ def _create_completion(

token_end_position = 0
for token in remaining_tokens:
token_end_position += len(self.detokenize([token]))
token_end_position += len(self.detokenize([token])) - 1
# Check if stop sequence is in the token
if token_end_position >= (remaining_length - first_stop_position):
break
Expand Down Expand Up @@ -1042,7 +1062,62 @@ def _create_completion(
"token_logprobs": [current_logprobs[int(token)]],
"top_logprobs": [top_logprob],
}
returned_tokens += 1

returned_tokens += 1

de_tokenized = int.from_bytes(self.detokenize([token]), sys.byteorder)

if de_tokenized & 0b11000000 == 0b11000000:
# Start of a multibyte sequence

if buffered_tokens:
# Handle edge case where we have subsequent multibyte generated
print('Empty buffer - went from one unicode to next')
yield {
"id": completion_id,
"object": "text_completion",
"created": created,
"model": model_name,
"choices": [
{
"text": self.detokenize(buffered_tokens).decode("utf-8", errors="ignore"),
"index": 0,
"logprobs": None,
"finish_reason": None,
}
],
}
buffered_tokens = []

buffered_tokens.append(token)
continue
elif de_tokenized & 0b10000000 == 0b10000000:
# continuation of a multibyte sequence
print('Hit a continuation')
buffered_tokens.append(token)
continue

# Handle standard tokens

if buffered_tokens:
# Assume multibyte sequence has ended so flush buffer
print('Hit a normal token, empty buffer')
yield {
"id": completion_id,
"object": "text_completion",
"created": created,
"model": model_name,
"choices": [
{
"text": self.detokenize(buffered_tokens).decode("utf-8", errors="ignore"),
"index": 0,
"logprobs": None,
"finish_reason": None,
}
],
}
buffered_tokens = []

yield {
"id": completion_id,
"object": "text_completion",
Expand All @@ -1065,6 +1140,22 @@ def _create_completion(
finish_reason = "length"
break

if buffered_tokens:
yield {
"id": completion_id,
"object": "text_completion",
"created": created,
"model": model_name,
"choices": [
{
"text": self.detokenize(buffered_tokens).decode("utf-8", errors="ignore"),
"index": 0,
"logprobs": None,
"finish_reason": None,
}
],
}

if stopping_criteria is not None and stopping_criteria(
self._input_ids.tolist(), self._scores[-1, :].tolist()
):
Expand Down