Extend sum of mul rewrite for multiple axis #484
Merged
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.
Extend the Sum of Mul rewrite beyond the scalar case. We extract variables that have only degenerate dims along the reduced dimensions. In the case of a total sum only scalars can be pulled out, which was exactly the case supported by the old rewrite.
I am not so hot on the Prod of Mul rewrite, but since it was there already, we keep it. This actually fixes a bug when the prod only does a partial reduction, in which case the old logic of raising the extracted values to the power of the whole inner size was wrong. I added a test that failed in main in the first commit. The expanded rewrite comes up in the second commit, and fixes the failing test.
I also separated the rewrite for Sum(-mul) since it shares almost no logic with the larger rewrite. The usefulness of generalizing this rewrite showed up when exploring the grad of the batched MvNormal logp/dlogp in #482 .
And finally, I merged this with the nearly identical rewrite for sum of div, that already handled multiple axes