Repository navigation
[inductor] Improve stability of scaled softmax - #124119
peterbell10 wants to merge 9 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/124119
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 3445a33 with merge base 8bf9e99 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This adds a pattern which replaces: ```python scale(x) - scale(x).amax(dim, keepdim=True) ``` with ```python scale(x - x.amax(dim, keepdim=True)) ``` where `scale` can be either multiplication or division by a scalar, or a tensor that is broadcast in the `dim` dimension. We can find this pattern inside of the decomposed graph of: ```python F.softmax(scale(x), dim=dim) ``` This has the effect of both reducing the chance of hitting the `fma` issue and also means we avoid recomputing `scale(x)` inside and outside the reduction which may be significant if we can remove an extra division. ghstack-source-id: d36b83d Pull Request resolved: #124119
|
Curious - do you think the right place to apply this is at the graph level or perhaps lower inside Inductor? |
|
Note that the general pattern here would be |
I think this has to be done at the graph level as reductions are always realized, so the pattern would be split across two computed buffers. One buffer does
While that pattern does make mathematical sense, I don't think it works here as |
This adds a pattern which replaces: ```python scale(x) - scale(x).amax(dim, keepdim=True) ``` with ```python scale(x - x.amax(dim, keepdim=True)) ``` where `scale` can be either multiplication or division by a scalar, or a tensor that is broadcast in the `dim` dimension. We can find this pattern inside of the decomposed graph of: ```python F.softmax(scale(x), dim=dim) ``` This has the effect of both reducing the chance of hitting the `fma` issue and also means we avoid recomputing `scale(x)` inside and outside the reduction which may be significant if we can remove an extra division. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy yf225 chenyang78 kadeng muchulee8 aakhundov ColinPeppler amjames desertfire chauhang [ghstack-poisoned]
This adds a pattern which replaces: ```python scale(x) - scale(x).amax(dim, keepdim=True) ``` with ```python scale(x - x.amax(dim, keepdim=True)) ``` where `scale` can be either multiplication or division by a scalar, or a tensor that is broadcast in the `dim` dimension. We can find this pattern inside of the decomposed graph of: ```python F.softmax(scale(x), dim=dim) ``` This has the effect of both reducing the chance of hitting the `fma` issue and also means we avoid recomputing `scale(x)` inside and outside the reduction which may be significant if we can remove an extra division. ghstack-source-id: 9d7c6e2 Pull Request resolved: #124119
|
yeah, the more general pattern just asks for F to be 1-homogeneous, but we don't have a good way of marking which ops are 1-homogeneous. (I realised of this after writing this but I don't currently have much access to internet lol). We could write a small tool that uses OpInfos to figure out which ops are 1-homogeneous (or, in general, have any property that is stable under compositions (i.e. it's functorial in some sense)). Of course this is well outside this PR, but I may pitch this problem to someone, as I think it would be quite useful to write mathematically sound transformations. |
[ghstack-poisoned]
[ghstack-poisoned]
This adds a pattern which replaces: ```python scale(x) - scale(x).amax(dim, keepdim=True) ``` with ```python scale(x - x.amax(dim, keepdim=True)) ``` where `scale` can be either multiplication or division by a scalar, or a tensor that is broadcast in the `dim` dimension. We can find this pattern inside of the decomposed graph of: ```python F.softmax(scale(x), dim=dim) ``` This has the effect of both reducing the chance of hitting the `fma` issue and also means we avoid recomputing `scale(x)` inside and outside the reduction which may be significant if we can remove an extra division. ghstack-source-id: 467a400 Pull Request resolved: #124119
lezcano
left a comment
There was a problem hiding this comment.
Nice! Just one question. What happens to the other PRs after this one? Are still relevant? Do we still want to disable fma at large?
| # | ||
| # which is more stable as we only compute the scaling once. | ||
| # | ||
| # NOTE: This pattern must come after fused attention matching! |
There was a problem hiding this comment.
I think the existing fused attention tests are sufficient. If this pattern is applied first then the attention patterns wouldn't match any more.
This is a tough question. On the stack disabling fma I'm seeing more accuracy failures after already fixing a handful. Given that the goal is ostensibly to improve accuracy, this isn't a great sign. I think it might be worth exploring an fx graph level fma insertion. That fixes potential discrepancies between the same value being recomputed in different places, while still preserving the accuracy improvements from fma. However, there's still the issue that many of these operations aren't visible at the fx graph level. Once we reach inductor IR it's too late though as we don't know if an intermediate has multiple users. |
|
@pytorchbot merge |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
[ghstack-poisoned]
This adds a pattern which replaces: ```python scale(x) - scale(x).amax(dim, keepdim=True) ``` with ```python scale(x - x.amax(dim, keepdim=True)) ``` where `scale` can be either multiplication or division by a scalar, or a tensor that is broadcast in the `dim` dimension. We can find this pattern inside of the decomposed graph of: ```python F.softmax(scale(x), dim=dim) ``` This has the effect of both reducing the chance of hitting the `fma` issue and also means we avoid recomputing `scale(x)` inside and outside the reduction which may be significant if we can remove an extra division. ghstack-source-id: 995ca2e Pull Request resolved: #124119
| one = torch.scalar_tensor(1, dtype=inp.dtype, device=inp.device) | ||
| sign = torch.where(other >= 0, one, -one) |
There was a problem hiding this comment.
nit. you can even
| one = torch.scalar_tensor(1, dtype=inp.dtype, device=inp.device) | |
| sign = torch.where(other >= 0, one, -one) | |
| sign = torch.where(other >= 0, 1, -1) |
There was a problem hiding this comment.
I need these to be tensors so where returns the correct dtype.
|
Why did the last commit fixes a perf regression? |
Basically |
This adds a pattern which replaces: ```python scale(x) - scale(x).amax(dim, keepdim=True) ``` with ```python scale(x - x.amax(dim, keepdim=True)) ``` where `scale` can be either multiplication or division by a scalar, or a tensor that is broadcast in the `dim` dimension. We can find this pattern inside of the decomposed graph of: ```python F.softmax(scale(x), dim=dim) ``` This has the effect of both reducing the chance of hitting the `fma` issue and also means we avoid recomputing `scale(x)` inside and outside the reduction which may be significant if we can remove an extra division. ghstack-source-id: 877abaa Pull Request resolved: pytorch#124119
|
Rebased |
Pull Request resolved: #124616 Approved by: https://github.com/lezcano ghstack dependencies: #124119
Stack from ghstack (oldest at bottom):
This adds a pattern which replaces:
with
where
scalecan be either multiplication or division by a scalar,or a tensor that is broadcast in the
dimdimension.We can find this pattern inside of the decomposed graph of:
This has the effect of both reducing the chance of hitting the
fmaissue and also means we avoid recomputing
scale(x)inside and outsidethe reduction which may be significant if we can remove an extra division.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @aakhundov @ColinPeppler @amjames @desertfire @chauhang