manage.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Functions for declaring overridable configuration for launch jobs."""
  2. from __future__ import annotations
  3. from typing import Any
  4. def manage_config_file(
  5. path: str,
  6. include: list[str] | None = None,
  7. exclude: list[str] | None = None,
  8. schema: Any | None = None,
  9. ):
  10. r"""Declare an overridable configuration file for a launch job.
  11. If a new job version is created from the active run, the configuration file
  12. will be added to the job's inputs. If the job is launched and overrides
  13. have been provided for the configuration file, this function will detect
  14. the overrides from the environment and update the configuration file on disk.
  15. Note that these overrides will only be applied in ephemeral containers.
  16. `include` and `exclude` are lists of dot separated paths with the config.
  17. The paths are used to filter subtrees of the configuration file out of the
  18. job's inputs.
  19. For example, given the following configuration file:
  20. ```yaml
  21. model:
  22. name: resnet
  23. layers: 18
  24. training:
  25. epochs: 10
  26. batch_size: 32
  27. ```
  28. Passing `include=['model']` will only include the `model` subtree in the
  29. job's inputs. Passing `exclude=['model.layers']` will exclude the `layers`
  30. key from the `model` subtree. Note that `exclude` takes precedence over
  31. `include`.
  32. `.` is used as a separator for nested keys. If a key contains a `.`, it
  33. should be escaped with a backslash, e.g. `include=[r'model\.layers']`. Note
  34. the use of `r` to denote a raw string when using escape chars.
  35. Args:
  36. path (str): The path to the configuration file. This path must be
  37. relative and must not contain backwards traversal, i.e. `..`.
  38. include (List[str]): A list of keys to include in the configuration file.
  39. exclude (List[str]): A list of keys to exclude from the configuration file.
  40. schema (dict | Pydantic model): A JSON Schema or Pydantic model describing
  41. describing which attributes will be editable from the Launch drawer.
  42. Accepts both an instance of a Pydantic BaseModel class or the BaseModel
  43. class itself.
  44. Raises:
  45. LaunchError: If the path is not valid, or if there is no active run.
  46. """
  47. # note: schema's Any type is because in the case where a BaseModel class is
  48. # provided, its type is a pydantic internal type that we don't want our typing
  49. # to depend on. schema's type should be considered
  50. # "Optional[dict | <something with a .model_json_schema() method>]"
  51. from .internal import handle_config_file_input
  52. return handle_config_file_input(path, include, exclude, schema)
  53. def manage_wandb_config(
  54. include: list[str] | None = None,
  55. exclude: list[str] | None = None,
  56. schema: Any | None = None,
  57. ):
  58. r"""Declare wandb.config as an overridable configuration for a launch job.
  59. If a new job version is created from the active run, the run config
  60. (wandb.config) will become an overridable input of the job. If the job is
  61. launched and overrides have been provided for the run config, the overrides
  62. will be applied to the run config when `wandb.init` is called.
  63. `include` and `exclude` are lists of dot separated paths with the config.
  64. The paths are used to filter subtrees of the configuration file out of the
  65. job's inputs.
  66. For example, given the following run config contents:
  67. ```yaml
  68. model:
  69. name: resnet
  70. layers: 18
  71. training:
  72. epochs: 10
  73. batch_size: 32
  74. ```
  75. Passing `include=['model']` will only include the `model` subtree in the
  76. job's inputs. Passing `exclude=['model.layers']` will exclude the `layers`
  77. key from the `model` subtree. Note that `exclude` takes precedence over
  78. `include`.
  79. `.` is used as a separator for nested keys. If a key contains a `.`, it
  80. should be escaped with a backslash, e.g. `include=[r'model\.layers']`. Note
  81. the use of `r` to denote a raw string when using escape chars.
  82. Args:
  83. include (List[str]): A list of subtrees to include in the configuration.
  84. exclude (List[str]): A list of subtrees to exclude from the configuration.
  85. schema (dict | Pydantic model): A JSON Schema or Pydantic model describing
  86. describing which attributes will be editable from the Launch drawer.
  87. Accepts both an instance of a Pydantic BaseModel class or the BaseModel
  88. class itself.
  89. Raises:
  90. LaunchError: If there is no active run.
  91. """
  92. # note: schema's Any type is because in the case where a BaseModel class is
  93. # provided, its type is a pydantic internal type that we don't want our typing
  94. # to depend on. schema's type should be considered
  95. # "Optional[dict | <something with a .model_json_schema() method>]"
  96. from .internal import handle_run_config_input
  97. handle_run_config_input(include, exclude, schema)