2020-05-27 16:19:13 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
2022-06-11 15:10:39 +02:00
|
|
|
from ray.rllib.algorithms.registry import get_algorithm_class
|
2021-02-10 15:21:46 +01:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2022-06-23 14:52:46 +01:00
|
|
|
from ray.tune.experiment.trial import ExportFormat
|
2020-05-27 16:19:13 +02:00
|
|
|
|
2021-02-10 15:21:46 +01:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
CONFIGS = {
|
|
|
|
"A3C": {
|
|
|
|
"explore": False,
|
|
|
|
"num_workers": 1,
|
|
|
|
},
|
|
|
|
"APEX_DDPG": {
|
|
|
|
"explore": False,
|
|
|
|
"observation_filter": "MeanStdFilter",
|
|
|
|
"num_workers": 2,
|
2022-06-10 17:09:18 +02:00
|
|
|
"min_time_s_per_iteration": 1,
|
2020-05-27 16:19:13 +02:00
|
|
|
"optimizer": {
|
|
|
|
"num_replay_buffer_shards": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"ARS": {
|
|
|
|
"explore": False,
|
|
|
|
"num_rollouts": 10,
|
|
|
|
"num_workers": 2,
|
|
|
|
"noise_size": 2500000,
|
|
|
|
"observation_filter": "MeanStdFilter",
|
|
|
|
},
|
|
|
|
"DDPG": {
|
|
|
|
"explore": False,
|
2022-06-10 17:09:18 +02:00
|
|
|
"min_sample_timesteps_per_iteration": 100,
|
2020-05-27 16:19:13 +02:00
|
|
|
},
|
|
|
|
"DQN": {
|
|
|
|
"explore": False,
|
|
|
|
},
|
|
|
|
"ES": {
|
|
|
|
"explore": False,
|
|
|
|
"episodes_per_batch": 10,
|
|
|
|
"train_batch_size": 100,
|
|
|
|
"num_workers": 2,
|
|
|
|
"noise_size": 2500000,
|
|
|
|
"observation_filter": "MeanStdFilter",
|
|
|
|
},
|
|
|
|
"PPO": {
|
|
|
|
"explore": False,
|
|
|
|
"num_sgd_iter": 5,
|
|
|
|
"train_batch_size": 1000,
|
|
|
|
"num_workers": 2,
|
|
|
|
},
|
|
|
|
"SAC": {
|
|
|
|
"explore": False,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-22 17:09:40 +01:00
|
|
|
def export_test(alg_name, failures, framework="tf"):
|
2020-05-27 16:19:13 +02:00
|
|
|
def valid_tf_model(model_dir):
|
|
|
|
return os.path.exists(os.path.join(model_dir, "saved_model.pb")) and os.listdir(
|
|
|
|
os.path.join(model_dir, "variables")
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
def valid_tf_checkpoint(checkpoint_dir):
|
|
|
|
return (
|
|
|
|
os.path.exists(os.path.join(checkpoint_dir, "model.meta"))
|
|
|
|
and os.path.exists(os.path.join(checkpoint_dir, "model.index"))
|
|
|
|
and os.path.exists(os.path.join(checkpoint_dir, "checkpoint"))
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
2020-05-27 16:19:13 +02:00
|
|
|
|
2022-06-11 15:10:39 +02:00
|
|
|
cls = get_algorithm_class(alg_name)
|
2021-02-22 17:09:40 +01:00
|
|
|
config = CONFIGS[alg_name].copy()
|
|
|
|
config["framework"] = framework
|
2020-05-27 16:19:13 +02:00
|
|
|
if "DDPG" in alg_name or "SAC" in alg_name:
|
[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
|
|
|
algo = cls(config=config, env="Pendulum-v1")
|
2020-05-27 16:19:13 +02:00
|
|
|
else:
|
2021-02-22 17:09:40 +01:00
|
|
|
algo = cls(config=config, env="CartPole-v0")
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
for _ in range(1):
|
|
|
|
res = algo.train()
|
|
|
|
print("current status: " + str(res))
|
|
|
|
|
2021-03-10 23:47:28 -07:00
|
|
|
export_dir = os.path.join(
|
|
|
|
ray._private.utils.get_user_temp_dir(), "export_dir_%s" % alg_name
|
2020-05-27 16:19:13 +02:00
|
|
|
)
|
|
|
|
print("Exporting model ", alg_name, export_dir)
|
|
|
|
algo.export_policy_model(export_dir)
|
2021-02-22 17:09:40 +01:00
|
|
|
if framework == "tf" and not valid_tf_model(export_dir):
|
2020-05-27 16:19:13 +02:00
|
|
|
failures.append(alg_name)
|
|
|
|
shutil.rmtree(export_dir)
|
|
|
|
|
2021-02-22 17:09:40 +01:00
|
|
|
if framework == "tf":
|
|
|
|
print("Exporting checkpoint", alg_name, export_dir)
|
|
|
|
algo.export_policy_checkpoint(export_dir)
|
|
|
|
if framework == "tf" and not valid_tf_checkpoint(export_dir):
|
|
|
|
failures.append(alg_name)
|
|
|
|
shutil.rmtree(export_dir)
|
2020-05-27 16:19:13 +02:00
|
|
|
|
2021-02-22 17:09:40 +01:00
|
|
|
print("Exporting default policy", alg_name, export_dir)
|
|
|
|
algo.export_model([ExportFormat.CHECKPOINT, ExportFormat.MODEL], export_dir)
|
|
|
|
if not valid_tf_model(
|
|
|
|
os.path.join(export_dir, ExportFormat.MODEL)
|
|
|
|
) or not valid_tf_checkpoint(os.path.join(export_dir, ExportFormat.CHECKPOINT)):
|
|
|
|
failures.append(alg_name)
|
2021-02-10 15:21:46 +01:00
|
|
|
|
2021-02-22 17:09:40 +01:00
|
|
|
# Test loading the exported model.
|
|
|
|
model = tf.saved_model.load(os.path.join(export_dir, ExportFormat.MODEL))
|
|
|
|
assert model
|
2021-02-10 15:21:46 +01:00
|
|
|
|
2021-02-22 17:09:40 +01:00
|
|
|
shutil.rmtree(export_dir)
|
2021-03-29 20:07:44 +02:00
|
|
|
algo.stop()
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestExport(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
2021-03-29 20:07:44 +02:00
|
|
|
ray.init(num_cpus=4)
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
|
|
|
ray.shutdown()
|
|
|
|
|
2021-03-29 20:07:44 +02:00
|
|
|
def test_export_a3c(self):
|
|
|
|
failures = []
|
|
|
|
export_test("A3C", failures, "tf")
|
|
|
|
assert not failures, failures
|
|
|
|
|
|
|
|
def test_export_ddpg(self):
|
|
|
|
failures = []
|
|
|
|
export_test("DDPG", failures, "tf")
|
|
|
|
assert not failures, failures
|
|
|
|
|
|
|
|
def test_export_dqn(self):
|
|
|
|
failures = []
|
|
|
|
export_test("DQN", failures, "tf")
|
|
|
|
assert not failures, failures
|
|
|
|
|
2021-02-22 17:09:40 +01:00
|
|
|
def test_export_ppo(self):
|
|
|
|
failures = []
|
|
|
|
export_test("PPO", failures, "torch")
|
|
|
|
export_test("PPO", failures, "tf")
|
|
|
|
assert not failures, failures
|
|
|
|
|
2021-03-29 20:07:44 +02:00
|
|
|
def test_export_sac(self):
|
2020-05-27 16:19:13 +02:00
|
|
|
failures = []
|
2021-03-29 20:07:44 +02:00
|
|
|
export_test("SAC", failures, "tf")
|
2020-05-27 16:19:13 +02:00
|
|
|
assert not failures, failures
|
2021-03-25 00:42:01 -07:00
|
|
|
print("All export tests passed!")
|
2020-05-27 16:19:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-05-27 16:19:13 +02:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|