easydel.utils.helpers#

Helper utilities for EasyDeL framework.

Provides logging, timing, caching, and general utility functions used throughout the EasyDeL framework.

Classes:

ColorFormatter: Colored console logging formatter LazyLogger: Deferred initialization logger Timer: Simple timing utility Timers: Multiple timer management with logging DummyStream: Null output stream for suppression

Functions:

get_logger: Create a lazy logger instance set_loggers_level: Set logging level globally capture_time: Context manager for timing get_cache_dir: Get EasyDeL cache directory quiet: Context manager to suppress output check_bool_flag: Parse boolean environment variables

Constants:

COLORS: Terminal color codes LEVEL_COLORS: Log level to color mapping _LOGGING_LEVELS: String to log level mapping

Example

>>> from easydel.utils.helpers import Timer
>>>
>>> with Timer("computation") as timer:
...     result = expensive_computation()
>>> print(f"Took {timer.elapsed_time()} seconds")
class easydel.utils.helpers.DummyStream[source]#

Bases: object

A null device-like stream that discards all writes.

Used for suppressing output by replacing stdout/stderr. All write and flush operations are no-ops.

flush(*args, **kwargs)[source]#

Discard all flush operations.

write(*args, **kwargs)[source]#

Discard all write operations.

class easydel.utils.helpers.Timer(name)[source]#

Bases: object

Simple timer for measuring execution time.

Can be used as a context manager or manually with start/stop methods. Accumulates time across multiple start/stop cycles.

name#

Timer name for identification.

elapsed#

Total elapsed time in seconds.

started#

Whether timer is currently running.

start_time#

Start time of current cycle.

Example

>>> timer = Timer("training")
>>> timer.start()
>>> # Do work
>>> timer.stop()
>>> print(f"Elapsed: {timer.elapsed_time()} seconds")
>>>
>>> # Or as context manager
>>> with Timer("inference") as t:
...     result = model(input)
elapsed_time(reset=True)[source]#

Get total elapsed time.

Parameters

reset – Whether to reset timer after reading.

Returns

Total elapsed time in seconds.

reset()[source]#
start()[source]#

Start the timer.

Raises

RuntimeError – If timer is already running.

stop()[source]#

Stop the timer and accumulate elapsed time.

Raises

RuntimeError – If timer is not running.

class easydel.utils.helpers.Timers(use_wandb, tensorboard_writer: SummaryWriter)[source]#

Bases: object

Manager for multiple named timers with logging support.

Manages a collection of timers and integrates with logging backends like Weights & Biases and TensorBoard for metrics tracking.

timers#

Dictionary of timer instances.

use_wandb#

Whether to log to Weights & Biases.

tensorboard_writer#

TensorBoard summary writer.

Example

>>> timers = Timers(use_wandb=True, tensorboard_writer=writer)
>>> with timers.timed("forward_pass"):
...     output = model(input)
>>> timers.write(["forward_pass"], iteration=100)
log(names, normalizer=1.0, reset=True)[source]#
timed(name, log=True, reset=True)[source]#
write(names, iteration, normalizer=1.0, reset=False)[source]#
easydel.utils.helpers.capture_time()[source]#

Context manager that measures elapsed time.

Yields a callable that returns the current elapsed time in seconds. The timer continues running until the context exits.

Yields

Callable that returns elapsed time in seconds.

Example

>>> with capture_time() as get_time:
...     # Do some work
...     print(f"After 1 second: {get_time()}")
...     # Do more work
...     print(f"After 2 seconds: {get_time()}")
>>> print(f"Total time: {get_time()}")
easydel.utils.helpers.check_bool_flag(name: str, default: bool = True) bool[source]#

Parse boolean environment variable.

Interprets various string representations as boolean values. Accepts: ‘true’, ‘yes’, ‘ok’, ‘1’, ‘easy’ (case-insensitive).

Parameters
  • name – Environment variable name.

  • default – Default value if variable not set.

Returns

Boolean interpretation of the environment variable.

Example

>>> os.environ['DEBUG'] = 'yes'
>>> check_bool_flag('DEBUG')
True
>>> check_bool_flag('MISSING', default=False)
False
easydel.utils.helpers.get_cache_dir() Path[source]#

Get the EasyDeL cache directory.

Returns the platform-specific cache directory for EasyDeL. Creates the directory if it doesn’t exist.

Returns

Path to the cache directory.

Example

>>> cache_dir = get_cache_dir()
>>> print(cache_dir)
/home/user/.cache/easydel
easydel.utils.helpers.quiet(suppress_stdout=True, suppress_stderr=True)[source]#

Context manager to temporarily suppress stdout and/or stderr output.

Replaces stdout/stderr with null streams to discard all output. Restores original streams on exit.

Parameters
  • suppress_stdout – Whether to suppress stdout.

  • suppress_stderr – Whether to suppress stderr.

Yields

None

Example

>>> with quiet():
...     print("This won't be displayed")
...     noisy_function()
>>> print("This will be displayed")

Note

This will suppress ALL output to the specified streams within the context, including output from C extensions and system calls.