test_utils.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. from collections import Counter
  2. import copy
  3. from gym.spaces import Box
  4. import logging
  5. import numpy as np
  6. import random
  7. import re
  8. import time
  9. import tree # pip install dm_tree
  10. from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
  11. import yaml
  12. import ray
  13. from ray.rllib.utils.framework import try_import_jax, try_import_tf, \
  14. try_import_torch
  15. from ray.rllib.utils.typing import PartialTrainerConfigDict
  16. from ray.tune import CLIReporter, run_experiments
  17. jax, _ = try_import_jax()
  18. tf1, tf, tfv = try_import_tf()
  19. if tf1:
  20. eager_mode = None
  21. try:
  22. from tensorflow.python.eager.context import eager_mode
  23. except (ImportError, ModuleNotFoundError):
  24. pass
  25. torch, _ = try_import_torch()
  26. logger = logging.getLogger(__name__)
  27. def framework_iterator(
  28. config: Optional[PartialTrainerConfigDict] = None,
  29. frameworks: Sequence[str] = ("tf2", "tf", "tfe", "torch"),
  30. session: bool = False,
  31. with_eager_tracing: bool = False,
  32. time_iterations: Optional[dict] = None,
  33. ) -> Union[str, Tuple[str, Optional["tf1.Session"]]]:
  34. """An generator that allows for looping through n frameworks for testing.
  35. Provides the correct config entries ("framework") as well
  36. as the correct eager/non-eager contexts for tfe/tf.
  37. Args:
  38. config: An optional config dict to alter in place depending on the
  39. iteration.
  40. frameworks: A list/tuple of the frameworks to be tested.
  41. Allowed are: "tf2", "tf", "tfe", "torch", and None.
  42. session: If True and only in the tf-case: Enter a tf.Session()
  43. and yield that as second return value (otherwise yield (fw, None)).
  44. Also sets a seed (42) on the session to make the test
  45. deterministic.
  46. with_eager_tracing: Include `eager_tracing=True` in the returned
  47. configs, when framework=[tfe|tf2].
  48. time_iterations: If provided, will write to the given dict (by
  49. framework key) the times in seconds that each (framework's)
  50. iteration takes.
  51. Yields:
  52. If `session` is False: The current framework [tf2|tf|tfe|torch] used.
  53. If `session` is True: A tuple consisting of the current framework
  54. string and the tf1.Session (if fw="tf", otherwise None).
  55. """
  56. config = config or {}
  57. frameworks = [frameworks] if isinstance(frameworks, str) else \
  58. list(frameworks)
  59. # Both tf2 and tfe present -> remove "tfe" or "tf2" depending on version.
  60. if "tf2" in frameworks and "tfe" in frameworks:
  61. frameworks.remove("tfe" if tfv == 2 else "tf2")
  62. for fw in frameworks:
  63. # Skip non-installed frameworks.
  64. if fw == "torch" and not torch:
  65. logger.warning(
  66. "framework_iterator skipping torch (not installed)!")
  67. continue
  68. if fw != "torch" and not tf:
  69. logger.warning("framework_iterator skipping {} (tf not "
  70. "installed)!".format(fw))
  71. continue
  72. elif fw == "tfe" and not eager_mode:
  73. logger.warning("framework_iterator skipping tf-eager (could not "
  74. "import `eager_mode` from tensorflow.python)!")
  75. continue
  76. elif fw == "tf2" and tfv != 2:
  77. logger.warning(
  78. "framework_iterator skipping tf2.x (tf version is < 2.0)!")
  79. continue
  80. elif fw == "jax" and not jax:
  81. logger.warning("framework_iterator skipping JAX (not installed)!")
  82. continue
  83. assert fw in ["tf2", "tf", "tfe", "torch", "jax", None]
  84. # Do we need a test session?
  85. sess = None
  86. if fw == "tf" and session is True:
  87. sess = tf1.Session()
  88. sess.__enter__()
  89. tf1.set_random_seed(42)
  90. config["framework"] = fw
  91. eager_ctx = None
  92. # Enable eager mode for tf2 and tfe.
  93. if fw in ["tf2", "tfe"]:
  94. eager_ctx = eager_mode()
  95. eager_ctx.__enter__()
  96. assert tf1.executing_eagerly()
  97. # Make sure, eager mode is off.
  98. elif fw == "tf":
  99. assert not tf1.executing_eagerly()
  100. # Additionally loop through eager_tracing=True + False, if necessary.
  101. if fw in ["tf2", "tfe"] and with_eager_tracing:
  102. for tracing in [True, False]:
  103. config["eager_tracing"] = tracing
  104. print(f"framework={fw} (eager-tracing={tracing})")
  105. time_started = time.time()
  106. yield fw if session is False else (fw, sess)
  107. if time_iterations is not None:
  108. time_total = time.time() - time_started
  109. time_iterations[fw + ("+tracing" if tracing else "")] = \
  110. time_total
  111. print(f".. took {time_total}sec")
  112. config["eager_tracing"] = False
  113. # Yield current framework + tf-session (if necessary).
  114. else:
  115. print(f"framework={fw}")
  116. time_started = time.time()
  117. yield fw if session is False else (fw, sess)
  118. if time_iterations is not None:
  119. time_total = time.time() - time_started
  120. time_iterations[fw + ("+tracing" if tracing else "")] = \
  121. time_total
  122. print(f".. took {time_total}sec")
  123. # Exit any context we may have entered.
  124. if eager_ctx:
  125. eager_ctx.__exit__(None, None, None)
  126. elif sess:
  127. sess.__exit__(None, None, None)
  128. def check(x, y, decimals=5, atol=None, rtol=None, false=False):
  129. """
  130. Checks two structures (dict, tuple, list,
  131. np.array, float, int, etc..) for (almost) numeric identity.
  132. All numbers in the two structures have to match up to `decimal` digits
  133. after the floating point. Uses assertions.
  134. Args:
  135. x (any): The value to be compared (to the expectation: `y`). This
  136. may be a Tensor.
  137. y (any): The expected value to be compared to `x`. This must not
  138. be a tf-Tensor, but may be a tfe/torch-Tensor.
  139. decimals (int): The number of digits after the floating point up to
  140. which all numeric values have to match.
  141. atol (float): Absolute tolerance of the difference between x and y
  142. (overrides `decimals` if given).
  143. rtol (float): Relative tolerance of the difference between x and y
  144. (overrides `decimals` if given).
  145. false (bool): Whether to check that x and y are NOT the same.
  146. """
  147. # A dict type.
  148. if isinstance(x, dict):
  149. assert isinstance(y, dict), \
  150. "ERROR: If x is dict, y needs to be a dict as well!"
  151. y_keys = set(x.keys())
  152. for key, value in x.items():
  153. assert key in y, \
  154. "ERROR: y does not have x's key='{}'! y={}".format(key, y)
  155. check(
  156. value,
  157. y[key],
  158. decimals=decimals,
  159. atol=atol,
  160. rtol=rtol,
  161. false=false)
  162. y_keys.remove(key)
  163. assert not y_keys, \
  164. "ERROR: y contains keys ({}) that are not in x! y={}".\
  165. format(list(y_keys), y)
  166. # A tuple type.
  167. elif isinstance(x, (tuple, list)):
  168. assert isinstance(y, (tuple, list)),\
  169. "ERROR: If x is tuple, y needs to be a tuple as well!"
  170. assert len(y) == len(x),\
  171. "ERROR: y does not have the same length as x ({} vs {})!".\
  172. format(len(y), len(x))
  173. for i, value in enumerate(x):
  174. check(
  175. value,
  176. y[i],
  177. decimals=decimals,
  178. atol=atol,
  179. rtol=rtol,
  180. false=false)
  181. # Boolean comparison.
  182. elif isinstance(x, (np.bool_, bool)):
  183. if false is True:
  184. assert bool(x) is not bool(y), \
  185. "ERROR: x ({}) is y ({})!".format(x, y)
  186. else:
  187. assert bool(x) is bool(y), \
  188. "ERROR: x ({}) is not y ({})!".format(x, y)
  189. # Nones or primitives.
  190. elif x is None or y is None or isinstance(x, (str, int)):
  191. if false is True:
  192. assert x != y, "ERROR: x ({}) is the same as y ({})!".format(x, y)
  193. else:
  194. assert x == y, \
  195. "ERROR: x ({}) is not the same as y ({})!".format(x, y)
  196. # String/byte comparisons.
  197. elif hasattr(x, "dtype") and \
  198. (x.dtype == np.object or str(x.dtype).startswith("<U")):
  199. try:
  200. np.testing.assert_array_equal(x, y)
  201. if false is True:
  202. assert False, \
  203. "ERROR: x ({}) is the same as y ({})!".format(x, y)
  204. except AssertionError as e:
  205. if false is False:
  206. raise e
  207. # Everything else (assume numeric or tf/torch.Tensor).
  208. else:
  209. if tf1 is not None:
  210. # y should never be a Tensor (y=expected value).
  211. if isinstance(y, (tf1.Tensor, tf1.Variable)):
  212. # In eager mode, numpyize tensors.
  213. if tf.executing_eagerly():
  214. y = y.numpy()
  215. else:
  216. raise ValueError(
  217. "`y` (expected value) must not be a Tensor. "
  218. "Use numpy.ndarray instead")
  219. if isinstance(x, (tf1.Tensor, tf1.Variable)):
  220. # In eager mode, numpyize tensors.
  221. if tf1.executing_eagerly():
  222. x = x.numpy()
  223. # Otherwise, use a new tf-session.
  224. else:
  225. with tf1.Session() as sess:
  226. x = sess.run(x)
  227. return check(
  228. x,
  229. y,
  230. decimals=decimals,
  231. atol=atol,
  232. rtol=rtol,
  233. false=false)
  234. if torch is not None:
  235. if isinstance(x, torch.Tensor):
  236. x = x.detach().cpu().numpy()
  237. if isinstance(y, torch.Tensor):
  238. y = y.detach().cpu().numpy()
  239. # Using decimals.
  240. if atol is None and rtol is None:
  241. # Assert equality of both values.
  242. try:
  243. np.testing.assert_almost_equal(x, y, decimal=decimals)
  244. # Both values are not equal.
  245. except AssertionError as e:
  246. # Raise error in normal case.
  247. if false is False:
  248. raise e
  249. # Both values are equal.
  250. else:
  251. # If false is set -> raise error (not expected to be equal).
  252. if false is True:
  253. assert False, \
  254. "ERROR: x ({}) is the same as y ({})!".format(x, y)
  255. # Using atol/rtol.
  256. else:
  257. # Provide defaults for either one of atol/rtol.
  258. if atol is None:
  259. atol = 0
  260. if rtol is None:
  261. rtol = 1e-7
  262. try:
  263. np.testing.assert_allclose(x, y, atol=atol, rtol=rtol)
  264. except AssertionError as e:
  265. if false is False:
  266. raise e
  267. else:
  268. if false is True:
  269. assert False, \
  270. "ERROR: x ({}) is the same as y ({})!".format(x, y)
  271. def check_compute_single_action(trainer,
  272. include_state=False,
  273. include_prev_action_reward=False):
  274. """Tests different combinations of args for trainer.compute_single_action.
  275. Args:
  276. trainer: The Trainer object to test.
  277. include_state: Whether to include the initial state of the Policy's
  278. Model in the `compute_single_action` call.
  279. include_prev_action_reward: Whether to include the prev-action and
  280. -reward in the `compute_single_action` call.
  281. Raises:
  282. ValueError: If anything unexpected happens.
  283. """
  284. # Have to import this here to avoid circular dependency.
  285. from ray.rllib.policy.sample_batch import SampleBatch
  286. # Some Trainers may not abide to the standard API.
  287. try:
  288. pol = trainer.get_policy()
  289. except AttributeError:
  290. pol = trainer.policy
  291. # Get the policy's model.
  292. model = pol.model
  293. action_space = pol.action_space
  294. def _test(what, method_to_test, obs_space, full_fetch, explore, timestep,
  295. unsquash, clip):
  296. call_kwargs = {}
  297. if what is trainer:
  298. call_kwargs["full_fetch"] = full_fetch
  299. obs = obs_space.sample()
  300. if isinstance(obs_space, Box):
  301. obs = np.clip(obs, -1.0, 1.0)
  302. state_in = None
  303. if include_state:
  304. state_in = model.get_initial_state()
  305. if not state_in:
  306. state_in = []
  307. i = 0
  308. while f"state_in_{i}" in model.view_requirements:
  309. state_in.append(model.view_requirements[f"state_in_{i}"]
  310. .space.sample())
  311. i += 1
  312. action_in = action_space.sample() \
  313. if include_prev_action_reward else None
  314. reward_in = 1.0 if include_prev_action_reward else None
  315. if method_to_test == "input_dict":
  316. assert what is pol
  317. input_dict = {SampleBatch.OBS: obs}
  318. if include_prev_action_reward:
  319. input_dict[SampleBatch.PREV_ACTIONS] = action_in
  320. input_dict[SampleBatch.PREV_REWARDS] = reward_in
  321. if state_in:
  322. for i, s in enumerate(state_in):
  323. input_dict[f"state_in_{i}"] = s
  324. input_dict_batched = SampleBatch(
  325. tree.map_structure(lambda s: np.expand_dims(s, 0), input_dict))
  326. action = pol.compute_actions_from_input_dict(
  327. input_dict=input_dict_batched,
  328. explore=explore,
  329. timestep=timestep,
  330. **call_kwargs)
  331. # Unbatch everything to be able to compare against single
  332. # action below.
  333. # ARS and ES return action batches as lists.
  334. if isinstance(action[0], list):
  335. action = (np.array(action[0]), action[1], action[2])
  336. action = tree.map_structure(lambda s: s[0], action)
  337. try:
  338. action2 = pol.compute_single_action(
  339. input_dict=input_dict,
  340. explore=explore,
  341. timestep=timestep,
  342. **call_kwargs)
  343. # Make sure these are the same, unless we have exploration
  344. # switched on (or noisy layers).
  345. if not explore and not pol.config.get("noisy"):
  346. check(action, action2)
  347. except TypeError:
  348. pass
  349. else:
  350. action = what.compute_single_action(
  351. obs,
  352. state_in,
  353. prev_action=action_in,
  354. prev_reward=reward_in,
  355. explore=explore,
  356. timestep=timestep,
  357. unsquash_action=unsquash,
  358. clip_action=clip,
  359. **call_kwargs)
  360. state_out = None
  361. if state_in or full_fetch or what is pol:
  362. action, state_out, _ = action
  363. if state_out:
  364. for si, so in zip(state_in, state_out):
  365. check(list(si.shape), so.shape)
  366. # Test whether unsquash/clipping works on the Trainer's
  367. # compute_single_action method: Both flags should force the action
  368. # to be within the space's bounds.
  369. if method_to_test == "single" and what == trainer:
  370. if not action_space.contains(action) and \
  371. (clip or unsquash or not isinstance(action_space, Box)):
  372. raise ValueError(
  373. f"Returned action ({action}) of trainer/policy {what} "
  374. f"not in Env's action_space {action_space}")
  375. # We are operating in normalized space: Expect only smaller action
  376. # values.
  377. if isinstance(action_space, Box) and not unsquash and \
  378. what.config.get("normalize_actions") and \
  379. np.any(np.abs(action) > 3.0):
  380. raise ValueError(
  381. f"Returned action ({action}) of trainer/policy {what} "
  382. "should be in normalized space, but seems too large/small "
  383. "for that!")
  384. # Loop through: Policy vs Trainer; Different API methods to calculate
  385. # actions; unsquash option; clip option; full fetch or not.
  386. for what in [pol, trainer]:
  387. if what is trainer:
  388. # Get the obs-space from Workers.env (not Policy) due to possible
  389. # pre-processor up front.
  390. worker_set = getattr(trainer, "workers",
  391. getattr(trainer, "_workers", None))
  392. assert worker_set
  393. if isinstance(worker_set, list):
  394. obs_space = trainer.get_policy().observation_space
  395. else:
  396. obs_space = worker_set.local_worker().for_policy(
  397. lambda p: p.observation_space)
  398. obs_space = getattr(obs_space, "original_space", obs_space)
  399. else:
  400. obs_space = pol.observation_space
  401. for method_to_test in ["single"] + \
  402. (["input_dict"] if what is pol else []):
  403. for explore in [True, False]:
  404. for full_fetch in ([False, True]
  405. if what is trainer else [False]):
  406. timestep = random.randint(0, 100000)
  407. for unsquash in [True, False]:
  408. for clip in ([False] if unsquash else [True, False]):
  409. _test(what, method_to_test, obs_space, full_fetch,
  410. explore, timestep, unsquash, clip)
  411. def check_learning_achieved(tune_results, min_reward, evaluation=False):
  412. """Throws an error if `min_reward` is not reached within tune_results.
  413. Checks the last iteration found in tune_results for its
  414. "episode_reward_mean" value and compares it to `min_reward`.
  415. Args:
  416. tune_results: The tune.run returned results object.
  417. min_reward (float): The min reward that must be reached.
  418. Raises:
  419. ValueError: If `min_reward` not reached.
  420. """
  421. # Get maximum reward of all trials
  422. # (check if at least one trial achieved some learning)
  423. avg_rewards = [(trial.last_result["episode_reward_mean"]
  424. if not evaluation else
  425. trial.last_result["evaluation"]["episode_reward_mean"])
  426. for trial in tune_results.trials]
  427. best_avg_reward = max(avg_rewards)
  428. if best_avg_reward < min_reward:
  429. raise ValueError("`stop-reward` of {} not reached!".format(min_reward))
  430. print("ok")
  431. def check_train_results(train_results):
  432. """Checks proper structure of a Trainer.train() returned dict.
  433. Args:
  434. train_results: The train results dict to check.
  435. Raises:
  436. AssertionError: If `train_results` doesn't have the proper structure or
  437. data in it.
  438. """
  439. # Import these here to avoid circular dependencies.
  440. from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
  441. from ray.rllib.utils.metrics.learner_info import LEARNER_INFO, \
  442. LEARNER_STATS_KEY
  443. from ray.rllib.utils.multi_agent import check_multi_agent
  444. # Assert that some keys are where we would expect them.
  445. for key in [
  446. "agent_timesteps_total",
  447. "config",
  448. "custom_metrics",
  449. "episode_len_mean",
  450. "episode_reward_max",
  451. "episode_reward_mean",
  452. "episode_reward_min",
  453. "episodes_total",
  454. "hist_stats",
  455. "info",
  456. "iterations_since_restore",
  457. "num_healthy_workers",
  458. "perf",
  459. "policy_reward_max",
  460. "policy_reward_mean",
  461. "policy_reward_min",
  462. "sampler_perf",
  463. "time_since_restore",
  464. "time_this_iter_s",
  465. "timesteps_since_restore",
  466. "timesteps_total",
  467. "timers",
  468. "time_total_s",
  469. "training_iteration",
  470. ]:
  471. assert key in train_results, \
  472. f"'{key}' not found in `train_results` ({train_results})!"
  473. _, is_multi_agent = check_multi_agent(train_results["config"])
  474. # Check in particular the "info" dict.
  475. info = train_results["info"]
  476. assert LEARNER_INFO in info, \
  477. f"'learner' not in train_results['infos'] ({info})!"
  478. assert "num_steps_trained" in info,\
  479. f"'num_steps_trained' not in train_results['infos'] ({info})!"
  480. learner_info = info[LEARNER_INFO]
  481. # Make sure we have a default_policy key if we are not in a
  482. # multi-agent setup.
  483. if not is_multi_agent:
  484. # APEX algos sometimes have an empty learner info dict (no metrics
  485. # collected yet).
  486. assert len(learner_info) == 0 or DEFAULT_POLICY_ID in learner_info, \
  487. f"'{DEFAULT_POLICY_ID}' not found in " \
  488. f"train_results['infos']['learner'] ({learner_info})!"
  489. for pid, policy_stats in learner_info.items():
  490. if pid == "batch_count":
  491. continue
  492. # Expect td-errors to be per batch-item.
  493. if "td_error" in policy_stats:
  494. configured_b = train_results["config"]["train_batch_size"]
  495. actual_b = policy_stats["td_error"].shape[0]
  496. # R2D2 case.
  497. if (configured_b - actual_b) / actual_b > 0.1:
  498. assert configured_b / (
  499. train_results["config"]["model"]["max_seq_len"] +
  500. train_results["config"]["burn_in"]) == actual_b
  501. # Make sure each policy has the LEARNER_STATS_KEY under it.
  502. assert LEARNER_STATS_KEY in policy_stats
  503. learner_stats = policy_stats[LEARNER_STATS_KEY]
  504. for key, value in learner_stats.items():
  505. # Min- and max-stats should be single values.
  506. if key.startswith("min_") or key.startswith("max_"):
  507. assert np.isscalar(
  508. value), f"'key' value not a scalar ({value})!"
  509. return train_results
  510. def run_learning_tests_from_yaml(
  511. yaml_files: List[str],
  512. *,
  513. max_num_repeats: int = 2,
  514. smoke_test: bool = False,
  515. ) -> Dict[str, Any]:
  516. """Runs the given experiments in yaml_files and returns results dict.
  517. Args:
  518. yaml_files (List[str]): List of yaml file names.
  519. max_num_repeats (int): How many times should we repeat a failed
  520. experiment?
  521. smoke_test (bool): Whether this is just a smoke-test. If True,
  522. set time_total_s to 5min and don't early out due to rewards
  523. or timesteps reached.
  524. """
  525. print("Will run the following yaml files:")
  526. for yaml_file in yaml_files:
  527. print("->", yaml_file)
  528. # All trials we'll ever run in this test script.
  529. all_trials = []
  530. # The experiments (by name) we'll run up to `max_num_repeats` times.
  531. experiments = {}
  532. # The results per experiment.
  533. checks = {}
  534. # Metrics per experiment.
  535. stats = {}
  536. start_time = time.monotonic()
  537. # Loop through all collected files and gather experiments.
  538. # Augment all by `torch` framework.
  539. for yaml_file in yaml_files:
  540. tf_experiments = yaml.safe_load(open(yaml_file).read())
  541. # Add torch version of all experiments to the list.
  542. for k, e in tf_experiments.items():
  543. # If framework explicitly given, only test for that framework.
  544. # Some algos do not have both versions available.
  545. if "frameworks" in e:
  546. frameworks = e["frameworks"]
  547. else:
  548. # By default we don't run tf2, because tf2's multi-gpu support
  549. # isn't complete yet.
  550. frameworks = ["tf", "torch"]
  551. # Pop frameworks key to not confuse Tune.
  552. e.pop("frameworks", None)
  553. e["stop"] = e["stop"] if "stop" in e else {}
  554. e["pass_criteria"] = e[
  555. "pass_criteria"] if "pass_criteria" in e else {}
  556. # For smoke-tests, we just run for n min.
  557. if smoke_test:
  558. # 0sec for each(!) experiment/trial.
  559. # This is such that if there are many experiments/trials
  560. # in a test (e.g. rllib_learning_test), each one can at least
  561. # create its trainer and run a first iteration.
  562. e["stop"]["time_total_s"] = 0
  563. else:
  564. # We also stop early, once we reach the desired reward.
  565. min_reward = e.get("pass_criteria",
  566. {}).get("episode_reward_mean")
  567. if min_reward is not None:
  568. e["stop"]["episode_reward_mean"] = min_reward
  569. # Generate `checks` dict for all experiments
  570. # (tf, tf2 and/or torch).
  571. for framework in frameworks:
  572. k_ = k + "-" + framework
  573. ec = copy.deepcopy(e)
  574. ec["config"]["framework"] = framework
  575. if framework == "tf2":
  576. ec["config"]["eager_tracing"] = True
  577. checks[k_] = {
  578. "min_reward": ec["pass_criteria"].get(
  579. "episode_reward_mean", 0.0),
  580. "min_throughput": ec["pass_criteria"].get(
  581. "timesteps_total", 0.0) /
  582. (ec["stop"].get("time_total_s", 1.0) or 1.0),
  583. "time_total_s": ec["stop"].get("time_total_s"),
  584. "failures": 0,
  585. "passed": False,
  586. }
  587. # This key would break tune.
  588. ec.pop("pass_criteria", None)
  589. # One experiment to run.
  590. experiments[k_] = ec
  591. # Print out the actual config.
  592. print("== Test config ==")
  593. print(yaml.dump(experiments))
  594. # Keep track of those experiments we still have to run.
  595. # If an experiment passes, we'll remove it from this dict.
  596. experiments_to_run = experiments.copy()
  597. try:
  598. ray.init(address="auto")
  599. except ConnectionError:
  600. ray.init()
  601. for i in range(max_num_repeats):
  602. # We are done.
  603. if len(experiments_to_run) == 0:
  604. print("All experiments finished.")
  605. break
  606. print(f"Starting learning test iteration {i}...")
  607. # Run remaining experiments.
  608. trials = run_experiments(
  609. experiments_to_run,
  610. resume=False,
  611. verbose=2,
  612. progress_reporter=CLIReporter(
  613. metric_columns={
  614. "training_iteration": "iter",
  615. "time_total_s": "time_total_s",
  616. "timesteps_total": "ts",
  617. "episodes_this_iter": "train_episodes",
  618. "episode_reward_mean": "reward_mean",
  619. },
  620. sort_by_metric=True,
  621. max_report_frequency=30,
  622. ))
  623. all_trials.extend(trials)
  624. # Check each experiment for whether it passed.
  625. # Criteria is to a) reach reward AND b) to have reached the throughput
  626. # defined by `timesteps_total` / `time_total_s`.
  627. for experiment in experiments_to_run.copy():
  628. print(f"Analyzing experiment {experiment} ...")
  629. # Collect all trials within this experiment (some experiments may
  630. # have num_samples or grid_searches defined).
  631. trials_for_experiment = []
  632. for t in trials:
  633. trial_exp = re.sub(".+/([^/]+)$", "\\1", t.local_dir)
  634. if trial_exp == experiment:
  635. trials_for_experiment.append(t)
  636. print(f" ... Trials: {trials_for_experiment}.")
  637. # If we have evaluation workers, use their rewards.
  638. # This is useful for offline learning tests, where
  639. # we evaluate against an actual environment.
  640. check_eval = experiments[experiment]["config"].get(
  641. "evaluation_interval", None) is not None
  642. # Error: Increase failure count and repeat.
  643. if any(t.status == "ERROR" for t in trials_for_experiment):
  644. print(" ... ERROR.")
  645. checks[experiment]["failures"] += 1
  646. # Smoke-tests always succeed.
  647. elif smoke_test:
  648. print(" ... SMOKE TEST (mark ok).")
  649. checks[experiment]["passed"] = True
  650. del experiments_to_run[experiment]
  651. # Experiment finished: Check reward achieved and timesteps done
  652. # (throughput).
  653. else:
  654. if check_eval:
  655. episode_reward_mean = np.mean([
  656. t.last_result["evaluation"]["episode_reward_mean"]
  657. for t in trials_for_experiment
  658. ])
  659. else:
  660. episode_reward_mean = np.mean([
  661. t.last_result["episode_reward_mean"]
  662. for t in trials_for_experiment
  663. ])
  664. desired_reward = checks[experiment]["min_reward"]
  665. timesteps_total = np.mean([
  666. t.last_result["timesteps_total"]
  667. for t in trials_for_experiment
  668. ])
  669. total_time_s = np.mean([
  670. t.last_result["time_total_s"]
  671. for t in trials_for_experiment
  672. ])
  673. # TODO(jungong) : track trainer and env throughput separately.
  674. throughput = timesteps_total / (total_time_s or 1.0)
  675. desired_throughput = checks[experiment]["min_throughput"]
  676. # Record performance.
  677. stats[experiment] = {
  678. "episode_reward_mean": episode_reward_mean,
  679. "throughput": throughput,
  680. }
  681. print(f" ... Desired reward={desired_reward}; "
  682. f"desired throughput={desired_throughput}")
  683. # We failed to reach desired reward or the desired throughput.
  684. if (desired_reward and
  685. episode_reward_mean < desired_reward) or \
  686. (desired_throughput and
  687. throughput < desired_throughput):
  688. print(" ... Not successful: Actual "
  689. f"reward={episode_reward_mean}; "
  690. f"actual throughput={throughput}")
  691. checks[experiment]["failures"] += 1
  692. # We succeeded!
  693. else:
  694. print(" ... Successful: (mark ok).")
  695. checks[experiment]["passed"] = True
  696. del experiments_to_run[experiment]
  697. ray.shutdown()
  698. time_taken = time.monotonic() - start_time
  699. # Create results dict and write it to disk.
  700. result = {
  701. "time_taken": time_taken,
  702. "trial_states": dict(Counter([trial.status for trial in all_trials])),
  703. "last_update": time.time(),
  704. "stats": stats,
  705. "passed": [k for k, exp in checks.items() if exp["passed"]],
  706. "failures": {
  707. k: exp["failures"]
  708. for k, exp in checks.items() if exp["failures"] > 0
  709. }
  710. }
  711. return result