-
Notifications
You must be signed in to change notification settings - Fork 29.7k
bad_words_ids no longer slow on mps #39556
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for identifying the issue and opening a PR! This does speed up things quite a lot, but it's still around 6 times slower than without setting bad_words_ids 🤔 Pretty much the same as just doing: eos_token_id_list = eos_token_id.tolist() # convert to python list before
bad_words_ids = list(
filter(lambda bad_token_seq: all(bad_token_seq != [i] for i in eos_token_id_list), bad_words_ids)
) |
Though... I guess, since this is just done once (in the init function), 2 seconds vs 36 seconds is still much better! |
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.
Already much better, forward pass could then be slow because of torch ops from SequenceBiasLogitsProcessor
happy to merge as is, but if you have time nice to investigate in the forward see if we can squeeze more perfs!
bad_words_ids = [ | ||
bad_token_seq for bad_token_seq in bad_words_ids | ||
if len(bad_token_seq) != 1 or bad_token_seq[0] not in eos_set | ||
] | ||
# Forbidding a sequence is equivalent to setting its bias to -inf | ||
sequence_bias = {tuple(sequence): float("-inf") for sequence in bad_words_ids} | ||
super().__init__(sequence_bias=sequence_bias) |
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.
SequenceBiasLogitsProcessor
is where I would look into slow downs!
Hi @ArthurZucker, the problem was in the transformers/src/transformers/generation/logits_process.py Lines 1187 to 1190 in 3bc726b
In the last commit I made, I modified it with a vectorized access and it's much faster, almost reaching the perfs when not using the |
What does this PR do?
Using the
bad_words_ids
on mps is slowing down a lot the text generation, this PR tries to address that.Fixes #39512
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@ArthurZucker