Skip to content

Commit a876fdf

Browse files
authored
[MISC] Increase fps by reducing logging frequency (#1956)
1 parent 977e2cf commit a876fdf

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

genesis/utils/tools.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,34 @@ def sleep(self):
177177

178178

179179
class FPSTracker:
180-
def __init__(self, n_envs, alpha=0.95):
180+
def __init__(self, n_envs, alpha=0.95, minimum_interval_seconds: float | None = 0.1):
181181
self.last_time = None
182182
self.n_envs = n_envs
183183
self.dt_ema = None
184184
self.alpha = alpha
185+
self.minimum_interval_seconds = minimum_interval_seconds
186+
self.steps_since_last_print: int = 0
185187

186-
def step(self):
187-
current_time = time.perf_counter()
188+
def step(self, current_time: float | None = None) -> float | None:
189+
if not current_time:
190+
current_time = time.perf_counter()
188191

189192
if self.last_time:
190193
dt = current_time - self.last_time
191194
else:
192195
self.last_time = current_time
193-
return
196+
return None
197+
198+
self.steps_since_last_print += 1
199+
200+
if self.minimum_interval_seconds and current_time - self.last_time < self.minimum_interval_seconds:
201+
return None
194202

195203
if self.dt_ema:
196204
self.dt_ema = self.alpha * self.dt_ema + (1 - self.alpha) * dt
197205
else:
198206
self.dt_ema = dt
199-
fps = 1 / self.dt_ema
207+
fps = 1 / self.dt_ema * self.steps_since_last_print
200208
if self.n_envs > 0:
201209
self.total_fps = fps * self.n_envs
202210
gs.logger.info(
@@ -206,3 +214,5 @@ def step(self):
206214
self.total_fps = fps
207215
gs.logger.info(f"Running at ~<{fps:.2f}>~ FPS.")
208216
self.last_time = current_time
217+
self.steps_since_last_print = 0
218+
return self.total_fps

tests/test_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from unittest.mock import patch
23

34
import pytest
@@ -6,6 +7,7 @@
67

78
import genesis as gs
89
import genesis.utils.geom as gu
10+
from genesis.utils.tools import FPSTracker
911
from genesis.utils.misc import tensor_to_array
1012
from genesis.utils import warnings as warnings_mod
1113
from genesis.utils.warnings import warn_once
@@ -404,3 +406,29 @@ def test_pyrender_vec3():
404406
tq = qz.as_tensor()
405407
assert isinstance(tq, torch.Tensor)
406408
assert_allclose(tq.cpu().numpy(), qz.v, tol=gs.EPS)
409+
410+
411+
def test_fps_tracker():
412+
n_envs = 23
413+
tracker = FPSTracker(alpha=0, n_envs=n_envs)
414+
tracker.step(current_time=10.0)
415+
assert not tracker.step(current_time=10.0)
416+
assert not tracker.step(current_time=10.0)
417+
assert not tracker.step(current_time=10.0)
418+
fps = tracker.step(current_time=10.2)
419+
# num envs * [num steps] / (delta time)
420+
assert math.isclose(fps, n_envs * 4 / 0.2)
421+
422+
assert not tracker.step(current_time=10.21)
423+
assert not tracker.step(current_time=10.22)
424+
assert not tracker.step(current_time=10.29)
425+
fps = tracker.step(current_time=10.31)
426+
# num envs * [num steps] / (delta time)
427+
assert math.isclose(fps, n_envs * 4 / 0.11)
428+
429+
assert not tracker.step(current_time=10.33)
430+
assert not tracker.step(current_time=10.37)
431+
assert not tracker.step(current_time=10.39)
432+
fps = tracker.step(current_time=10.45)
433+
# num envs * [num steps] / (delta time)
434+
assert math.isclose(fps, n_envs * 4 / 0.14)

0 commit comments

Comments
 (0)