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

Fix inconsistant cuda type in utils and so3 #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 62 additions & 28 deletions liegroups/torch/so3.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ def exp(cls, phi):

mat = phi.new_empty(phi.shape[0], cls.dim, cls.dim)
angle = phi.norm(p=2, dim=1)
cuda = mat.is_cuda

# Near phi==0, use first order Taylor expansion
small_angle_mask = utils.isclose(angle, 0.)
small_angle_inds = small_angle_mask.nonzero().squeeze_(dim=1)

if len(small_angle_inds) > 0:
mat[small_angle_inds] = \
torch.eye(cls.dim, dtype=phi.dtype).expand_as(mat[small_angle_inds]) + \
cls.wedge(phi[small_angle_inds])

if cuda:
mat[small_angle_inds] = \
torch.eye(cls.dim, dtype=phi.dtype).cuda().expand_as(mat[small_angle_inds]) + \
cls.wedge(phi[small_angle_inds])
else:
mat[small_angle_inds] = \
torch.eye(cls.dim, dtype=phi.dtype).expand_as(mat[small_angle_inds]) + \
cls.wedge(phi[small_angle_inds])

# Otherwise...
large_angle_mask = small_angle_mask.logical_not()
large_angle_inds = large_angle_mask.nonzero().squeeze_(dim=1)
Expand All @@ -46,9 +52,12 @@ def exp(cls, phi):
dim=2).expand_as(mat[large_angle_inds])
c = angle.cos().unsqueeze_(dim=1).unsqueeze_(
dim=2).expand_as(mat[large_angle_inds])

A = c * torch.eye(cls.dim, dtype=phi.dtype).unsqueeze_(dim=0).expand_as(
mat[large_angle_inds])
if cuda:
A = c * torch.eye(cls.dim, dtype=phi.dtype).cuda().unsqueeze_(dim=0).expand_as(
mat[large_angle_inds])
else:
A = c * torch.eye(cls.dim, dtype=phi.dtype).unsqueeze_(dim=0).expand_as(
mat[large_angle_inds])
B = (1. - c) * utils.outer(axis, axis)
C = s * cls.wedge(axis)

Expand Down Expand Up @@ -123,17 +132,22 @@ def inv_left_jacobian(cls, phi):
if phi.shape[1] != cls.dof:
raise ValueError(
"phi must have shape ({},) or (N,{})".format(cls.dof, cls.dof))

cuda = phi.is_cuda
jac = phi.new_empty(phi.shape[0], cls.dof, cls.dof)
angle = phi.norm(p=2, dim=1)

# Near phi==0, use first order Taylor expansion
small_angle_mask = utils.isclose(angle, 0.)
small_angle_inds = small_angle_mask.nonzero().squeeze_(dim=1)
if len(small_angle_inds) > 0:
jac[small_angle_inds] = \
torch.eye(cls.dof, dtype=phi.dtype).expand_as(jac[small_angle_inds]) - \
0.5 * cls.wedge(phi[small_angle_inds])
if cuda:
jac[small_angle_inds] = \
torch.eye(cls.dof, dtype=phi.dtype).cuda().expand_as(jac[small_angle_inds]) - \
0.5 * cls.wedge(phi[small_angle_inds])
else:
jac[small_angle_inds] = \
torch.eye(cls.dof, dtype=phi.dtype).expand_as(jac[small_angle_inds]) - \
0.5 * cls.wedge(phi[small_angle_inds])

# Otherwise...
large_angle_mask = small_angle_mask.logical_not()
Expand All @@ -151,10 +165,14 @@ def inv_left_jacobian(cls, phi):
dim=2).expand_as(jac[large_angle_inds])
hacha.unsqueeze_(dim=1).unsqueeze_(
dim=2).expand_as(jac[large_angle_inds])

A = hacha * \
torch.eye(cls.dof, dtype=phi.dtype).unsqueeze_(
dim=0).expand_as(jac[large_angle_inds])
if cuda:
A = hacha * \
torch.eye(cls.dof, dtype=phi.dtype).cuda().unsqueeze_(
dim=0).expand_as(jac[large_angle_inds])
else:
A = hacha * \
torch.eye(cls.dof, dtype=phi.dtype).unsqueeze_(
dim=0).expand_as(jac[large_angle_inds])
B = (1. - hacha) * utils.outer(axis, axis)
C = -ha * cls.wedge(axis)

Expand All @@ -173,14 +191,20 @@ def left_jacobian(cls, phi):

jac = phi.new_empty(phi.shape[0], cls.dof, cls.dof)
angle = phi.norm(p=2, dim=1)
cuda = phi.is_cuda

# Near phi==0, use first order Taylor expansion
small_angle_mask = utils.isclose(angle, 0.)
small_angle_inds = small_angle_mask.nonzero().squeeze_(dim=1)
if len(small_angle_inds) > 0:
jac[small_angle_inds] = \
torch.eye(cls.dof, dtype=phi.dtype).expand_as(jac[small_angle_inds]) + \
0.5 * cls.wedge(phi[small_angle_inds])
if cuda:
jac[small_angle_inds] = \
torch.eye(cls.dof, dtype=phi.dtype).cuda().expand_as(jac[small_angle_inds]) + \
0.5 * cls.wedge(phi[small_angle_inds])
else:
jac[small_angle_inds] = \
torch.eye(cls.dof, dtype=phi.dtype).expand_as(jac[small_angle_inds]) + \
0.5 * cls.wedge(phi[small_angle_inds])

# Otherwise...
large_angle_mask = small_angle_mask.logical_not()
Expand All @@ -192,11 +216,16 @@ def left_jacobian(cls, phi):
angle.unsqueeze(dim=1).expand(len(angle), cls.dof)
s = angle.sin()
c = angle.cos()

A = (s / angle).unsqueeze_(dim=1).unsqueeze_(
dim=2).expand_as(jac[large_angle_inds]) * \
torch.eye(cls.dof, dtype=phi.dtype).unsqueeze_(dim=0).expand_as(
jac[large_angle_inds])
if cuda:
A = (s / angle).unsqueeze_(dim=1).unsqueeze_(
dim=2).expand_as(jac[large_angle_inds]) * \
torch.eye(cls.dof, dtype=phi.dtype).cuda().unsqueeze_(dim=0).expand_as(
jac[large_angle_inds])
else:
A = (s / angle).unsqueeze_(dim=1).unsqueeze_(
dim=2).expand_as(jac[large_angle_inds]) * \
torch.eye(cls.dof, dtype=phi.dtype).unsqueeze_(dim=0).expand_as(
jac[large_angle_inds])
B = (1. - s / angle).unsqueeze_(dim=1).unsqueeze_(
dim=2).expand_as(jac[large_angle_inds]) * \
utils.outer(axis, axis)
Expand All @@ -213,7 +242,7 @@ def log(self):
mat = self.mat.unsqueeze(dim=0)
else:
mat = self.mat

cuda = mat.is_cuda
phi = mat.new_empty(mat.shape[0], self.dof)

# The cosine of the rotation angle is related to the utils.trace of C
Expand All @@ -224,11 +253,16 @@ def log(self):
# Near phi==0, use first order Taylor expansion
small_angle_mask = utils.isclose(angle, 0.)
small_angle_inds = small_angle_mask.nonzero().squeeze_(dim=1)

if len(small_angle_inds) > 0:
phi[small_angle_inds, :] = \
self.vee(mat[small_angle_inds] -
torch.eye(self.dim, dtype=mat.dtype).expand_as(mat[small_angle_inds]))
if cuda:
phi[small_angle_inds, :] = \
self.vee(mat[small_angle_inds] -
torch.eye(self.dim, dtype=mat.dtype).cuda().expand_as(mat[small_angle_inds]))
else:
phi[small_angle_inds, :] = \
self.vee(mat[small_angle_inds] -
torch.eye(self.dim, dtype=mat.dtype).expand_as(mat[small_angle_inds]))

# Otherwise...
large_angle_mask = small_angle_mask.logical_not()
Expand Down
6 changes: 4 additions & 2 deletions liegroups/torch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def trace(mat):
# Default batch size is 1
if mat.dim() < 3:
mat = mat.unsqueeze(dim=0)

if mat.is_cuda:
tr = (torch.eye(mat.shape[1], dtype=mat.dtype).cuda() * mat).sum(dim=1).sum(dim=1)
# Element-wise multiply by identity and take the sum
tr = (torch.eye(mat.shape[1], dtype=mat.dtype) * mat).sum(dim=1).sum(dim=1)
else:
tr = (torch.eye(mat.shape[1], dtype=mat.dtype) * mat).sum(dim=1).sum(dim=1)

return tr.view(mat.shape[0])