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

Small vectorization improvements #336

Merged
merged 3 commits into from
Oct 28, 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
9 changes: 6 additions & 3 deletions theseus/core/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,7 @@ def _get_batch_size(batch_sizes: Sequence[int]) -> int:
self._batch_size = _get_batch_size(batch_sizes)

def _vectorization_needs_update(self):
num_updates = dict(
(name, v._num_updates) for name, v in self._all_variables.items()
)
num_updates = {name: v._num_updates for name, v in self._all_variables.items()}
needs = False
if num_updates != self._num_updates_variables:
self._num_updates_variables = num_updates
Expand Down Expand Up @@ -575,6 +573,11 @@ def retract_optim_vars(
self._retract_method(
delta, ordering, ignore_mask=ignore_mask, force_update=force_update
)
# Updating immediately is useful, since it will keep grad history if
# needed. Otherwise, with lazy waitng we can be in a situation where
# vectorization is updated with torch.no_grad() (e.g., for error logging),
# and then it has to be run again later when grad is back on.
self.update_vectorization_if_needed()

def _enable_vectorization(
self,
Expand Down
6 changes: 5 additions & 1 deletion theseus/core/vectorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ def _update_all_cost_fns_var_tensors(
# when updating the vectorized variable containers.
tensor = (
var.tensor
if (var_batch_size > 1 or Vectorize._SHARED_TOKEN in name)
if (
var_batch_size > 1
or objective_batch_size == 1
or Vectorize._SHARED_TOKEN in name
)
else Vectorize._expand(var.tensor, objective_batch_size)
)
names_to_tensors[name].append(tensor)
Expand Down