-
Notifications
You must be signed in to change notification settings - Fork 450
[Feature] Add Safety-Gymnasium environment wrapper #3689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
46b02a3
[Feature] Add Safety-Gymnasium environment wrapper
theap06 a5dfcf7
added documentation, torch tensor, and autodiscover. implementated th…
theap06 55cea27
linter
vmoens 40ab5e5
Merge remote-tracking branch 'origin/main' into feat/safety-gymnasium
vmoens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
.github/unittest/linux_libs/scripts_safety_gymnasium/install.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| unset PYTORCH_VERSION | ||
| # For unittest, nightly PyTorch is used as the following section, | ||
| # so no need to set PYTORCH_VERSION. | ||
| # In fact, keeping PYTORCH_VERSION forces us to hardcode PyTorch version in config. | ||
|
|
||
| set -e | ||
|
|
||
| # Ensure uv is in PATH | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
|
|
||
| # Activate the virtual environment | ||
| source ./env/bin/activate | ||
|
|
||
| if [ "${CU_VERSION:-}" == cpu ] ; then | ||
| version="cpu" | ||
| else | ||
| if [[ ${#CU_VERSION} -eq 4 ]]; then | ||
| CUDA_VERSION="${CU_VERSION:2:1}.${CU_VERSION:3:1}" | ||
| elif [[ ${#CU_VERSION} -eq 5 ]]; then | ||
| CUDA_VERSION="${CU_VERSION:2:2}.${CU_VERSION:4:1}" | ||
| fi | ||
| echo "Using CUDA $CUDA_VERSION as determined by CU_VERSION ($CU_VERSION)" | ||
| version="$(python -c "print('.'.join(\"${CUDA_VERSION}\".split('.')[:2]))")" | ||
| fi | ||
|
|
||
| # submodules | ||
| git submodule sync && git submodule update --init --recursive | ||
|
|
||
| printf "Installing PyTorch with cu128" | ||
| if [[ "$TORCH_VERSION" == "nightly" ]]; then | ||
| if [ "${CU_VERSION:-}" == cpu ] ; then | ||
| uv pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cpu -U | ||
| else | ||
| uv pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu128 -U | ||
| fi | ||
| elif [[ "$TORCH_VERSION" == "stable" ]]; then | ||
| if [ "${CU_VERSION:-}" == cpu ] ; then | ||
| uv pip install torch --index-url https://download.pytorch.org/whl/cpu -U | ||
| else | ||
| uv pip install torch --index-url https://download.pytorch.org/whl/cu128 -U | ||
| fi | ||
| else | ||
| printf "Failed to install pytorch" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Ensure tensordict and torchrl dependencies are installed | ||
| # (since we use --no-deps for tensordict and torchrl) | ||
| uv pip install numpy pyvers packaging cloudpickle | ||
|
|
||
| # Install build dependencies for torchrl (needed with --no-build-isolation) | ||
| uv pip install setuptools wheel setuptools_scm ninja "pybind11[global]" | ||
|
|
||
| # install tensordict | ||
| if [[ "$RELEASE" == 0 ]]; then | ||
| uv pip install --no-deps git+https://github.com/pytorch/tensordict.git | ||
| else | ||
| uv pip install --no-deps tensordict | ||
| fi | ||
|
|
||
| # smoke test | ||
| python -c "import functorch;import tensordict" | ||
|
|
||
| printf "* Installing torchrl\n" | ||
| python -m pip install -e . --no-build-isolation --no-deps | ||
|
|
||
| # smoke test | ||
| python -c "import torchrl" |
6 changes: 6 additions & 0 deletions
6
.github/unittest/linux_libs/scripts_safety_gymnasium/post_process.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -e | ||
|
|
||
| # Activate the virtual environment | ||
| source ./env/bin/activate |
18 changes: 18 additions & 0 deletions
18
.github/unittest/linux_libs/scripts_safety_gymnasium/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Core dependencies for tensordict/torchrl (installed with --no-deps) | ||
| numpy | ||
| pyvers | ||
| packaging | ||
| cloudpickle | ||
|
|
||
| # Test dependencies | ||
| pytest | ||
| pytest-xdist | ||
| pytest-instafail | ||
| pytest-error-for-skips | ||
| coverage | ||
|
|
||
| # Safety-Gymnasium and friends | ||
| safety-gymnasium | ||
| gymnasium | ||
| mujoco | ||
| imageio |
28 changes: 28 additions & 0 deletions
28
.github/unittest/linux_libs/scripts_safety_gymnasium/run_test.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -e | ||
|
|
||
| # Activate the virtual environment | ||
| source ./env/bin/activate | ||
|
|
||
| apt-get update && apt-get install -y git wget cmake | ||
|
|
||
| export PYTORCH_TEST_WITH_SLOW='1' | ||
| export LAZY_LEGACY_OP=False | ||
| python -m torch.utils.collect_env | ||
| # Avoid error: "fatal: unsafe repository" | ||
| git config --global --add safe.directory '*' | ||
|
|
||
| root_dir="$(git rev-parse --show-toplevel)" | ||
| env_dir="${root_dir}/env" | ||
| lib_dir="${env_dir}/lib" | ||
|
|
||
| deactivate 2>/dev/null || true && source ./env/bin/activate | ||
|
|
||
| # this workflow only tests the libs | ||
| python -c "import safety_gymnasium" | ||
|
|
||
| python .github/unittest/helpers/coverage_run_parallel.py -m pytest test/libs --instafail -v --durations 200 --capture no -k TestSafetyGymnasium --error-for-skips --runslow | ||
|
|
||
| coverage combine -q | ||
| coverage xml -i |
65 changes: 65 additions & 0 deletions
65
.github/unittest/linux_libs/scripts_safety_gymnasium/setup_env.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # This script is for setting up environment in which unit test is ran. | ||
| # To speed up the CI time, the resulting environment is cached. | ||
| # | ||
| # Do not install PyTorch and torchvision here, otherwise they also get cached. | ||
|
|
||
| set -e | ||
| set -v | ||
|
|
||
| apt-get update && apt-get upgrade -y && apt-get install -y git cmake | ||
| # Avoid error: "fatal: unsafe repository" | ||
| git config --global --add safe.directory '*' | ||
| apt-get install -y wget \ | ||
| gcc \ | ||
| g++ \ | ||
| unzip \ | ||
| curl \ | ||
| patchelf \ | ||
| libosmesa6-dev \ | ||
| libgl1-mesa-glx \ | ||
| libglfw3 \ | ||
| swig3.0 \ | ||
| libglew-dev \ | ||
| libglvnd0 \ | ||
| libgl1 \ | ||
| libglx0 \ | ||
| libegl1 \ | ||
| libgles2 \ | ||
| libglib2.0-0 | ||
|
|
||
| # Upgrade specific package | ||
| apt-get upgrade -y libstdc++6 | ||
|
|
||
| this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
| root_dir="$(git rev-parse --show-toplevel)" | ||
| env_dir="${root_dir}/env" | ||
|
|
||
| cd "${root_dir}" | ||
|
|
||
| # Install uv if not already installed | ||
| if ! command -v uv &> /dev/null; then | ||
| printf "* Installing uv\n" | ||
| curl -LsSf https://astral.sh/uv/install.sh | sh | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
| fi | ||
|
|
||
| # Create virtual environment using uv | ||
| printf "python: ${PYTHON_VERSION}\n" | ||
| if [ ! -d "${env_dir}" ]; then | ||
| printf "* Creating a test environment with uv\n" | ||
| uv venv "${env_dir}" --python "${PYTHON_VERSION}" | ||
| fi | ||
|
|
||
| # Activate the virtual environment | ||
| source "${env_dir}/bin/activate" | ||
|
|
||
| # Upgrade pip | ||
| uv pip install --upgrade pip | ||
|
|
||
| # Install dependencies from requirements.txt | ||
| printf "* Installing dependencies (except PyTorch)\n" | ||
| if [ -f "${this_dir}/requirements.txt" ]; then | ||
| uv pip install -r "${this_dir}/requirements.txt" | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the MIT license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from torchrl.envs.libs.safety_gymnasium import ( | ||
| _has_safety_gymnasium, | ||
| SafetyGymnasiumEnv, | ||
| SafetyGymnasiumWrapper, | ||
| ) | ||
| from torchrl.envs.utils import check_env_specs | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not _has_safety_gymnasium, reason="safety-gymnasium not installed") | ||
| class TestSafetyGymnasium: | ||
| def test_wrapper_specs(self): | ||
| import safety_gymnasium | ||
|
|
||
| base = safety_gymnasium.make("SafetyPointGoal1-v0") | ||
| env = SafetyGymnasiumWrapper(base) | ||
| check_env_specs(env) | ||
| assert "cost" in env.observation_spec.keys() | ||
|
|
||
| def test_env_from_name_specs(self): | ||
| env = SafetyGymnasiumEnv(env_name="SafetyPointGoal1-v0") | ||
| check_env_specs(env) | ||
| assert "cost" in env.observation_spec.keys() | ||
|
|
||
| def test_rollout_exposes_cost(self): | ||
| env = SafetyGymnasiumEnv(env_name="SafetyPointGoal1-v0") | ||
| env.set_seed(0) | ||
| td = env.rollout(5) | ||
| assert ("next", "cost") in td.keys(True) | ||
| assert td["next", "cost"].dtype == torch.float64 | ||
| assert td["next", "cost"].shape == td["next", "reward"].shape[:-1] | ||
|
|
||
| def test_cost_fires_on_hazard_contact(self): | ||
| # SafetyCarPush2-v0 has dense hazards; under random actions we expect | ||
| # at least one positive cost in a long rollout. Without this signal | ||
| # being plumbed through, every cost would be zero. | ||
| env = SafetyGymnasiumEnv(env_name="SafetyCarPush2-v0") | ||
| env.set_seed(0) | ||
| td = env.rollout(2000, break_when_any_done=False) | ||
| assert (td["next", "cost"] > 0).any(), ( | ||
| "Expected at least one nonzero cost over 2000 random steps; " | ||
| "cost signal may not be plumbed correctly." | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.