test_supported_spaces.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import logging
  2. import unittest
  3. import ray
  4. from ray.rllib.algorithms.a3c import A3CConfig
  5. from ray.rllib.algorithms.appo import APPOConfig
  6. from ray.rllib.algorithms.ars import ARSConfig
  7. from ray.rllib.algorithms.ddpg import DDPGConfig
  8. from ray.rllib.algorithms.dqn import DQNConfig
  9. from ray.rllib.algorithms.es import ESConfig
  10. from ray.rllib.algorithms.impala import ImpalaConfig
  11. from ray.rllib.algorithms.ppo import PPOConfig
  12. from ray.rllib.algorithms.sac import SACConfig
  13. from ray.rllib.utils.test_utils import check_supported_spaces
  14. logger = logging.getLogger(__name__)
  15. class TestSupportedSpacesIMPALA(unittest.TestCase):
  16. @classmethod
  17. def setUpClass(cls) -> None:
  18. ray.init()
  19. @classmethod
  20. def tearDownClass(cls) -> None:
  21. ray.shutdown()
  22. def test_impala(self):
  23. check_supported_spaces(
  24. "IMPALA",
  25. (
  26. ImpalaConfig()
  27. .resources(num_gpus=0)
  28. .training(model={"fcnet_hiddens": [10]})
  29. ),
  30. )
  31. class TestSupportedSpacesAPPO(unittest.TestCase):
  32. @classmethod
  33. def setUpClass(cls) -> None:
  34. ray.init()
  35. @classmethod
  36. def tearDownClass(cls) -> None:
  37. ray.shutdown()
  38. def test_appo(self):
  39. config = (
  40. APPOConfig()
  41. .resources(num_gpus=0)
  42. .training(vtrace=False, model={"fcnet_hiddens": [10]})
  43. )
  44. config.training(vtrace=True)
  45. check_supported_spaces("APPO", config)
  46. class TestSupportedSpacesA3C(unittest.TestCase):
  47. @classmethod
  48. def setUpClass(cls) -> None:
  49. ray.init()
  50. @classmethod
  51. def tearDownClass(cls) -> None:
  52. ray.shutdown()
  53. def test_a3c(self):
  54. config = (
  55. A3CConfig()
  56. .rollouts(num_rollout_workers=1)
  57. .training(
  58. optimizer={"grads_per_step": 1},
  59. model={"fcnet_hiddens": [10]},
  60. )
  61. )
  62. check_supported_spaces("A3C", config, check_bounds=True)
  63. class TestSupportedSpacesPPO(unittest.TestCase):
  64. @classmethod
  65. def setUpClass(cls) -> None:
  66. ray.init()
  67. @classmethod
  68. def tearDownClass(cls) -> None:
  69. ray.shutdown()
  70. def test_ppo(self):
  71. config = (
  72. PPOConfig()
  73. .rollouts(num_rollout_workers=2, rollout_fragment_length=50)
  74. .training(
  75. train_batch_size=100,
  76. num_sgd_iter=1,
  77. sgd_minibatch_size=50,
  78. model={
  79. "fcnet_hiddens": [10],
  80. },
  81. )
  82. )
  83. check_supported_spaces("PPO", config, check_bounds=True)
  84. class TestSupportedSpacesPPONoPreprocessorGPU(unittest.TestCase):
  85. @classmethod
  86. def setUpClass(cls) -> None:
  87. ray.init(num_gpus=1)
  88. @classmethod
  89. def tearDownClass(cls) -> None:
  90. ray.shutdown()
  91. def test_ppo_no_preprocessors_gpu(self):
  92. # Same test as test_ppo, but also test if we are able to move models and tensors
  93. # on the same device when not using preprocessors.
  94. # (Artur) This covers a superposition of these edge cases that can lead to
  95. # obscure errors.
  96. config = (
  97. PPOConfig()
  98. .rollouts(num_rollout_workers=2, rollout_fragment_length=50)
  99. .training(
  100. train_batch_size=100,
  101. num_sgd_iter=1,
  102. sgd_minibatch_size=50,
  103. model={
  104. "fcnet_hiddens": [10],
  105. },
  106. )
  107. .experimental(_disable_preprocessor_api=True)
  108. .resources(num_gpus=1)
  109. )
  110. # (Artur): This test only works under the old ModelV2 API because we
  111. # don't offer arbitrarily complex Models under the RLModules API without
  112. # preprocessors. Such input spaces require custom implementations of the
  113. # input space.
  114. # TODO (Artur): Delete this test once we remove ModelV2 API.
  115. config.rl_module(_enable_rl_module_api=False).training(
  116. _enable_learner_api=False
  117. )
  118. check_supported_spaces(
  119. "PPO",
  120. config,
  121. check_bounds=True,
  122. frameworks=["torch", "tf"],
  123. use_gpu=True,
  124. )
  125. class TestSupportedSpacesDQN(unittest.TestCase):
  126. @classmethod
  127. def setUpClass(cls) -> None:
  128. ray.init()
  129. @classmethod
  130. def tearDownClass(cls) -> None:
  131. ray.shutdown()
  132. def test_dqn(self):
  133. config = (
  134. DQNConfig()
  135. .reporting(min_sample_timesteps_per_iteration=1)
  136. .training(
  137. replay_buffer_config={
  138. "capacity": 1000,
  139. }
  140. )
  141. )
  142. check_supported_spaces("DQN", config, frameworks=["tf2", "torch", "tf"])
  143. class TestSupportedSpacesOffPolicy(unittest.TestCase):
  144. @classmethod
  145. def setUpClass(cls) -> None:
  146. ray.init(num_cpus=4)
  147. @classmethod
  148. def tearDownClass(cls) -> None:
  149. ray.shutdown()
  150. def test_ddpg(self):
  151. check_supported_spaces(
  152. "DDPG",
  153. DDPGConfig()
  154. .exploration(exploration_config={"ou_base_scale": 100.0})
  155. .reporting(min_sample_timesteps_per_iteration=1)
  156. .training(
  157. replay_buffer_config={"capacity": 1000},
  158. use_state_preprocessor=True,
  159. ),
  160. check_bounds=True,
  161. )
  162. def test_sac(self):
  163. check_supported_spaces(
  164. "SAC",
  165. SACConfig().training(replay_buffer_config={"capacity": 1000}),
  166. check_bounds=True,
  167. )
  168. class TestSupportedSpacesEvolutionAlgos(unittest.TestCase):
  169. @classmethod
  170. def setUpClass(cls) -> None:
  171. ray.init(num_cpus=4)
  172. @classmethod
  173. def tearDownClass(cls) -> None:
  174. ray.shutdown()
  175. def test_ars(self):
  176. check_supported_spaces(
  177. "ARS",
  178. ARSConfig()
  179. .rollouts(num_rollout_workers=1)
  180. .training(noise_size=1500000, num_rollouts=1, rollouts_used=1),
  181. # framework=None corresponds to numpy since ARS uses a numpy policy
  182. frameworks=[None],
  183. )
  184. def test_es(self):
  185. check_supported_spaces(
  186. "ES",
  187. ESConfig()
  188. .rollouts(num_rollout_workers=1)
  189. .training(noise_size=1500000, episodes_per_batch=1, train_batch_size=1),
  190. # framework=None corresponds to numpy since ES uses a numpy policy
  191. frameworks=[None],
  192. )
  193. if __name__ == "__main__":
  194. import pytest
  195. import sys
  196. # One can specify the specific TestCase class to run.
  197. # None for all unittest.TestCase classes in this file.
  198. class_ = sys.argv[1] if len(sys.argv) > 1 else None
  199. sys.exit(pytest.main(["-v", __file__ + ("" if class_ is None else "::" + class_)]))