`tfp.math.log1mexp` has a NaN gradient for small inputs (reaches `OrderedLogistic.log_prob`)
@aviralgarg05 y travaille déjà.
Depuis le 19/11/2026.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 86/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- python
- Domaine
- machine-learning
Piste de recherche
Commence dans tensorflow_probability/python/math/generic.py au niveau de log1mexp, où un tf.where choisit entre log(-expm1(-x)) et log1p(-exp(-x)). Reproduis le problème avec l'extrait de l'issue, puis ajoute un test de gradient dans generic_test.py. C'est terminé lorsque le gradient est fini pour de petits x et que le gradient du log_prob de OrderedLogistic en loc=18 vaut environ 1.13e-07, les valeurs directes restant inchangées.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
OrderedLogistic(...).log_prob gets a NaN gradient in float32 at loc = 18 (17 is still fine), and the NaN comes from tfp.math.log1mexp:
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
x = tf.constant([1e-9, 1e-7, 1e-3])
with tf.GradientTape() as tape:
tape.watch(x)
y = tfp.math.log1mexp(x)
print(tape.gradient(y, x).numpy()) # [nan 1.e+07 999.50006] exact 1/expm1(x): [1e9, 1e7, 999.5]
loc = tf.Variable(18.)
with tf.GradientTape() as tape:
lp = tfd.OrderedLogistic(cutpoints=[0., 1., 2.], loc=loc).log_prob(3)
print(tape.gradient(lp, loc).numpy()) # nan exact sigmoid(-16) = 1.13e-07
log1mexp picks log(-expm1(-x)) or log1p(-exp(-x)) with one tf.where, so the gradient of the branch it didn't pick is still computed. For small x, exp(-x) rounds to 1, that branch's derivative is inf, and the tf.where backward turns 0 * inf into NaN. StoppingRatioLogistic, Kumaraswamy.log_cdf and Geometric.log_cdf go through the same path.
The double-where trick keeps the forward values as they are:
--- a/tensorflow_probability/python/math/generic.py
+++ b/tensorflow_probability/python/math/generic.py
@@ -703,10 +703,14 @@ def log1mexp(x, name=None):
dtype = dtype_util.common_dtype([x], dtype_hint=tf.float32)
x = tf.convert_to_tensor(x, dtype=dtype, name='x')
x = tf.math.abs(x)
+ # This switching point is recommended in [1].
+ small = x < np.log(2)
+ # The branch not taken must not see small x: its derivative is infinite
+ # where exp(-x) rounds to 1, and would make the gradient NaN.
+ safe_x = tf.where(small, tf.ones_like(x), x)
return tf.where(
- # This switching point is recommended in [1].
- x < np.log(2), tf.math.log(-tf.math.expm1(-x)),
- tf.math.log1p(-tf.math.exp(-x)))
+ small, tf.math.log(-tf.math.expm1(-x)),
+ tf.math.log1p(-tf.math.exp(-safe_x)))
I ran into this while formalizing a few distribution formulas in Lean (TorchLean) and comparing them with TFP. 0.25.0 and the 2026-11-12 nightlies give the numbers above, on CPU. I have a gradient test for it in generic_test.py and can send the PR whenever you'd like :)
- Langage dominant
- Jupyter Notebook
- Étoiles
- 4.4k
- Forks
- 1.1k
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de tensorflow/probability
-
KL divergence between two `TransformedDistribution`s with a shared non-injective bijector (`AbsoluteValue`) returns the base KLPeut-être pris @Andreas-Hum l’a pris il y a 19 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
tensorflow/probability#2036 ·
-
`Weibull` and `Gumbel` `survival_function` / `log_survival_function` are 0 / -inf in the upper tailOuverte
Difficulté 3/5 1-2 jours Accessibilité débutants 70/100
tensorflow/probability#2042 ·
-
Chi2, Gamma, Beta and Binomial survival_function lose the upper tail because they fall back to 1 - cdfPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 54/100
tensorflow/probability#2040 · 1 commentaire ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
tensorflow/probability#2034 ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
tensorflow/probability#2021 ·
Toutes les issues de tensorflow/probability
Issues similaires
-
needs-triage
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
flashinfer-ai/flashinfer#9803 ·
Les mainteneurs répondent en général sous 1 jour
-
bug module: rl
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
NVIDIA/Megatron-LM#9644 ·
Les mainteneurs répondent en général sous 2 jours
-
`build_flavor_config_from_local_checkpoint` fails with transformers 5.23.0: pipeline `impl` is now a dotted-path stringPeut-être pris @mlflow-app l’a pris aujourd’hui. Ouvertehas-closing-pr nightly triage: reproduced
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
mlflow/mlflow#28000 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
FluxML/Optimisers.jl#254 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
google-deepmind/sonnet#323 ·