-
Notifications
You must be signed in to change notification settings - Fork 11
Part 1 of autograd backend: DifferentiationInterface extention #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6baeca3
Add DifferentiationInterface extension on top of evaluator interface
yebai 9c371b3
Inline AD output-shape assertions into the DI extension
yebai 6bc564a
Fold AD output-shape checks into prepare's dispatch
yebai c4a6e3c
Move AD test cases into package test resources
yebai 76d2ea8
Use ForwardDiff in DI extension tests
yebai ff96fc1
Address PR review: simplify AD prepare and move test fixtures to Test…
yebai 936ddb4
Fix empty-input gradient arity, advertise LDP capabilities for DI
yebai c7ed718
Unify LDP capabilities: Prepared = order 1, bare evaluator = order 0
yebai 3f48763
format
yebai f9ac110
Skip Aqua persistent_tasks on Julia 1.10; drop dead --project from ex…
yebai 4861b66
Polish evaluator API: public stubs, docstrings, AD-missing hints
yebai dd0360f
Merge branch 'main' into evaluator-interface-di-ext
yebai 7848947
Update JULIA.md
yebai 5797c83
Address PR review: drop LDP ext, fix DI gradient/jacobian edges
yebai 17ebe0e
Reject integer inputs in DI value_and_{gradient,jacobian}!!
yebai ff1a3ff
Merge branch 'main' into evaluator-interface-di-ext
yebai 4fe4a93
Add coverage upload to ext job, bump setup-julia and codecov-action v…
shravanngoswamii e0adffe
Add regression test for AutoReverseDiff compiled tape no-context path
shravanngoswamii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| module AbstractPPLDifferentiationInterfaceExt | ||
|
|
||
| using AbstractPPL: AbstractPPL | ||
| using AbstractPPL.Evaluators: Evaluators, Prepared, VectorEvaluator | ||
| using ADTypes: AbstractADType, AutoReverseDiff | ||
| using DifferentiationInterface: DifferentiationInterface as DI | ||
|
|
||
| # Differentiate only `x`; the evaluator is passed as a `DI.Constant` context so | ||
| # that in DynamicPPL the model and other evaluator state stay constant. | ||
| @inline _call_evaluator(x, evaluator) = evaluator(x) | ||
|
|
||
| struct DICache{F,GP,JP} | ||
| target::F | ||
| gradient_prep::GP | ||
| jacobian_prep::JP | ||
| use_context::Bool | ||
| end | ||
|
|
||
| # Compiled ReverseDiff only reuses a compiled tape on the one-argument path; | ||
| # `DI.Constant` deactivates tape recording, so close the evaluator into the | ||
| # target and call DI without contexts. | ||
| function _prepare_di(prep::F, adtype::AutoReverseDiff{true}, x, evaluator) where {F} | ||
| target = Base.Fix2(_call_evaluator, evaluator) | ||
| return target, prep(target, adtype, x), false | ||
| end | ||
|
|
||
| function _prepare_di(prep::F, adtype::AbstractADType, x, evaluator) where {F} | ||
| return _call_evaluator, prep(_call_evaluator, adtype, x, DI.Constant(evaluator)), true | ||
| end | ||
|
|
||
| function AbstractPPL.prepare( | ||
| adtype::AbstractADType, problem, x::AbstractVector{<:Real}; check_dims::Bool=true | ||
| ) | ||
| evaluator = AbstractPPL.prepare(problem, x; check_dims)::VectorEvaluator | ||
| y = evaluator(x) | ||
| y isa Union{Number,AbstractVector} || throw( | ||
| ArgumentError( | ||
| "A prepared AD evaluator must return a scalar or AbstractVector; got $(typeof(y)).", | ||
| ), | ||
| ) | ||
| if length(x) == 0 | ||
| # DI prep crashes on length-0 input (e.g. ForwardDiff `BoundsError`); the | ||
| # `Val(0)` sentinel keeps the `gradient_prep === nothing` arity check meaningful. | ||
| gp, jp = y isa Number ? (Val(0), nothing) : (nothing, Val(0)) | ||
| return Prepared(adtype, evaluator, DICache(_call_evaluator, gp, jp, true)) | ||
| end | ||
| if y isa Number | ||
| target, gradient_prep, use_context = _prepare_di( | ||
| DI.prepare_gradient, adtype, x, evaluator | ||
| ) | ||
| return Prepared( | ||
| adtype, evaluator, DICache(target, gradient_prep, nothing, use_context) | ||
| ) | ||
| end | ||
| target, jacobian_prep, use_context = _prepare_di( | ||
| DI.prepare_jacobian, adtype, x, evaluator | ||
| ) | ||
| return Prepared(adtype, evaluator, DICache(target, nothing, jacobian_prep, use_context)) | ||
| end | ||
|
|
||
| @inline function AbstractPPL.value_and_gradient!!( | ||
| p::Prepared{<:AbstractADType,<:VectorEvaluator,<:DICache}, x::AbstractVector{T} | ||
| ) where {T<:Real} | ||
| p.cache.gradient_prep === nothing && | ||
| throw(ArgumentError("`value_and_gradient!!` requires a scalar-valued function.")) | ||
| T <: Integer && Evaluators._reject_integer_input(x) | ||
| Evaluators._check_vector_length(p.evaluator.dim, x) | ||
| # Bypass DI on length-0 input — DI prep paths fail (e.g. ForwardDiff | ||
| # `BoundsError`); typed `T[]` matches the caller's element type. | ||
| length(x) == 0 && return (p.evaluator(x), T[]) | ||
|
yebai marked this conversation as resolved.
|
||
| return if p.cache.use_context | ||
| DI.value_and_gradient( | ||
| p.cache.target, p.cache.gradient_prep, p.adtype, x, DI.Constant(p.evaluator) | ||
| ) | ||
| else | ||
| DI.value_and_gradient(p.cache.target, p.cache.gradient_prep, p.adtype, x) | ||
| end | ||
| end | ||
|
|
||
| @inline function AbstractPPL.value_and_jacobian!!( | ||
| p::Prepared{<:AbstractADType,<:VectorEvaluator,<:DICache}, x::AbstractVector{T} | ||
| ) where {T<:Real} | ||
| p.cache.jacobian_prep === nothing && | ||
| throw(ArgumentError("`value_and_jacobian!!` requires a vector-valued function.")) | ||
| T <: Integer && Evaluators._reject_integer_input(x) | ||
| Evaluators._check_vector_length(p.evaluator.dim, x) | ||
| if length(x) == 0 | ||
| val = p.evaluator(x) | ||
| return (val, similar(x, length(val), 0)) | ||
| end | ||
| return if p.cache.use_context | ||
| DI.value_and_jacobian( | ||
| p.cache.target, p.cache.jacobian_prep, p.adtype, x, DI.Constant(p.evaluator) | ||
| ) | ||
| else | ||
| DI.value_and_jacobian(p.cache.target, p.cache.jacobian_prep, p.adtype, x) | ||
| end | ||
| end | ||
|
|
||
| end # module | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.