Skip to navigation

nemo_automodel.components.training.timers

View as MarkdownOpen in Claude

Megatron based timers.

Module Contents

Classes

NameDescription
DummyTimerDummy Timer.
TimerTimer class with ability to start/stop.
TimerBaseTimer base class.
TimersClass for a group of Timers.

Data

dist_all_gather_func

API

class nemo_automodel.components.training.timers.DummyTimer()

Bases: TimerBase

Dummy Timer.

nemo_automodel.components.training.timers.DummyTimer.active_time() -> float

Returns the cumulative duration the timer has been active.

Note: Not supported for DummyTimer.

nemo_automodel.components.training.timers.DummyTimer.elapsed(
reset: bool = True,
barrier: bool = False
) -> float

Dummy timer elapsed time.

nemo_automodel.components.training.timers.DummyTimer.reset() -> None

Dummy timer reset.

nemo_automodel.components.training.timers.DummyTimer.start(
barrier: bool = False
) -> None

Dummy timer start.

nemo_automodel.components.training.timers.DummyTimer.stop(
barrier: bool = False
) -> None

Dummy timer stop.

class nemo_automodel.components.training.timers.Timer(
name: str
)

Bases: TimerBase

Timer class with ability to start/stop.

Comment on using barrier: If this flag is passed, then all the caller processes will wait till all reach the timing routine. It is up to the user to make sure all the ranks in barrier_group call it otherwise, it will result in a hang. Comment on barrier_group: By default it is set to None which in torch distributed land, it will result in the global communicator.

_active_time
= 0.0
_elapsed
= 0.0
_start_time
= time.time()
nemo_automodel.components.training.timers.Timer.active_time() -> float

Calculates the cumulative duration for which the timer has been active.

nemo_automodel.components.training.timers.Timer.elapsed(
reset: bool = True,
barrier: bool = False
) -> float

Calculates the elapsed time and restarts timer.

Parameters:

reset
boolDefaults to True

Resets timer before restarting. Defaults to True.

barrier
boolDefaults to False

Synchronizes ranks before stopping. Defaults to False.

Returns: float

Elapsed time.

nemo_automodel.components.training.timers.Timer.reset() -> None

Reset timer.

nemo_automodel.components.training.timers.Timer.set_barrier_group(
barrier_group: typing.Any
) -> None

Sets barrier group.

Parameters:

barrier_group
ProcessGroup

Torch ProcessGroup for barrier.

nemo_automodel.components.training.timers.Timer.start(
barrier: bool = False
) -> None

Start the timer.

Parameters:

barrier
boolDefaults to False

Synchronizes ranks before starting. Defaults to False.

nemo_automodel.components.training.timers.Timer.stop(
barrier: bool = False
) -> None

Stop the timer.

Parameters:

barrier
boolDefaults to False

Synchronizes ranks before stopping. Defaults to False.

class nemo_automodel.components.training.timers.TimerBase(
name: str
)
Abstract

Timer base class.

Start the timer when entering a context using the configured barrier option.

nemo_automodel.components.training.timers.TimerBase.__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: typing.Any
) -> None

Stop the timer when exiting a context using the configured barrier option.

nemo_automodel.components.training.timers.TimerBase.active_time() -> float
abstract

Calculates the cumulative duration for which the timer has been active.

nemo_automodel.components.training.timers.TimerBase.elapsed(
reset: bool = True,
barrier: bool = False
) -> float
abstract

Calculates the elapsed time and restarts timer.

Parameters:

reset
boolDefaults to True

Resets timer before restarting. Defaults to True.

barrier
boolDefaults to False

Synchronizes ranks before stopping. Defaults to False.

Returns: float

Elapsed time.

nemo_automodel.components.training.timers.TimerBase.reset() -> None
abstract

Reset timer.

nemo_automodel.components.training.timers.TimerBase.start(
barrier: bool = False
) -> None
abstract

Start the timer.

Parameters:

barrier
boolDefaults to False

Synchronizes ranks before starting. Defaults to False.

nemo_automodel.components.training.timers.TimerBase.stop(
barrier: bool = False
) -> None
abstract

Stop the timer.

Parameters:

barrier
boolDefaults to False

Synchronizes ranks before stopping. Defaults to False.

Set the barrier option for use in context manager.

Parameters:

barrier
boolDefaults to True

Whether to use barrier in context manager. Defaults to True.

Returns: TimerBase

Returns self for chaining.

class nemo_automodel.components.training.timers.Timers(
log_level: int,
log_option: str
)

Class for a group of Timers.

_dummy_timer
= DummyTimer()
_log_levels
dict[str, int] = {}
_max_log_level
= 2
_timers
dict[str, Timer] = {}
nemo_automodel.components.training.timers.Timers.__call__(
name: str,
log_level: int | None = None,
barrier: bool = False

Call timer with name and log level.

Returns a timer object that can be used as a context manager.

Example

with timers(“my_timer”):

Code to time

…

With barrier

with timers(“my_timer”, barrier=True):

Code to time

…

Parameters:

name
str

Name of the timer.

log_level
intDefaults to None

Log level of the timer. Defaults to None.

barrier
boolDefaults to False

Whether to use barrier in context manager. Defaults to False.

nemo_automodel.components.training.timers.Timers._get_all_ranks_time_string(
names: collections.abc.Sequence[str],
reset: bool,
barrier: bool,
normalizer: float
) -> str | None

Report times across all ranks.

nemo_automodel.components.training.timers.Timers._get_elapsed_time_all_ranks(
names: collections.abc.Sequence[str],
reset: bool,
barrier: bool
) -> torch.Tensor

Returns elapsed times of timers in names.

Assumptions

  • All the ranks call this function.
  • names are identical on all ranks.

If the above assumptions are not met, calling this function will result in hang.

Parameters:

names
List[str]

list of timer names

reset
bool

reset the timer after recording the elapsed time

barrier
bool

if set, do a global barrier before time measurments

Returns: torch.Tensor

torch.tensor: Tensor of size [world_size, len(names)] with times in float.

nemo_automodel.components.training.timers.Timers._get_global_min_max_time(
names: collections.abc.Sequence[str],
reset: bool,
barrier: bool,
normalizer: float
) -> dict[str, tuple[float, float]]

Report only min and max times across all ranks.

nemo_automodel.components.training.timers.Timers._get_global_min_max_time_string(
names: collections.abc.Sequence[str],
reset: bool,
barrier: bool,
normalizer: float,
max_only: bool
) -> str | None

Report strings for max/minmax times across all ranks.

nemo_automodel.components.training.timers.Timers.get_all_timers_string(
names: collections.abc.Sequence[str] | None = None,
normalizer: float = 1.0,
reset: bool = True,
barrier: bool = False
) -> str | None

Returns the output string with logged timer values according to configured options.

Parameters:

names
List[str]Defaults to None

Names of the timers to log. If None, all registered timers are fetched. Defaults to None.

normalizer
floatDefaults to 1.0

Normalizes the timer values by the factor. Defaults to 1.0.

reset
boolDefaults to True

Whether to reset timer values after logging. Defaults to True.

barrier
boolDefaults to False

Whether to do a global barrier before time measurments. Defaults to False.

Returns: str | None

Formatted string with the timer values.

Raises:

  • Exception: Raises if log option is invalid.
nemo_automodel.components.training.timers.Timers.log(
names: collections.abc.Sequence[str],
rank: int | None = None,
normalizer: float = 1.0,
reset: bool = True,
barrier: bool = False
) -> None

Logs the timers passed in names to stdout.

Example usage is to log average per step value for timer ‘foo’, this function can be called with normalizer factor set to logging interval.

Parameters:

names
List[str]

Names of the timers to log.

rank
intDefaults to None

logs the timers to a specific rank. If set to None, logs to the last rank. Defaults to None.

normalizer
floatDefaults to 1.0

Normalizes the timer values by the factor. Defaults to 1.0.

reset
boolDefaults to True

Whether to reset timer values after logging. Defaults to True.

barrier
boolDefaults to False

Whether to do a global barrier before time measurments. Defaults to False.

nemo_automodel.components.training.timers.Timers.write(
names: collections.abc.Sequence[str],
writer: typing.Any,
iteration: int,
normalizer: float = 1.0,
reset: bool = True,
barrier: bool = False
) -> None

Write timers to a tensorboard writer.

Note that we only report maximum time across ranks to tensorboard.

Parameters:

names
List[str]

Names of the timers to log.

writer
SummaryWriter

Tensorboard SummaryWriter object

iteration
int

Current iteration.

normalizer
floatDefaults to 1.0

Normalizes the timer values by the factor. Defaults to 1.0.

reset
boolDefaults to True

Whether to reset timer values after logging. Defaults to True.

barrier
boolDefaults to False

Whether to do a global barrier before time measurments. Defaults to False.

nemo_automodel.components.training.timers.Timers.write_to_wandb(
names: collections.abc.Sequence[str],
writer: typing.Any,
iteration: int,
normalizer: float = 1.0,
reset: bool = True,
barrier: bool = False
) -> None

Patch to write timers to wandb for Megatron Core Timers.

nemo_automodel.components.training.timers.dist_all_gather_func = torch.distributed.all_gather_into_tensor