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

test fails on Julia 1.8 #93

Closed
Moelf opened this issue Aug 5, 2022 · 8 comments
Closed

test fails on Julia 1.8 #93

Moelf opened this issue Aug 5, 2022 · 8 comments

Comments

@Moelf
Copy link

Moelf commented Aug 5, 2022

using Integrals, Zygote

julia> f(x, p) = sum(sin.(p[1] * x))
f (generic function with 1 method)

julia> lb = 1.0
1.0

julia> ub = 3.0
3.0

julia> p = 2.0
2.0

julia> function testf(lb, ub, p)
           prob = IntegralProblem(f, lb, ub, p)
           sin(solve(prob, QuadGKJL(), reltol = 1e-3, abstol = 1e-3)[1])
       end
testf (generic function with 1 method)

julia> dlb1, dub1, dp1 = Zygote.gradient(testf, lb, ub, p)
ERROR: BoundsError: attempt to access 0-element Vector{Any} at index []
Stacktrace:
  [1] throw_boundserror(A::Vector{Any}, I::Tuple{})
    @ Base ./abstractarray.jl:703
@ChrisRackauckas
Copy link
Member

On v1.8 with the latest Integrals this outputs:

julia > dlb1, dub1, dp1 = Zygote.gradient(testf, lb, ub, p)
(-0.9092974268256817, -0.27941549819892586, -0.4091435516769757)

@nelslind
Copy link

nelslind commented Apr 20, 2023

I'm having the same issue in Julia 1.8.5. What version of Julia and versions of Zygote and Integrals was this working under?

@ChrisRackauckas
Copy link
Member

The example is incorrect. Corrected:

using Integrals, Zygote
f(x, p) = sum(sin.(p * x))
lb = 1.0
ub = 3.0
p = 2.0
function testf(lb, ub, p)
    prob = IntegralProblem(f, lb, ub, p)
    sin(solve(prob, QuadGKJL(), reltol = 1e-3, abstol = 1e-3)[1])
end
dlb1, dub1, dp1 = Zygote.gradient(testf, lb, ub, p)

The difference is f(x, p) = sum(sin.(p * x)) instead of f(x, p) = sum(sin.(p[1] * x)). The OP's original example fails because p[1] fails because p is a scalar, not a vector. I don't know why Zygote throws that weird error, but that's an issue for Zygote.

If you have an example that' supposed to work, please open a new issue.

@Moelf
Copy link
Author

Moelf commented Apr 21, 2023

? that's the problem though

julia> p = [2.0]
1-element Vector{Float64}:
 2.0

julia> function testf(lb, ub, p)
           prob = IntegralProblem(f, lb, ub, p)
           sin(solve(prob, QuadGKJL(), reltol = 1e-3, abstol = 1e-3)[1])
       end
testf (generic function with 1 method)

julia> dlb1, dub1, dp1 = Zygote.gradient(testf, lb, ub, p)
ERROR: BoundsError: attempt to access 0-element Vector{Any} at index []
Stacktrace:
  [1] throw_boundserror(A::Vector{Any}, I::Tuple{})
    @ Base ./abstractarray.jl:744
  [2] checkbounds
    @ ./abstractarray.jl:709 [inlined]
  [3] _getindex
    @ ./abstractarray.jl:1326 [inlined]
  [4] getindex
    @ ./abstractarray.jl:1294 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/Zygote/SuKWp/src/lib/lib.jl:314 [inlined]
  [6] (::Zygote.Jnew{Base.MethodList, Vector{Any}, false})(Δ::Nothing)
    @ Zygote ~/.julia/packages/Zygote/SuKWp/src/lib/lib.jl:308

also Zygote can handle this:

julia> p = [2.0]
1-element Vector{Float64}:
 2.0

julia> Zygote.gradient(f, 30, p)
(-1.9048259608303126, [-28.57238941245469])

julia> p = 2.0
2.0

julia> Zygote.gradient(f, 30, p)
(-1.9048259608303126, -28.57238941245469)

@nelslind
Copy link

nelslind commented Apr 21, 2023

Hi All, Thanks for the help with this.

Here is a simple example:

`
foo(x, p) = p + x

lb = 1.0
ub = 3.0
p = 2.0

function testf(lb, ub, p)
prob = IntegralProblem(foo, lb, ub, p)
solve(prob, QuadGKJL(), reltol = 1e-3, abstol = 1e-3)[1]
end

dlb1, dub1, dp1 = Zygote.gradient(testf, lb, ub, p)
`

I get "ERROR: BoundsError: attempt to access 0-element Vector{Any} at index []"

@nelslind
Copy link

nelslind commented Apr 21, 2023

Also, the example at

https://docs.juliahub.com/Integrals/QIwht/3.1.1/tutorials/differentiating_integrals/

creates the same error for Zygote, but works fine for ForwardDiff and and FiniteDiff (I had to replace CubaCuhre with HCubatureJL). This is true using either Integrals 3.1.1 (which is the version listed at that page) and 3.7.

@ChrisRackauckas
Copy link
Member

@ArnoStrouwen does that already have an issue?

@ArnoStrouwen
Copy link
Member

I think it is this issue #99?

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

No branches or pull requests

4 participants