Skip to content

Fix compatibility with gymnasium >= 1.0.0#12

Open
traversaro wants to merge 2 commits intohuggingface:mainfrom
traversaro:fixgym1
Open

Fix compatibility with gymnasium >= 1.0.0#12
traversaro wants to merge 2 commits intohuggingface:mainfrom
traversaro:fixgym1

Conversation

@traversaro
Copy link
Copy Markdown

The change in Farama-Foundation/Gymnasium#868 broke the code in gym-xarm, resulting in the error:

TypeError: MujocoRenderer.render() got an unexpected keyword argument 'camera_name'

when executed.

This PR fixes the problem, by keeping compatibility with gymnasium 0.29.1 .

Fix #9 .
Fix huggingface/lerobot#474 .

@traversaro
Copy link
Copy Markdown
Author

traversaro commented Sep 1, 2025

Even with this fix, some tests are failing with gymnasium==1.2.0 and gymnasium-robotics==1.4.1 (the same test pass successfully with older versions, such as the one installed by default by lerobot).

(lerobot) traversaro@IITBMP014LW012:~/lerobotws/gym-xarm$ pytest
============================================================================================================================ test session starts ============================================================================================================================
platform linux -- Python 3.10.18, pytest-8.4.1, pluggy-1.6.0
rootdir: /home/traversaro/lerobotws/gym-xarm
configfile: pyproject.toml
plugins: mock-serial-0.0.1, cov-6.2.1, timeout-2.4.0
collected 3 items

tests/test_env.py FFF                                                                                                                                                                                                                                                 [100%]

================================================================================================================================= FAILURES ==================================================================================================================================
________________________________________________________________________________________________________________________ test_env[XarmLift-v0-state] ________________________________________________________________________________________________________________________

env_task = 'XarmLift-v0', obs_type = 'state'

    @pytest.mark.parametrize(
        "env_task, obs_type",
        [
            ("XarmLift-v0", "state"),
            ("XarmLift-v0", "pixels"),
            ("XarmLift-v0", "pixels_agent_pos"),
        ],
    )
    def test_env(env_task, obs_type):
        env = gym.make(f"gym_xarm/{env_task}", obs_type=obs_type)
>       check_env(env.unwrapped)

tests/test_env.py:18:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:420: in check_env
    check_step_determinism(env)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

env = <gym_xarm.tasks.lift.Lift object at 0x773c5d453fa0>, seed = 123

    def check_step_determinism(env: gym.Env, seed=123):
        """Check that the environment steps deterministically after reset.

        Note: This check assumes that seeded `reset()` is deterministic (it must have passed `check_reset_seed`) and that `step()` returns valid values (passed `env_step_passive_checker`).
        Note: A single step should be enough to assert that the state transition function is deterministic (at least for most environments).

        Raises:
            AssertionError: The environment cannot be step deterministically after resetting with a random seed,
                or it truncates after 1 step.
        """
        if env.spec is not None and env.spec.nondeterministic is True:
            return

        env.action_space.seed(seed)
        action = env.action_space.sample()

        env.reset(seed=seed)
        obs_0, rew_0, term_0, trunc_0, info_0 = env.step(action)
        seeded_rng: np.random.Generator = deepcopy(env.unwrapped._np_random)

        env.reset(seed=seed)
        obs_1, rew_1, term_1, trunc_1, info_1 = env.step(action)

        assert (
            env.unwrapped._np_random.bit_generator.state  # pyright: ignore [reportOptionalMemberAccess]
            == seeded_rng.bit_generator.state
        ), "The `.np_random` is not properly been updated after step."

>       assert data_equivalence(
            obs_0, obs_1
        ), "Deterministic step observations are not equivalent for the same seed and action"
E       AssertionError: Deterministic step observations are not equivalent for the same seed and action

../../miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:216: AssertionError
_______________________________________________________________________________________________________________________ test_env[XarmLift-v0-pixels] ________________________________________________________________________________________________________________________

env_task = 'XarmLift-v0', obs_type = 'pixels'

    @pytest.mark.parametrize(
        "env_task, obs_type",
        [
            ("XarmLift-v0", "state"),
            ("XarmLift-v0", "pixels"),
            ("XarmLift-v0", "pixels_agent_pos"),
        ],
    )
    def test_env(env_task, obs_type):
        env = gym.make(f"gym_xarm/{env_task}", obs_type=obs_type)
>       check_env(env.unwrapped)

tests/test_env.py:18:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:420: in check_env
    check_step_determinism(env)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

env = <gym_xarm.tasks.lift.Lift object at 0x773c4e9974f0>, seed = 123

    def check_step_determinism(env: gym.Env, seed=123):
        """Check that the environment steps deterministically after reset.

        Note: This check assumes that seeded `reset()` is deterministic (it must have passed `check_reset_seed`) and that `step()` returns valid values (passed `env_step_passive_checker`).
        Note: A single step should be enough to assert that the state transition function is deterministic (at least for most environments).

        Raises:
            AssertionError: The environment cannot be step deterministically after resetting with a random seed,
                or it truncates after 1 step.
        """
        if env.spec is not None and env.spec.nondeterministic is True:
            return

        env.action_space.seed(seed)
        action = env.action_space.sample()

        env.reset(seed=seed)
        obs_0, rew_0, term_0, trunc_0, info_0 = env.step(action)
        seeded_rng: np.random.Generator = deepcopy(env.unwrapped._np_random)

        env.reset(seed=seed)
        obs_1, rew_1, term_1, trunc_1, info_1 = env.step(action)

        assert (
            env.unwrapped._np_random.bit_generator.state  # pyright: ignore [reportOptionalMemberAccess]
            == seeded_rng.bit_generator.state
        ), "The `.np_random` is not properly been updated after step."

        assert data_equivalence(
            obs_0, obs_1
        ), "Deterministic step observations are not equivalent for the same seed and action"
        if not data_equivalence(obs_0, obs_1, exact=True):
            logger.warn(
                "Step observations are not equal although similar given the same seed and action"
            )

>       assert data_equivalence(
            rew_0, rew_1
        ), "Deterministic step rewards are not equivalent for the same seed and action"
E       AssertionError: Deterministic step rewards are not equivalent for the same seed and action

../../miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:224: AssertionError
__________________________________________________________________________________________________________________ test_env[XarmLift-v0-pixels_agent_pos] ___________________________________________________________________________________________________________________

env_task = 'XarmLift-v0', obs_type = 'pixels_agent_pos'

    @pytest.mark.parametrize(
        "env_task, obs_type",
        [
            ("XarmLift-v0", "state"),
            ("XarmLift-v0", "pixels"),
            ("XarmLift-v0", "pixels_agent_pos"),
        ],
    )
    def test_env(env_task, obs_type):
        env = gym.make(f"gym_xarm/{env_task}", obs_type=obs_type)
>       check_env(env.unwrapped)

tests/test_env.py:18:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:420: in check_env
    check_step_determinism(env)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

env = <gym_xarm.tasks.lift.Lift object at 0x773c4eaf79a0>, seed = 123

    def check_step_determinism(env: gym.Env, seed=123):
        """Check that the environment steps deterministically after reset.

        Note: This check assumes that seeded `reset()` is deterministic (it must have passed `check_reset_seed`) and that `step()` returns valid values (passed `env_step_passive_checker`).
        Note: A single step should be enough to assert that the state transition function is deterministic (at least for most environments).

        Raises:
            AssertionError: The environment cannot be step deterministically after resetting with a random seed,
                or it truncates after 1 step.
        """
        if env.spec is not None and env.spec.nondeterministic is True:
            return

        env.action_space.seed(seed)
        action = env.action_space.sample()

        env.reset(seed=seed)
        obs_0, rew_0, term_0, trunc_0, info_0 = env.step(action)
        seeded_rng: np.random.Generator = deepcopy(env.unwrapped._np_random)

        env.reset(seed=seed)
        obs_1, rew_1, term_1, trunc_1, info_1 = env.step(action)

        assert (
            env.unwrapped._np_random.bit_generator.state  # pyright: ignore [reportOptionalMemberAccess]
            == seeded_rng.bit_generator.state
        ), "The `.np_random` is not properly been updated after step."

>       assert data_equivalence(
            obs_0, obs_1
        ), "Deterministic step observations are not equivalent for the same seed and action"
E       AssertionError: Deterministic step observations are not equivalent for the same seed and action

../../miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:216: AssertionError
============================================================================================================================= warnings summary ==============================================================================================================================
tests/test_env.py::test_env[XarmLift-v0-state]
tests/test_env.py::test_env[XarmLift-v0-pixels_agent_pos]
  /home/traversaro/miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:120: UserWarning: WARN: Using `env.reset(seed=123)` observations are not equal although similar.
    logger.warn(

tests/test_env.py::test_env[XarmLift-v0-state]
tests/test_env.py::test_env[XarmLift-v0-pixels_agent_pos]
  /home/traversaro/miniforge3/envs/lerobot/lib/python3.10/site-packages/gymnasium/utils/env_checker.py:124: UserWarning: WARN: Using `env.reset(seed=123)` then `env.reset()` observations are not equal although similar.
    logger.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================================================================================================================== short test summary info ==========================================================================================================================
FAILED tests/test_env.py::test_env[XarmLift-v0-state] - AssertionError: Deterministic step observations are not equivalent for the same seed and action
FAILED tests/test_env.py::test_env[XarmLift-v0-pixels] - AssertionError: Deterministic step rewards are not equivalent for the same seed and action
FAILED tests/test_env.py::test_env[XarmLift-v0-pixels_agent_pos] - AssertionError: Deterministic step observations are not equivalent for the same seed and action
======================================================================================================================= 3 failed, 4 warnings in 1.82s =======================================================================================================================

@jadechoghari
Copy link
Copy Markdown
Collaborator

Hi @traversaro thanks for your PR, is it compatible with gymnasium >= 1.0.0, ? or are you encountering issues. otherwise we are planning to depreciate gym-xarm in LeRobot

@traversaro
Copy link
Copy Markdown
Author

It wanted to be compatible with gymnaisum 1.0.0, but there was the test failure in #12 (comment) . I suspect it was not a regression, but rather the environment tests on gymnasium that became more strict. Anyhow I was only interested in this as it was a transitive dependency of gymnasium[all], if that is going to be dropped, I would not be interested in it anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: MujocoRenderer.render() got an unexpected keyword argument 'camera_name' tdmpc xarm example not working

2 participants