Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Facebookresearch Habitat lab Timing

From Leeroopedia
Revision as of 12:36, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Facebookresearch_Habitat_lab_Timing.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Embodied_AI, Performance_Profiling
Last Updated 2026-02-15 00:00 GMT

Overview

The Timing module provides a dictionary-based performance profiling system with context managers for measuring code execution time, supporting overwrite, additive, and windowed-average timing modes with configurable logging levels.

Description

The module defines three classes and two module-level constants:

TimingContext is a context manager and decorator that measures the wall-clock time of a code block using time.perf_counter. It supports three modes: overwrite (default, where each measurement replaces the previous), additive (where measurements accumulate), and windowed average (where measurements are tracked via a WindowedRunningMean). A minimum time of 1e-5 seconds prevents division-by-zero issues.

EmptyContext extends nullcontext and also functions as a pass-through decorator, used when a timing measurement is filtered out by the logging level.

Timing extends dict and serves as the timing registry. It provides three methods: timeit (overwrite mode), add_time (additive mode), and avg_time (windowed average with configurable level filtering). The avg_time method accepts a level parameter; if the level exceeds the timing_level_threshold, it returns an EmptyContext instead of measuring. The __str__ method produces a comma-separated summary of all timing entries.

A global g_timer instance is created using the HABITAT_TIMING_LEVEL environment variable, and is used throughout the PPO trainer for performance reporting.

Usage

Use Timing and its context managers to profile code sections during training. The global g_timer is available for use throughout the baselines codebase. Set the HABITAT_TIMING_LEVEL environment variable to control verbosity.

Code Reference

Source Location

Signature

class TimingContext:
    def __init__(self, timer, key, additive=False, average=None):

class EmptyContext(nullcontext):
    def __call__(self, f):

class Timing(dict):
    def __init__(self, timing_level_threshold: int = 0):
    def timeit(self, key):
    def add_time(self, key):
    def avg_time(self, key, average=float("inf"), level=0):

Import

from habitat_baselines.utils.timing import Timing, g_timer

I/O Contract

Inputs (Timing.__init__)

Name Type Required Description
timing_level_threshold int No Minimum allowed timing log level; higher values filter out more measurements (default: 0)

Inputs (avg_time)

Name Type Required Description
key str Yes Name of the timing measurement
average float No Window size for the running mean (default: float("inf") for cumulative)
level int No Logging level; filtered if greater than timing_level_threshold (default: 0)

Outputs

Name Type Description
(context manager) TimingContext or EmptyContext Context manager that measures and records execution time, or a no-op if filtered

Usage Examples

Basic Usage

from habitat_baselines.utils.timing import Timing

timer = Timing()

# Overwrite timing: records the last measurement
with timer.timeit("forward_pass"):
    output = model(input_batch)

# Additive timing: accumulates across calls
for batch in dataloader:
    with timer.add_time("total_train_time"):
        train_step(batch)

# Windowed average timing
with timer.avg_time("rollout_time", average=100):
    collect_rollouts()

print(timer)  # "forward_pass: 0.0123, total_train_time: 45.6789, rollout_time: 0.5432"

Using as a Decorator

from habitat_baselines.utils.timing import Timing

timer = Timing()

@timer.timeit("my_function")
def my_function():
    # Code to time
    pass

my_function()
print(timer["my_function"])  # Elapsed time in seconds

Using the Global Timer

from habitat_baselines.utils.timing import g_timer

# In training code
with g_timer.avg_time("ppo.update_time"):
    losses = updater.update(rollouts)

# Higher-level timing (always logged)
with g_timer.avg_time("env.step_time", level=0):
    observations = envs.step(actions)

# Detailed timing (only logged if HABITAT_TIMING_LEVEL >= 2)
with g_timer.avg_time("env.obs_transform", level=2):
    observations = transform(observations)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment