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

Improve float conversions in PI controllers #2499

Merged
merged 2 commits into from
Oct 21, 2024
Merged

Improve float conversions in PI controllers #2499

merged 2 commits into from
Oct 21, 2024

Conversation

ChrisRackauckas
Copy link
Member

This is fairly hard to test, but it basically stems from float(1//50)::Float64, which means that this was always doing some float64 stuff, even when it should've been doing fastpow stuff. The test case just really required a Float32:

using DiffEqCallbacks, OrdinaryDiffEq, Tracker

Base.prevfloat(r::Tracker.TrackedReal) = Tracker.track(prevfloat, r)
Tracker.@grad function prevfloat(r::Real)
    prevfloat(Tracker.data(r)), Δ -> (Δ,)
end
Base.nextfloat(r::Tracker.TrackedReal) = Tracker.track(nextfloat, r)
Tracker.@grad function nextfloat(r::Real)
    nextfloat(Tracker.data(r)), Δ -> (Δ,)
end

function rober(u, p::TrackedArray, t)
    y₁, y₂, y₃ = u
    k₁, k₂, k₃ = p
    return Tracker.collect([-k₁ * y₁ + k₃ * y₂ * y₃,
        k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃,
        k₂ * y₂^2])
end

p = TrackedArray([1.9f0, 1.0f0, 3.0f0])
u0 = TrackedArray([1.0f0, 0.0f0, 0.0f0])
tspan = TrackedArray([0.0f0, 1.0f0])
prob = ODEProblem{false}(rober, u0, tspan, p)

p = TrackedArray([1.9f0, 1.0f0, 3.0f0])
u0 = TrackedArray([1.0f0, 0.0f0, 0.0f0])
tspan = TrackedArray([0.0f0, 1.0f0])
prob = ODEProblem{false}(rober, u0, tspan, p)
saved_values = SavedValues(eltype(tspan), eltype(p))
cb = SavingCallback((u, t, integrator) -> integrator.EEst * integrator.dt, saved_values)

solve(remake(prob, u0 = u0, p = p, tspan = tspan),
            Tsit5(),
            sensealg = SensitivityADPassThrough(),
            callback = cb)
        
@test !all(iszero.(Tracker.gradient(
    p -> begin
        solve(remake(prob, u0 = u0, p = p, tspan = tspan),
            Tsit5(),
            sensealg = SensitivityADPassThrough(),
            callback = cb)
        return sum(saved_values.saveval)
    end,
    p)[1]))

Thus downstream tests with FastPower.jl used catches this, and it's somewhat hard to construct a case that's this sensitive to the type.

This is fairly hard to test, but it basically stems from `float(1//50)::Float64`, which means that this was always doing some float64 stuff, even when it should've been doing fastpow stuff. The test case just really required a Float32:

```julia
using DiffEqCallbacks, OrdinaryDiffEq, Tracker

Base.prevfloat(r::Tracker.TrackedReal) = Tracker.track(prevfloat, r)
Tracker.@Grad function prevfloat(r::Real)
    prevfloat(Tracker.data(r)), Δ -> (Δ,)
end
Base.nextfloat(r::Tracker.TrackedReal) = Tracker.track(nextfloat, r)
Tracker.@Grad function nextfloat(r::Real)
    nextfloat(Tracker.data(r)), Δ -> (Δ,)
end

function rober(u, p::TrackedArray, t)
    y₁, y₂, y₃ = u
    k₁, k₂, k₃ = p
    return Tracker.collect([-k₁ * y₁ + k₃ * y₂ * y₃,
        k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃,
        k₂ * y₂^2])
end

p = TrackedArray([1.9f0, 1.0f0, 3.0f0])
u0 = TrackedArray([1.0f0, 0.0f0, 0.0f0])
tspan = TrackedArray([0.0f0, 1.0f0])
prob = ODEProblem{false}(rober, u0, tspan, p)

p = TrackedArray([1.9f0, 1.0f0, 3.0f0])
u0 = TrackedArray([1.0f0, 0.0f0, 0.0f0])
tspan = TrackedArray([0.0f0, 1.0f0])
prob = ODEProblem{false}(rober, u0, tspan, p)
saved_values = SavedValues(eltype(tspan), eltype(p))
cb = SavingCallback((u, t, integrator) -> integrator.EEst * integrator.dt, saved_values)

solve(remake(prob, u0 = u0, p = p, tspan = tspan),
            Tsit5(),
            sensealg = SensitivityADPassThrough(),
            callback = cb)
        
@test !all(iszero.(Tracker.gradient(
    p -> begin
        solve(remake(prob, u0 = u0, p = p, tspan = tspan),
            Tsit5(),
            sensealg = SensitivityADPassThrough(),
            callback = cb)
        return sum(saved_values.saveval)
    end,
    p)[1]))
```

Thus downstream tests with FastPower.jl used catches this, and it's somewhat hard to construct a case that's this sensitive to the type.
@ChrisRackauckas ChrisRackauckas merged commit 2f9f17a into master Oct 21, 2024
1 check was pending
@ChrisRackauckas ChrisRackauckas deleted the pifloat branch October 21, 2024 08:23
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.

1 participant