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

[Logger Refactor] Add timer in logger manager #1967

Merged
merged 4 commits into from
Jan 19, 2024
Merged
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
20 changes: 20 additions & 0 deletions src/sparseml/core/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import time
import warnings
from abc import ABC
from contextlib import contextmanager
from datetime import datetime
from logging import CRITICAL, DEBUG, ERROR, INFO, WARN, Logger
from pathlib import Path
Expand Down Expand Up @@ -1123,6 +1124,25 @@ def log_string(
level=level,
)

@contextmanager
def time(self, tag: Optional[str] = None, *args, **kwargs):
"""
Context manager to log the time it takes to run the block of code

Usage:
>>> with LoggerManager().system.time("my_block"):
>>> time.sleep(1)

:param tag: identifying tag to log the values with
"""

start = time.time()
yield
elapsed = time.time() - start
if not tag:
tag = f"{DEFAULT_TAG}_time"
self.log_string(tag=tag, string=f"{elapsed:.3f}s", *args, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be logging a float not a string, will allow for easier plotting and such


def debug(self, tag, string, *args, **kwargs):
"""
logs a string message with level DEBUG on all
Expand Down