Skip to content

Commit

Permalink
Refactor Gym and DMControl wrappers (pytorch#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens authored Jun 15, 2022
1 parent 87eb981 commit 97f4d48
Show file tree
Hide file tree
Showing 21 changed files with 703 additions and 319 deletions.
1 change: 1 addition & 0 deletions .circleci/unittest/linux/scripts/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ dependencies:
- scipy
- dm_control
- mujoco_py
- pyrender
32 changes: 22 additions & 10 deletions .circleci/unittest/linux/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ esac
# submodules
git submodule sync && git submodule update --init --recursive

#printf "Installing PyTorch with %s\n" "${cudatoolkit}"
#if [ "${os}" == "MacOSX" ]; then
# conda install -y -c "pytorch-${UPLOAD_CHANNEL}" "pytorch-${UPLOAD_CHANNEL}"::pytorch "${cudatoolkit}" pytest
#else
# conda install -y -c "pytorch-${UPLOAD_CHANNEL}" "pytorch-${UPLOAD_CHANNEL}"::pytorch[build="*${version}*"] "${cudatoolkit}" pytest
#fi

#printf "Installing PyTorch with %s\n" "${CU_VERSION}"
printf "Installing PyTorch with %s\n" "${CU_VERSION}"
if [ "${CU_VERSION:-}" == cpu ] ; then
# conda install -y pytorch torchvision cpuonly -c pytorch-nightly
# use pip to install pytorch as conda can frequently pick older release
pip install torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html --pre
if [[ $OSTYPE == 'darwin'* ]]; then
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
else
pip3 install torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html --pre
fi
else
conda install -y pytorch torchvision cudatoolkit=10.2 -c pytorch-nightly
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu102
fi

printf "Installing functorch\n"
Expand All @@ -57,3 +54,18 @@ python -c "import functorch"

printf "* Installing torchrl\n"
python setup.py develop

if [[ $OSTYPE == 'darwin'* ]]; then
PRIVATE_MUJOCO_GL=glfw
else
conda install -y -c conda-forge mesa
conda install -y -c menpo osmesa
PRIVATE_MUJOCO_GL=osmesa
fi

conda env config vars set MUJOCO_PY_MUJOCO_PATH=$root_dir/.mujoco/mujoco210 \
DISPLAY=unix:0.0 \
MJLIB_PATH=$root_dir/.mujoco/mujoco-2.1.1/lib/libmujoco.so.2.1.1 \
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$root_dir/.mujoco/mujoco210/bin \
SDL_VIDEODRIVER=dummy \
MUJOCO_GL=$PRIVATE_MUJOCO_GL
31 changes: 18 additions & 13 deletions .circleci/unittest/linux/scripts/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ set -e
eval "$(./conda/bin/conda shell.bash hook)"
conda activate ./env

if [[ $OSTYPE == 'darwin'* ]]; then
PRIVATE_MUJOCO_GL=glfw
else
PRIVATE_MUJOCO_GL=glfw
fi

export PYTORCH_TEST_WITH_SLOW='1'
python -m torch.utils.collect_env
# Avoid error: "fatal: unsafe repository"
git config --global --add safe.directory '*'

root_dir="$(git rev-parse --show-toplevel)"
export MJLIB_PATH=$root_dir/.mujoco/mujoco-2.1.1/lib/libmujoco.so.2.1.1
export MUJOCO_PY_MUJOCO_PATH=$root_dir/.mujoco/mujoco210
export DISPLAY=unix:0.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/project/.mujoco/mujoco210/bin
env_dir="${root_dir}/env"
lib_dir="${env_dir}/lib"

# solves ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$lib_dir
#if [[ $OSTYPE == 'darwin'* ]]; then
# PRIVATE_MUJOCO_GL=glfw
#else
# PRIVATE_MUJOCO_GL=egl
#fi
## pygame.error: No available video device
#export SDL_VIDEODRIVER=dummy
## solves AttributeError: 'NoneType' object has no attribute 'eglQueryString'
#export MUJOCO_GL=$PRIVATE_MUJOCO_GL

MUJOCO_GL=$PRIVATE_MUJOCO_GL pytest test/smoke_test.py -v --durations 20
MUJOCO_GL=$PRIVATE_MUJOCO_GL pytest test/smoke_test_deps.py -v --durations 20
MUJOCO_GL=$PRIVATE_MUJOCO_GL pytest -v --durations 20
pytest test/smoke_test.py -v --durations 20
pytest test/smoke_test_deps.py -v --durations 20
pytest -v --durations 20
15 changes: 7 additions & 8 deletions .circleci/unittest/linux/scripts/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ git config --global --add safe.directory '*'
root_dir="$(git rev-parse --show-toplevel)"
conda_dir="${root_dir}/conda"
env_dir="${root_dir}/env"
lib_dir="${env_dir}/lib"

cd "${root_dir}"

Expand All @@ -39,7 +40,7 @@ conda activate "${env_dir}"

# 3. Install mujoco
printf "* Installing mujoco and related\n"
mkdir $root_dir/.mujoco
mkdir -p $root_dir/.mujoco
cd $root_dir/.mujoco/
wget https://github.com/deepmind/mujoco/releases/download/2.1.1/mujoco-2.1.1-linux-x86_64.tar.gz
tar -xf mujoco-2.1.1-linux-x86_64.tar.gz
Expand All @@ -51,12 +52,10 @@ cd $this_dir
printf "* Installing dependencies (except PyTorch)\n"
echo " - python=${PYTHON_VERSION}" >> "${this_dir}/environment.yml"
cat "${this_dir}/environment.yml"
conda env update --file "${this_dir}/environment.yml" --prune

conda install -y -c conda-forge glfw
conda install -y -c conda-forge glew
#conda install -y -c conda-forge glfw
#conda install -y -c conda-forge mesa
#conda install -y -c menpo osmesa
#conda install -y -c conda-forge glew

#yum makecache
#yum -y install glfw-devel
#yum -y install libGLEW
#yum -y install gcc-c++
conda env update --file "${this_dir}/environment.yml" --prune
2 changes: 1 addition & 1 deletion .circleci/unittest/linux_optdeps/scripts/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export MUJOCO_PY_MUJOCO_PATH=$root_dir/.mujoco/mujoco210
export DISPLAY=unix:0.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/project/.mujoco/mujoco210/bin
#MUJOCO_GL=glfw pytest --cov=torchrl --junitxml=test-results/junit.xml -v --durations 20
MUJOCO_GL=glfw pytest -v --durations 20
MUJOCO_GL=egl pytest -v --durations 20
1 change: 1 addition & 0 deletions .circleci/unittest/linux_stable/scripts/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ dependencies:
- scipy
- dm_control
- mujoco_py
- pyrender
19 changes: 17 additions & 2 deletions .circleci/unittest/linux_stable/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ printf "Installing PyTorch with %s\n" "${CU_VERSION}"
if [ "${CU_VERSION:-}" == cpu ] ; then
# conda install -y pytorch torchvision cpuonly -c pytorch-nightly
# use pip to install pytorch as conda can frequently pick older release
pip install torch torchvision
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
else
conda install -y pytorch torchvision cudatoolkit=10.2 -c pytorch
pip3 install torch torchvision torchaudio
fi

printf "Installing functorch\n"
Expand All @@ -52,3 +52,18 @@ printf "g++ version: "
gcc --version

python setup.py install

if [[ $OSTYPE == 'darwin'* ]]; then
PRIVATE_MUJOCO_GL=glfw
else
conda install -y -c conda-forge mesa
conda install -y -c menpo osmesa
PRIVATE_MUJOCO_GL=osmesa
fi

conda env config vars set MUJOCO_PY_MUJOCO_PATH=$root_dir/.mujoco/mujoco210 \
DISPLAY=unix:0.0 \
MJLIB_PATH=$root_dir/.mujoco/mujoco-2.1.1/lib/libmujoco.so.2.1.1 \
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$root_dir/.mujoco/mujoco210/bin \
SDL_VIDEODRIVER=dummy \
MUJOCO_GL=$PRIVATE_MUJOCO_GL
31 changes: 18 additions & 13 deletions .circleci/unittest/linux_stable/scripts/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ set -e
eval "$(./conda/bin/conda shell.bash hook)"
conda activate ./env

if [[ $OSTYPE == 'darwin'* ]]; then
PRIVATE_MUJOCO_GL=glfw
else
PRIVATE_MUJOCO_GL=glfw
fi

export PYTORCH_TEST_WITH_SLOW='1'
python -m torch.utils.collect_env
# Avoid error: "fatal: unsafe repository"
git config --global --add safe.directory '*'

root_dir="$(git rev-parse --show-toplevel)"
export MJLIB_PATH=$root_dir/.mujoco/mujoco-2.1.1/lib/libmujoco.so.2.1.1
export MUJOCO_PY_MUJOCO_PATH=$root_dir/.mujoco/mujoco210
export DISPLAY=unix:0.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/project/.mujoco/mujoco210/bin
env_dir="${root_dir}/env"
lib_dir="${env_dir}/lib"

# solves ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$lib_dir
#if [[ $OSTYPE == 'darwin'* ]]; then
# PRIVATE_MUJOCO_GL=glfw
#else
# PRIVATE_MUJOCO_GL=egl
#fi
## pygame.error: No available video device
#export SDL_VIDEODRIVER=dummy
## solves AttributeError: 'NoneType' object has no attribute 'eglQueryString'
#export MUJOCO_GL=$PRIVATE_MUJOCO_GL

MUJOCO_GL=$PRIVATE_MUJOCO_GL pytest test/smoke_test.py -v --durations 20
MUJOCO_GL=$PRIVATE_MUJOCO_GL pytest test/smoke_test_deps.py -v --durations 20
MUJOCO_GL=$PRIVATE_MUJOCO_GL pytest -v --durations 20
pytest test/smoke_test.py -v --durations 20
pytest test/smoke_test_deps.py -v --durations 20
pytest -v --durations 20
13 changes: 6 additions & 7 deletions .circleci/unittest/linux_stable/scripts/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ git config --global --add safe.directory '*'
root_dir="$(git rev-parse --show-toplevel)"
conda_dir="${root_dir}/conda"
env_dir="${root_dir}/env"
lib_dir="${env_dir}/lib"

cd "${root_dir}"

Expand Down Expand Up @@ -51,12 +52,10 @@ cd $this_dir
printf "* Installing dependencies (except PyTorch)\n"
echo " - python=${PYTHON_VERSION}" >> "${this_dir}/environment.yml"
cat "${this_dir}/environment.yml"
conda env update --file "${this_dir}/environment.yml" --prune

conda install -y -c conda-forge glfw
conda install -y -c conda-forge glew
#conda install -y -c conda-forge glfw
#conda install -y -c conda-forge mesa
##conda install -y -c menpo osmesa
#conda install -y -c conda-forge glew

#yum makecache
#yum -y install glfw-devel
#yum -y install libGLEW
#yum -y install gcc-c++
conda env update --file "${this_dir}/environment.yml" --prune
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ per-file-ignores =
__init__.py: F401, F403, F405
./hubconf.py: F401
test/smoke_test.py: F401
test/smoke_test_deps.py: F401
test_*.py: F841, E731, E266
exclude = venv

Expand Down
2 changes: 1 addition & 1 deletion test/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def test_imports():
TensorDict,
TensorSpec,
)
from torchrl.envs import GymLikeEnv
from torchrl.envs import TransformedEnv, Transform
from torchrl.envs.gym_like import GymLikeEnv
from torchrl.modules import TensorDictModule
from torchrl.objectives.costs.common import _LossModule
16 changes: 11 additions & 5 deletions test/smoke_test_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

from torch.utils.tensorboard import SummaryWriter
from torchrl.envs import DMControlEnv, GymEnv
from torchrl.envs.libs.dm_control import _has_dmc
from torchrl.envs.libs.gym import _has_gym


def test_dm_control():
import dm_control
import dm_env
from dm_control import suite
from dm_control.suite.wrappers import pixels

assert _has_dmc
env = DMControlEnv("cheetah", "run")
env.reset()

Expand All @@ -16,12 +24,10 @@ def test_dm_control():


def test_gym():
env = GymEnv("ALE/Pong-v5")
env.reset()
import gym


def test_gym_pixels():
env = GymEnv("ALE/Pong-v5", from_pixels=True)
assert _has_gym
env = GymEnv("ALE/Pong-v5")
env.reset()


Expand Down
55 changes: 0 additions & 55 deletions test/test_gym.py

This file was deleted.

Loading

0 comments on commit 97f4d48

Please sign in to comment.