-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'flyteorg:master' into input-through-file-and-pipe
- Loading branch information
Showing
26 changed files
with
1,086 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Flytekit OmegaConf Plugin | ||
|
||
Flytekit python natively supports serialization of many data types for exchanging information between tasks. | ||
The Flytekit OmegaConf Plugin extends these by the `DictConfig` type from the | ||
[OmegaConf package](https://omegaconf.readthedocs.io/) as well as related types | ||
that are being used by the [hydra package](https://hydra.cc/) for configuration management. | ||
|
||
## Task example | ||
``` | ||
from dataclasses import dataclass | ||
import flytekitplugins.omegaconf # noqa F401 | ||
from flytekit import task, workflow | ||
from omegaconf import DictConfig | ||
@dataclass | ||
class MySimpleConf: | ||
_target_: str = "lightning_module.MyEncoderModule" | ||
learning_rate: float = 0.0001 | ||
@task | ||
def my_task(cfg: DictConfig) -> None: | ||
print(f"Doing things with {cfg.learning_rate=}") | ||
@workflow | ||
def pipeline(cfg: DictConfig) -> None: | ||
my_task(cfg=cfg) | ||
if __name__ == "__main__": | ||
from omegaconf import OmegaConf | ||
cfg = OmegaConf.structured(MySimpleConf) | ||
pipeline(cfg=cfg) | ||
``` | ||
|
||
## Transformer configuration | ||
|
||
The transformer can be set to one of three modes: | ||
|
||
`Dataclass` - This mode should be used with a StructuredConfig and will reconstruct the config from the matching dataclass | ||
during deserialisation in order to make typing information from the dataclass and continued validation thereof available. | ||
This requires the dataclass definition to be available via python import in the Flyte execution environment in which | ||
objects are (de-)serialised. | ||
|
||
`DictConfig` - This mode will deserialize the config into a DictConfig object. In particular, dataclasses are translated | ||
into DictConfig objects and only primitive types are being checked. The definition of underlying dataclasses for | ||
structured configs is only required during the initial serialization for this mode. | ||
|
||
`Auto` - This mode will try to deserialize according to the Dataclass mode and fall back to the DictConfig mode if the | ||
dataclass definition is not available. This is the default mode. | ||
|
||
You can set the transformer mode globally or for the current context only the following ways: | ||
```python | ||
from flytekitplugins.omegaconf import set_transformer_mode, set_local_transformer_mode, OmegaConfTransformerMode | ||
|
||
# Set the global transformer mode using the new function | ||
set_transformer_mode(OmegaConfTransformerMode.DictConfig) | ||
|
||
# You can also the mode for the current context only | ||
with set_local_transformer_mode(OmegaConfTransformerMode.Dataclass): | ||
# This will use the Dataclass mode | ||
pass | ||
``` | ||
|
||
```note | ||
Since the DictConfig is flattened and keys transformed into dot notation, the keys of the DictConfig must not contain | ||
dots. | ||
``` |
33 changes: 33 additions & 0 deletions
33
plugins/flytekit-omegaconf/flytekitplugins/omegaconf/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from contextlib import contextmanager | ||
|
||
from flytekitplugins.omegaconf.config import OmegaConfTransformerMode | ||
from flytekitplugins.omegaconf.dictconfig_transformer import DictConfigTransformer # noqa: F401 | ||
from flytekitplugins.omegaconf.listconfig_transformer import ListConfigTransformer # noqa: F401 | ||
|
||
_TRANSFORMER_MODE = OmegaConfTransformerMode.Auto | ||
|
||
|
||
def set_transformer_mode(mode: OmegaConfTransformerMode) -> None: | ||
"""Set the global serialization mode for OmegaConf objects.""" | ||
global _TRANSFORMER_MODE | ||
_TRANSFORMER_MODE = mode | ||
|
||
|
||
def get_transformer_mode() -> OmegaConfTransformerMode: | ||
"""Get the global serialization mode for OmegaConf objects.""" | ||
return _TRANSFORMER_MODE | ||
|
||
|
||
@contextmanager | ||
def local_transformer_mode(mode: OmegaConfTransformerMode): | ||
"""Context manager to set a local serialization mode for OmegaConf objects.""" | ||
global _TRANSFORMER_MODE | ||
previous_mode = _TRANSFORMER_MODE | ||
set_transformer_mode(mode) | ||
try: | ||
yield | ||
finally: | ||
set_transformer_mode(previous_mode) | ||
|
||
|
||
__all__ = ["set_transformer_mode", "get_transformer_mode", "local_transformer_mode", "OmegaConfTransformerMode"] |
15 changes: 15 additions & 0 deletions
15
plugins/flytekit-omegaconf/flytekitplugins/omegaconf/config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from enum import Enum | ||
|
||
|
||
class OmegaConfTransformerMode(Enum): | ||
""" | ||
Operation Mode indicating whether a (potentially unannotated) DictConfig object or a structured config using the | ||
underlying dataclass is returned. | ||
Note: We define a single shared config across all transformers as recursive calls should refer to the same config | ||
Note: The latter requires the use of structured configs. | ||
""" | ||
|
||
DictConfig = "DictConfig" | ||
DataClass = "DataClass" | ||
Auto = "Auto" |
Oops, something went wrong.