Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

top_p_renorm_prob is not numerically stable for small probability values #708

Open
merrymercy opened this issue Dec 30, 2024 · 0 comments
Open
Assignees

Comments

@merrymercy
Copy link

merrymercy commented Dec 30, 2024

When top_p is 1.0, we expect top_p_renorm_prob to be an identity function. However, it will make small values zero.

Reproduce

The following example constructs some value small probability values (1e-9). After top_p_renorm_prob, all of them become zero.

import torch
import flashinfer

pre_norm_prob = torch.tensor([[1 - 1e-9 * 20] + [1e-9] * 20]).to("cuda")
probs = pre_norm_prob / pre_norm_prob.sum(dim=-1, keepdim=True)
print(f"sum={probs.sum(dim=1)}")

renormed_probs = flashinfer.sampling.top_p_renorm_prob(probs, 1.0)
print(f"{probs=}")
print(f"{renormed_probs=}")

print(f"{probs.min()=}")
print(f"{renormed_probs.min()=}")

assert torch.all(probs == probs)
assert torch.all(probs == renormed_probs)

Output

sum=tensor([1.], device='cuda:0')
probs=tensor([[1.0000e+00, 1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09,
         1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09,
         1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09, 1.0000e-09,
         1.0000e-09, 1.0000e-09, 1.0000e-09]], device='cuda:0')
renormed_probs=tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.]], device='cuda:0')
probs.min()=tensor(1.0000e-09, device='cuda:0')
renormed_probs.min()=tensor(0., device='cuda:0')
Traceback (most recent call last):
  File "/root/test_top_p_renorm.py", line 16, in <module>
    assert torch.all(probs == renormed_probs)
AssertionError
@yzh119 yzh119 self-assigned this Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants