Logging¶
masa.common.metrics.BaseLogger ¶
BaseLogger(stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: Optional[SummaryWriter] = None, wandb: bool = False, stats_window_size: int = 100, stats_window_overrides: Dict[str, int] = {}, prefix: str = '')
Base class for logging scalar statistics and distributions.
A logger ingests objects via add and produces aggregated outputs via
log. Concrete subclasses define how they ingest and summarise data.
TensorBoard logging uses a tf.summary.SummaryWriter. If
tensorboard is True then a writer must be provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stdout
|
bool
|
If |
True
|
tqdm
|
bool
|
If |
True
|
tensorboard
|
bool
|
If |
False
|
summary_writer
|
Optional[SummaryWriter]
|
TensorBoard writer. Required when |
None
|
stats_window_size
|
int
|
Maximum number of recent scalar values retained per metric. |
100
|
stats_window_overrides
|
Dict[str, int]
|
A dictionary for overriding logged values with
a different |
{}
|
prefix
|
str
|
Optional string prefix for TensorBoard tag names and stdout
display. If non-empty, a trailing |
''
|
Attributes:
| Name | Type | Description |
|---|---|---|
stdout |
bool
|
Whether stdout logging is enabled. |
tqdm |
bool
|
Whether tqdm-compatible printing is enabled. |
tensorboard |
bool
|
Whether TensorBoard logging is enabled. |
summary_writer |
Optional[SummaryWriter]
|
TensorBoard summary writer (may be |
stats_window_size |
int
|
Window size for scalar smoothing. |
prefix |
str
|
Namespace prefix ending with |
stats |
Dict[str, Deque[float]]
|
Mapping from metric key to a deque of recent values. |
Source code in masa/common/metrics.py
stats_window_overrides
instance-attribute
¶
stats_window_overrides: Dict[str, int] = {str(k): int(v) for k, v in stats_window_overrides.items()}
prefix
instance-attribute
¶
reset ¶
add ¶
Ingest a new object into the logger.
Concrete subclasses define the supported input types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new
|
Any
|
Object to ingest. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always, in the base class. |
Source code in masa/common/metrics.py
log ¶
Emit logs for a given global step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for TensorBoard summary steps. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always, in the base class. |
masa.common.metrics.StatsLogger ¶
StatsLogger(stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: Optional[SummaryWriter] = None, wandb: bool = False, stats_window_size: int = 100, stats_window_overrides: Dict[str, int] = {}, prefix: str = '')
Bases: BaseLogger
Logger for streaming scalar stats (Stats) and distributions (Dist).
The add method accepts a mapping whose values are one of:
Stats: expanded into multiple scalar keys (mean/std/min/max/mag).Dist: captured for histogram logging.float/int/ numpy scalar: treated as a scalar time series.
Aggregation
- Scalars are smoothed by taking the mean of the most recent
stats_window_sizevalues. - Distributions are logged as histograms using the stored reservoir.
Notes
This class creates internal dictionaries stats_to_log and
dists_to_log during log.
Source code in masa/common/metrics.py
reset ¶
add ¶
Add a batch of metrics to the logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new
|
Mapping[str, Union['Stats', 'Dist', float, int, floating]]
|
Mapping from metric name to a supported metric object. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If a value type is unsupported. |
Source code in masa/common/metrics.py
log ¶
Aggregate buffered values and emit logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for TensorBoard summary steps. |
required |
Source code in masa/common/metrics.py
_create_logs ¶
_create_stats_to_log ¶
Compute smoothed scalar values to emit.
Source code in masa/common/metrics.py
_create_dists_to_log ¶
_log_to_tensorboard ¶
Write scalars and histograms to TensorBoard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for TensorBoard summary steps. |
required |
Source code in masa/common/metrics.py
_get_wandb_payload ¶
Return the current W&B payload dict without logging it.
Used by TrainLogger to batch all sub-logger payloads into a
single wandb.log call.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Mapping from metric name to scalar or |
Source code in masa/common/metrics.py
_log_to_stdout ¶
Print the current scalar log table to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index (unused; included for API symmetry). |
required |
Source code in masa/common/metrics.py
masa.common.metrics.RolloutLogger ¶
RolloutLogger(stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: Optional[SummaryWriter] = None, wandb: bool = False, stats_window_size: int = 100, stats_window_overrides: Dict[str, int] = {}, prefix: str = '')
Bases: BaseLogger
Logger for episodic metrics produced during environment rollouts.
This logger is designed for per-episode summaries that arrive via an info
dict (e.g., from Gymnasium environments). It looks for:
info["constraint"]["episode"]: constraint-related episode metricsinfo["metrics"]["episode"]: generic episode metrics
and treats the values as scalars.
It also reports simple runtime diagnostics to stdout:
fps: \(\frac{\text{timesteps}}{\text{wall-clock seconds}}\)time_elapsed: wall-clock seconds since the firstaddtotal_timesteps: the provided global step
Notes
The most recent value in each deque is treated as the "current episode" and excluded from the mean shown in stdout/TensorBoard (so the displayed mean reflects completed episodes only).
Source code in masa/common/metrics.py
add ¶
Ingest an info dict and extract episodic scalars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info
|
Mapping[str, Any]
|
Rollout |
required |
verbose
|
int
|
Reserved for compatibility; currently unused. |
0
|
Source code in masa/common/metrics.py
log ¶
Aggregate buffered episode metrics and emit logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for TensorBoard summary steps. |
required |
Source code in masa/common/metrics.py
_create_logs ¶
_add_scalars ¶
Append scalar episode metrics into rolling windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scalars
|
Mapping[str, Union[float, int, floating]]
|
Mapping from metric names to numeric values. |
required |
Source code in masa/common/metrics.py
_create_stats_to_log ¶
Compute per-metric mean over completed episodes.
Source code in masa/common/metrics.py
_log_to_tensorboard ¶
Write episode scalar summaries to TensorBoard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for TensorBoard summary steps. |
required |
Source code in masa/common/metrics.py
_get_wandb_payload ¶
Return the current W&B payload dict without logging it.
Used by TrainLogger to batch all sub-logger payloads into a
single wandb.log call.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Mapping from metric name to scalar value. |
Source code in masa/common/metrics.py
_log_to_stdout ¶
Print episode summaries and runtime diagnostics to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for fps and total timestep display. |
required |
Source code in masa/common/metrics.py
masa.common.metrics.TrainLogger ¶
TrainLogger(loggers: List[Tuple[str, Any]], stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: Optional[SummaryWriter] = None, wandb: bool = False, stats_window_size: Union[int, List[int]] = 100, stats_window_overrides: Dict[str, int] = {}, prefix: str = '')
Bases: BaseLogger
Orchestrate multiple loggers for a training run.
A TrainLogger is a thin wrapper around a set of sub-loggers (e.g.
StatsLogger, RolloutLogger). It forwards add calls
to the appropriate sub-logger and aggregates stdout/TensorBoard output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loggers
|
List[Tuple[str, Any]]
|
A list of |
required |
stdout
|
bool
|
If |
True
|
tqdm
|
bool
|
If |
True
|
tensorboard
|
bool
|
If |
False
|
summary_writer
|
Optional[SummaryWriter]
|
TensorBoard writer passed to each sub-logger when
|
None
|
stats_window_size
|
Union[int, List[int]]
|
Either a single window size used for all sub-loggers,
or a list of per-logger window sizes aligned with |
100
|
stats_window_overrides
|
Dict[str, int]
|
A dictionary of string integer pairs for overriding
specific logged metrics with a different |
{}
|
prefix
|
str
|
Optional display prefix for stdout tables. |
''
|
Attributes:
| Name | Type | Description |
|---|---|---|
loggers |
Dict[str, BaseLogger]
|
Mapping from logger key to instantiated |
start_time |
Optional[float]
|
Wall-clock time at which the first |
Source code in masa/common/metrics.py
add ¶
Add an object to a named sub-logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The sub-logger key as provided in |
required |
obj
|
Any
|
The object to forward to |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in masa/common/metrics.py
log ¶
Emit TensorBoard logs (per sub-logger) and a combined stdout table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for TensorBoard summary steps. |
required |
Source code in masa/common/metrics.py
_log_to_stdout ¶
Print a combined stdout table for all sub-loggers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Global step index used for runtime diagnostics. |
required |