ray.rllib.utils.metrics.metrics_logger.MetricsLogger.log_time#
- MetricsLogger.log_time(key: str | Tuple[str, ...], *, reduce: str = 'mean', window: int | float | None = None, ema_coeff: float | None = None, clear_on_reduce: bool = False, with_throughput: bool = False, throughput_ema_coeff: float = 0.05, reduce_per_index_on_aggregate: bool = False) Stats[source]#
Measures and logs a time delta value under
keywhen used with a with-block.import time from ray.rllib.utils.metrics.metrics_logger import MetricsLogger logger = MetricsLogger() # First delta measurement: with logger.log_time("my_block_to_be_timed", ema_coeff=0.1): time.sleep(1.0) # EMA should be ~1sec. assert 1.1 > logger.peek("my_block_to_be_timed") > 0.9 # Second delta measurement (note that we don't have to repeat the args # again, as the stats under that name have already been created above with # the correct args). with logger.log_time("my_block_to_be_timed"): time.sleep(2.0) # EMA should be ~1.1sec. assert 1.15 > logger.peek("my_block_to_be_timed") > 1.05 # When calling `reduce()`, the latest, reduced value is returned. results = logger.reduce() # EMA should be ~1.1sec. assert 1.15 > results["my_block_to_be_timed"] > 1.05
- Parameters:
key – The key (or tuple of keys) to log the measured time delta under.
reduce – The reduction method to apply, once
self.reduce()is called. If None, will collect all logged values underkeyin a list (and also return that list upon callingself.reduce()).window – An optional window size to reduce over. If not None, then the reduction operation is only applied to the most recent
windowitems, and - after reduction - the internal values list underkeyis shortened to hold at mostwindowitems (the most recent ones). Must be None ifema_coeffis provided. If None (andema_coeffis None), reduction must not be “mean”.ema_coeff – An optional EMA coefficient to use if
reduceis “mean” and nowindowis provided. Note that if bothwindowandema_coeffare provided, an error is thrown. Also, ifema_coeffis provided,reducemust be “mean”. The reduction formula for EMA is: EMA(t1) = (1.0 - ema_coeff) * EMA(t0) + ema_coeff * new_valueclear_on_reduce – If True, all values under
keywill be emptied afterself.reduce()is called. Setting this to True is useful for cases, in which the internal values list would otherwise grow indefinitely, for example if reduce is None and there is nowindowprovided.with_throughput – Whether to track a throughput estimate together with this metric. This is only supported for
reduce=sumandclear_on_reduce=Falsemetrics (aka. “lifetime counts”). TheStatsobject under the logged key then keeps track of the time passed between two consecutive calls toreduce()and update its throughput estimate. The current throughput estimate of a key can be obtained through: <MetricsLogger>.peek(key, throughput=True).throughput_ema_coeff – The EMA coefficient to use for throughput tracking. Only used if with_throughput=True. Defaults to 0.05.
reduce_per_index_on_aggregate – If True, when merging Stats objects, we reduce incoming values per index such that the new value at index
nwill be the reduced value of all incoming values at indexn. If False, when reducingnStats, the firstnmerged values will be the reduced value of all incoming values at index0, the nextnmerged values will be the reduced values of all incoming values at index1, etc.