-
Notifications
You must be signed in to change notification settings - Fork 27
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
Handle char level tokenization #11
Merged
+138
−11
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright (c) Together | ||
# This software is distributed under the terms of the Apache License, Version 2.0 | ||
# Author: Michael Poli | ||
|
||
|
@@ -8,6 +8,7 @@ | |
|
||
from src.sample import sample | ||
from src.utils import print_rank_0 | ||
from src.tokenizer import CharLevelTokenizer | ||
|
||
|
||
class Generator: | ||
|
@@ -17,7 +18,7 @@ | |
self.top_k = top_k | ||
self.top_p = top_p | ||
self.temperature = temperature | ||
self.untils = ["\n\n"] | ||
|
||
def generate( | ||
self, | ||
|
@@ -32,11 +33,20 @@ | |
stop_at_eos=True, | ||
max_seqlen=None, | ||
): | ||
eos_token_ids = self.tokenizer.tokenize(self.tokenizer.eos).to(device) | ||
|
||
if isinstance(self.tokenizer.eos, int): | ||
eos_token_ids = torch.LongTensor([self.tokenizer.eos]).to(device) | ||
else: | ||
# is a tensor | ||
eos_token_ids = self.tokenizer.tokenize(self.tokenizer.eos).to(device) | ||
|
||
if input_ids is None: | ||
input = self.tokenizer.tokenize(input_string) | ||
input = input.unsqueeze(0).to(device) | ||
if isinstance(input, list): | ||
input = torch.LongTensor(input).unsqueeze(0).to(device) | ||
# is a tensor | ||
else: | ||
input = input.unsqueeze(0).to(device) | ||
|
||
else: | ||
input = input_ids | ||
x = input | ||
|
@@ -76,6 +86,7 @@ | |
mem_after_tok = torch.cuda.memory_allocated(device=x.device) / 1e9 | ||
print_rank_0(f"Memory after tokenization: {mem_after_tok} GB") | ||
print_rank_0("Starting generation...") | ||
torch.cuda.memory._record_memory_history(enabled=True) | ||
if input_string is not None: | ||
print_rank_0("Prompt: " + input_string) | ||
else: | ||
|
@@ -96,10 +107,12 @@ | |
inference_params_dict_out["mha"].seqlen_offset += 1 | ||
inference_params_dict_out["hyena"].seqlen_offset += 1 | ||
|
||
logits, inference_params_dict_out = self.model( | ||
x, | ||
inference_params_dict=inference_params_dict_out, | ||
) | ||
# do forward pass with no gradient | ||
with torch.no_grad(): | ||
logits, inference_params_dict_out = self.model( | ||
x, | ||
inference_params_dict=inference_params_dict_out, | ||
) | ||
|
||
last_logits = logits[:, -1] | ||
|
||
|
@@ -113,7 +126,7 @@ | |
if stop_at_eos and (generation[0, -2:] == eos_token_ids).all(): | ||
print_rank_0("Stopping generation at EOS") | ||
|
||
if print_generation and verbose: | ||
if print_generation and verbose and batch_size == 1: | ||
print_rank_0( | ||
f"{self.tokenizer.detokenize([new_idx.item()])}", | ||
end=" ", | ||
|
@@ -128,12 +141,18 @@ | |
x = torch.cat([x, new_idx[:, None]], dim=-1) | ||
|
||
if verbose: | ||
y = self.tokenizer.detokenize_batch( | ||
generation[:, : i + 1], | ||
skip_special_tokens=skip_special_tokens, | ||
) | ||
if isinstance(self.tokenizer, CharLevelTokenizer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe could slightly reformat this to have different args depending on the Tokenizer class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reformatted |
||
y = self.tokenizer.detokenize_batch( | ||
generation[:, : i + 1], | ||
# skip_special_tokens=skip_special_tokens, # this isn't supported in the Char level tokenizer | ||
) | ||
else: | ||
y = self.tokenizer.detokenize_batch( | ||
generation[:, : i + 1], | ||
skip_special_tokens=skip_special_tokens, | ||
) | ||
|
||
for until in self.untils: | ||
if until in y: | ||
y = y.split(until)[0] | ||
break | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be on by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed