Split coloring and jacrev/jacfwd#25
Open
nardi wants to merge 1 commit into
Open
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
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.
Hi, I wanted to use this library but for efficiency purposes I wanted to split the graph coloring step from the actual evaluation of the Jacobian.
This is because I am calculating many Jacobians with the same sparsity pattern, and also doing a nested differentiation, e.g. using
jacfwdto calculate the sparse Jacobian matrix, solving a linear system involving it using a sparse linear solver, and then usingjax.jvpto differentiate through that solve. This means thejacfwd(f, sparsity=...)call is inside ajax.jitcontext, which means the sparsity pattern array needs to be passed through as a static argument, which is not really nice (since arrays are not hashable).I had a go at adding a feature to support this. I added
ForwardModeColoringandReverseModeColoringobjects, and functions to produce them. These are PyTrees (easy to JIT) and can be passed tojacfwd/revasjacfwd(f, coloring=...). I also made the representation quite compact, in that e.g. the projection matrix for the reverse mode coloring is not fully constructed. This is quite a clean way to split the coloring and the evaluation, IMO :)I used LLM as a coding assistant, but reviewed all code myself, and it of course passes all existing tests and the new ones added to test the split functionality specifically. I also added tests specific to my use case, for example
test_jacfwd_with_precomputed_coloring_jit, where the coloring is computed outside of JIT and then passed into a JIT-compiled function.