serialization.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import base64
  2. import importlib
  3. import io
  4. import zlib
  5. from typing import Any, Dict, Optional, Sequence, Type, Union
  6. import numpy as np
  7. import ray
  8. from ray.rllib.utils.annotations import DeveloperAPI
  9. from ray.rllib.utils.gym import try_import_gymnasium_and_gym
  10. from ray.rllib.utils.error import NotSerializable
  11. from ray.rllib.utils.spaces.flexdict import FlexDict
  12. from ray.rllib.utils.spaces.repeated import Repeated
  13. from ray.rllib.utils.spaces.simplex import Simplex
  14. NOT_SERIALIZABLE = "__not_serializable__"
  15. gym, old_gym = try_import_gymnasium_and_gym()
  16. old_gym_text_class = None
  17. if old_gym:
  18. old_gym_text_class = getattr(old_gym.spaces, "Text", None)
  19. @DeveloperAPI
  20. def convert_numpy_to_python_primitives(obj: Any):
  21. """Convert an object that is a numpy type to a python type.
  22. If the object is not a numpy type, it is returned unchanged.
  23. Args:
  24. obj: The object to convert.
  25. """
  26. if isinstance(obj, np.integer):
  27. return int(obj)
  28. elif isinstance(obj, np.floating):
  29. return float(obj)
  30. elif isinstance(obj, np.bool_):
  31. return bool(obj)
  32. elif isinstance(obj, np.str_):
  33. return str(obj)
  34. elif isinstance(obj, np.ndarray):
  35. ret = obj.tolist()
  36. for i, v in enumerate(ret):
  37. ret[i] = convert_numpy_to_python_primitives(v)
  38. return ret
  39. else:
  40. return obj
  41. def _serialize_ndarray(array: np.ndarray) -> str:
  42. """Pack numpy ndarray into Base64 encoded strings for serialization.
  43. This function uses numpy.save() instead of pickling to ensure
  44. compatibility.
  45. Args:
  46. array: numpy ndarray.
  47. Returns:
  48. b64 escaped string.
  49. """
  50. buf = io.BytesIO()
  51. np.save(buf, array)
  52. return base64.b64encode(zlib.compress(buf.getvalue())).decode("ascii")
  53. def _deserialize_ndarray(b64_string: str) -> np.ndarray:
  54. """Unpack b64 escaped string into numpy ndarray.
  55. This function assumes the unescaped bytes are of npy format.
  56. Args:
  57. b64_string: Base64 escaped string.
  58. Returns:
  59. numpy ndarray.
  60. """
  61. return np.load(io.BytesIO(zlib.decompress(base64.b64decode(b64_string))))
  62. @DeveloperAPI
  63. def gym_space_to_dict(space: gym.spaces.Space) -> Dict:
  64. """Serialize a gym Space into a JSON-serializable dict.
  65. Args:
  66. space: gym.spaces.Space
  67. Returns:
  68. Serialized JSON string.
  69. """
  70. def _box(sp: gym.spaces.Box) -> Dict:
  71. return {
  72. "space": "box",
  73. "low": _serialize_ndarray(sp.low),
  74. "high": _serialize_ndarray(sp.high),
  75. "shape": sp._shape, # shape is a tuple.
  76. "dtype": sp.dtype.str,
  77. }
  78. def _discrete(sp: gym.spaces.Discrete) -> Dict:
  79. d = {
  80. "space": "discrete",
  81. "n": int(sp.n),
  82. }
  83. # Offset is a relatively new Discrete space feature.
  84. if hasattr(sp, "start"):
  85. d["start"] = int(sp.start)
  86. return d
  87. def _multi_binary(sp: gym.spaces.MultiBinary) -> Dict:
  88. return {
  89. "space": "multi-binary",
  90. "n": sp.n,
  91. }
  92. def _multi_discrete(sp: gym.spaces.MultiDiscrete) -> Dict:
  93. return {
  94. "space": "multi-discrete",
  95. "nvec": _serialize_ndarray(sp.nvec),
  96. "dtype": sp.dtype.str,
  97. }
  98. def _tuple(sp: gym.spaces.Tuple) -> Dict:
  99. return {
  100. "space": "tuple",
  101. "spaces": [gym_space_to_dict(sp) for sp in sp.spaces],
  102. }
  103. def _dict(sp: gym.spaces.Dict) -> Dict:
  104. return {
  105. "space": "dict",
  106. "spaces": {k: gym_space_to_dict(sp) for k, sp in sp.spaces.items()},
  107. }
  108. def _simplex(sp: Simplex) -> Dict:
  109. return {
  110. "space": "simplex",
  111. "shape": sp._shape, # shape is a tuple.
  112. "concentration": sp.concentration,
  113. "dtype": sp.dtype.str,
  114. }
  115. def _repeated(sp: Repeated) -> Dict:
  116. return {
  117. "space": "repeated",
  118. "child_space": gym_space_to_dict(sp.child_space),
  119. "max_len": sp.max_len,
  120. }
  121. def _flex_dict(sp: FlexDict) -> Dict:
  122. d = {
  123. "space": "flex_dict",
  124. }
  125. for k, s in sp.spaces:
  126. d[k] = gym_space_to_dict(s)
  127. return d
  128. def _text(sp: "gym.spaces.Text") -> Dict:
  129. # Note (Kourosh): This only works in gym >= 0.25.0
  130. charset = getattr(sp, "character_set", None)
  131. if charset is None:
  132. charset = getattr(sp, "charset", None)
  133. if charset is None:
  134. raise ValueError(
  135. "Text space must have a character_set or charset attribute"
  136. )
  137. return {
  138. "space": "text",
  139. "min_length": sp.min_length,
  140. "max_length": sp.max_length,
  141. "charset": charset,
  142. }
  143. if isinstance(space, gym.spaces.Box):
  144. return _box(space)
  145. elif isinstance(space, gym.spaces.Discrete):
  146. return _discrete(space)
  147. elif isinstance(space, gym.spaces.MultiBinary):
  148. return _multi_binary(space)
  149. elif isinstance(space, gym.spaces.MultiDiscrete):
  150. return _multi_discrete(space)
  151. elif isinstance(space, gym.spaces.Tuple):
  152. return _tuple(space)
  153. elif isinstance(space, gym.spaces.Dict):
  154. return _dict(space)
  155. elif isinstance(space, gym.spaces.Text):
  156. return _text(space)
  157. elif isinstance(space, Simplex):
  158. return _simplex(space)
  159. elif isinstance(space, Repeated):
  160. return _repeated(space)
  161. elif isinstance(space, FlexDict):
  162. return _flex_dict(space)
  163. # Old gym Spaces.
  164. elif old_gym and isinstance(space, old_gym.spaces.Box):
  165. return _box(space)
  166. elif old_gym and isinstance(space, old_gym.spaces.Discrete):
  167. return _discrete(space)
  168. elif old_gym and isinstance(space, old_gym.spaces.MultiDiscrete):
  169. return _multi_discrete(space)
  170. elif old_gym and isinstance(space, old_gym.spaces.Tuple):
  171. return _tuple(space)
  172. elif old_gym and isinstance(space, old_gym.spaces.Dict):
  173. return _dict(space)
  174. elif old_gym and old_gym_text_class and isinstance(space, old_gym_text_class):
  175. return _text(space)
  176. else:
  177. raise ValueError("Unknown space type for serialization, ", type(space))
  178. @DeveloperAPI
  179. def space_to_dict(space: gym.spaces.Space) -> Dict:
  180. d = {"space": gym_space_to_dict(space)}
  181. if "original_space" in space.__dict__:
  182. d["original_space"] = space_to_dict(space.original_space)
  183. return d
  184. @DeveloperAPI
  185. def gym_space_from_dict(d: Dict) -> gym.spaces.Space:
  186. """De-serialize a dict into gym Space.
  187. Args:
  188. str: serialized JSON str.
  189. Returns:
  190. De-serialized gym space.
  191. """
  192. def __common(d: Dict):
  193. """Common updates to the dict before we use it to construct spaces"""
  194. ret = d.copy()
  195. del ret["space"]
  196. if "dtype" in ret:
  197. ret["dtype"] = np.dtype(ret["dtype"])
  198. return ret
  199. def _box(d: Dict) -> gym.spaces.Box:
  200. ret = d.copy()
  201. ret.update(
  202. {
  203. "low": _deserialize_ndarray(d["low"]),
  204. "high": _deserialize_ndarray(d["high"]),
  205. }
  206. )
  207. return gym.spaces.Box(**__common(ret))
  208. def _discrete(d: Dict) -> gym.spaces.Discrete:
  209. return gym.spaces.Discrete(**__common(d))
  210. def _multi_binary(d: Dict) -> gym.spaces.MultiBinary:
  211. return gym.spaces.MultiBinary(**__common(d))
  212. def _multi_discrete(d: Dict) -> gym.spaces.MultiDiscrete:
  213. ret = d.copy()
  214. ret.update(
  215. {
  216. "nvec": _deserialize_ndarray(ret["nvec"]),
  217. }
  218. )
  219. return gym.spaces.MultiDiscrete(**__common(ret))
  220. def _tuple(d: Dict) -> gym.spaces.Discrete:
  221. spaces = [gym_space_from_dict(sp) for sp in d["spaces"]]
  222. return gym.spaces.Tuple(spaces=spaces)
  223. def _dict(d: Dict) -> gym.spaces.Discrete:
  224. spaces = {k: gym_space_from_dict(sp) for k, sp in d["spaces"].items()}
  225. return gym.spaces.Dict(spaces=spaces)
  226. def _simplex(d: Dict) -> Simplex:
  227. return Simplex(**__common(d))
  228. def _repeated(d: Dict) -> Repeated:
  229. child_space = gym_space_from_dict(d["child_space"])
  230. return Repeated(child_space=child_space, max_len=d["max_len"])
  231. def _flex_dict(d: Dict) -> FlexDict:
  232. spaces = {k: gym_space_from_dict(s) for k, s in d.items() if k != "space"}
  233. return FlexDict(spaces=spaces)
  234. def _text(d: Dict) -> "gym.spaces.Text":
  235. return gym.spaces.Text(**__common(d))
  236. space_map = {
  237. "box": _box,
  238. "discrete": _discrete,
  239. "multi-binary": _multi_binary,
  240. "multi-discrete": _multi_discrete,
  241. "tuple": _tuple,
  242. "dict": _dict,
  243. "simplex": _simplex,
  244. "repeated": _repeated,
  245. "flex_dict": _flex_dict,
  246. "text": _text,
  247. }
  248. space_type = d["space"]
  249. if space_type not in space_map:
  250. raise ValueError("Unknown space type for de-serialization, ", space_type)
  251. return space_map[space_type](d)
  252. @DeveloperAPI
  253. def space_from_dict(d: Dict) -> gym.spaces.Space:
  254. space = gym_space_from_dict(d["space"])
  255. if "original_space" in d:
  256. assert "space" in d["original_space"]
  257. if isinstance(d["original_space"]["space"], str):
  258. # For backward compatibility reasons, if d["original_space"]["space"]
  259. # is a string, this original space was serialized by gym_space_to_dict.
  260. space.original_space = gym_space_from_dict(d["original_space"])
  261. else:
  262. # Otherwise, this original space was serialized by space_to_dict.
  263. space.original_space = space_from_dict(d["original_space"])
  264. return space
  265. @DeveloperAPI
  266. def check_if_args_kwargs_serializable(args: Sequence[Any], kwargs: Dict[str, Any]):
  267. """Check if parameters to a function are serializable by ray.
  268. Args:
  269. args: arguments to be checked.
  270. kwargs: keyword arguments to be checked.
  271. Raises:
  272. NoteSerializable if either args are kwargs are not serializable
  273. by ray.
  274. """
  275. for arg in args:
  276. try:
  277. # if the object is truly serializable we should be able to
  278. # ray.put and ray.get it.
  279. ray.get(ray.put(arg))
  280. except TypeError as e:
  281. raise NotSerializable(
  282. "RLModule constructor arguments must be serializable. "
  283. f"Found non-serializable argument: {arg}.\n"
  284. f"Original serialization error: {e}"
  285. )
  286. for k, v in kwargs.items():
  287. try:
  288. # if the object is truly serializable we should be able to
  289. # ray.put and ray.get it.
  290. ray.get(ray.put(v))
  291. except TypeError as e:
  292. raise NotSerializable(
  293. "RLModule constructor arguments must be serializable. "
  294. f"Found non-serializable keyword argument: {k} = {v}.\n"
  295. f"Original serialization error: {e}"
  296. )
  297. @DeveloperAPI
  298. def serialize_type(type_: Union[Type, str]) -> str:
  299. """Converts a type into its full classpath ([module file] + "." + [class name]).
  300. Args:
  301. type_: The type to convert.
  302. Returns:
  303. The full classpath of the given type, e.g. "ray.rllib.algorithms.ppo.PPOConfig".
  304. """
  305. # TODO (avnishn): find a way to incorporate the tune registry here.
  306. # Already serialized.
  307. if isinstance(type_, str):
  308. return type_
  309. return type_.__module__ + "." + type_.__qualname__
  310. @DeveloperAPI
  311. def deserialize_type(
  312. module: Union[str, Type], error: bool = False
  313. ) -> Optional[Union[str, Type]]:
  314. """Resolves a class path to a class.
  315. If the given module is already a class, it is returned as is.
  316. If the given module is a string, it is imported and the class is returned.
  317. Args:
  318. module: The classpath (str) or type to resolve.
  319. error: Whether to throw a ValueError if `module` could not be resolved into
  320. a class. If False and `module` is not resolvable, returns None.
  321. Returns:
  322. The resolved class or `module` (if `error` is False and no resolution possible).
  323. Raises:
  324. ValueError: If `error` is True and `module` cannot be resolved.
  325. """
  326. # Already a class, return as-is.
  327. if isinstance(module, type):
  328. return module
  329. # A string.
  330. elif isinstance(module, str):
  331. # Try interpreting (as classpath) and importing the given module.
  332. try:
  333. module_path, class_name = module.rsplit(".", 1)
  334. module = importlib.import_module(module_path)
  335. return getattr(module, class_name)
  336. # Module not found OR not a module (but a registered string?).
  337. except (ModuleNotFoundError, ImportError, AttributeError, ValueError) as e:
  338. # Ignore if error=False.
  339. if error:
  340. raise ValueError(
  341. f"Could not deserialize the given classpath `module={module}` into "
  342. "a valid python class! Make sure you have all necessary pip "
  343. "packages installed and all custom modules are in your "
  344. "`PYTHONPATH` env variable."
  345. ) from e
  346. else:
  347. raise ValueError(f"`module` ({module} must be type or string (classpath)!")
  348. return module