Skip to content

Fix 2DGS projection backward on non-contiguous gradients - #1098

Open
nicolaihaeni wants to merge 1 commit into
nerfstudio-project:mainfrom
nicolaihaeni:fix/2dgs-bwd-contiguous-grads
Open

nicolaihaeni wants to merge 1 commit into
nerfstudio-project:mainfrom
nicolaihaeni:fix/2dgs-bwd-contiguous-grads

Conversation

@nicolaihaeni

Copy link
Copy Markdown

projection_2dgs_fused_bwd checks its incoming gradients with CHECK_INPUT, which requires them to be contiguous. Autograd can hand in non-contiguous ones, e.g. for depths when the normals or the distortion loss are used, so 2DGS training with the normal/distortion regularizers (as in simple_trainer_2dgs.py) fails with

RuntimeError: grad.depths must be contiguous

This makes the gradients contiguous before the check, as the 3DGS backward (projection_ewa_3dgs_fused_bwd) already does.

Repro (fails on main at backward()):

import torch
from gsplat import rasterization_2dgs
n, C = 20_000, 1
means = (torch.randn(n, 3) * 0.7 + torch.tensor([0, 0, 4.0])).cuda().requires_grad_()
quats = torch.nn.functional.normalize(torch.randn(n, 4), dim=-1).cuda().requires_grad_()
scales = (torch.rand(n, 3) * 0.03).cuda().requires_grad_()
opacities = torch.rand(n).cuda().requires_grad_()
colors = (torch.randn(n, 16, 3) * 0.3).cuda().requires_grad_()
viewmats = torch.eye(4).cuda()[None].repeat(C, 1, 1)
Ks = torch.tensor([[500.0, 0, 320], [0, 500.0, 240], [0, 0, 1]]).cuda()[None].repeat(C, 1, 1)
out = rasterization_2dgs(means, quats, scales, opacities, colors, viewmats, Ks, 640, 480,
                         sh_degree=3, render_mode="RGB+ED", distloss=True)
out[3].sum().backward()  # normals_from_depth

projection_2dgs_fused_bwd required its incoming gradients to be contiguous, but autograd can hand in
non-contiguous ones (e.g. for depths when normals or the distortion loss are rendered), which raised
"grad.depths must be contiguous". Make them contiguous first, as the 3DGS backward already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant