Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR proposes an optimization for the ProphetNet model. The current implementation calculates an attention bias mask by looping through the position to unmask. It performs a high number of assignments (
ngram*sequence_length) which can be in the order of ~1000. Single tensor assignments, especially on accelerators, are inefficient.This PR proposes a vectorized implementation which performs at most
ngramassignments, which should be significantly lower thanngram * sequence_length.A quick experiment shown at https://gist.github.com/guillaume-be/e6b862c701fac1b54765e7af7e71c641 shows that:
ngram_attention_biascalculation is very expensive, taking close to 230ms (!) on a GPUWho can review?
@patrickvonplaten maybe you would be a good candidate? I could not find anyone assigned for ProphetNet
edit: pushed some further optimization, further accelerating by ~40%