Skip to content
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

Add second forward output and autodiff backend extensions #40

Merged
merged 16 commits into from
May 8, 2023
Merged

Conversation

gdalle
Copy link
Member

@gdalle gdalle commented Apr 23, 2023

Main changes planned:

  • Make code more readable by switching to AbstractDifferentiation.jl
  • Forward pass can return f(x) = y, z where y is the actual output and z is any additional information used in the optimality conditions.
  • Add ForwardDiff.jl compatibility in an extension (conditionally loaded < 1.9 with Requires)
  • Move ChainRules.jl compatibility to an extension (always loaded < 1.9)
  • Support Julia 1.6
  • Clearer docs with FAQ and more details

Link to preview of new docs: https://gdalle.github.io/ImplicitDifferentiation.jl/previews/PR40/

@gdalle gdalle added the feature New feature or request label Apr 23, 2023
@gdalle gdalle marked this pull request as draft April 23, 2023 07:13
@codecov
Copy link

codecov bot commented Apr 23, 2023

Codecov Report

Patch coverage: 95.94% and project coverage change: +1.55 🎉

Comparison is base (05ff9b7) 94.82% compared to head (451f7fd) 96.38%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
+ Coverage   94.82%   96.38%   +1.55%     
==========================================
  Files           2        5       +3     
  Lines          58       83      +25     
==========================================
+ Hits           55       80      +25     
  Misses          3        3              
Impacted Files Coverage Δ
src/utils.jl 85.00% <85.00%> (ø)
ext/ImplicitDifferentiationChainRulesExt.jl 100.00% <100.00%> (ø)
ext/ImplicitDifferentiationForwardDiffExt.jl 100.00% <100.00%> (ø)
src/ImplicitDifferentiation.jl 100.00% <100.00%> (ø)
src/implicit_function.jl 100.00% <100.00%> (+5.88%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@gdalle gdalle marked this pull request as ready for review April 27, 2023 07:29
We compute the Jacobian-vector product `Jv` by solving `Au = -Bv` and setting `Jv = u`.
Keyword arguments are given to both `implicit.forward` and `implicit.conditions`.
"""
function ChainRulesCore.frule(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly because

- version: 'nightly'
os: ubuntu-latest
arch: x64
allow_failure: true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added so that failures on nightly (currently due to Zygote precompilation) do not impact the "CI passing" badge

ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Krylov = "ba0b0d4f-ebba-5204-a429-3ac8c609bfb7"
LinearOperators = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ForwardDiff.jl compatibility in Julia versions < 1.9


The same trick works for multiple outputs.

## Constrained optimization modeling
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the tutorial more simple on this aspect, and added details in the FAQ

@test ForwardDiff.jacobian(first ∘ implicit, X) ≈ JJ #src
@test Zygote.jacobian(first ∘ implicit, X)[1] ≈ JJ #src

# Skipped because of https://github.com/JuliaDiff/ChainRulesTestUtils.jl/issues/232 #src
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea what to do here?


#=
Unsurprisingly, the Jacobian is the identity.
In this instance, we could use ForwardDiff.jl directly on the solver, but it returns the wrong result (not sure why).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still no clue why ForwardDiff fails here

When we call it as a function, it just falls back on `implicit.forward`, so unsurprisingly we get the same tuple $(y(x), z(x))$.
=#

(first ∘ implicit)(x) ≈ sqrt.(x)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always using first is cumbersome but it is the price to pay to return y, z

Dual{T}(y[i], Partials(Tuple(dy[k][i] for k in 1:N)))
end

z_and_dz = Dual{T}(z, Partials(Tuple(zero(z) for k in 1:N)))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires z to support zero, which is less than ideal, but I don't know how to indicate to ForwardDiff that a derivative is zero. We need something like ZeroTangent() that is dimension-agnostic

end

"""
PushforwardMul!{P,N}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced lazy_jacobian with pushforward/pullback_function due to JuliaDiff/AbstractDifferentiation.jl#83

@gdalle gdalle requested a review from mohamed82008 April 30, 2023 19:19
@gdalle gdalle merged commit c6c8fc2 into main May 8, 2023
@gdalle gdalle deleted the more_stuff branch May 8, 2023 10:52
@gdalle gdalle removed the feature New feature or request label May 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants