jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.lax.betainc does not support gradients with respect to a and b parameters

Open anna-researcher opened this issue 5 months ago • 6 comments

Description

jax.lax.betainc(a, b, x) supports gradient computation with respect to x but raises ValueError when differentiating with respect to a or b. This makes betainc unusable in training loops where a or b are learnable parameters.

Minimal Reproducer

import jax
import jax.lax as lax
import jax.numpy as jnp

a = jnp.array([1.0, 2.0])
b = jnp.array([2.0, 3.0])
x = jnp.array([0.5, 0.5])

# Forward works
print(lax.betainc(a, b, x))                                       # [0.75, 0.69]

# Gradient w.r.t. x works
print(jax.grad(lambda x: lax.betainc(a, b, x).sum())(x))         # [1.0, 1.5]

# Gradient w.r.t. a fails
jax.grad(lambda a: lax.betainc(a, b, x).sum())(a)                 # ValueError

# Gradient w.r.t. b also fails
jax.grad(lambda b: lax.betainc(a, b, x).sum())(b)                 # ValueError

Error

ValueError: Betainc gradient with respect to a and b not supported.

Expected Behavior

jax.lax.betainc should support gradients with respect to all three parameters a, b, and x. The regularized incomplete beta function has known closed-form derivatives with respect to a and b involving the digamma function and logarithms, so this is a missing implementation rather than a fundamental limitation.

System info (python version, jaxlib version, accelerator, etc.)

jax:    0.10.1
jaxlib: 0.10.1
numpy:  2.4.6
python: 3.12.13 | packaged by Anaconda, Inc. | (main, Mar 19 2026, 20:20:58) [GCC 14.3.0]
device info: cpu-1, 1 local devices
process_count: 1
platform: uname_result(system='Linux', node='unicorn-login-01', release='6.8.0-110-generic', version='#110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026', machine='x86_64')
JAX_PLATFORMS=cpu

anna-researcher avatar Jun 20 '26 05:06 anna-researcher

Hi - this is not a bug, it's working as expected. Gradients of betainc with respect to a and b have no easily-expressible closed-form solution, and the ValueError you're seeing was explicitly added for this case.

I can leave this open as a feature request, but it's not one that will likely be resolved any time soon.

jakevdp avatar Jun 22 '26 12:06 jakevdp

Hi - this is not a bug, it's working as expected. Gradients of betainc with respect to a and b have no easily-expressible closed-form solution, and the ValueError you're seeing was explicitly added for this case.

I can leave this open as a feature request, but it's not one that will likely be resolved any time soon.

I see, thank you for marking it as a feature request, did not spot this in the documentation, so thought it was a bug.

anna-researcher avatar Jun 23 '26 03:06 anna-researcher

Confirmed on 0.10.2: gradient w.r.t. x works but a/b raise ValueError: Betainc gradient with respect to a and b not supported. This is explicit rather than incidental — in jax/_src/lax/special.py, betainc_gradx (L129) implements the x derivative, but the a and b positions are wired to betainc_grad_not_implemented (L135-136, registered at L697-698), which just raises. So it is a missing-rule issue, not a fundamental one: the derivatives of the regularized incomplete beta w.r.t. a and b have closed forms (involving betainc itself, digamma, and a 3F2/log term, as implemented in tensorflow_probability.math.betaincinv/betainc and SciPy's derivative notes). Adding those as the JVP rules for the a/b operands would make betainc usable with learnable shape parameters. @jakevdp

CodersAcademy006 avatar Jul 16 '26 09:07 CodersAcademy006

oh yeah, i hit this exact wall last month when trying to optimize some shape parameters in a probabilistic model. honestly, the error message is super blunt, "Betainc gradient with respect to a and b not supported.", and the thing is, it's not just a docs gap, the implementation literally throws that ValueError. i looked at the source and jax.lax.betainc only wires up the gradient for x, not a or b, so autodiff just falls over.

i ended up patching around it by defining betainc via scipy's special.betainc (wrapped with jax.scipy), which does support gradients for all params thanks to their custom defjvp. something like:

import jax
import jax.scipy as jsp

def safe_betainc(a, b, x):
    return jsp.special.betainc(a, b, x)

print(jax.grad(lambda a: safe_betainc(a, b, x).sum())(a)) # works!

not gonna lie, it's slower for big arrays because it's not tied to XLA, but it unlocked my training loop. are you trying to run this on TPU/GPU, or just CPU? also, curious if you've found any reason you'd need jax.lax.betainc specifically vs jax.scipy.special.betainc?

rehan243 avatar Jul 16 '26 14:07 rehan243

i ended up patching around it by defining betainc via scipy's special.betainc

I don't understand how this would change anything, as jsp.special.betainc executes via lax.betainc. Trying your code (after defining the undefined variables) raises the same ValueError as reported above.

jakevdp avatar Jul 16 '26 16:07 jakevdp

Until #38977 (or something like it) lands, this works today: chebax (https://github.com/andreshernandez-spec/chebax) has

chebax.betainc_fn(a, b, x)          # jax.grad works w.r.t. a and b
jax.grad(chebax.betainc_fn, (0, 1))(a, b, x)

The shape parameters are traced scalars, the parameter derivatives come from differentiating a fixed-degree Chebyshev representation (no extra series iteration on the gradient path), measured 2.0e-14 against mp.diff at 40 dps. Values 7.5e-15 absolute.

Limits: the range of the shape parameters need to be defined in advance. Currently the generator sets (a, b) in [0.1, 10]^2, but that would be easy to change, it just should not be done on the fly since the calculation might take a few minutes. Since Beta is known, it is a cost that can be offset once and then just store as a table. Another limit it needs to be uniform per call, so the shape parameters are traced scalars, not per-element arrays. For a censored StudentT/Beta likelihood with a scalar latent shape, that is the served case; for batched per-element shapes it is not. Although for something like NUTS, this would not be a problem.

andreshernandez-spec avatar Jul 31 '26 14:07 andreshernandez-spec