-
Notifications
You must be signed in to change notification settings - Fork 64
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
Append primal results to batched jacobian computations #1198
Comments
Thanks for the detailed write-up. In terms of API, I think
This is actually the way to go. We have been using Reactant internally for PINNs with higher-order AD and it works like a charm. Some operations are still missing but we can add them quite easily (if you open issues with models that you have which are not working, I would be more than happy to implement them in EnzymeJAX). FWIW I was adding benchmarks in Reactant and nested ad there is about 27x faster than doing the Zygote + ForwardDiff trick (https://github.com/EnzymeAD/Reactant.jl/blob/ap/perf_static/perf/HNN/results_gpu.csv) |
Thank you for your kind answer. I agree that writing the custom AD rules for the proposed version of the batched-jacobian seems like quite a nightmare indeed. I don't think I could write those in a reasonable amount of time in a PR, so I'd understand if you want to close this issue in the meantime. Quick question about EnzymeJAX: is it faster in the end than Reactant+Enzyme+NestedAD in your experience? And is it better in some way than regular Jax? |
cc @wsmoses do we have any numbers for EnzymeJAX vs JAX on nested AD? |
not at the moment, but for tensor programs I presume it would be faster. For EnzymeJaX vs pure jax, the various tesnro optimizations have shown double digit perf speedups on various ML training codes, but obviously this is program dependent |
Problem statement
While reading the docs I came to wonder about the following MWE in the nested-AD section:
It is noticeable that the forward pass must be done at least twice (once at$\hat{y}$ and $J$ , which is inefficient (or is it?). This may imply some costly resources wasting in cases where the number of chunks is small.
$\hat{y}$ anyway, so it would be interesting to use it.
ŷ = smodel(x)
and at least once atJ = batched_jacobian(smodel, AutoForwardDiff(), x)
) here to obtain bothbatched_jacobian
will internally computeDesign ideas to fix it
Here are some ways that one may fix this by fetching the primal computation results in the internal API.
In all of those, the
batched_jacobian
function accepts a new parameter which defaults to false, asking whether to return the primal (new api) or not (as before).FowrardDiff
possible solutionsSince both$\hat{y}$ internally, may it be possible to extract it ?
Zygote
andForwardDiff
must compute at some pointFor the
ForwardDiff
par for example, it seems to be related to the filesrc/autodiff/batched_autodiff.jl
line 160:166 (main branch). One may do the same aspartials_wrap
line 159 for the primal "values" while computing the first chunk.Then the first chunck would be computed with this method by changing line 100 as follows and then returning (or not)$y$ .
Zygote
possible solutionsIf I understood the code correctly, this one may be simpler, at
ext/LuxZygoteExt/batched_autodiff.jl
.About Enzyme
When
ho/ho-enzyme
(#954) will be ready, using e.g.julia AutoEnzyme(; mode=Enzyme.ForwardWithPrimal)
will make this issue almost trivial.From personal experience with
Reactant
+Enzyme
+nestedAD, this looks like it is particularly difficult for now though.vjp/jvp
All the ideas mentioned above also seem applicable to both
vector_jacobian_product
andjacobian_vector_product
.Conclusion
Are those ideas applicable in the way described, or some other way? Are they useful to the API?
I could implement changes myself if needed, but I might need guidance wrt the codebase, especially the tests targeting this part of the code. If this seems relevant, I'll open a PR.
Disclaimer
(I am not proficient in Julia coding, especially regarding the autodiff libraries and GPU technicalities. I may have misunderstood some mechanisms (e.g. some inplace operation that may make my proposed solutions inapplicable, or any scalar indexing issues that may arise), in which case I would not know how to address the above issue.)
The text was updated successfully, but these errors were encountered: