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

Activate mypy in ignite.distributed #1355

Merged
merged 7 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 4 additions & 31 deletions ignite/distributed/comp_models/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from abc import ABCMeta, abstractmethod
from numbers import Number
from typing import Any, Callable, List, Optional, Union, cast, overload
from typing import Any, Callable, List, Optional, Union, cast

import torch

Expand Down Expand Up @@ -134,20 +134,9 @@ def _apply_op(
return tensor.to(device=tensor_device)
return tensor

@overload
def _collective_op(
self, tensor: Union[torch.Tensor, Number], fn: Callable, *args: Any, **kwargs: Any
) -> Union[torch.Tensor, Number]:
...

@overload
def _collective_op(
self, tensor: Union[torch.Tensor, Number, str], fn: Callable, *args: Any, **kwargs: Any
) -> Union[torch.Tensor, Number, List[str]]:
...

# mypy doesn't support overload for no-untyped-def check
def _collective_op(self, tensor, fn, *args, **kwargs): # type: ignore
tensor_to_number = tensor_to_str = False
device = self.device()
if isinstance(tensor, Number):
Expand All @@ -160,7 +149,7 @@ def _collective_op(self, tensor, fn, *args, **kwargs): # type: ignore
tensor = self._apply_op(tensor, device, fn, *args, **kwargs)

if tensor_to_number and tensor.numel() == 1:
return tensor.item()
return cast(Number, tensor.item())
elif tensor_to_str:
return self._decode_str(tensor)
return tensor
Expand All @@ -169,23 +158,15 @@ def all_reduce(self, tensor: Union[torch.Tensor, Number], op: str = "sum") -> Un
if not isinstance(tensor, (torch.Tensor, Number)):
raise TypeError("Unhandled input type {}".format(type(tensor)))

return self._collective_op(tensor, self._do_all_reduce, op)
return cast(Union[torch.Tensor, Number], self._collective_op(tensor, self._do_all_reduce, op))

def all_gather(self, tensor: Union[torch.Tensor, Number, str]) -> Union[torch.Tensor, Number, List[str]]:
if not isinstance(tensor, (torch.Tensor, Number, str)):
raise TypeError("Unhandled input type {}".format(type(tensor)))

return self._collective_op(tensor, self._do_all_gather)

@overload
def broadcast(self, tensor: Union[torch.Tensor, Number], src: int = 0) -> Union[torch.Tensor, Number]:
...

@overload
def broadcast(self, tensor: Union[torch.Tensor, Number, str], src: int = 0) -> Union[torch.Tensor, Number, str]:
...

def broadcast(self, tensor, src=0): # type: ignore # mypy doesn't support overload for no-untyped-def check
if not isinstance(tensor, (torch.Tensor, Number, str)):
raise TypeError("Unhandled input type {}".format(type(tensor)))

Expand All @@ -210,7 +191,7 @@ def broadcast(self, tensor, src=0): # type: ignore # mypy doesn't support overl
tensor = self._apply_op(tensor, device, self._do_broadcast, src)

if tensor_to_number:
return tensor.item()
return cast(Number, tensor.item())
if tensor_to_str:
list_str = self._decode_str(tensor)
return list_str[0]
Expand Down Expand Up @@ -293,15 +274,7 @@ def all_reduce(self, tensor: Union[torch.Tensor, Number], op: str = "sum") -> Un
def all_gather(self, tensor: Union[torch.Tensor, Number, str]) -> Union[torch.Tensor, Number, List[str]]:
gruebel marked this conversation as resolved.
Show resolved Hide resolved
return cast(Union[torch.Tensor, Number], tensor)

@overload
def broadcast(self, tensor: Union[torch.Tensor, Number], src: int = 0) -> Union[torch.Tensor, Number]:
...

@overload
def broadcast(self, tensor: Union[torch.Tensor, Number, str], src: int = 0) -> Union[torch.Tensor, Number, str]:
...

def broadcast(self, tensor, src=0): # type: ignore # mypy doesn't support overload for no-untyped-def check
return tensor

def _do_all_reduce(self, tensor: torch.Tensor, op: str = "sum") -> torch.Tensor:
Expand Down
4 changes: 2 additions & 2 deletions ignite/distributed/comp_models/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
import warnings
from distutils.version import LooseVersion
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, overload
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -245,7 +245,7 @@ def _dist_worker_task_fn(
node_rank: int,
master_addr: str,
master_port: str,
**kw: Any,
kw: Any,
vfdev-5 marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
from ignite.distributed.utils import _set_model, finalize

Expand Down