wandb_sweep.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from __future__ import annotations
  2. import urllib.parse
  3. from typing import TYPE_CHECKING, Callable
  4. import wandb
  5. from wandb import env
  6. from . import wandb_login
  7. if TYPE_CHECKING:
  8. from wandb.wandb_controller import _WandbController
  9. def _get_sweep_url(api, sweep_id):
  10. """Return sweep url if we can figure it out."""
  11. if api.api_key:
  12. if api.settings("entity") is None:
  13. viewer = api.viewer()
  14. if viewer.get("entity"):
  15. api.set_setting("entity", viewer["entity"])
  16. project = api.settings("project")
  17. if not project:
  18. return
  19. if api.settings("entity"):
  20. return "{base}/{entity}/{project}/sweeps/{sweepid}".format(
  21. base=api.app_url,
  22. entity=urllib.parse.quote(api.settings("entity")),
  23. project=urllib.parse.quote(project),
  24. sweepid=urllib.parse.quote(sweep_id),
  25. )
  26. def sweep(
  27. sweep: dict | Callable,
  28. entity: str | None = None,
  29. project: str | None = None,
  30. prior_runs: list[str] | None = None,
  31. ) -> str:
  32. """Initialize a hyperparameter sweep.
  33. Search for hyperparameters that optimizes a cost function
  34. of a machine learning model by testing various combinations.
  35. Make note the unique identifier, `sweep_id`, that is returned.
  36. At a later step provide the `sweep_id` to a sweep agent.
  37. See [Sweep configuration structure](https://docs.wandb.ai/models/sweeps/define-sweep-configuration)
  38. for information on how to define your sweep.
  39. Args:
  40. sweep: The configuration of a hyperparameter search.
  41. (or configuration generator).
  42. If you provide a callable, ensure that the callable does
  43. not take arguments and that it returns a dictionary that
  44. conforms to the W&B sweep config spec.
  45. entity: The username or team name where you want to send W&B
  46. runs created by the sweep to. Ensure that the entity you
  47. specify already exists. If you don't specify an entity,
  48. the run will be sent to your default entity,
  49. which is usually your username.
  50. project: The name of the project where W&B runs created from
  51. the sweep are sent to. If the project is not specified, the
  52. run is sent to a project labeled 'Uncategorized'.
  53. prior_runs: The run IDs of existing runs to add to this sweep.
  54. Returns:
  55. str: A unique identifier for the sweep.
  56. """
  57. from wandb.apis import InternalApi
  58. from wandb.sdk.launch.sweeps.utils import handle_sweep_config_violations
  59. if callable(sweep):
  60. sweep = sweep()
  61. """Sweep create for controller api and jupyter (eventually for cli)."""
  62. # Project may be only found in the sweep config.
  63. if project is None and isinstance(sweep, dict):
  64. project = sweep.get("project", None)
  65. if entity:
  66. env.set_entity(entity)
  67. if project:
  68. env.set_project(project)
  69. # Make sure we are logged in
  70. if wandb.run is None:
  71. wandb_login._login(_silent=True)
  72. api = InternalApi()
  73. sweep_id, warnings = api.upsert_sweep(sweep, prior_runs=prior_runs)
  74. handle_sweep_config_violations(warnings)
  75. print("Create sweep with ID:", sweep_id) # noqa: T201
  76. sweep_url = _get_sweep_url(api, sweep_id)
  77. if sweep_url:
  78. print("Sweep URL:", sweep_url) # noqa: T201
  79. return sweep_id
  80. def controller(
  81. sweep_id_or_config: str | dict | None = None,
  82. entity: str | None = None,
  83. project: str | None = None,
  84. ) -> _WandbController:
  85. """Public sweep controller constructor.
  86. Examples:
  87. ```python
  88. import wandb
  89. tuner = wandb.controller(...)
  90. print(tuner.sweep_config)
  91. print(tuner.sweep_id)
  92. tuner.configure_search(...)
  93. tuner.configure_stopping(...)
  94. ```
  95. """
  96. from ..wandb_controller import _WandbController
  97. c = _WandbController(
  98. sweep_id_or_config=sweep_id_or_config, entity=entity, project=project
  99. )
  100. return c