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

Implement SE3.normalize() #506

Merged
merged 12 commits into from
May 2, 2023
Prev Previous commit
Next Next commit
add _skew_symm to simplify the code
  • Loading branch information
fantaosha committed May 1, 2023
commit 2e875757822f073f0c6ef896ffe2fd46b877a497
9 changes: 5 additions & 4 deletions theseus/labs/lie/functional/so3_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,9 @@ def setup_context(ctx, inputs, outputs):

@classmethod
def backward(cls, ctx, grad_output, _):
def _skew_symm(matrix: torch.Tensor) -> torch.Tensor:
return matrix - matrix.transpose(-1, -2)

u, s, v, sign = ctx.saved_tensors
ut = u.transpose(1, 2)
vt = v.transpose(1, 2)
Expand All @@ -1033,13 +1036,11 @@ def backward(cls, ctx, grad_output, _):
F = torch.where(F == 0, grad_output.new_ones(1) * torch.inf, F)
F = F.pow(-1)

u_term: torch.Tensor = u @ (F * (ut @ grad_u - grad_u.transpose(1, 2) @ u))
u_term: torch.Tensor = u @ (F * _skew_symm(ut @ grad_u))
u_term = torch.einsum("n...ij, nj->n...ij", u_term, s)
u_term = u_term @ vt

v_term: torch.Tensor = (
F * (vt @ grad_v - grad_v.transpose(1, 2) @ v)
) @ v.transpose(1, 2)
v_term: torch.Tensor = (F * _skew_symm(vt @ grad_v)) @ v.transpose(1, 2)
v_term = torch.einsum("ni, n...ij->n...ij", s, v_term)
v_term = u @ v_term
return u_term + v_term, None
Expand Down