-
Notifications
You must be signed in to change notification settings - Fork 12
[DISCUSS] (E)KFAC + inverses refactor #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
f-dangel
wants to merge
26
commits into
main
Choose a base branch
from
kronecker-product
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
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
Owner
Author
|
@runame I probably need to clarify more aspects. Let me know if something is weird. Tests are passing locally for me. |
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.
This PR is a massive refactoring with the goal to reduce code duplication between KFAC, EKFAC, and their inverses, and remove unnecessary complexity. We will not directly merge this PR. The goal is to discuss the current refactor, incorporate feedback, then try to break it down into smaller submitt-able PRs.
KroneckerProductLinearOperatorto represent Kronecker productsS_1 \otimes S_2 \otimes ...withtorch.TensorsS_irepresenting the Kronecker factors. This operator contains the logic to multiply Kronecker products onto vectors and bundles alleinsumcalls into a single class without leaking into(E)KFACLinearOperator. It also implements the propertiestrace,det,logdetandfrobenius_norm, and an.inverse(...)function that accepts arguments to specify the damping strategy and value.EighLinearOperatorrepresenting an eigen-decomposed matrixQ @ diag(lam) @ Q.TwhereQcan be either atorch.Tensoror a linear operator. It implements the same functions asKroneckerProductLinearOperator.BlockdiagonalLinearOperatorrepresentingblock_diag(B_1, B_2, ...)whereB_iis a linear operator (either Kronecker product or eigen-decomposed matrix). Implements the same functions as the above operators, by calling the appropriate functions for each block (e.g. the trace is the sum of all block traces).Each of the above points can be a separate PR. I can start this immediately after we decide the overall approach makes sense.
Refactoring: New structure of a
KFACLinearOperatorcalledK:K_op = P @ B @ P.T.Pis an operator that converts the parameter space to a canonical form by undoing parameter shuffling and potentially grouping together weight and bias components that are treated jointly.Bis a block-diagonal linear operator (BlockDiagonalLinearOperator) and each block is a Kronecker-factored linear operator (KroneckerProductLinearOperator). This makes it much easier to share code betweenKFACandEKFACas they only differ howBis set up. It also simplifies their inversion, becauseK_op_inv = P @ B_inv @ P.T.PandP.Tare separate linear operators that take care of the canonicalization.state_dictfunctionality. Users can simply saveK_opviatorch.save(K._operator)and then load it back. This is simpler to the current solution because we can discard the neural network, loss function, etc.K._operatoris a_ChainPyTorchLinearOperatorand can be used like a normal linear operator.KFACLinearOperatorto populate it with astate_dictanymore (due to the previous point).K.inverse(...)which returns a_ChainPyTorchLinearOperatorrepresentingP @ B_inv @ P.T. It accepts arguments to specify the damping value and strategy. As a result, we can removeKFACInverseLinearOperator.self._input_covariances. This massively reduces dependencies across class methods.Refactoring: New structure of an
EKFACLinearOperatorcalledE.KFAC, the operatorBis different in that its blocks containEighLinearOperators and notKroneckerProductLinearOperators. The eigencorrection part requires rotating the gradients into the Kronecker-factored basis and this is done purely with functionality from the above base operators, removing a lot ofeinsumcalls.KFAC.We need to discuss how to best split these refactorings up into manage-able PRs.
Kbe aKFACLinearOperator. Instead ofK.det(or.logdet,.trace,.frobenius_norm), we now callK.det()etc. Why? This makesKfeel more like atorch.Tensor.