Add rough version of an autodiff refactor#788
Open
aseyboldt wants to merge 3 commits intopymc-devs:mainfrom
Open
Add rough version of an autodiff refactor#788aseyboldt wants to merge 3 commits intopymc-devs:mainfrom
aseyboldt wants to merge 3 commits intopymc-devs:mainfrom
Conversation
ricardoV94
reviewed
Jul 10, 2024
| from pytensor.tensor.basic import zeros_like | ||
|
|
||
| tangents_filled = [ | ||
| # TODO do the R_op methods also accept a disconnected_grad? |
Member
|
@aseyboldt let's try to replace the L_op / R_op to see what needs to be added. I would also like a non-trivial example of "alternative outputs" to test ways we can achieve that. |
This was referenced May 21, 2025
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.
Completely redo the implementation of the Rop/Lop/grad functions and replace them by
pull_back,push_forward.gradcan just callpull_backthen. They are currently not really working, but only a sketch of how they could look like.Add new alternative methods
pull_backandpush_forwardandlinear_transposetoOp. In the long-term they should replace thegrad,RopandLopmethods, and give more control and context to the Op when implementing those operations.Invesigate a bit more how we can derive Rop from Lop and the other way round. We can go both ways: Given an implementation of
push_forwardwe can use the fact that the mapping between the tangents is linear, and if all involved linear ops implementlinear_transpose, we can use that to derivepull_back. Givenpull_backwe can use the fact that pull_back is a push_forward and transpose. And since the push_forward of a linear function is itself, we can apply it twice to effectively remove the transpose. (see pull_back_through_transpose and push_forward_through_pull_back).We can possibly provide hints to the graph optimizer that the values can sometimes be computed in a smarter way if we also want gradients. Ricardo convinced me that we probably don't want to produce new output values in the
push_forwardandpull_backfunctions, but maybe if the gradients are computed lazily we could collect those possiblerewrites and apply them during the rewrite that computes the gradients?
Hopefully delete 2000 lines of complicated gradient code once the new stuff is working well.
cc @ricardoV94