Skip to content

Add ReverseDiff extension with analytical pullbacks#216

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-extension-for-reversediff
Draft

Add ReverseDiff extension with analytical pullbacks#216
Copilot wants to merge 3 commits intomainfrom
copilot/add-extension-for-reversediff

Conversation

Copy link
Contributor

Copilot AI commented Jan 1, 2026

Adds ReverseDiff.jl support using the analytical pullbacks already defined via ChainRulesCore rrules. Since @grad_from_chainrules doesn't support keyword arguments, implements custom SpecialInstruction types that record operations on ReverseDiff's tape and execute analytical gradients during the reverse pass.

Supported functions

  • solve_lyapunov_equation / solve_sylvester_equation - all tracked/untracked input combinations
  • calculate_jacobian, calculate_hessian, calculate_third_order_derivatives
  • get_NSSS_and_parameters - with proper zero-gradient handling for failed solutions

Usage

using MacroModelling, ReverseDiff

@model RBC begin
    # ... model equations
end
@parameters RBC begin
    # ... parameters
end

function objective(params)
    SS, _ = MacroModelling.get_NSSS_and_parameters(RBC, params)
    return sum(SS)
end

grad = ReverseDiff.gradient(objective, param_values)

Implementation

  • New ext/ReverseDiffExt.jl with SpecialInstruction dispatch types
  • special_forward_exec! / special_reverse_exec! methods for each function
  • Pullback formulas match existing rrules (e.g., Lyapunov: ∂A = ∂C * A * P' + ∂C' * A * P)
Original prompt

write an extension for reversediff.jl that uses @grad_from_chainrules on the rrules already written in the package. make sure they are compatible with reversediff.jl otherwise copy them over to the extension and modify them


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 1, 2026 22:37
…apunov, and model functions

Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
… failed NSSS solutions

Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
Copilot AI changed the title [WIP] Add extension for reversediff.jl using chainrules Add ReverseDiff extension with analytical pullbacks Jan 1, 2026
Copilot AI requested a review from thorek1 January 1, 2026 22:48
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