-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Fix remaining issues in beam score calculation #27808
Conversation
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.
Good catch, thanks for fixing!
BTW, run RUN_SLOW=1 py.test tests/models/vision_encoder_decoder/test_modeling_vision_encoder_decoder.py::ViT2GPT2ModelIntegrationTest::test_inference_coco_en
-- this test may need an update in its value
if decoder_prompt_len == input_ids.shape[-1]: | ||
continue | ||
# add up to the length which the next_scores is calculated on | ||
generated_len = input_ids[batch_beam_idx].shape[-1] + 1 - decoder_prompt_len |
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.
If I'm not mistaken, this is the same as cur_len
(L228). I'd suggest renaming cur_len
into generated_len
, which is more representative of the variable contents!
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.
Yes, the input_ids[batch_beam_idx].shape[-1]
should be same for each batch_beam_idx
, so we can simply use cur_len
here.
): | ||
""" | ||
Add a new hypothesis to the list. | ||
""" | ||
score = sum_logprobs / ((hyp.shape[-1] - decoder_prompt_len) ** self.length_penalty) | ||
if generated_len is not None: |
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.
I'd add a note that the else
case here exists for retrocompatibility reasons :)
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.
Thanks for the reminder. Added!
@gante I have updated the expectation value for this test. I have also incorporated all your suggestions. Ready to go! |
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.
Perfect, thanks for iterating 💛
Note for @ArthurZucker: there may be slow CI failures due to this change. Although I suspect there won't, since the correction is small. In any case, I'll keep an eye on the CI after this gets merged.
@@ -634,7 +634,7 @@ def test_eos_token_id_int_and_list_beam_search(self): | |||
"num_beams": 3, | |||
} | |||
if is_pt: |
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.
nit: we can actually remove this if/else, if the result is back into being the same :P
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.
redundant if/else removed
…he decoder prompt
All suggested changes are incorporated. Ready to go! @gante @ArthurZucker |
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.
thanks for this! 🤗
score = sum_logprobs / ((hyp.shape[-1] - decoder_prompt_len) ** self.length_penalty) | ||
if generated_len is not None: | ||
score = sum_logprobs / (generated_len**self.length_penalty) | ||
# This 'else' case exists for retrocompatibility |
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 'else' case exists for retrocompatibility | |
# This 'else' case exists for backward compatibility |
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.
oops, PR already merged, maybe let's stay with it for now?
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.
of course no worries
if is_pt: | ||
expectation = 20 | ||
else: | ||
# TODO (joao): fix me | ||
expectation = 13 |
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.
nice! 🔥
What does this PR do?
This PR further fixes the remaining issues in beam score calculation following #27351 .
More specifically:
hyp
inprocess
does not include the next generated token on which the current beam score is calculated, but thehyp
infinalize
includes all the generated tokens so far. This inconsistency is resolved by changing theadd
function ofBeamHypotheses
. Now we directly pass the current length of the generated tokens toadd
.is_done
function ofBeamHypotheses
, we are directly usingmax_length
without deductingdecoder_prompt_len
. It is fixed now.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@gante