resources.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import json
  2. import logging
  3. from collections import namedtuple
  4. # For compatibility under py2 to consider unicode as str
  5. from typing import Optional
  6. from ray.tune.error import TuneError
  7. from ray.tune.execution.placement_groups import (
  8. PlacementGroupFactory,
  9. resource_dict_to_pg_factory,
  10. )
  11. from ray.tune.utils.resource_updater import _Resources
  12. from ray.util.annotations import Deprecated, DeveloperAPI
  13. logger = logging.getLogger(__name__)
  14. @Deprecated
  15. class Resources(
  16. namedtuple(
  17. "Resources",
  18. [
  19. "cpu",
  20. "gpu",
  21. "memory",
  22. "object_store_memory",
  23. "extra_cpu",
  24. "extra_gpu",
  25. "extra_memory",
  26. "extra_object_store_memory",
  27. "custom_resources",
  28. "extra_custom_resources",
  29. "has_placement_group",
  30. ],
  31. )
  32. ):
  33. __slots__ = ()
  34. def __new__(
  35. cls,
  36. cpu: float,
  37. gpu: float,
  38. memory: float = 0,
  39. object_store_memory: float = 0.0,
  40. extra_cpu: float = 0.0,
  41. extra_gpu: float = 0.0,
  42. extra_memory: float = 0.0,
  43. extra_object_store_memory: float = 0.0,
  44. custom_resources: Optional[dict] = None,
  45. extra_custom_resources: Optional[dict] = None,
  46. has_placement_group: bool = False,
  47. ):
  48. raise DeprecationWarning(
  49. "tune.Resources is depracted. Use tune.PlacementGroupFactory instead."
  50. )
  51. @DeveloperAPI
  52. def json_to_resources(data: Optional[str]) -> Optional[PlacementGroupFactory]:
  53. if data is None or data == "null":
  54. return None
  55. if isinstance(data, str):
  56. data = json.loads(data)
  57. for k in data:
  58. if k in ["driver_cpu_limit", "driver_gpu_limit"]:
  59. raise TuneError(
  60. "The field `{}` is no longer supported. Use `extra_cpu` "
  61. "or `extra_gpu` instead.".format(k)
  62. )
  63. if k not in _Resources._fields:
  64. raise ValueError(
  65. "Unknown resource field {}, must be one of {}".format(
  66. k, Resources._fields
  67. )
  68. )
  69. resource_dict_to_pg_factory(
  70. dict(
  71. cpu=data.get("cpu", 1),
  72. gpu=data.get("gpu", 0),
  73. memory=data.get("memory", 0),
  74. custom_resources=data.get("custom_resources"),
  75. )
  76. )
  77. @Deprecated
  78. def resources_to_json(*args, **kwargs):
  79. raise DeprecationWarning(
  80. "tune.Resources is depracted. Use tune.PlacementGroupFactory instead."
  81. )