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

Added option to clear cuda cache when vectorization cache is cleared. #249

Merged
merged 2 commits into from
Jul 19, 2022
Merged
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
1 change: 1 addition & 0 deletions examples/configs/pose_graph/pose_graph_synthetic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ inner_optim:
ratio_known_poses: 0.1
reg_w: 1e-3
vectorize: true
empty_cuda_cache: false
mhmukadam marked this conversation as resolved.
Show resolved Hide resolved

outer_optim:
lr: 0.1
Expand Down
3 changes: 2 additions & 1 deletion examples/pose_graph/pose_graph_synthetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ def run(
linearization_cls=LINEARIZATION_MODE[cast(str, cfg.inner_optim.solver)],
linear_solver_cls=LINEAR_SOLVER_MODE[cast(str, cfg.inner_optim.solver)],
vectorize=cfg.inner_optim.vectorize,
empty_cuda_cache=cfg.inner_optim.empty_cuda_cache,
)

# Set up Theseus layer
theseus_optim = th.TheseusLayer(optimizer, vectorize=cfg.inner_optim.vectorize)
theseus_optim = th.TheseusLayer(optimizer)
mhmukadam marked this conversation as resolved.
Show resolved Hide resolved
theseus_optim.to(device=device)

# Outer optimization loop
Expand Down
5 changes: 4 additions & 1 deletion theseus/core/vectorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __repr__(self) -> str:
class Vectorize:
_SHARED_TOKEN = "__shared__"

def __init__(self, objective: Objective):
def __init__(self, objective: Objective, empty_cuda_cache: bool = False):
# Each cost function is assigned a wrapper. The wrapper will hold the error
# and jacobian after vectorization.
self._cost_fn_wrappers: List[_CostFunctionWrapper] = []
Expand Down Expand Up @@ -127,6 +127,7 @@ def __init__(self, objective: Objective):
)

self._objective = objective
self._empty_cuda_cache = empty_cuda_cache

# Returns a dictionary which maps every schema to information about its shared
# variables.
Expand Down Expand Up @@ -288,6 +289,8 @@ def _clear_wrapper_caches(self):
for cf in cost_fn_wrappers:
cf._cached_error = None
cf._cached_jacobians = None
if self._empty_cuda_cache:
torch.cuda.empty_cache()
mhmukadam marked this conversation as resolved.
Show resolved Hide resolved

# This could be a static method, but writing like this makes some unit tests
# easier
Expand Down
2 changes: 1 addition & 1 deletion theseus/optimizer/linear/linear_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
linear_solver_kwargs: Optional[Dict[str, Any]] = None,
**kwargs,
):
super().__init__(objective, vectorize=vectorize)
super().__init__(objective, vectorize=vectorize, **kwargs)
linearization_kwargs = linearization_kwargs or {}
linear_solver_kwargs = linear_solver_kwargs or {}
self.linear_solver = linear_solver_cls(
Expand Down
2 changes: 2 additions & 0 deletions theseus/optimizer/nonlinear/gauss_newton.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
rel_err_tolerance: float = 1e-8,
max_iterations: int = 20,
step_size: float = 1.0,
**kwargs
):
super().__init__(
objective,
Expand All @@ -39,6 +40,7 @@ def __init__(
rel_err_tolerance=rel_err_tolerance,
max_iterations=max_iterations,
step_size=step_size,
**kwargs
)

def compute_delta(self, **kwargs) -> torch.Tensor:
Expand Down
2 changes: 2 additions & 0 deletions theseus/optimizer/nonlinear/levenberg_marquardt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
rel_err_tolerance: float = 1e-8,
max_iterations: int = 20,
step_size: float = 1.0,
**kwargs,
):
super().__init__(
objective,
Expand All @@ -51,6 +52,7 @@ def __init__(
rel_err_tolerance=rel_err_tolerance,
max_iterations=max_iterations,
step_size=step_size,
**kwargs,
)
self._allows_ellipsoidal = _check_ellipsoidal_damping_cls(self.linear_solver)

Expand Down
1 change: 1 addition & 0 deletions theseus/optimizer/nonlinear/nonlinear_least_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
rel_err_tolerance=rel_err_tolerance,
max_iterations=max_iterations,
step_size=step_size,
**kwargs,
)

@abc.abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion theseus/optimizer/nonlinear/nonlinear_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
step_size: float = 1.0,
**kwargs,
):
super().__init__(objective, vectorize=vectorize)
super().__init__(objective, vectorize=vectorize, **kwargs)
linear_solver_kwargs = linear_solver_kwargs or {}
self.linear_solver = linear_solver_cls(
objective,
Expand Down
4 changes: 3 additions & 1 deletion theseus/optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Optimizer(abc.ABC):
def __init__(self, objective: Objective, *args, vectorize: bool = True, **kwargs):
self.objective = objective
if vectorize:
Vectorize(self.objective)
Vectorize(
self.objective, empty_cuda_cache=kwargs.get("empty_cuda_cache", False)
)
self._objectives_version = objective.current_version

@abc.abstractmethod
Expand Down
9 changes: 7 additions & 2 deletions theseus/theseus_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@


class TheseusLayer(nn.Module):
def __init__(self, optimizer: Optimizer, vectorize: bool = True):
def __init__(
self,
optimizer: Optimizer,
vectorize: bool = True,
empty_cuda_cache: bool = False,
mhmukadam marked this conversation as resolved.
Show resolved Hide resolved
):
super().__init__()
self.objective = optimizer.objective
if vectorize and not self.objective.vectorized:
Vectorize(self.objective)
Vectorize(self.objective, empty_cuda_cache=empty_cuda_cache)
self.optimizer = optimizer
self._objectives_version = optimizer.objective.current_version
self._dlm_bwd_objective = None
Expand Down