Fix precision errors from casting rotary parameters to FP16 with AMP#27700
Merged
ArthurZucker merged 10 commits intohuggingface:mainfrom Nov 29, 2023
kevinhu:fix-einsum-amp
Merged
Fix precision errors from casting rotary parameters to FP16 with AMP#27700ArthurZucker merged 10 commits intohuggingface:mainfrom kevinhu:fix-einsum-amp
ArthurZucker merged 10 commits intohuggingface:mainfrom
kevinhu:fix-einsum-amp
Conversation
Collaborator
ArthurZucker
left a comment
There was a problem hiding this comment.
Hey! Thanks for opening this PR, it seems to me that the issue lies with AMP no?
My only concern would have been performances, outer might be a little bit slower but it seems to be negligible so LGTM.
Let's make sure that the failing test is fixed!
Collaborator
ArthurZucker
left a comment
There was a problem hiding this comment.
I ran the following script for benchmarking:
import torch
from torch.utils import benchmark
results = []
for b in [10, 10000, 2000000]:
for n in [10, 100, 10000, 1000000]:
if b * n >= 1000000000:
continue
description = f'[{b}, {n}]'
x = torch.rand(b, device='mps')
y = torch.rand(n, device='mps')
results.append(benchmark.Timer(
stmt='torch.outer(x,y)',
globals={'x': x, 'y': y},
description=description,
).blocked_autorange())
results.append(benchmark.Timer(
stmt='torch.einsum("i,j->ij",x,y)',
globals={'x': x, 'y': y},
description=description,
).blocked_autorange())
compare = benchmark.Compare(results)
compare.trim_significant_figures()
compare.colorize()
compare.print()So looks good to me 😉
ArthurZucker
approved these changes
Nov 28, 2023
Collaborator
ArthurZucker
left a comment
There was a problem hiding this comment.
failing test is unrelated to the PR i'll fix it on main
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
Collaborator
|
FYI @gante and @Rocketknight1 if we see anything failing. I ran slow tests locally and it was all good |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
When training with AMP, using
einsumto multiplytandself.inv_freqwill introduce precision errors because it casts the result to FP16. This can be avoided by usingtorch.outerinstead, as originally mentioned here: https://github.com/Dao-AILab/flash-attention/blob/2c3baba4a63c4007c8a132c5380edc9430f88a22/flash_attn/layers/rotary.py#L396C1-L398C45Before 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.