-
Notifications
You must be signed in to change notification settings - Fork 30.2k
TF: XLA logits processors - minimum length, forced eos, and forced bos #16912
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -215,13 +215,18 @@ def __init__(self, min_length: int, eos_token_id: int): | |||||||||
self.min_length = min_length | ||||||||||
self.eos_token_id = eos_token_id | ||||||||||
|
||||||||||
def __call__(self, input_ids: tf.Tensor, scores: tf.Tensor, cur_len: int) -> tf.Tensor: | ||||||||||
# TODO(Matt) - this if statement has to be rewritten for XLA. Leaving it now though since | ||||||||||
# generate is not XLA - compileable anyways | ||||||||||
if cur_len < self.min_length: | ||||||||||
eos_token_id_mask = tf.broadcast_to(tf.range(scores.shape[-1]) == self.eos_token_id, scores.shape) | ||||||||||
scores = tf.where(eos_token_id_mask, float("-inf"), scores) | ||||||||||
def _apply_eos_token_mask(self, scores: tf.Tensor) -> tf.Tensor: | ||||||||||
eos_token_id_mask = tf.broadcast_to(tf.range(scores.shape[-1]) == self.eos_token_id, scores.shape) | ||||||||||
scores = tf.where(eos_token_id_mask, float("-inf"), scores) | ||||||||||
return scores | ||||||||||
|
||||||||||
def __call__(self, input_ids: tf.Tensor, scores: tf.Tensor, cur_len: int) -> tf.Tensor: | ||||||||||
# applies eos token masking if the first argument is true | ||||||||||
scores = tf.cond( | ||||||||||
tf.less(cur_len, self.min_length), | ||||||||||
lambda: self._apply_eos_token_mask(scores), | ||||||||||
lambda: tf.identity(scores), | ||||||||||
Comment on lines
+227
to
+228
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.
Suggested change
Would this work without the lambdas and identity call? I feel like it should but I'm not sure if I'm missing something obvious. 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. Nope, it doesn't. Super unintuitive, but It fails if we remove the lambda. |
||||||||||
) | ||||||||||
return scores | ||||||||||
|
||||||||||
|
||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.