Skip to content

Add EnzymeGammaExt with analytic derivative rule for Gamma.gamma#3298

Open
oschulz wants to merge 1 commit into
EnzymeAD:mainfrom
oschulz:gamma-ext
Open

Add EnzymeGammaExt with analytic derivative rule for Gamma.gamma#3298
oschulz wants to merge 1 commit into
EnzymeAD:mainfrom
oschulz:gamma-ext

Conversation

@oschulz

@oschulz oschulz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Claude Opus (max effort) says:

HypergeometricFunctions v0.3.29 switched its gamma import from
SpecialFunctions to the Gamma package. Unlike SpecialFunctions.gamma
(a ccall to tgamma), Gamma.gamma is a pure-Julia Cephes implementation
with internal while loops. When it is inlined into a larger reverse-mode
gradient -- e.g. logpdf(truncated(Beta(a,b),lo,hi), x) differentiated
w.r.t. the shape parameters, which routes through the Beta CDF and the
2F1 hypergeometric function -- Enzyme cannot rematerialize the loop and
fails in libEnzyme with an internal "ficticious phi" recompute error.
This is what breaks the "truncated Beta" case of the Distributions
integration test on Julia 1.10.

Add a Gamma package extension supplying the analytic derivative
d/dx gamma(x) = gamma(x) * digamma(x), so Enzyme differentiates the
closed form instead of descending into the loops. This mirrors the
existing special-function rules (e.g. _logabsgamma, beta_inc).

Add test/ext/gamma.jl covering the scalar derivative across every branch
of the implementation and the previously-failing in-context 2F1 case.

@oschulz

oschulz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Should I reduce/remove the AI code comments?

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