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

Remove the stripping of the prefix space (and any other mangling that tokenizers might do). #1065

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 5 additions & 2 deletions server/text_generation_server/models/causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,11 @@ def generate_token(
if i % self.world_size == self.rank:
if stop:
# Decode generated tokens
output_text = self.decode(
all_input_ids[-stopping_criteria.current_tokens :, 0]
output_text, _, _ = self.decode_token(
all_input_ids[:, 0],
prefix_offset=len(all_input_ids) - stopping_criteria.current_tokens - 1,
read_offset=len(all_input_ids) - stopping_criteria.current_tokens,
skip_special_tokens=True
)
# Get seed
if isinstance(next_token_chooser.choice, Sampling):
Expand Down
12 changes: 5 additions & 7 deletions server/text_generation_server/models/flash_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,6 @@ def warmup(self, batch: FlashCausalLMBatch):

return int(num_blocks * BLOCK_SIZE)

def decode(self, generated_ids: Union[torch.Tensor, List[int]]) -> str:
return self.tokenizer.decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)

def forward(
self,
input_ids: torch.Tensor,
Expand Down Expand Up @@ -1008,8 +1003,11 @@ def generate_token(
if i % self.world_size == self.rank:
if stop:
# Decode generated tokens
output_text = self.decode(
all_input_ids[-stopping_criteria.current_tokens :]
output_text, _, _ = self.decode_token(
all_input_ids,
prefix_offset=len(all_input_ids) - stopping_criteria.current_tokens - 1,
read_offset=len(all_input_ids) - stopping_criteria.current_tokens,
skip_special_tokens=True
)
generated_text = GeneratedText(
output_text,
Expand Down
12 changes: 5 additions & 7 deletions server/text_generation_server/models/idefics_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,6 @@ def __init__(
def batch_type(self) -> Type[IdeficsCausalLMBatch]:
return IdeficsCausalLMBatch

def decode(self, generated_ids: List[int]) -> str:
return self.tokenizer.decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)

def forward(
self,
input_ids,
Expand Down Expand Up @@ -728,8 +723,11 @@ def generate_token(
if i % self.world_size == self.rank:
if stop:
# Decode generated tokens
output_text = self.decode(
all_input_ids[-stopping_criteria.current_tokens :, 0]
output_text, _, _ = self.decode_token(
all_input_ids[:, 0],
prefix_offset=len(all_input_ids) - stopping_criteria.current_tokens - 1,
read_offset=len(all_input_ids) - stopping_criteria.current_tokens,
skip_special_tokens=True
)
# Get seed
if isinstance(next_token_chooser.choice, Sampling):
Expand Down
5 changes: 3 additions & 2 deletions server/text_generation_server/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,17 @@ def decode_token(
all_input_ids: List[int],
prefix_offset: int = 0,
read_offset: int = 0,
skip_special_tokens: bool = False,
) -> Tuple[str, int, int]:
"""Hack to hopefully support generate_stream for the maximum number of tokenizers"""

# The prefix text is necessary only to defeat cleanup algorithms in the decode
# which decide to add a space or not depending on the surrounding ids.
prefix_text = self.tokenizer.decode(
all_input_ids[prefix_offset:read_offset], skip_special_tokens=False
all_input_ids[prefix_offset:read_offset], skip_special_tokens=skip_special_tokens
)
new_text = self.tokenizer.decode(
all_input_ids[prefix_offset:], skip_special_tokens=False
all_input_ids[prefix_offset:], skip_special_tokens=skip_special_tokens
)

if len(new_text) > len(prefix_text) and not new_text.endswith("�"):
Expand Down
7 changes: 5 additions & 2 deletions server/text_generation_server/models/seq2seq_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,11 @@ def generate_token(
if stop:
# Slice with decoder_input_length to remove padding
# Decode all tokens
output_text = self.decode(
all_decoder_input_ids[-decoder_input_length:]
output_text, _, _ = self.decode_token(
all_decoder_input_ids,
prefix_offset=len(all_decoder_input_ids) - decoder_input_length - 1,
read_offset=len(all_decoder_input_ids) - decoder_input_length,
skip_special_tokens=True
)

# Get seed
Expand Down