Skip to content

control_flow model runs unreliably #4

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

Closed
penelopeysm opened this issue Apr 8, 2025 · 3 comments
Closed

control_flow model runs unreliably #4

penelopeysm opened this issue Apr 8, 2025 · 3 comments
Labels
this-repo Something to do with just this repo

Comments

@penelopeysm
Copy link
Member

penelopeysm commented Apr 8, 2025

The purpose of the control_flow model is to demonstrate errors with compiled ReverseDiff:

using DynamicPPL, LogDensityProblems, Distributions, ADTypes, ForwardDiff, ReverseDiff

@model function control_flow()
    a ~ Normal()
    if a > 0
        b ~ Normal()
    else
        b ~ Beta(2, 2)
    end
end

# Gradient preparation
model = control_flow()
adtype = AutoReverseDiff(; compile=true)
vi = DynamicPPL.unflatten(VarInfo(model), [0.5, -0.5]) # note: a > 0 and b < 0
ldf = LogDensityFunction(model, vi; adtype=adtype)

# For comparison
ldf_ref = LogDensityFunction(model, vi; adtype=AutoForwardDiff())

# Two sets of parameters
p1 = [0.5, -0.5]  # a > 0, b < 0
p2 = [-0.5, 0.5]  # a < 0, b > 0

julia> LogDensityProblems.logdensity_and_gradient(ldf, p1)
(-2.0878770664093453, [-0.5, 0.5])

julia> LogDensityProblems.logdensity_and_gradient(ldf_ref, p1)
(-2.0878770664093453, [-0.5, 0.5])

julia> LogDensityProblems.logdensity_and_gradient(ldf, p2)
(-2.0878770664093453, [0.5, -0.5])

julia> LogDensityProblems.logdensity_and_gradient(ldf_ref, p2)
(-0.6384734250965083, [0.5, 0.0])

As can be seen from the above, the gradient evaluates correctly at p1, but incorrectly at p2. The issue is that reproducing this is contingent on choosing the right set parameters to pass to the model; if we pass p1 it looks like compiled ReverseDiff runs fine.

@penelopeysm penelopeysm changed the title control_flow model control_flow model runs unreliably Apr 8, 2025
@penelopeysm
Copy link
Member Author

Funnily enough, this is where the idea of a DITest scenario is perfect because the scenario also encapsulates x

@penelopeysm
Copy link
Member Author

penelopeysm commented Apr 9, 2025

Although (as far as I can tell) it's not currently possible to control the values used to prepare the gradient in a Scenario JuliaDiff/DifferentiationInterface.jl#771

@penelopeysm
Copy link
Member Author

Fixed here by 1874554 and 7ac92d2

@penelopeysm penelopeysm added the this-repo Something to do with just this repo label Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
this-repo Something to do with just this repo
Projects
None yet
Development

No branches or pull requests

1 participant