2019-08-23 02:21:11 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
|
|
|
from ray import tune
|
2021-02-08 12:05:16 +01:00
|
|
|
from ray.rllib.agents.registry import get_trainer_class
|
2020-07-11 22:06:35 +02:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
|
|
|
|
|
|
|
tf1, tf, tfv = try_import_tf()
|
2019-08-23 02:21:11 -04:00
|
|
|
|
|
|
|
|
2020-07-08 16:12:20 +02:00
|
|
|
def check_support(alg, config, test_eager=False, test_trace=True):
|
2020-05-27 16:19:13 +02:00
|
|
|
config["framework"] = "tfe"
|
2020-07-08 16:12:20 +02:00
|
|
|
config["log_level"] = "ERROR"
|
2020-03-12 04:39:47 +01:00
|
|
|
# Test both continuous and discrete actions.
|
|
|
|
for cont in [True, False]:
|
|
|
|
if cont and alg in ["DQN", "APEX", "SimpleQ"]:
|
|
|
|
continue
|
|
|
|
elif not cont and alg in ["DDPG", "APEX_DDPG", "TD3"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if cont:
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
config["env"] = "Pendulum-v1"
|
2020-03-12 04:39:47 +01:00
|
|
|
else:
|
|
|
|
config["env"] = "CartPole-v0"
|
|
|
|
|
2021-02-08 12:05:16 +01:00
|
|
|
a = get_trainer_class(alg)
|
2020-07-08 16:12:20 +02:00
|
|
|
if test_eager:
|
|
|
|
print("tf-eager: alg={} cont.act={}".format(alg, cont))
|
|
|
|
config["eager_tracing"] = False
|
|
|
|
tune.run(
|
|
|
|
a, config=config, stop={"training_iteration": 1}, verbose=1)
|
2020-03-12 04:39:47 +01:00
|
|
|
if test_trace:
|
|
|
|
config["eager_tracing"] = True
|
2020-07-08 16:12:20 +02:00
|
|
|
print("tf-eager-tracing: alg={} cont.act={}".format(alg, cont))
|
|
|
|
tune.run(
|
|
|
|
a, config=config, stop={"training_iteration": 1}, verbose=1)
|
2020-03-12 04:39:47 +01:00
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
|
2020-07-08 16:12:20 +02:00
|
|
|
class TestEagerSupportPG(unittest.TestCase):
|
2019-08-23 02:21:11 -04:00
|
|
|
def setUp(self):
|
2020-07-08 16:12:20 +02:00
|
|
|
ray.init(num_cpus=4)
|
2019-08-23 02:21:11 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ray.shutdown()
|
|
|
|
|
2020-07-11 22:06:35 +02:00
|
|
|
def test_simple_q(self):
|
|
|
|
check_support("SimpleQ", {"num_workers": 0, "learning_starts": 0})
|
|
|
|
|
|
|
|
def test_dqn(self):
|
|
|
|
check_support("DQN", {"num_workers": 0, "learning_starts": 0})
|
|
|
|
|
|
|
|
def test_ddpg(self):
|
|
|
|
check_support("DDPG", {"num_workers": 0})
|
|
|
|
|
|
|
|
# TODO(sven): Add these once APEX_DDPG supports eager.
|
|
|
|
# def test_apex_ddpg(self):
|
|
|
|
# check_support("APEX_DDPG", {"num_workers": 1})
|
|
|
|
|
|
|
|
def test_td3(self):
|
|
|
|
check_support("TD3", {"num_workers": 0})
|
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_a2c(self):
|
2019-08-23 02:21:11 -04:00
|
|
|
check_support("A2C", {"num_workers": 0})
|
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_a3c(self):
|
2020-02-06 18:44:08 +01:00
|
|
|
check_support("A3C", {"num_workers": 1})
|
2019-08-23 02:21:11 -04:00
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_pg(self):
|
2019-08-23 02:21:11 -04:00
|
|
|
check_support("PG", {"num_workers": 0})
|
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_ppo(self):
|
2019-08-23 02:21:11 -04:00
|
|
|
check_support("PPO", {"num_workers": 0})
|
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_appo(self):
|
2019-08-23 02:21:11 -04:00
|
|
|
check_support("APPO", {"num_workers": 1, "num_gpus": 0})
|
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_impala(self):
|
2020-07-08 16:12:20 +02:00
|
|
|
check_support(
|
2020-08-07 16:49:49 -07:00
|
|
|
"IMPALA", {
|
|
|
|
"num_workers": 1,
|
|
|
|
"num_gpus": 0
|
|
|
|
}, test_eager=True)
|
2020-07-08 16:12:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestEagerSupportOffPolicy(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
ray.init(num_cpus=4)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_simple_q(self):
|
|
|
|
check_support("SimpleQ", {"num_workers": 0, "learning_starts": 0})
|
|
|
|
|
|
|
|
def test_dqn(self):
|
|
|
|
check_support("DQN", {"num_workers": 0, "learning_starts": 0})
|
|
|
|
|
|
|
|
def test_ddpg(self):
|
|
|
|
check_support("DDPG", {"num_workers": 0})
|
|
|
|
|
|
|
|
# def test_apex_ddpg(self):
|
|
|
|
# check_support("APEX_DDPG", {"num_workers": 1})
|
|
|
|
|
|
|
|
def test_td3(self):
|
|
|
|
check_support("TD3", {"num_workers": 0})
|
2019-08-23 02:21:11 -04:00
|
|
|
|
2020-03-12 04:39:47 +01:00
|
|
|
def test_apex_dqn(self):
|
2019-08-23 02:21:11 -04:00
|
|
|
check_support(
|
|
|
|
"APEX", {
|
|
|
|
"num_workers": 2,
|
|
|
|
"learning_starts": 0,
|
|
|
|
"num_gpus": 0,
|
|
|
|
"min_iter_time_s": 1,
|
2020-02-23 13:13:43 -08:00
|
|
|
"timesteps_per_iteration": 100,
|
|
|
|
"optimizer": {
|
|
|
|
"num_replay_buffer_shards": 1,
|
|
|
|
},
|
2019-08-23 02:21:11 -04:00
|
|
|
})
|
|
|
|
|
2020-07-08 16:12:20 +02:00
|
|
|
def test_sac(self):
|
|
|
|
check_support("SAC", {"num_workers": 0, "learning_starts": 0})
|
2020-02-22 23:19:49 +01:00
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-12-13 13:58:04 -08:00
|
|
|
import sys
|
2020-07-11 22:06:35 +02:00
|
|
|
# Don't test anything for version 2.x (all tests are eager anyways).
|
|
|
|
# TODO: (sven) remove entire file in the future.
|
|
|
|
if tfv == 2:
|
|
|
|
print("\tskip due to tf==2.x")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-07-08 16:12:20 +02:00
|
|
|
# One can specify the specific TestCase class to run.
|
|
|
|
# None for all unittest.TestCase classes in this file.
|
2020-07-11 22:06:35 +02:00
|
|
|
import pytest
|
|
|
|
class_ = sys.argv[1] if len(sys.argv) > 1 else None
|
2020-08-07 16:49:49 -07:00
|
|
|
sys.exit(
|
|
|
|
pytest.main(
|
|
|
|
["-v", __file__ + ("" if class_ is None else "::" + class_)]))
|