JuliaMath / QuadGK.jl

adaptive 1d numerical Gauss–Kronrod integration in Julia
MIT License
272 stars 37 forks source link

Error with QuadGK 2.11 and Zygote #117

Open sschlenkrich opened 1 month ago

sschlenkrich commented 1 month ago

Hi,

I maintain a Julia package for Monte Carlo simulation in the finance domain. We use QuadGK in conjunction with Zygote.

The new QuadGK 2.11 seems to cause the following error: Compiling Tuple{typeof(QuadGK.cachedrule), Type{Float64}, Int64}: try/catch is not supported. See also

https://github.com/frame-consulting/DiffFusion.jl/actions/runs/10649757211/job/29520331450

It all worked well with earlier versions of QuadGK.

Any chance you could have a look and fix it?

stevengj commented 1 month ago

We might have to add a custom ChainRule, since I can't eliminate the new trycatch without eliminating thread-safety. For now, maybe you could just do

using ChainRulesCore
@non_differentiable QuadGK.cachedrule(Any...)

to prevent Zygote from trying to differentiate through that function (which in fact is not differentiable, nor is it necessary for the derivative, so Zygote is just wasting effort with it).

You could also use QuadGK via Integrals.jl, which has custom chain rules already.

(It would be good for us to add a custom ChainRule anyway, since the new eval_segbuf feature allows us to evaluate derivatives much more accurately and efficiently than was previously possible.)

sschlenkrich commented 1 month ago

Thansks a lot for quick response!

Your suggestion with flagging the problematic function as non-differentiable works for me.

stevengj commented 1 month ago

Re-opening because this should ideally be fixed in QuadGK.