Logging¶
- class masa.common.metrics.BaseLogger(stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: SummaryWriter | None = None, stats_window_size: int = 100, prefix: str = '')[source]¶
Bases:
objectBase class for logging scalar statistics and distributions.
A logger ingests objects via
add()and produces aggregated outputs vialog(). Concrete subclasses define how they ingest and summarise data.TensorBoard logging uses a
tf.summary.SummaryWriter. IftensorboardisTruethen a writer must be provided.- Parameters:
stdout – If
True, print logs to stdout viaprint()ortqdm.write().tqdm – If
True, usetqdm.write()for stdout printing.tensorboard – If
True, emit TensorBoard summaries.summary_writer – TensorBoard writer. Required when
tensorboard=True.stats_window_size – Maximum number of recent scalar values retained per metric.
prefix – Optional string prefix for TensorBoard tag names and stdout display. If non-empty, a trailing
"/"is ensured.
- Variables:
stdout – Whether stdout logging is enabled.
tqdm – Whether tqdm-compatible printing is enabled.
tensorboard – Whether TensorBoard logging is enabled.
summary_writer – TensorBoard summary writer (may be
None).stats_window_size – Window size for scalar smoothing.
prefix – Namespace prefix ending with
"/"(or empty).stats – Mapping from metric key to a deque of recent values.
- class masa.common.metrics.StatsLogger(stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: SummaryWriter | None = None, stats_window_size: int = 100, prefix: str = '')[source]¶
Bases:
BaseLoggerLogger 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_loganddists_to_logduringlog().
- class masa.common.metrics.RolloutLogger(stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: SummaryWriter | None = None, stats_window_size: int = 100, prefix: str = '')[source]¶
Bases:
BaseLoggerLogger for episodic metrics produced during environment rollouts.
This logger is designed for per-episode summaries that arrive via an
infodict (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 firstadd()total_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).
- add(info: Mapping[str, Any], verbose: int = 0)[source]¶
Ingest an
infodict and extract episodic scalars.- Parameters:
info – Rollout
infomapping (typically from environment step). If present, the logger reads:info["constraint"]["episode"]and/orinfo["metrics"]["episode"].verbose – Reserved for compatibility; currently unused.
- class masa.common.metrics.TrainLogger(loggers: List[Tuple[str, Any]], stdout: bool = True, tqdm: bool = True, tensorboard: bool = False, summary_writer: SummaryWriter | None = None, stats_window_size: int | List[int] = 100, prefix: str = '')[source]¶
Bases:
BaseLoggerOrchestrate multiple loggers for a training run.
A
TrainLoggeris a thin wrapper around a set of sub-loggers (e.g.StatsLogger,RolloutLogger). It forwardsadd()calls to the appropriate sub-logger and aggregates stdout/TensorBoard output.- Parameters:
loggers – A list of
(name, ctor)pairs.ctormust be aBaseLoggersubclass (or compatible callable) that can be constructed with the same keyword arguments asBaseLogger.stdout – If
True, print a combined stdout table for all sub-loggers.tqdm – If
True, usetqdm.write()when printing.tensorboard – If
True, forward TensorBoard logging to sub-loggers.summary_writer – TensorBoard writer passed to each sub-logger when
tensorboard=True.stats_window_size – Either a single window size used for all sub-loggers, or a list of per-logger window sizes aligned with
loggers.prefix – Optional display prefix for stdout tables.
- Variables:
loggers – Mapping from logger key to instantiated
BaseLogger.start_time – Wall-clock time at which the first
add()occurred, used for runtime diagnostics.