Skip to content

Commit

Permalink
[Logger Refactor] Add timer in logger manager (#1967)
Browse files Browse the repository at this point in the history
* Add timer in logger manager

* Address review comments

* update docstring

* fix argument
  • Loading branch information
rahul-tuli authored Jan 19, 2024
1 parent 4fe232d commit fc92b2f
Showing 1 changed file with 20 additions and 0 deletions.
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 @@ -1074,6 +1075,25 @@ def save(
if log.enabled:
log.save(file_path, **kwargs)

@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().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_secs"
self.log_scalar(tag=tag, value=float(f"{elapsed:.3f}"), *args, **kwargs)


class LoggingWrapperBase:
"""
Expand Down

0 comments on commit fc92b2f

Please sign in to comment.