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

return correct finish reasons in generate_text_func #210

Merged
merged 4 commits into from
Oct 31, 2023
Merged
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
24 changes: 16 additions & 8 deletions caikit_nlp/toolkit/text_generation/model_run_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Third Party
from peft.peft_model import PeftModel
from transformers import StoppingCriteria, TextStreamer
from transformers import AutoModel, AutoTokenizer, StoppingCriteria, TextStreamer
import numpy as np
import torch

Expand Down Expand Up @@ -131,10 +131,10 @@ def __iter__(self):


def generate_text_func(
model,
tokenizer,
model: "Union[PeftModel, AutoModel]",
tokenizer: "AutoTokenizer",
producer_id: ProducerId,
eos_token: str,
eos_token: Optional[str],
text: str,
max_new_tokens: Optional[int] = 20,
min_new_tokens: Optional[int] = 0,
Expand Down Expand Up @@ -234,12 +234,20 @@ def generate_text_func(
for g in generate_ids
]

if generate_ids[0][-1].item() == eos_token:
if (eos_token and tokenizer.decode(generate_ids[0, -1].item()) == eos_token) or (
gkumbhat marked this conversation as resolved.
Show resolved Hide resolved
generate_ids[0, -1] == tokenizer.eos_token_id
):
finish_reason = "EOS_TOKEN"
elif generate_ids.size(1) - 1 == max_new_tokens:
finish_reason = "MAX_TOKENS"
elif ("stopping_criteria" in gen_optional_params) and (
gen_optional_params["stopping_criteria"](
generate_ids,
None, # scores, unused by SequenceStoppingCriteria
)
):
finish_reason = "STOP_SEQUENCE"
else:
dtrifiro marked this conversation as resolved.
Show resolved Hide resolved
finish_reason = "OTHER"
finish_reason = "MAX_TOKENS"

return GeneratedTextResult(
generated_tokens=token_count,
generated_text=preds[0],
Expand Down