Repository: Genesis-Embodied-AI/Genesis
File: tests/conftest.py (Python)

Code before:
    # Get available memory (RAM & VRAM) and number of physical cores.
    physical_core_count = psutil.cpu_count(logical=False)
    _, _, ram_memory, _ = gs.utils.get_device(gs.cpu)
    _, _, vram_memory, _ = gs.utils.get_device(gs.gpu)

    # Compute the default number of workers based on available RAM, VRAM, and number of physical cores.
    # Note that if `forked` is not enabled, up to 7.5Gb per worker is necessary on Linux because Taichi
    # does not completely release memory between each test.
    if sys.platform == "darwin":
        ram_memory_per_worker = 3.0
        vram_memory_per_worker = 1.0  # Does not really makes sense on Apple Silicon
    elif config.option.forked:
        ram_memory_per_worker = 5.5
        vram_memory_per_worker = 1.2
    else:
        ram_memory_per_worker = 7.5
        vram_memory_per_worker = 1.6
    return min(
        int(ram_memory / ram_memory_per_worker),
        int(vram_memory / vram_memory_per_worker),
        physical_core_count,
    )


# def pytest_set_filtered_exceptions():
#     return (HTTPError,)


def pytest_addoption(parser):
    parser.addoption("--backend", action="store", default=gs.cpu, help="Default simulation backend.")
    parser.addoption("--vis", action="store_true", default=False, help="Enable interactive viewer.")


@pytest.fixture(scope="session")
def show_viewer(pytestconfig):
    return pytestconfig.getoption("--vis")


@pytest.fixture(scope="session")
def backend(pytestconfig):
    backend = pytestconfig.getoption("--backend", "cpu")
    if isinstance(backend, str):
        return getattr(gs.constants.backend, backend)
    return backend


@pytest.fixture(scope="session")
def asset_tmp_path(tmp_path_factory):
    return tmp_path_factory.mktemp("assets")



Review comment (security):
To resolve the following error.
```bash
$ pytest tests/test_utils.py::test_numpy_identity_transform

============================================================================================================================================================== test session starts ==============================================================================================================================================================

platform linux -- Python 3.10.16, pytest-8.3.5, pluggy-1.6.0

Using --random-order-bucket=global

Using --random-order-seed=0


rootdir: /home/kashu/research/Genesis

configfile: pyproject.toml

plugins: forked-1.6.0, xdist-3.6.1, random-order-1.1.1, anyio-4.9.0

initialized: 4/4 workers/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/random_order/plugin.py:54: PytestCacheWarning: cache could not write path /home/kashu/research/Genesis/.pytest_cache/v/random_order_seed: [Errno 13] Permission denied: '/home/kashu/research/Genesis/.pytest_cache/v/random_order_seed'

  config.cache.set('random_order_seed', seed)

collecting: 1/4 workers[I 06/08/25 14:12:41.705 2247055] [shell.py:_shell_pop_print@23] Graphical python shell detected, using wrapped sys.stdout

INTERNALERROR> def worker_internal_error(

INTERNALERROR>         self, node: WorkerController, formatted_error: str

INTERNALERROR>     ) -> None:

INTERNALERROR>         """

INTERNALERROR>         pytest_internalerror() was called on the worker.

INTERNALERROR>     

INTERNALERROR>         pytest_internalerror() arguments are an excinfo and an excrepr, which can't

INTERNALERROR>         be serialized, so we go with a poor man's solution of raising an exception

INTERNALERROR>         here ourselves using the formatted message.

INTERNALERROR>         """

INTERNALERROR>         self._active_nodes.remove(node)

INTERNALERROR>         try:

INTERNALERROR> >           assert False, formatted_error

INTERNALERROR> E           AssertionError: Traceback (most recent call last):

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/main.py", line 283, in wrap_session

INTERNALERROR> E                 session.exitstatus = doit(config, session) or 0

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/main.py", line 336, in _main

INTERNALERROR> E                 config.hook.pytest_collection(session=session)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_hooks.py", line 512, in __call__

INTERNALERROR> E                 return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec

INTERNALERROR> E                 return self._inner_hookexec(hook_name, methods, kwargs, firstresult)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 167, in _multicall

INTERNALERROR> E                 raise exception

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall

INTERNALERROR> E                 teardown.throw(exception)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/logging.py", line 790, in pytest_collection

INTERNALERROR> E                 return (yield)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall

INTERNALERROR> E                 teardown.throw(exception)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/warnings.py", line 121, in pytest_collection

INTERNALERROR> E                 return (yield)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall

INTERNALERROR> E                 teardown.throw(exception)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1417, in pytest_collection

INTERNALERROR> E                 return (yield)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 121, in _multicall

INTERNALERROR> E                 res = hook_impl.function(*args)

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/main.py", line 347, in pytest_collection

INTERNALERROR> E                 session.perform_collect()

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/main.py", line 811, in perform_collect

INTERNALERROR> E                 self.config.pluginmanager.check_pending()

INTERNALERROR> E               File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_manager.py", line 387, in check_pending

INTERNALERROR> E                 raise PluginValidationError(

INTERNALERROR> E             pluggy._manager.PluginValidationError: unknown hook 'pytest_set_filtered_exceptions' in plugin <module 'tests.conftest' from '/home/kashu/research/Genesis/tests/conftest.py'>

INTERNALERROR> E           assert False

INTERNALERROR>

INTERNALERROR> ../../miniconda3/envs/genesis/lib/python3.10/site-packages/xdist/dsession.py:232: AssertionError

INTERNALERROR> Traceback (most recent call last):

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/main.py", line 283, in wrap_session

INTERNALERROR>     session.exitstatus = doit(config, session) or 0

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/main.py", line 337, in _main

INTERNALERROR>     config.hook.pytest_runtestloop(session=session)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_hooks.py", line 512, in __call__

INTERNALERROR>     return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec

INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 167, in _multicall

INTERNALERROR>     raise exception

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall

INTERNALERROR>     teardown.throw(exception)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/logging.py", line 803, in pytest_runtestloop

INTERNALERROR>     return (yield)  # Run all the tests.

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall

INTERNALERROR>     teardown.throw(exception)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/_pytest/terminal.py", line 673, in pytest_runtestloop

INTERNALERROR>     result = yield

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/pluggy/_callers.py", line 121, in _multicall

INTERNALERROR>     res = hook_impl.function(*args)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/xdist/dsession.py", line 138, in pytest_runtestloop

INTERNALERROR>     self.loop_once()

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/xdist/dsession.py", line 163, in loop_once

INTERNALERROR>     call(**kwargs)

INTERNALERROR>   File "/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/xdist/dsession.py", line 218, in worker_workerfinished

INTERNALERROR>     self._active_nodes.remove(node)

INTERNALERROR> KeyError: <WorkerController gw0>

/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/random_order/plugin.py:54: PytestCacheWarning: cache could not write path /home/kashu/research/Genesis/.pytest_cache/v/random_order_seed: [Errno 13] Permission denied: '/home/kashu/research/Genesis/.pytest_cache/v/random_order_seed'

  config.cache.set('random_order_seed', seed)

/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/random_order/plugin.py:54: PytestCacheWarning: cache could not write path /home/kashu/research/Genesis/.pytest_cache/v/random_order_seed: [Errno 13] Permission denied: '/home/kashu/research/Genesis/.pytest_cache/v/random_order_seed'

  config.cache.set('random_order_seed', seed)

/home/kashu/miniconda3/envs/genesis/lib/python3.10/site-packages/random_order/plugin.py:54: PytestCacheWarning: cache could not write path /home/kashu/research/Genesis/.pytest_cache/v/random_order_seed: [Errno 13] Permission denied: '/home/kashu/research/Genesis/.pytest_cache/v/random_order_seed'

  config.cache.set('random_order_seed', seed) 
```


Code after:
    physical_core_count = psutil.cpu_count(logical=False)
    _, _, ram_memory, _ = gs.utils.get_device(gs.cpu)
    _, _, vram_memory, _ = gs.utils.get_device(gs.gpu)

    # Compute the default number of workers based on available RAM, VRAM, and number of physical cores.
    # Note that if `forked` is not enabled, up to 7.5Gb per worker is necessary on Linux because Taichi
    # does not completely release memory between each test.
    if sys.platform == "darwin":
        ram_memory_per_worker = 3.0
        vram_memory_per_worker = 1.0  # Does not really makes sense on Apple Silicon
    elif config.option.forked:
        ram_memory_per_worker = 5.5
        vram_memory_per_worker = 1.2
    else:
        ram_memory_per_worker = 7.5
        vram_memory_per_worker = 1.6
    return min(
        int(ram_memory / ram_memory_per_worker),
        int(vram_memory / vram_memory_per_worker),
        physical_core_count,
    )


def pytest_addoption(parser):
    parser.addoption("--backend", action="store", default=gs.cpu, help="Default simulation backend.")
    parser.addoption("--vis", action="store_true", default=False, help="Enable interactive viewer.")


@pytest.fixture(scope="session")
def show_viewer(pytestconfig):
    return pytestconfig.getoption("--vis")


@pytest.fixture(scope="session")
def backend(pytestconfig):
    backend = pytestconfig.getoption("--backend", "cpu")
    if isinstance(backend, str):
        return getattr(gs.constants.backend, backend)
    return backend


@pytest.fixture(scope="session")
def asset_tmp_path(tmp_path_factory):
    return tmp_path_factory.mktemp("assets")


@pytest.fixture
def tol():
    return TOL_DOUBLE if gs.np_float == np.float64 else TOL_SINGLE