debug.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import numpy as np
  2. import os
  3. import pprint
  4. import random
  5. from typing import Any, Mapping, Optional
  6. from ray.rllib.policy.sample_batch import SampleBatch, MultiAgentBatch
  7. from ray.rllib.utils.framework import try_import_tf, try_import_torch
  8. _printer = pprint.PrettyPrinter(indent=2, width=60)
  9. def summarize(obj: Any) -> Any:
  10. """Return a pretty-formatted string for an object.
  11. This has special handling for pretty-formatting of commonly used data types
  12. in RLlib, such as SampleBatch, numpy arrays, etc.
  13. Args:
  14. obj: The object to format.
  15. Returns:
  16. The summarized object.
  17. """
  18. return _printer.pformat(_summarize(obj))
  19. def _summarize(obj):
  20. if isinstance(obj, Mapping):
  21. return {k: _summarize(v) for k, v in obj.items()}
  22. elif hasattr(obj, "_asdict"):
  23. return {
  24. "type": obj.__class__.__name__,
  25. "data": _summarize(obj._asdict()),
  26. }
  27. elif isinstance(obj, list):
  28. return [_summarize(x) for x in obj]
  29. elif isinstance(obj, tuple):
  30. return tuple(_summarize(x) for x in obj)
  31. elif isinstance(obj, np.ndarray):
  32. if obj.size == 0:
  33. return _StringValue("np.ndarray({}, dtype={})".format(
  34. obj.shape, obj.dtype))
  35. elif obj.dtype == np.object or obj.dtype.type is np.str_:
  36. return _StringValue("np.ndarray({}, dtype={}, head={})".format(
  37. obj.shape, obj.dtype, _summarize(obj[0])))
  38. else:
  39. return _StringValue(
  40. "np.ndarray({}, dtype={}, min={}, max={}, mean={})".format(
  41. obj.shape, obj.dtype, round(float(np.min(obj)), 3),
  42. round(float(np.max(obj)), 3), round(
  43. float(np.mean(obj)), 3)))
  44. elif isinstance(obj, MultiAgentBatch):
  45. return {
  46. "type": "MultiAgentBatch",
  47. "policy_batches": _summarize(obj.policy_batches),
  48. "count": obj.count,
  49. }
  50. elif isinstance(obj, SampleBatch):
  51. return {
  52. "type": "SampleBatch",
  53. "data": {k: _summarize(v)
  54. for k, v in obj.items()},
  55. }
  56. else:
  57. return obj
  58. class _StringValue:
  59. def __init__(self, value):
  60. self.value = value
  61. def __repr__(self):
  62. return self.value
  63. def update_global_seed_if_necessary(framework: Optional[str] = None,
  64. seed: Optional[int] = None) -> None:
  65. """Seed global modules such as random, numpy, torch, or tf.
  66. This is useful for debugging and testing.
  67. Args:
  68. framework: The framework specifier (may be None).
  69. seed: An optional int seed. If None, will not do
  70. anything.
  71. """
  72. if seed is None:
  73. return
  74. # Python random module.
  75. random.seed(seed)
  76. # Numpy.
  77. np.random.seed(seed)
  78. # Torch.
  79. if framework == "torch":
  80. torch, _ = try_import_torch()
  81. torch.manual_seed(seed)
  82. # See https://github.com/pytorch/pytorch/issues/47672.
  83. cuda_version = torch.version.cuda
  84. if cuda_version is not None and float(torch.version.cuda) >= 10.2:
  85. os.environ["CUBLAS_WORKSPACE_CONFIG"] = "4096:8"
  86. else:
  87. from distutils.version import LooseVersion
  88. if LooseVersion(torch.__version__) >= LooseVersion("1.8.0"):
  89. # Not all Operations support this.
  90. torch.use_deterministic_algorithms(True)
  91. else:
  92. torch.set_deterministic(True)
  93. # This is only for Convolution no problem.
  94. torch.backends.cudnn.deterministic = True
  95. elif framework == "tf2" or framework == "tfe":
  96. tf1, tf, _ = try_import_tf()
  97. # Tf2.x.
  98. if framework == "tf2":
  99. tf.random.set_seed(seed)
  100. # Tf-eager.
  101. elif framework == "tfe":
  102. tf1.set_random_seed(seed)