runner.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import abc
  2. import logging
  3. from typing import TYPE_CHECKING, Any, Union
  4. from ray.rllib.utils.actor_manager import FaultAwareApply
  5. from ray.rllib.utils.metrics.metrics_logger import MetricsLogger
  6. from ray.rllib.utils.typing import DeviceType, TensorType
  7. if TYPE_CHECKING:
  8. from ray.rllib.algorithms.algorithm_config import AlgorithmConfig
  9. logger = logging.getLogger(__name__)
  10. class Runner(FaultAwareApply, metaclass=abc.ABCMeta):
  11. def __init__(self, *, config: "AlgorithmConfig", **kwargs):
  12. """Initializes a `Runner` instance.
  13. Args:
  14. config: The `AlgorithmConfig` to use to setup this `Runner`.
  15. **kwargs: Forward compatibility `kwargs`.
  16. """
  17. self.worker_index: int = kwargs.get("worker_index")
  18. self.config: AlgorithmConfig = config.copy(copy_frozen=False)
  19. # Set the device.
  20. self.set_device()
  21. # Generate the `RLModule`.
  22. self.make_module()
  23. self._weights_seq_no = 0
  24. # Create a MetricsLogger object for logging custom stats.
  25. self.metrics: MetricsLogger = MetricsLogger(
  26. stats_cls_lookup=config.stats_cls_lookup,
  27. root=False,
  28. )
  29. # Initialize the `FaultAwareApply`.
  30. super().__init__()
  31. @abc.abstractmethod
  32. def assert_healthy(self):
  33. """Checks that self.__init__() has been completed properly.
  34. Useful in case an `Runner` is run as @ray.remote (Actor) and the owner
  35. would like to make sure the Ray Actor has been properly initialized.
  36. Raises:
  37. AssertionError: If the `Runner` Actor has NOT been properly initialized.
  38. """
  39. @abc.abstractmethod
  40. def make_module(self):
  41. """Creates the `RLModule` for this `Runner` and assigns it to `self.module`.
  42. Note that users should be able to change the `Runner`'s config (e.g. change
  43. `self.config.rl_module_spec`) and then call this method to create a new `RLModule`
  44. with the updated configuration.
  45. """
  46. pass
  47. @abc.abstractmethod
  48. def run(self, **kwargs) -> Any:
  49. """Runs the `Runner`.
  50. The exact logic of this method could have very different forms.
  51. Args:
  52. **kwargs: Forward compatibility kwargs.
  53. Returns:
  54. Anything.
  55. """
  56. @abc.abstractmethod
  57. def get_metrics(self) -> Any:
  58. """Returns metrics (in any form) of the logic run in this `Runner`.
  59. Returns:
  60. Metrics of any form.
  61. """
  62. @abc.abstractmethod
  63. def stop(self) -> None:
  64. """Releases all resources used by this `Runner`.
  65. For example, when using a `gym.Env` in this `Runner`, you should make sure
  66. that its `close()` method is called.
  67. """
  68. @property
  69. @abc.abstractmethod
  70. def _device(self) -> Union[DeviceType, None]:
  71. """Returns the device of this `Runner`. None if framework is not supported."""
  72. pass
  73. @abc.abstractmethod
  74. def set_device(self) -> None:
  75. """Sets the device for this `Runner`."""
  76. pass
  77. @abc.abstractmethod
  78. def __del__(self) -> None:
  79. """If this Actor is deleted, clears all resources used by it."""
  80. @abc.abstractmethod
  81. def _convert_to_tensor(self, struct) -> TensorType:
  82. """Converts structs to a framework-specific tensor."""