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

numerical instability in compositing.norm_weighted_sum #1135

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

polarbart
Copy link

This PR fixes a numerical instability in norm_weighted_sum on cpu and cuda.

When points_idx only contains one point with a small weight (<1e-4) the gradient gets disproportionately large.

Reason:
For numerical stability the normalization scalar (the sum of the weights/alphas) is clipped at 1e-4. However, this clipping isn't applied to the weighted sum of the features in the numerator.
If the sum of the weights is small the numerator then has a large value which results in an unwanted large gradient.

Fix:
Just clip the normalization scalar for the denominator and not for the numerator.
This also ensures the numerical stability for which the clipping was originally intended.

Code example:

import torch
from pytorch3d.renderer.compositing import norm_weighted_sum

pointsidx = torch.zeros(1, 1, 1, 1, dtype=torch.int64)
alphas = torch.full((1, 1, 1, 1), 1e-10, requires_grad=True)
pt_clds = torch.ones(1, 1)

out = norm_weighted_sum(pointsidx, alphas, pt_clds)
out.sum().backward()
print(f'value:    {out.item()}')
print(f'gradient: {alphas.grad.item()}')

Results of current master:

value:    9.999999974752427e-07
gradient: 9999.990234375

Results with this branch:

value:    9.999999974752427e-07
gradient: 0.0

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 22, 2022
@github-actions
Copy link

github-actions bot commented May 7, 2022

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the Stale label May 7, 2022
@polarbart polarbart mentioned this pull request May 7, 2022
@bottler bottler removed the Stale label May 9, 2022
@facebook-github-bot
Copy link
Contributor

@bottler has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@github-actions
Copy link

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the Stale label Jun 24, 2022
@bottler bottler removed the Stale label Jun 27, 2022
@github-actions
Copy link

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the Stale label Aug 12, 2022
@bottler bottler removed the Stale label Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants