Skip to content

Commit

Permalink
Added th.device to represent both str and torch.device. (#357)
Browse files Browse the repository at this point in the history
* Added th.device to represent both str and torch.device.

* _TheseusDevice now includes optional as well.

* Renamed TheseusDevice as DeviceType.
  • Loading branch information
luisenp authored Nov 9, 2022
1 parent a8d6034 commit 1b579ee
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 38 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include LICENSE README.md
include requirements/*.txt
include py.typed
graft theseus/extlib
1 change: 1 addition & 0 deletions theseus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.
__version__ = "0.1.2"

from .constants import DeviceType as DeviceType

from .core import ( # usort: skip
AutoDiffCostFunction,
Expand Down
4 changes: 3 additions & 1 deletion theseus/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from typing import Optional, Union
import math

import torch
Expand Down Expand Up @@ -56,3 +56,5 @@ def __getitem__(self, dtype):
_SE3_NEAR_ZERO_EPS = EPSDict(float32_eps=1e-2, float64_eps=5e-3)

_SE3_HAT_EPS = EPSDict(float32_eps=5e-6, float64_eps=5e-7)

DeviceType = Optional[Union[str, torch.device]]
3 changes: 2 additions & 1 deletion theseus/core/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import torch

from theseus.constants import DeviceType
from theseus.core.theseus_function import TheseusFunction
from theseus.geometry.manifold import Manifold

Expand Down Expand Up @@ -55,7 +56,7 @@ def __init__(self, dtype: Optional[torch.dtype] = None):

self._batch_size: Optional[int] = None

self.device: torch.device = torch.device("cpu")
self.device: DeviceType = torch.device("cpu")

self.dtype: Optional[torch.dtype] = dtype or torch.get_default_dtype()

Expand Down
6 changes: 5 additions & 1 deletion theseus/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Optional, Sequence, Union

import torch
from theseus.constants import DeviceType


class Variable:
Expand Down Expand Up @@ -107,12 +108,15 @@ def __setitem__(self, item, value):
# In this case, the device, dtype and name can be specified.
def as_variable(
value: Union[float, Sequence[float], torch.Tensor, Variable],
device: Optional[torch.device] = None,
device: DeviceType = None,
dtype: Optional[torch.dtype] = None,
name: Optional[str] = None,
) -> Variable:
if isinstance(value, Variable):
return value
if isinstance(device, str):
# mypy doesn't like passing str to torch.as_tensor so convert
device = torch.device(device)
tensor = torch.as_tensor(value, dtype=dtype, device=device)
if isinstance(value, float):
tensor = tensor.view(1, 1)
Expand Down
3 changes: 2 additions & 1 deletion theseus/embodied/kinematics/kinematics_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import torch

from theseus.constants import DeviceType
from theseus.geometry import SE3, LieGroup, Point2, Vector

RobotModelInput = Union[torch.Tensor, Vector]
Expand Down Expand Up @@ -36,7 +37,7 @@ def forward_kinematics(self, robot_pose: RobotModelInput) -> Dict[str, LieGroup]


class UrdfRobotModel(KinematicsModel):
def __init__(self, urdf_path: str, device: Optional[str] = None):
def __init__(self, urdf_path: str, device: DeviceType = None):
try:
import differentiable_robot_model as drm
except ModuleNotFoundError as e:
Expand Down
5 changes: 3 additions & 2 deletions theseus/geometry/lie_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import torch

from theseus.constants import DeviceType
from theseus.geometry.manifold import Manifold


Expand Down Expand Up @@ -51,7 +52,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "LieGroup":
pass
Expand All @@ -62,7 +63,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "LieGroup":
pass
Expand Down
9 changes: 5 additions & 4 deletions theseus/geometry/point_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import torch

from theseus.constants import DeviceType
from .vector import Vector


Expand Down Expand Up @@ -41,7 +42,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "Point2":
if len(size) != 1:
Expand All @@ -62,7 +63,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "Point2":
if len(size) != 1:
Expand Down Expand Up @@ -143,7 +144,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "Point3":
if len(size) != 1:
Expand All @@ -164,7 +165,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "Point3":
if len(size) != 1:
Expand Down
4 changes: 2 additions & 2 deletions theseus/geometry/se2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SE2":
if len(size) != 1:
Expand All @@ -66,7 +66,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SE2":
if len(size) != 1:
Expand Down
5 changes: 2 additions & 3 deletions theseus/geometry/se3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import torch

import theseus
import theseus.constants

from .lie_group import LieGroup
Expand Down Expand Up @@ -45,7 +44,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SE3":
if len(size) != 1:
Expand Down Expand Up @@ -73,7 +72,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SE3":
if len(size) != 1:
Expand Down
4 changes: 2 additions & 2 deletions theseus/geometry/so2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SO2":
if len(size) != 1:
Expand All @@ -67,7 +67,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SO2":
if len(size) != 1:
Expand Down
4 changes: 2 additions & 2 deletions theseus/geometry/so3.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SO3":
# Reference:
Expand Down Expand Up @@ -80,7 +80,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: theseus.constants.DeviceType = None,
requires_grad: bool = False,
) -> "SO3":
if len(size) != 1:
Expand Down
5 changes: 3 additions & 2 deletions theseus/geometry/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import torch

from theseus.constants import DeviceType
from theseus.geometry.manifold import Manifold

from .lie_group import LieGroup
Expand Down Expand Up @@ -46,7 +47,7 @@ def rand(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "Vector":
if len(size) != 2:
Expand All @@ -66,7 +67,7 @@ def randn(
*size: int,
generator: Optional[torch.Generator] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
device: DeviceType = None,
requires_grad: bool = False,
) -> "Vector":
if len(size) != 2:
Expand Down
3 changes: 2 additions & 1 deletion theseus/optimizer/linear/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.
from typing import Tuple, Union
import torch
from theseus.constants import DeviceType


# See Nocedal and Wright, Numerical Optimization, pp. 260 and 261
Expand All @@ -13,7 +14,7 @@ def convert_to_alpha_beta_damping_tensors(
damping_eps: float,
ellipsoidal_damping: bool,
batch_size: int,
device: torch.device,
device: DeviceType,
dtype: torch.dtype,
) -> Tuple[torch.Tensor, torch.Tensor]:
damping = torch.as_tensor(damping).to(device=device, dtype=dtype)
Expand Down
Empty file added theseus/py.typed
Empty file.
4 changes: 2 additions & 2 deletions theseus/theseus_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Variable,
Vectorize,
)
from theseus.constants import __FROM_THESEUS_LAYER_TOKEN__
from theseus.constants import __FROM_THESEUS_LAYER_TOKEN__, DeviceType
from theseus.geometry import LieGroup, Manifold
from theseus.optimizer import Optimizer, OptimizerInfo
from theseus.optimizer.linear import LinearSolver
Expand Down Expand Up @@ -139,7 +139,7 @@ def to(self, *args, **kwargs):
self.objective.to(*args, **kwargs)

@property
def device(self) -> torch.device:
def device(self) -> DeviceType:
return self.objective.device

@property
Expand Down
2 changes: 1 addition & 1 deletion theseus/utils/examples/motion_planning/motion_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __init__(
max_optim_iters: int,
step_size: float = 1.0,
objective: Optional[MotionPlannerObjective] = None,
device: str = "cpu",
device: th.DeviceType = "cpu",
dtype: torch.dtype = torch.double,
# The following are only used if objective is None
map_size: Optional[int] = None,
Expand Down
2 changes: 1 addition & 1 deletion theseus/utils/examples/pose_graph/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __init__(
edges: List[PoseGraphEdge],
gt_poses: Optional[Union[List[th.SE2], List[th.SE3]]] = None,
batch_size: int = 1,
device: Optional[torch.device] = None,
device: th.DeviceType = None,
):
dataset_sizes: List[int] = [pose.shape[0] for pose in poses]
if gt_poses is not None:
Expand Down
8 changes: 5 additions & 3 deletions theseus/utils/examples/tactile_pose_estimation/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import torch

import theseus as th

# ----------------------------------------------------------------------------------- #
# ------------------------------------ Data loading --------------------------------- #
# ----------------------------------------------------------------------------------- #
Expand All @@ -19,7 +21,7 @@ def __init__(
batch_size: int,
max_episodes: int,
max_steps: int,
device: torch.device,
device: th.DeviceType,
split_episodes: bool = False,
data_mode: str = "all",
val_ratio: float = 0.1,
Expand Down Expand Up @@ -71,7 +73,7 @@ def _load_dataset_from_file(
filename: str,
episode_length: int,
max_episodes: int,
device: torch.device,
device: th.DeviceType,
split_episodes: bool = False,
) -> Dict[str, torch.Tensor]:
# Load all episode data
Expand Down Expand Up @@ -148,7 +150,7 @@ def _get_tensor_splits(

@staticmethod
def _load_tactile_sdf_from_file(
filename: str, device: torch.device
filename: str, device: th.DeviceType
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:

with open(filename) as f:
Expand Down
Loading

0 comments on commit 1b579ee

Please sign in to comment.