-
Notifications
You must be signed in to change notification settings - Fork 16
Metric Logging #59
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
Merged
Merged
Metric Logging #59
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bb0386a
wandb metric logging
calvinpelletier 1c7483d
tensorboard, stdout, disk loggers
calvinpelletier de31799
move base class to interfaces file
calvinpelletier 120a510
fix config
calvinpelletier a964fcb
fix
calvinpelletier f116007
fix
calvinpelletier 096bd20
fmt
calvinpelletier e6ed572
addressing comments
calvinpelletier b1dd753
reverting sft_v2 changes
calvinpelletier 903204a
fixing docstrings
calvinpelletier b38c936
merge
calvinpelletier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
from .logging import deprecated, get_logger, log_once, log_rank_zero | ||
from .metric_logging import get_metric_logger | ||
|
||
__all__ = [ | ||
"deprecated", | ||
"get_logger", | ||
"log_once", | ||
"log_rank_zero", | ||
"get_metric_logger", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,147 @@ | ||||||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||||||
# All rights reserved. | ||||||
# | ||||||
# This source code is licensed under the BSD-style license found in the | ||||||
# LICENSE file in the root directory of this source tree. | ||||||
|
||||||
import inspect | ||||||
import logging | ||||||
import warnings | ||||||
from functools import lru_cache, wraps | ||||||
from typing import Callable, Optional, TypeVar | ||||||
|
||||||
from torch import distributed as dist | ||||||
|
||||||
T = TypeVar("T", bound=type) | ||||||
|
||||||
|
||||||
def get_logger(level: Optional[str] = None) -> logging.Logger: | ||||||
""" | ||||||
Get a logger with a stream handler. | ||||||
Args: | ||||||
level (Optional[str]): The logging level. See https://docs.python.org/3/library/logging.html#levels for list of levels. | ||||||
Example: | ||||||
>>> logger = get_logger("INFO") | ||||||
>>> logger.info("Hello world!") | ||||||
INFO:torchtune.utils._logging:Hello world! | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Returns: | ||||||
logging.Logger: The logger. | ||||||
""" | ||||||
logger = logging.getLogger(__name__) | ||||||
if not logger.hasHandlers(): | ||||||
logger.addHandler(logging.StreamHandler()) | ||||||
if level is not None: | ||||||
level = getattr(logging, level.upper()) | ||||||
logger.setLevel(level) | ||||||
return logger | ||||||
|
||||||
|
||||||
def log_rank_zero(logger: logging.Logger, msg: str, level: int = logging.INFO) -> None: | ||||||
""" | ||||||
Logs a message only on rank zero. | ||||||
Args: | ||||||
logger (logging.Logger): The logger. | ||||||
msg (str): The warning message. | ||||||
level (int): The logging level. See https://docs.python.org/3/library/logging.html#levels for values. | ||||||
Defaults to ``logging.INFO``. | ||||||
""" | ||||||
rank = dist.get_rank() if dist.is_available() and dist.is_initialized() else 0 | ||||||
if rank != 0: | ||||||
return | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't log rank be configurable like how titan did? But I guess we can always improve later. |
||||||
logger.log(level, msg, stacklevel=2) | ||||||
|
||||||
|
||||||
@lru_cache(None) | ||||||
def log_once(logger: logging.Logger, msg: str, level: int = logging.INFO) -> None: | ||||||
""" | ||||||
Logs a message only once. LRU cache is used to ensure a specific message is | ||||||
logged only once, similar to how :func:`~warnings.warn` works when the ``once`` | ||||||
rule is set via command-line or environment variable. | ||||||
Args: | ||||||
logger (logging.Logger): The logger. | ||||||
msg (str): The warning message. | ||||||
level (int): The logging level. See https://docs.python.org/3/library/logging.html#levels for values. | ||||||
Defaults to ``logging.INFO``. | ||||||
""" | ||||||
log_rank_zero(logger=logger, msg=msg, level=level) | ||||||
|
||||||
|
||||||
def deprecated(msg: str = "") -> Callable[[T], T]: | ||||||
calvinpelletier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
""" | ||||||
Decorator to mark an object as deprecated and print additional message. | ||||||
Args: | ||||||
msg (str): additional information to print after warning. | ||||||
Returns: | ||||||
Callable[[T], T]: the decorated object. | ||||||
""" | ||||||
|
||||||
@lru_cache(maxsize=1) | ||||||
def warn(obj): | ||||||
rank = dist.get_rank() if dist.is_available() and dist.is_initialized() else 0 | ||||||
if rank != 0: | ||||||
return | ||||||
warnings.warn( | ||||||
f"{obj.__name__} is deprecated and will be removed in future versions. " | ||||||
+ msg, | ||||||
category=FutureWarning, | ||||||
stacklevel=3, | ||||||
) | ||||||
|
||||||
def decorator(obj): | ||||||
@wraps(obj) | ||||||
def wrapper(*args, **kwargs): | ||||||
warn(obj) | ||||||
return obj(*args, **kwargs) | ||||||
|
||||||
return wrapper | ||||||
|
||||||
return decorator | ||||||
|
||||||
|
||||||
def deprecate_parameter(param_name: str, msg: str = "") -> Callable[[T], T]: | ||||||
""" | ||||||
Decorator to mark a parameter as deprecated and print additional message. | ||||||
Args: | ||||||
param_name (str): The name of the parameter. | ||||||
msg (str): additional information to print after warning. | ||||||
Returns: | ||||||
Callable[[T], T]: the decorated object. | ||||||
""" | ||||||
|
||||||
@lru_cache(maxsize=1) | ||||||
def warn(obj): | ||||||
rank = dist.get_rank() if dist.is_available() and dist.is_initialized() else 0 | ||||||
if rank != 0: | ||||||
return | ||||||
warnings.warn( | ||||||
f"{param_name} is deprecated for {obj.__name__} and will be removed in future versions. " | ||||||
+ msg, | ||||||
category=FutureWarning, | ||||||
stacklevel=3, | ||||||
) | ||||||
|
||||||
def decorator(obj): | ||||||
sig = inspect.signature(obj) | ||||||
|
||||||
@wraps(obj) | ||||||
def wrapper(*args, **kwargs): | ||||||
# Check positional and kwargs | ||||||
bound_args = sig.bind_partial(*args, **kwargs) | ||||||
all_args = {**bound_args.arguments} | ||||||
all_args.update(kwargs) | ||||||
if param_name in all_args: | ||||||
warn(obj) | ||||||
return obj(*args, **kwargs) | ||||||
|
||||||
return wrapper | ||||||
|
||||||
return decorator |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.