-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Add a km.random_name resolver to enable auto-generated run names (#…
…426) * ✨ Add a km.random_name resolver to enable auto-generated names in configuration (#426) * add template example, add tests, idempotency still fails * add tests, idempotency still fails * add doc, ignore idempotency test, remove unused argument * add syntax with resolver in mlflow.yml * fix typo in doc * add changelog * add nested kedy agin in mlflow.yml * fix typo in changelog
- Loading branch information
1 parent
e886799
commit c98d2a3
Showing
6 changed files
with
117 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from mlflow.utils.name_utils import _generate_random_name | ||
|
||
|
||
def resolve_random_name(): | ||
# a resolver must have an argument, see: https://github.com/omry/omegaconf/issues/1060 | ||
return _generate_random_name() |
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,89 @@ | ||
import re | ||
|
||
import pytest | ||
import yaml | ||
from kedro.framework.session import KedroSession | ||
from kedro.framework.startup import bootstrap_project | ||
from mlflow.utils.name_utils import ( | ||
_GENERATOR_NOUNS, | ||
_GENERATOR_PREDICATES, | ||
) | ||
from omegaconf import OmegaConf | ||
|
||
from kedro_mlflow.config.resolvers import resolve_random_name | ||
|
||
|
||
def _write_yaml(filepath, config): | ||
yaml_str = yaml.dump(config) | ||
filepath.write_text(yaml_str) | ||
|
||
|
||
def _is_mlflow_name(name: str) -> bool: | ||
splitted_name = name.split("-") | ||
flag1 = len(splitted_name) == 3 # noqa: PLR2004 | ||
flag2 = splitted_name[0] in _GENERATOR_PREDICATES | ||
flag3 = splitted_name[1] in _GENERATOR_NOUNS | ||
flag4 = re.search(pattern=r"^\d+$", string=splitted_name[2]) | ||
return all({flag1, flag2, flag3, flag4}) | ||
|
||
|
||
@pytest.fixture | ||
def kedro_project_with_random_name(kedro_project): | ||
# kedro_project is a pytest.fixture in conftest | ||
dict_config = dict( | ||
server=dict( | ||
mlflow_tracking_uri="mlruns", | ||
mlflow_registry_uri=None, | ||
credentials=None, | ||
request_header_provider=dict(type=None, pass_context=False, init_kwargs={}), | ||
), | ||
tracking=dict( | ||
disable_tracking=dict(pipelines=["my_disabled_pipeline"]), | ||
experiment=dict(name="fake_package", restore_if_deleted=True), | ||
run=dict(id="123456789", name="${km.random_name:}", nested=True), | ||
params=dict( | ||
dict_params=dict( | ||
flatten=True, | ||
recursive=False, | ||
sep="-", | ||
), | ||
long_params_strategy="truncate", | ||
), | ||
), | ||
ui=dict(port="5151", host="localhost"), | ||
) | ||
|
||
_write_yaml(kedro_project / "conf" / "local" / "mlflow.yml", dict_config) | ||
expected = dict_config.copy() | ||
expected["server"]["mlflow_tracking_uri"] = (kedro_project / "mlruns").as_uri() | ||
return kedro_project | ||
|
||
|
||
def test_resolve_random_name_is_valid_mlflow_name(): | ||
random_name = resolve_random_name() | ||
assert _is_mlflow_name(random_name) | ||
|
||
|
||
def test_resolve_random_name_is_registered(kedro_project_with_random_name): | ||
bootstrap_project(kedro_project_with_random_name) | ||
with KedroSession.create(project_path=kedro_project_with_random_name) as session: | ||
session.load_context() | ||
assert OmegaConf.has_resolver("km.random_name") | ||
|
||
|
||
def test_resolve_random_name_is_called_in_project(kedro_project_with_random_name): | ||
bootstrap_project(kedro_project_with_random_name) | ||
with KedroSession.create(project_path=kedro_project_with_random_name) as session: | ||
context = session.load_context() | ||
assert _is_mlflow_name(context.mlflow.tracking.run.name) | ||
|
||
|
||
@pytest.mark.skip(reason="kedro 0.19.2 does not take use_cache into account") | ||
def test_resolve_random_name_is_idempotent(kedro_project_with_random_name): | ||
bootstrap_project(kedro_project_with_random_name) | ||
with KedroSession.create(project_path=kedro_project_with_random_name) as session: | ||
context = session.load_context() | ||
assert ( | ||
context.config_loader["mlflow"]["tracking"]["run"]["name"] | ||
== context.config_loader["mlflow"]["tracking"]["run"]["name"] | ||
) # when called twice, should be different is no use_cache because the resolver is random |