You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 21, 2025. It is now read-only.
We have trouble doing vmap(grad(foo)) where foo includes one of the above operations. This is because Autograd ends up decomposing e.g. index_fill into tensor.clone().index_fill_(...) and the in-place operation is not vmap-compatible.
I've brainstormed two ways to solve this. There are tradeoffs for each and I'm wondering if someone else has a preference.
We use the functionalization pass to functionalize index_fill. Unfortunately this results in the following code:
self.clone(...).index_fill(dim, index, source)
which results in an unnecessary clone() which is not good for performance. IDK if we want to make the functionalization pass smarter in the future, this sounds complicated.
Approach 2: Add backward formulas for index_fill and all of the operations above (aka, turn them into primitive operations).
This means that both index_fill and index_fill_ get backward formulas (Could we get away with only giving index_fill a backward formula?). This is a pretty simple solution, the tradeoff is that we need to duplicate the formulas and we are setting the precedence that "operations that have an out-of-place variant must have a backward formula defined on the out-of-place variant".
Discussion
I prefer Approach 2 for its simplicity. To address the code duplication we can put the formulas into helper functions. Thoughts?