Created
May 22, 2026 03:10
-
-
Save ezyang/1990294c1086cd0a1b288240bc8b44d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import torch.nn.functional as F | |
| def topk_softmax(logits, k): | |
| vals, idx = torch.topk(logits, k, dim=-1) | |
| return idx, F.softmax(vals, dim=-1) | |
| def full_softmax_then_renorm(logits, k): | |
| probs = F.softmax(logits, dim=-1) | |
| idx = torch.topk(logits, k, dim=-1).indices | |
| p = probs.gather(dim=-1, index=idx) | |
| w = p / p.sum(dim=-1, keepdim=True) | |
| return idx, w | |
| torch.manual_seed(0) | |
| T, E, k = 4, 8, 2 | |
| logits1 = torch.randn(T, E, dtype=torch.double, requires_grad=True) | |
| logits2 = logits1.detach().clone().requires_grad_() | |
| idx1, w1 = topk_softmax(logits1, k) | |
| idx2, w2 = full_softmax_then_renorm(logits2, k) | |
| assert torch.equal(idx1, idx2) | |
| print("forward max diff:", (w1 - w2).abs().max().item()) | |
| # Some arbitrary downstream gradient. | |
| grad_out = torch.randn_like(w1) | |
| loss1 = (w1 * grad_out).sum() | |
| loss2 = (w2 * grad_out).sum() | |
| loss1.backward() | |
| loss2.backward() | |
| print("backward max diff:", (logits1.grad - logits2.grad).abs().max().item()) | |
| """ | |
| forward max diff: 1.1102230246251565e-16 | |
| backward max diff: 1.1102230246251565e-16 | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment