Lightning-AI / Lightning-AI/lightning-thunder
Thunder-generated backward for sigmoid may compute `nan` for large (in absolute values) negative inputs
@kiya00 is already working on this.
Since Oct 9, 2025.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 121
- PR merge metrics
- No merged PRs in 30d
Description
## 🐛 Bug
The derivative of `sigmoid(x)` is `sigmoid(x) * (1 - sigmoid(x))` which is obtained after a couple of algebraic simplifications and substitutions. There's no manual implementation of this derivative in Thunder and therefore it's generated from its decomposition and chain rule that together with fusion rematerialization results in:
```py
def backward(output_grad, x):
return -(-output_grad * sigmoid(x) * sigmoid(x) * exp(-x))
```
Reproducer:
```py
In [1]: import thunder
/home/iyashchuk/dev/lightning-thunder/thunder/executors/transformer_engineex.py:56: UserWarning: transformer_engine failed to import with exception cannot import name 'MXFP8BlockScaling' from 'transformer_engine.common.recipe' (/home/iyashchuk/miniforge3/envs/pytorch-cuda-dev/lib/python3.10/site-packages/transformer_engine-1.11.0+c27ee60-py3.10-linux-x86_64.egg/transformer_engine/common/recipe/__init__.py)
warnings.warn(f"transformer_engine failed to import with exception {ex}")
/home/iyashchuk/dev/lightning-thunder/thunder/executors/transformer_engineex.py:62: UserWarning: Installed version of transformer_engine 1.11.0+c27ee60 is not supported, please upgrade to version 2.0 from https://github.com/NVIDIA/TransformerEngine/tree/release_v2.0. `transformer_engine_ex` will not be used.
warnings.warn(msg)
In [2]: import torch
In [3]: @thunder.jit
...: def sigmoid(x): return torch.sigmoid(x)
In [4]: a = torch.tensor(-1000., device="cuda", requires_grad=True)
In [5]: y = sigmoid(a)
In [6]: y.backward(y)
In [7]: a.grad
Out[7]: tensor(nan, device='cuda:0') # INCORRECT!
In [8]: a.grad = None
In [9]: y = torch.sigmoid(a)
In [10]: y.backward(y)
In [11]: a.grad
Out[11]: tensor(0., device='cuda:0')
```
Here's the backward execution trace:
```py
In [13]: sigmoid._lc_cs.last_backward_traces[-1]
Out[13]:
def backward_fn(saved_for_backward, cotangents):
# saved_for_backward: "Collection"
# cotangents: "Collection"
C0, _, = saved_for_backward
# C0: "Collection"
# None
clear_mutable_collection(saved_for_backward)
del saved_for_backward
t8, = cotangents
# t8: "cuda:0 f32[]"
clear_mutable_collection(cotangents)
del cotangents
t3, x, = C0
# t3: "cuda:0 f32[]"
# x: "cuda:0 f32[]"
clear_mutable_collection(C0)
del C0
[bw_t13] = nvFusion0(t8, x, t3)
# bw_t9 = prims.neg(t8) # bw_t9: "cuda:0 f32[]"
# t4 = prims.neg(x) # t4: "cuda:0 f32[]"
# bw_t10 = prims.mul(bw_t9, t3) # bw_t10: "cuda:0 f32[]"
# t5 = prims.exp(t4) # t5: "cuda:0 f32[]"
# bw_t11 = prims.mul(bw_t10, t3) # bw_t11: "cuda:0 f32[]"
# bw_t12 = prims.mul(bw_t11, t5) # bw_t12: "cuda:0 f32[]"
# bw_t13 = prims.neg(bw_t12) # bw_t13: "cuda:0 f32[]"
del t8, x, t3
return (bw_t13,)
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.