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

Add support for returning alternative_tokens with return_k_alternatives #697

Merged
merged 1 commit into from
Nov 27, 2024
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
61 changes: 31 additions & 30 deletions server/lorax_server/models/flash_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from lorax_server.models.model import Model
from lorax_server.models.types import (
AlternativeTokens,
Batch,
GeneratedText,
Generation,
Expand Down Expand Up @@ -1877,36 +1878,6 @@ def generate_token(
) in enumerate(iterator):
all_alternative_tokens = [] if request.parameters.return_k_alternatives > 0 else None

# TODO(travis): return_k_alternatives
# if request.parameters.return_k_alternatives > 0:
# # Limit the number of alternatives to the vocabulary size
# num_alternatives = min(
# request.parameters.return_k_alternatives,
# len(alternative_token_ids[token_idx]),
# )

# # Select top-k logprobs
# request_alternative_token_ids = alternative_token_ids[token_idx][:num_alternatives]
# request_alternative_token_logprobs = alternative_token_logprobs[token_idx][:num_alternatives]

# # Decode tokens
# request_alternative_token_texts = []
# for alternative_token_id in request_alternative_token_ids:
# all_input_ids.append(alternative_token_id)
# alternative_token_text, _, _ = self.decode_token(
# all_input_ids,
# prefix_offset,
# read_offset,
# )
# request_alternative_token_texts.append(alternative_token_text)
# all_input_ids.pop()
# alternative_tokens = AlternativeTokens(
# request_alternative_token_ids,
# request_alternative_token_logprobs,
# request_alternative_token_texts,
# )
# all_alternative_tokens.append(alternative_tokens)

# Compute logprobs first as, even though we might skip the token,
# it can still be required to compute the logprobs
# modulo on request.id as it is robust to batch.filter whereas the index in the batch is not and we need
Expand Down Expand Up @@ -1985,6 +1956,36 @@ def generate_token(
)
next_token_texts.append(next_token_text)

if request.parameters.return_k_alternatives > 0:
# Limit the number of alternatives to the vocabulary size
num_alternatives = min(
request.parameters.return_k_alternatives,
len(alternative_token_ids[j]),
)

# Select top-k logprobs
request_alternative_token_ids = alternative_token_ids[j][:num_alternatives]
request_alternative_token_logprobs = alternative_token_logprobs[j][:num_alternatives]

# Decode tokens
request_alternative_token_texts = []
for alternative_token_id in request_alternative_token_ids:
all_input_ids.append(alternative_token_id)
alternative_token_text, _, _ = self.decode_token(
all_input_ids,
prefix_offset,
read_offset,
)
request_alternative_token_texts.append(alternative_token_text)
all_input_ids.pop()
alternative_tokens = AlternativeTokens(
request_alternative_token_ids,
request_alternative_token_logprobs,
request_alternative_token_texts,
)
all_alternative_tokens.append(alternative_tokens)


stop, reason = stopping_criteria(
next_token_id,
next_token_text,
Expand Down