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] Iterables are consumed and are not linked lists.. #365

Merged
merged 1 commit into from
Feb 5, 2021
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
22 changes: 14 additions & 8 deletions fairscale/optim/oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from itertools import chain
import logging
from math import inf
from typing import TYPE_CHECKING, Any, Callable, Deque, Dict, Iterable, List, Optional, Type, Union
from typing import TYPE_CHECKING, Any, Callable, Deque, Dict, List, Optional, Type, Union

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -81,7 +81,7 @@ def __init__(
self._partition_parameters: List[List[dict]] = []
self._index_to_param: Dict[int, torch.Tensor] = {}
self._param_to_index: Dict[int, int] = {}
self._local_params: Optional[Iterable[Any]] = None
self._local_params: Optional[List[torch.Tensor]] = None

# Build the wrapped optimizer, responsible for a shard of the params
self.group = group if group is not None else dist.group.WORLD
Expand Down Expand Up @@ -145,14 +145,20 @@ def partition_parameters(self) -> List[List[dict]]:
return self._partition_parameters

@property
def local_params(self) -> Iterable[torch.Tensor]:
def local_params(self) -> List[torch.Tensor]:
""" Iterable which goes through the parameters that this rank owns
"""
if self._local_params is None:
self._local_params = chain(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the catch in #363 was that I had the wrong mental model for this in Python, I thought that self._local_params was just the top of a linked list, which was never reassigned -> always pointing to the right place. That's actually completely wrong, all the elements in the chain are consumed so this only worked for the first step through

*[
list(filter(lambda x: x.grad is not None, device_params[self.rank]))
for device_params in self.per_device_params.values()
]
self._local_params = list(
chain(
*[
list(filter(lambda x: x.grad is not None, device_params[self.rank]))
for device_params in self.per_device_params.values()
]
)
)

# Make sure that the iterator is not consumed, only expose a copy
return self._local_params

@property
Expand Down
3 changes: 3 additions & 0 deletions tests/optim/test_oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ def check(norm):
print(f"Checking norm {norm}")
check(norm)

# Check twice, catch an hypothetic iterator dumb mistake
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure that I don't do this mistake again

check(norm)

dist.destroy_process_group()


Expand Down