[XLA] betainc returns NaN at endpoints with infinite shape parameters
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
binary
TensorFlow version
TensorFlow: 2.23.0-dev20261011
Custom code
Yes
OS platform and distribution
Ubuntu24.04
Mobile device
No response
Python version
3.11.15
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
12.0.140
GPU model and memory
No response
Current behavior?
tf.math.betainc returns NaN under XLA when either shape parameter is +Inf and x is 0 or 1, instead of preserving the expected endpoint values.
Eager / graph: [0, 0, 1, 1]
XLA: [NaN, NaN, NaN, NaN]
This reproduces on both CPU and GPU with float32 and float64.
Standalone code to reproduce the issue
import numpy as np
import tensorflow as tf
def fn(a, b, x):
return tf.math.betainc(a, b, x)
a = tf.constant([np.inf, 1., np.inf, 1.])
b = tf.constant([1., np.inf, 1., np.inf])
x = tf.constant([0., 0., 1., 1.])
with tf.device("/CPU:0"):
print("Eager:", fn(a, b, x).numpy())
print("XLA:", tf.function(fn, jit_compile=True)(a, b, x).numpy())
Relevant log output
Eager: [0. 0. 1. 1.]
XLA: [nan nan nan nan]
Hi @xyt66665000,
Thank you for reporting this issue and for providing the clear standalone code to reproduce the behavior. The issue indicates where tf.math.betainc returns NaN under XLA (on both CPU and GPU) when evaluated at the endpoints (x = 0 or 1) with infinite shape parameters, whereas eager execution correctly yields the expected values.
We will investigate the XLA lowering logic for betainc and provide an update.
Hi @nileshau-afk , As a beginner open-source contributer, I would really like to work on this issue. Is this issue still open?
Hi @parth-bansal081 ,
Thanks for your interest in working on this issue!
Yes, you are welcome to investigate and work on a fix for this. Please review the existing issue discussion and TensorFlow’s contribution guidelines before getting started, and include appropriate tests with the proposed change.
Official contribution guidelines: https://www.tensorflow.org/community/contribute
Thanks!
hi @nileshau-afk, I'd like to investigate this issue and work on a regression-tested fix.
The reported behavior suggests that the XLA betainc path is not preserving the endpoint identities (I_0(a,b)=0) and (I_1(a,b)=1) for the reported positive shape-parameter cases involving (+\infty). One possibility is that the general numerical approximation produces invalid intermediate values before endpoint handling is applied, although I would like to verify this against the actual lowering and implementation before attributing the failure to a specific expression.
My initial investigation would focus on:
- Tracing the XLA lowering and numerical implementation of
tf.math.betainc, including any existing endpoint special cases. - Adding a focused regression test for the four reported combinations of infinite shape parameters and (x\in{0,1}), covering both
float32andfloat64. - Verifying eager, graph, and XLA behavior, while preserving the existing semantics for invalid inputs and finite interior values.
- Checking the related issue #128984, which reports NaNs for large but finite shape parameters under XLA, to determine whether the two failures share an underlying numerical defect.
If endpoint handling is missing or applied too late, a narrowly scoped correction that preserves the exact endpoint values without masking unrelated NaNs may be sufficient. I would first verify that approach against the implementation and existing tests.
Please let me know if there is an existing investigation or preferred implementation path for this operation that I should build on.