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

update scimlbase for function unwrapping #715

Merged
merged 12 commits into from
Aug 25, 2022
Merged

update scimlbase for function unwrapping #715

merged 12 commits into from
Aug 25, 2022

Conversation

ChrisRackauckas
Copy link
Member

No description provided.

ChrisRackauckas added a commit to SciML/DiffEqBase.jl that referenced this pull request Aug 25, 2022
This dispatch was found in SciML/SciMLSensitivity.jl#715 stiff adjoint tests `grad6 = Zygote.gradient(x -> sum_of_solution_CASA(x, vjp = false), [u0; p])[1]`

```julia

    # using SciMLSensitivity, OrdinaryDiffEq, ForwardDiff, Zygote, Test

    function rober(du, u, p, t)
        y₁, y₂, y₃ = u
        k₁, k₂, k₃ = p[1], p[2], p[3]
        du[1] = -k₁ * y₁ + k₃ * y₂ * y₃
        du[2] = k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃
        du[3] = k₂ * y₂^2 + sum(p)
        nothing
    end

    function sum_of_solution_fwd(x)
        _prob = ODEProblem(rober, x[1:3], (0.0, 1e4), x[4:end])
        sum(solve(_prob, Rodas5(), saveat = 1, reltol = 1e-12, abstol = 1e-12))
    end

    function sum_of_solution_CASA(x; vjp = EnzymeVJP())
        sensealg = QuadratureAdjoint(autodiff = false, autojacvec = vjp)
        _prob = ODEProblem(rober, x[1:3], (0.0, 1e4), x[4:end])
        sum(solve(_prob, Rodas5(), reltol = 1e-8, abstol = 1e-8, saveat = 1,
                  sensealg = sensealg))
    end

    u0 = [1.0, 0.0, 0.0]
    p = ones(8)  # change me, the number of parameters

    grad1 = ForwardDiff.gradient(sum_of_solution_fwd, [u0; p])
    grad2 = Zygote.gradient(sum_of_solution_CASA, [u0; p])[1]
    grad3 = Zygote.gradient(x -> sum_of_solution_CASA(x, vjp = ReverseDiffVJP()), [u0; p])[1]
    grad4 = Zygote.gradient(x -> sum_of_solution_CASA(x, vjp = ReverseDiffVJP(true)),
                            [u0; p])[1]
    @test_throws Any Zygote.gradient(x -> sum_of_solution_CASA(x, vjp = true), [u0; p])[1]
    grad6 = Zygote.gradient(x -> sum_of_solution_CASA(x, vjp = false), [u0; p])[1]
```
@ChrisRackauckas ChrisRackauckas merged commit 500da17 into master Aug 25, 2022
@ChrisRackauckas ChrisRackauckas deleted the scimlbase branch August 25, 2022 14:38
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.

None yet

1 participant