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

Fixing device selection (cuda or cpu) #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions liegroups/torch/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ def is_valid_matrix(cls, mat):

# Determinants of each matrix in the batch should be 1
det_check = utils.isclose(mat.__class__(
np.linalg.det(mat.detach().cpu().numpy())), 1.)
np.linalg.det(mat.detach().cpu().numpy())), 1.).to(mat.device)

# The transpose of each matrix in the batch should be its inverse
inv_check = utils.isclose(mat.transpose(2, 1).bmm(mat),
torch.eye(cls.dim, dtype=mat.dtype)).sum(dim=1).sum(dim=1) \
torch.eye(cls.dim, dtype=mat.dtype, device=mat.device)).sum(dim=1).sum(dim=1) \
== cls.dim * cls.dim

return shape_check & det_check & inv_check
Expand Down
14 changes: 7 additions & 7 deletions liegroups/torch/so3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def exp(cls, phi):

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

# Otherwise...
Expand All @@ -47,7 +47,7 @@ def exp(cls, phi):
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(
A = c * torch.eye(cls.dim, dtype=phi.dtype, device=phi.device).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 @@ -132,7 +132,7 @@ def inv_left_jacobian(cls, phi):
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]) - \
torch.eye(cls.dof, dtype=phi.dtype, device=phi.device).expand_as(jac[small_angle_inds]) - \
0.5 * cls.wedge(phi[small_angle_inds])

# Otherwise...
Expand All @@ -153,7 +153,7 @@ def inv_left_jacobian(cls, phi):
dim=2).expand_as(jac[large_angle_inds])

A = hacha * \
torch.eye(cls.dof, dtype=phi.dtype).unsqueeze_(
torch.eye(cls.dof, dtype=phi.dtype, device=phi.device).unsqueeze_(
dim=0).expand_as(jac[large_angle_inds])
B = (1. - hacha) * utils.outer(axis, axis)
C = -ha * cls.wedge(axis)
Expand All @@ -179,7 +179,7 @@ def left_jacobian(cls, phi):
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]) + \
torch.eye(cls.dof, dtype=phi.dtype, device=phi.device).expand_as(jac[small_angle_inds]) + \
0.5 * cls.wedge(phi[small_angle_inds])

# Otherwise...
Expand All @@ -195,7 +195,7 @@ def left_jacobian(cls, phi):

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(
torch.eye(cls.dof, dtype=phi.dtype, device=phi.device).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]) * \
Expand Down Expand Up @@ -228,7 +228,7 @@ def log(self):
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]))
torch.eye(self.dim, dtype=mat.dtype, device=mat.device).expand_as(mat[small_angle_inds]))

# Otherwise...
large_angle_mask = small_angle_mask.logical_not()
Expand Down
2 changes: 1 addition & 1 deletion liegroups/torch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def trace(mat):
mat = mat.unsqueeze(dim=0)

# 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)
tr = (torch.eye(mat.shape[1], dtype=mat.dtype, device=mat.device) * mat).sum(dim=1).sum(dim=1)

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