2020-05-03 13:44:25 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ray
|
|
|
|
import ray.rllib.agents.impala as impala
|
2020-12-27 09:46:03 -05:00
|
|
|
from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
|
2020-05-03 13:44:25 +02:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2021-09-30 16:39:05 +02:00
|
|
|
from ray.rllib.utils.metrics.learner_info import LEARNER_INFO, \
|
|
|
|
LEARNER_STATS_KEY
|
2021-04-27 17:19:52 +02:00
|
|
|
from ray.rllib.utils.test_utils import check, \
|
2021-09-30 16:39:05 +02:00
|
|
|
check_compute_single_action, check_train_results, framework_iterator
|
2020-05-03 13:44:25 +02:00
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2020-05-03 13:44:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestIMPALA(unittest.TestCase):
|
|
|
|
@classmethod
|
2020-06-27 20:50:01 +02:00
|
|
|
def setUpClass(cls) -> None:
|
|
|
|
ray.init()
|
2020-05-03 13:44:25 +02:00
|
|
|
|
|
|
|
@classmethod
|
2020-06-27 20:50:01 +02:00
|
|
|
def tearDownClass(cls) -> None:
|
2020-05-03 13:44:25 +02:00
|
|
|
ray.shutdown()
|
|
|
|
|
|
|
|
def test_impala_compilation(self):
|
|
|
|
"""Test whether an ImpalaTrainer can be built with both frameworks."""
|
|
|
|
config = impala.DEFAULT_CONFIG.copy()
|
2021-08-02 17:29:59 -04:00
|
|
|
config["num_gpus"] = 0
|
2021-03-18 20:27:41 +01:00
|
|
|
config["model"]["lstm_use_prev_action"] = True
|
|
|
|
config["model"]["lstm_use_prev_reward"] = True
|
2020-05-03 13:44:25 +02:00
|
|
|
num_iterations = 1
|
2021-03-18 20:27:41 +01:00
|
|
|
env = "CartPole-v0"
|
2020-05-03 13:44:25 +02:00
|
|
|
|
2020-07-11 22:06:35 +02:00
|
|
|
for _ in framework_iterator(config):
|
2020-05-03 13:44:25 +02:00
|
|
|
local_cfg = config.copy()
|
2021-03-18 20:27:41 +01:00
|
|
|
for lstm in [False, True]:
|
|
|
|
local_cfg["num_aggregation_workers"] = 0 if not lstm else 1
|
|
|
|
local_cfg["model"]["use_lstm"] = lstm
|
2021-09-05 15:37:05 +02:00
|
|
|
print("lstm={} aggregation-workers={}".format(
|
2021-03-18 20:27:41 +01:00
|
|
|
lstm, local_cfg["num_aggregation_workers"]))
|
|
|
|
# Test with and w/o aggregation workers (this has nothing
|
|
|
|
# to do with LSTMs, though).
|
2020-05-03 13:44:25 +02:00
|
|
|
trainer = impala.ImpalaTrainer(config=local_cfg, env=env)
|
|
|
|
for i in range(num_iterations):
|
2021-09-30 16:39:05 +02:00
|
|
|
results = trainer.train()
|
|
|
|
check_train_results(results)
|
|
|
|
print(results)
|
|
|
|
|
2020-06-27 20:50:01 +02:00
|
|
|
check_compute_single_action(
|
|
|
|
trainer,
|
2021-03-18 20:27:41 +01:00
|
|
|
include_state=lstm,
|
|
|
|
include_prev_action_reward=lstm,
|
|
|
|
)
|
2020-05-11 20:24:43 -07:00
|
|
|
trainer.stop()
|
2020-05-03 13:44:25 +02:00
|
|
|
|
2020-10-27 10:00:24 +01:00
|
|
|
def test_impala_lr_schedule(self):
|
|
|
|
config = impala.DEFAULT_CONFIG.copy()
|
2021-08-02 17:29:59 -04:00
|
|
|
config["num_gpus"] = 0
|
2021-04-27 17:19:52 +02:00
|
|
|
# Test whether we correctly ignore the "lr" setting.
|
|
|
|
# The first lr should be 0.0005.
|
|
|
|
config["lr"] = 0.1
|
2020-10-27 10:00:24 +01:00
|
|
|
config["lr_schedule"] = [
|
|
|
|
[0, 0.0005],
|
|
|
|
[10000, 0.000001],
|
|
|
|
]
|
2021-07-20 14:58:13 -04:00
|
|
|
config["num_gpus"] = 0 # Do not use any (fake) GPUs.
|
2021-04-27 17:19:52 +02:00
|
|
|
config["env"] = "CartPole-v0"
|
2020-10-27 10:00:24 +01:00
|
|
|
|
|
|
|
def get_lr(result):
|
2021-09-30 16:39:05 +02:00
|
|
|
return result["info"][LEARNER_INFO][DEFAULT_POLICY_ID][
|
|
|
|
LEARNER_STATS_KEY]["cur_lr"]
|
2020-10-27 10:00:24 +01:00
|
|
|
|
2021-04-27 17:19:52 +02:00
|
|
|
for fw in framework_iterator(config, frameworks=("tf", "torch")):
|
|
|
|
trainer = impala.ImpalaTrainer(config=config)
|
|
|
|
policy = trainer.get_policy()
|
|
|
|
|
|
|
|
try:
|
|
|
|
if fw == "tf":
|
2021-07-19 13:16:03 -04:00
|
|
|
check(policy.get_session().run(policy.cur_lr), 0.0005)
|
2021-04-27 17:19:52 +02:00
|
|
|
else:
|
|
|
|
check(policy.cur_lr, 0.0005)
|
|
|
|
r1 = trainer.train()
|
|
|
|
r2 = trainer.train()
|
|
|
|
assert get_lr(r2) < get_lr(r1), (r1, r2)
|
|
|
|
finally:
|
|
|
|
trainer.stop()
|
2020-10-27 10:00:24 +01:00
|
|
|
|
2020-05-03 13:44:25 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|