Skip to content

StackOverflow Issue on Certain Functions #525

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
jmurphy6895 opened this issue Oct 2, 2024 · 8 comments · Fixed by #526
Closed

StackOverflow Issue on Certain Functions #525

jmurphy6895 opened this issue Oct 2, 2024 · 8 comments · Fixed by #526
Labels
backend Related to one or more autodiff backends

Comments

@jmurphy6895
Copy link

jmurphy6895 commented Oct 2, 2024

I'm adding some tests to some packages using DifferentiationInterface to make sure everything is differentiable and I'm occasionally running into a weird bug where I get a StackOverflow error

Here's a simple MWE where I run into it.

When trying to differentiate through this function --> the derivative is 0.0 everywhere

using DifferentiationInterface
import FiniteDiff, ForwardDiff

function get_Δat(JD::Number)
    # If JD is before ΔAT_Data[1,1], then return 10.0.
    @inbounds if JD < _ΔAT[1, 1]
        return 10.0
    else
        for i = 2:size(_ΔAT, 1)
            (JD < _ΔAT[i, 1]) && return _ΔAT[i - 1, 2]
        end
    end

    # In this case, JD is after ΔAT_Data[end,1].
    return @inbounds _ΔAT[end, 2]
end

I get a StackOverflow error when running this code (only on ForwardDiff not FiniteDiff):

jd_utc = date_to_jd(2004, 4, 6, 7, 51, 28.386009)

f_fd, df_fd = value_and_derivative(
    get_Δat,
    AutoFiniteDiff(),
    jd_utc
)

f_ad, df_ad = value_and_derivative(
    get_Δat,
    AutoForwardDiff(),
    jd_utc
)

But have no problem when running
ForwardDiff.derivative(get_Δat, jd_utc)

Do you have any idea of what might be happening? I copied the function I'm working with because it's short but to get the pure function you can add using SatelliteToolboxTransformations.

Pkg Status for completeness -- Julia v"1.10.0"
Project SatelliteToolboxTransformations v0.1.8
Status C:\Users\jmurp\.julia\dev\SatelliteToolboxTransformations\Project.toml
[a8cc5b0e] Crayons v4.1.1
[8bb1440f] DelimitedFiles v1.9.1
[a0c0ee7d] DifferentiationInterface v0.6.1
[6a86dc24] FiniteDiff v2.24.0
[f6369f11] ForwardDiff v0.10.36
[a98d9a8b] Interpolations v0.15.1
[189a3867] Reexport v1.2.2
[74f56ac7] ReferenceFrameRotations v3.0.2
[9e17983a] SatelliteToolboxBase v0.3.2
[6c6a2e73] Scratch v1.2.1
[90137ffa] StaticArrays v1.9.7
[ade2ca70] Dates
[f43a241f] Downloads v1.6.0
[37e2e46d] LinearAlgebra

@gdalle
Copy link
Member

gdalle commented Oct 2, 2024

Hi, thanks for reporting this! Can you provide a self-sufficient runnable example, including the definition of what seem to be global variables in your code (ΔAT_Data for example)? I'm assuming the function date_to_jd comes from SatelliteToolboxTransformations? Are there any other imports I need?

@jmurphy6895
Copy link
Author

Yeah, sorry about that here is a simpler self-contained version

using DifferentiationInterface
import ForwardDiff, FiniteDiff

using SatelliteToolboxTransformations

jd_utc = date_to_jd(2004, 4, 6, 7, 51, 28.386009)

f_fd, df_fd = value_and_derivative(
    get_Δat,
    AutoFiniteDiff(),
    jd_utc
)

f_ad, df_ad = value_and_derivative(
    get_Δat,
    AutoForwardDiff(),
    jd_utc
)

ForwardDiff.derivative(get_Δat, jd_utc)

@gdalle
Copy link
Member

gdalle commented Oct 2, 2024

Okay I know what's happening, your function is piecewise constant and its output is not created from its input, so ForwardDiff doesn't get a Dual number in the end. I didn't plan for this case but there's an easy fix.

@gdalle
Copy link
Member

gdalle commented Oct 2, 2024

Can you try installing DI from the branch in the PR #526 to see if it solves your issue?

@gdalle gdalle added bug backend Related to one or more autodiff backends labels Oct 2, 2024
@jmurphy6895
Copy link
Author

No more StackOverflow, but there a new error message

ERROR: ArgumentError: Tuple contains 0 elements, must contain exactly 1 element
Stacktrace:
[1] only(x::Tuple{})
@ Base.Iterators .\iterators.jl:1537
[2] value_and_derivative(::typeof(get_Δat), ::DifferentiationInterfaceForwardDiffExt.ForwardDiffOneArgDerivativePrep{…}, ::AutoForwardDiff{…}, ::Float64)
@ DifferentiationInterfaceForwardDiffExt C:\Users\jmurp.julia\packages\DifferentiationInterface\dRZrJ\ext\DifferentiationInterfaceForwardDiffExt\onearg.jl:119
[3] value_and_derivative(::typeof(get_Δat), ::AutoForwardDiff{nothing, Nothing}, ::Float64)
@ DifferentiationInterface C:\Users\jmurp.julia\packages\DifferentiationInterface\dRZrJ\src\fallbacks\no_prep.jl:61
[4] top-level scope
@ Untitled-1:14
Some type information was truncated. Use show(err) to see complete types.

@gdalle
Copy link
Member

gdalle commented Oct 2, 2024

How about now?

@jmurphy6895
Copy link
Author

Yeah, it's working! Thanks so much for the quick help!

@gdalle
Copy link
Member

gdalle commented Oct 2, 2024

I opened #527 to keep track of the tests I need to add, but I'll release the fix before that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to one or more autodiff backends
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants