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
WIP: add gradcheck #235
WIP: add gradcheck #235
Conversation
This PR is implemented based on |
This is cool. I totally agree that Zygote needs improved testing facilities, but we've already got a similar thing implemented in FDM.jl that we're utilising in ChainRules.jl and Nabla.jl. Is there a reason that Zygote can't just use that? edit: in particular see here edit 2: you could be forgiven for not realising this exists as our documentation for this is quite poor it seems. Will try to get this improved. |
I see. I didn't know there's such implementation in test. However, the Edit: Another issue is the numerical
https://github.com/invenia/FDM.jl/blob/574136a76fbc9182fe8e5195b4a17acd686732b0/src/grad.jl#L28 I think maybe I should make PRs to FDM for the numerical part. But I still think it is necessary to provide a gradcheck that check the jacobian of an arbitrary julia function explicitly. |
What's the plan of using ChainRules for Zygote BTW? |
I think RE chainrules it's in the pipeline but needs someone to bring it in. |
@MikeInnes More or less. Which utilities are you referring to here? In particular, we do have a
The
I partly take your point here, but this isn't the complete picture in FDM. The That being said, you are correct that our (We didn't originally make it easy to work with jacobians of arbitrary functions because we were building a tool designed specifically to test AD implementations, which literally just compute the In any case, we would be very open to collaborating on this to improve FDM to ensure that it is sufficient for Zygote's needs 🙂. |
I'm more or less thinking of It probably makes sense to start simple here, just reworking |
I'm willing to contribute to upstream and make use of FDM here. But I just think in this PR we only to make use of the finite diff from FDM and replace that njacobian function later when a more generic version of jacobian is implemented and tagged. And one issue for this PR is I was trying to make sure we don't need to check reentrants like torch does in their gradcheck, or do we need to? I'm not sure what is this for. |
All sounds reasonable to me :) |
Yeah, but anyone has idea on the reentrant thing? I don't quite get it why they check this in torch, is this because of the tape (or high order derivatives?) Update: in case you want to look at the code: https://github.com/pytorch/pytorch/blob/d4119f8fcbde15569b8f2993973439f2961ea22a/torch/autograd/gradcheck.py#L291 I didn't find any docs for this. |
Is this not just checking for non-determinism / stateful behaviour that changes each time you evaluate the function / do AD? Seems like a sensible thing to check for in any case. |
If it is non-deterministic, why just check it only once? and I guess we don't need it at the moment? or there is a use case in Zygote at the moment? |
I think we can probably ignore that for now. There are cases where this might be worth checking (e.g. that |
@willtebbutt could you tag a new version of FDM so I could finish this PR? thanks |
Done -- JuliaRegistries/General#4055 :) |
I removed the |
It's fine to have a branch of FD in the manifest, for CI. We could even merge that as long as we're fairly confident FD will get tagged soonish. |
I'm very happy to tag as soon as the PR is complete. |
@Roger-luo 0.9.0 of FiniteDifferences is now available 🙂 |
I think should be good once CI passed. I also fixed |
err, it seems the test timeout? |
One reason that your tests might be timing out is because
The point is that |
so do you think I should just check say the vjp instead of jacobian then? I use |
Yup, that's my opinion on the matter. IMHO the jacobian is the wrong thing to check, as it's essentially just computing a bunch of special cases of a vjp, which can fail to catch bugs. Better just to randomize the input to |
the eigen value test still fails with this FDM PR (JuliaDiff/FiniteDifferences.jl#54) somwhow any idea why? eigvals(::Hermitian{Complex{Float64},S} where S<:(AbstractArray{#s617,2} where #s617<:Complex{Float64})) |
890: Add `jacobian`, at last? r=CarloLucibello a=mcabbott This adds a Jacobian function. Compared to #747 this one: * has tests * accepts multiple arguments * shouldn't fail if `back` returns `nothing` * inserts `vec` a few more places * has a method which accepts implicit `Params`, and returns `Grads`. (This was the only part I actually needed in real life.) * now works on the GPU too! Compared to #414 this one: * always inserts `vec`, never makes higher-dimensional arrays * runs on current Zygote * has tests. Compared to #235 this one: * doesn't try to provide numerical jacobian * doesn't alter testing infrastructure * doesn't provide `jacobian!`, nor any code for structured matrices. This does not address #564's concerns about functions which return a tuple of arrays. Only functions returning an array (or a scalar) are permitted. Similar considerations might give sensible jacobians when the argument of a function is a tuple, or some other struct, but for now these are handled by putting up a giant warning sign. Nothing in the file `utils.jl` seems to have any tests at all. So I also added tests for `hessian`. And, while I was there, made `hessian` actually accept a real number like its docstring promises. (Hence this closes #891.) And, made a version that is reverse-over-reverse, using this `jacobian`, which works less well of course but may as well exist to test things. (See for example #865.) Ideally there would be a pure-Zygote version using its own forward mode, but I didn't write that. Fixes #51, fixes #98, fixes #413. Closes #747. Co-authored-by: Michael Abbott <me@escbook> Co-authored-by: Michael Abbott <32575566+mcabbott@users.noreply.github.com>
closing as stale and also because I would prefer to rely on FiniteDifferences.jl (as #464 attempts, also stale though) |
This PR provides three functions:
njacobian
: calculate the numerical jacobianjacobian
: use Zygote to calculate the analytical jacobiangradcheck
: a torch-likegradcheck
it compares the jacobian instead of calculating the summationTODO:
gradcheck
instead of the one in testjacobian