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

Stop at the stop token #6957

Merged
merged 2 commits into from
Jul 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions nemo/collections/nlp/modules/common/megatron_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def get_generation(prompt, greedy, add_BOS, token_to_gen, min_tokens, temp, top_
response = text_generation(data, port=port)
sentences = response['sentences']
bot_message = sentences[0]
if bot_message.find('<extra_id_0') < 0:
# hack due to the problem that huggingface's tokenizer strips out the <extra_id_x> token
prompt = prompt.replace('<extra_id_0>', '').replace('<extra_id_1>', '').replace('<extra_id_2>', '')
bot_message = bot_message[len(prompt) :]
return bot_message

Expand Down
16 changes: 10 additions & 6 deletions nemo/collections/nlp/modules/common/text_generation_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,19 @@ def end_of_generation_condition(
else:
tokenizer = self.model.tokenizer
conditions = []
end_tokens = set()
end_tokens.add(eod_id)
for end_string in end_strings:
ids_1 = tokenizer.text_to_ids(f'<extra_id_1>{end_string}')
ids_2 = tokenizer.text_to_ids('<extra_id_1>')
if len(ids_1) <= len(ids_2):
continue
token_id = ids_1[len(ids_2) :][0]
end_tokens.add(token_id)
for p, token_item in zip(prev, tokens):
text = tokenizer.ids_to_text(token_item.tolist())
conditions.append(
any(
[
p.item() == eod_id if end_string == END_OF_SEQ else text.endswith(end_string)
for end_string in end_strings
]
)
any([text.endswith(end_string) for end_string in end_strings] + [p.item() in end_tokens])
)
return torch.tensor(conditions, dtype=torch.bool, device=tokens.device)

Expand Down