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
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.metrics.learner_info import LEARNER_INFO, LEARNER_STATS_KEY
|
|
|
|
from ray.rllib.utils.test_utils import (
|
|
|
|
check,
|
|
|
|
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."""
|
2022-05-02 12:05:30 +02:00
|
|
|
config = (
|
|
|
|
impala.ImpalaConfig()
|
|
|
|
.resources(num_gpus=0)
|
|
|
|
.training(
|
|
|
|
model={
|
|
|
|
"lstm_use_prev_action": True,
|
|
|
|
"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
|
|
|
|
2021-11-05 16:10:00 +01:00
|
|
|
for _ in framework_iterator(config, with_eager_tracing=True):
|
2021-03-18 20:27:41 +01:00
|
|
|
for lstm in [False, True]:
|
2022-05-02 12:05:30 +02:00
|
|
|
config.num_aggregation_workers = 0 if not lstm else 1
|
|
|
|
config.model["use_lstm"] = lstm
|
2022-01-29 18:41:57 -08:00
|
|
|
print(
|
|
|
|
"lstm={} aggregation-workers={}".format(
|
2022-05-02 12:05:30 +02:00
|
|
|
lstm, config.num_aggregation_workers
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
|
|
|
)
|
2021-03-18 20:27:41 +01:00
|
|
|
# Test with and w/o aggregation workers (this has nothing
|
|
|
|
# to do with LSTMs, though).
|
2022-05-02 12:05:30 +02:00
|
|
|
trainer = config.build(env=env)
|
2020-05-03 13:44:25 +02:00
|
|
|
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):
|
2021-04-27 17:19:52 +02:00
|
|
|
# Test whether we correctly ignore the "lr" setting.
|
2022-01-25 14:16:58 +01:00
|
|
|
# The first lr should be 0.05.
|
2022-05-02 12:05:30 +02:00
|
|
|
config = (
|
|
|
|
impala.ImpalaConfig()
|
|
|
|
.resources(num_gpus=0)
|
|
|
|
.training(
|
|
|
|
lr=0.1,
|
|
|
|
lr_schedule=[
|
|
|
|
[0, 0.05],
|
|
|
|
[10000, 0.000001],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
config.environment(env="CartPole-v0")
|
2020-10-27 10:00:24 +01:00
|
|
|
|
|
|
|
def get_lr(result):
|
2022-01-29 18:41:57 -08:00
|
|
|
return result["info"][LEARNER_INFO][DEFAULT_POLICY_ID][LEARNER_STATS_KEY][
|
|
|
|
"cur_lr"
|
|
|
|
]
|
2020-10-27 10:00:24 +01:00
|
|
|
|
2022-01-25 14:16:58 +01:00
|
|
|
for fw in framework_iterator(config):
|
2022-05-02 12:05:30 +02:00
|
|
|
trainer = config.build()
|
2021-04-27 17:19:52 +02:00
|
|
|
policy = trainer.get_policy()
|
|
|
|
|
|
|
|
try:
|
|
|
|
if fw == "tf":
|
2022-01-25 14:16:58 +01:00
|
|
|
check(policy.get_session().run(policy.cur_lr), 0.05)
|
2021-04-27 17:19:52 +02:00
|
|
|
else:
|
2022-01-25 14:16:58 +01:00
|
|
|
check(policy.cur_lr, 0.05)
|
2021-04-27 17:19:52 +02:00
|
|
|
r1 = trainer.train()
|
|
|
|
r2 = trainer.train()
|
2022-01-25 14:16:58 +01:00
|
|
|
r3 = trainer.train()
|
|
|
|
# Due to the asynch'ness of IMPALA, learner-stats metrics
|
|
|
|
# could be delayed by one iteration. Do 3 train() calls here
|
|
|
|
# and measure guaranteed decrease in lr between 1st and 3rd.
|
|
|
|
lr1 = get_lr(r1)
|
|
|
|
lr2 = get_lr(r2)
|
|
|
|
lr3 = get_lr(r3)
|
|
|
|
assert lr2 <= lr1, (lr1, lr2)
|
|
|
|
assert lr3 <= lr2, (lr2, lr3)
|
|
|
|
assert lr3 < lr1, (lr1, lr3)
|
2021-04-27 17:19:52 +02:00
|
|
|
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
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2020-05-03 13:44:25 +02:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|