Shrink attention_mask if it's larger than the cache #23
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.
Resolves #22
Hello!
Pull Request overview
attention_mask
if it's larger than the cache.Details
The
attention_mask
intransformers
lives under a condition that it can only grow. Makes sense: we only add a new token on every new model forward call. However, that isn't the case withattention_sinks
. The first forward call will parse the entire input text, and the subsequent one will process just one token, with the history in the cache.However, if the cache is smaller than the input size, the
attention_mask
will still have the input size, e.g. [1, 3700] while the history + key_size is just 257.Tests
I ran the script from #22 and got the following output:
cc: @pseudotensor I do want to point out that
attention_sinks
doesn't extend the context size of a model - if the window size is 256 tokens (i.e. 252 window size and 4 sink tokens), then the model won't be able to use the full 3700 tokens of the document when generating its output. You likely know this, but I'm just making sure.