Skip to content

Commit

Permalink
Centralize rank_zero_only utilities into their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthsub committed Feb 4, 2022
1 parent 8c07d8b commit bfd617e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 83 deletions.
53 changes: 1 addition & 52 deletions pytorch_lightning/utilities/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import logging
import os
from functools import wraps
from platform import python_version
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import torch
Expand All @@ -25,6 +23,7 @@

import pytorch_lightning as pl
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_8, _TORCH_GREATER_EQUAL_1_9, _TPU_AVAILABLE
from pytorch_lightning.utilities.rank_zero import rank_zero_debug, rank_zero_info, rank_zero_only # noqa: F401

if _TPU_AVAILABLE:
import torch_xla.core.xla_model as xm
Expand All @@ -44,56 +43,6 @@ class group: # type: ignore
log = logging.getLogger(__name__)


def rank_zero_only(fn: Callable) -> Callable:
"""Function that can be used as a decorator to enable a function/method being called only on rank 0."""

@wraps(fn)
def wrapped_fn(*args: Any, **kwargs: Any) -> Optional[Any]:
if rank_zero_only.rank == 0:
return fn(*args, **kwargs)
return None

return wrapped_fn


# TODO: this should be part of the cluster environment
def _get_rank() -> int:
rank_keys = ("RANK", "SLURM_PROCID", "LOCAL_RANK")
for key in rank_keys:
rank = os.environ.get(key)
if rank is not None:
return int(rank)
return 0


# add the attribute to the function but don't overwrite in case Trainer has already set it
rank_zero_only.rank = getattr(rank_zero_only, "rank", _get_rank())


def _info(*args: Any, stacklevel: int = 2, **kwargs: Any) -> None:
if python_version() >= "3.8.0":
kwargs["stacklevel"] = stacklevel
log.info(*args, **kwargs)


def _debug(*args: Any, stacklevel: int = 2, **kwargs: Any) -> None:
if python_version() >= "3.8.0":
kwargs["stacklevel"] = stacklevel
log.debug(*args, **kwargs)


@rank_zero_only
def rank_zero_debug(*args: Any, stacklevel: int = 4, **kwargs: Any) -> None:
"""Function used to log debug-level messages only on rank 0."""
_debug(*args, stacklevel=stacklevel, **kwargs)


@rank_zero_only
def rank_zero_info(*args: Any, stacklevel: int = 4, **kwargs: Any) -> None:
"""Function used to log info-level messages only on rank 0."""
_info(*args, stacklevel=stacklevel, **kwargs)


def gather_all_tensors(result: torch.Tensor, group: Optional[Any] = None) -> List[torch.Tensor]:
"""Function to gather all tensors from several ddp processes onto a list that is broadcasted to all processes.
Expand Down
101 changes: 101 additions & 0 deletions pytorch_lightning/utilities/rank_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities that can be used for calling functions on a particular rank."""
import logging
import os
import warnings
from functools import partial, wraps
from platform import python_version
from typing import Any, Callable, Optional, Union

log = logging.getLogger(__name__)


def rank_zero_only(fn: Callable) -> Callable:
"""Function that can be used as a decorator to enable a function/method being called only on rank 0."""

@wraps(fn)
def wrapped_fn(*args: Any, **kwargs: Any) -> Optional[Any]:
if rank_zero_only.rank == 0:
return fn(*args, **kwargs)
return None

return wrapped_fn


# TODO: this should be part of the cluster environment
def _get_rank() -> int:
rank_keys = ("RANK", "SLURM_PROCID", "LOCAL_RANK")
for key in rank_keys:
rank = os.environ.get(key)
if rank is not None:
return int(rank)
return 0


# add the attribute to the function but don't overwrite in case Trainer has already set it
rank_zero_only.rank = getattr(rank_zero_only, "rank", _get_rank())


def _info(*args: Any, stacklevel: int = 2, **kwargs: Any) -> None:
if python_version() >= "3.8.0":
kwargs["stacklevel"] = stacklevel
log.info(*args, **kwargs)


def _debug(*args: Any, stacklevel: int = 2, **kwargs: Any) -> None:
if python_version() >= "3.8.0":
kwargs["stacklevel"] = stacklevel
log.debug(*args, **kwargs)


@rank_zero_only
def rank_zero_debug(*args: Any, stacklevel: int = 4, **kwargs: Any) -> None:
"""Function used to log debug-level messages only on rank 0."""
_debug(*args, stacklevel=stacklevel, **kwargs)


@rank_zero_only
def rank_zero_info(*args: Any, stacklevel: int = 4, **kwargs: Any) -> None:
"""Function used to log info-level messages only on rank 0."""
_info(*args, stacklevel=stacklevel, **kwargs)


def _warn(message: Union[str, Warning], stacklevel: int = 2, **kwargs: Any) -> None:
if type(stacklevel) is type and issubclass(stacklevel, Warning):
rank_zero_deprecation(
"Support for passing the warning category positionally is deprecated in v1.6 and will be removed in v1.8"
f" Please, use `category={stacklevel.__name__}`."
)
kwargs["category"] = stacklevel
stacklevel = kwargs.pop("stacklevel", 2)
warnings.warn(message, stacklevel=stacklevel, **kwargs)


@rank_zero_only
def rank_zero_warn(message: Union[str, Warning], stacklevel: int = 4, **kwargs: Any) -> None:
"""Function used to log warn-level messages only on rank 0."""
_warn(message, stacklevel=stacklevel, **kwargs)


class PossibleUserWarning(UserWarning):
"""Warnings that could be false positives."""


class LightningDeprecationWarning(DeprecationWarning):
"""Deprecation warnings raised by PyTorch Lightning."""


rank_zero_deprecation = partial(rank_zero_warn, category=LightningDeprecationWarning)
38 changes: 7 additions & 31 deletions pytorch_lightning/utilities/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,18 @@
"""Warning-related utilities."""

import warnings
from functools import partial
from typing import Any, Union

from pytorch_lightning.utilities.distributed import rank_zero_only


def _warn(message: Union[str, Warning], stacklevel: int = 2, **kwargs: Any) -> None:
if type(stacklevel) is type and issubclass(stacklevel, Warning):
rank_zero_deprecation(
"Support for passing the warning category positionally is deprecated in v1.6 and will be removed in v1.8"
f" Please, use `category={stacklevel.__name__}`."
)
kwargs["category"] = stacklevel
stacklevel = kwargs.pop("stacklevel", 2)
warnings.warn(message, stacklevel=stacklevel, **kwargs)


@rank_zero_only
def rank_zero_warn(message: Union[str, Warning], stacklevel: int = 4, **kwargs: Any) -> None:
"""Function used to log warn-level messages only on rank 0."""
_warn(message, stacklevel=stacklevel, **kwargs)


class PossibleUserWarning(UserWarning):
"""Warnings that could be false positives."""


class LightningDeprecationWarning(DeprecationWarning):
"""Deprecation warnings raised by PyTorch Lightning."""
from typing import Any

from pytorch_lightning.utilities.rank_zero import ( # noqa: F401
LightningDeprecationWarning,
rank_zero_deprecation,
rank_zero_only,
rank_zero_warn,
)

# enable our warnings
warnings.simplefilter("default", category=LightningDeprecationWarning)

rank_zero_deprecation = partial(rank_zero_warn, category=LightningDeprecationWarning)


class WarningCache(set):
def warn(self, message: str, stacklevel: int = 5, **kwargs: Any) -> None:
Expand Down

0 comments on commit bfd617e

Please sign in to comment.