`tfp.math.log1mexp` has a NaN gradient for small inputs (reaches `OrderedLogistic.log_prob`)
@aviralgarg05 已经在做这个了。
开始于 2026年11月19日。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 86/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
从 tensorflow_probability/python/math/generic.py 中的 log1mexp 开始,其中一个 tf.where 在 log(-expm1(-x)) 和 log1p(-exp(-x)) 之间做选择。使用 issue 中的代码片段复现问题,然后在 generic_test.py 中添加一个梯度测试。完成的标准是:对于较小的 x,梯度是有限的;OrderedLogistic 在 loc=18 时的 log_prob 梯度约为 1.13e-07;前向计算结果保持不变。
由索引模型根据 Issue 内容生成。
描述
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 :)
- 主要语言
- Jupyter Notebook
- 星标
- 4.4k
- 派生
- 1.1k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/probability 的其他 Issue
-
KL divergence between two `TransformedDistribution`s with a shared non-injective bijector (`AbsoluteValue`) returns the base KL可能已有人在做 @Andreas-Hum 于 19 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
tensorflow/probability#2036 ·
-
`Weibull` and `Gumbel` `survival_function` / `log_survival_function` are 0 / -inf in the upper tail未关闭
难度 3/5 1-2 天 新手友好度 70/100
tensorflow/probability#2042 ·
-
Chi2, Gamma, Beta and Binomial survival_function lose the upper tail because they fall back to 1 - cdf可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 4/5 3-5 天 新手友好度 54/100
tensorflow/probability#2040 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 68/100
tensorflow/probability#2034 ·
-
难度 5/5 一周以上 新手友好度 35/100
tensorflow/probability#2021 ·
查看 tensorflow/probability 的全部 Issue
相似的 Issue
-
needs-triage
难度 1/5 1 小时以内 新手友好度 88/100
flashinfer-ai/flashinfer#9803 ·
维护者通常 1 天内回复
-
bug module: rl
难度 2/5 1-3 小时 新手友好度 78/100
NVIDIA/Megatron-LM#9644 ·
维护者通常 2 天内回复
-
`build_flavor_config_from_local_checkpoint` fails with transformers 5.23.0: pipeline `impl` is now a dotted-path string可能已有人在做 @mlflow-app 今天认领。 未关闭has-closing-pr nightly triage: reproduced
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
FluxML/Optimisers.jl#254 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
google-deepmind/sonnet#323 ·