Skip to content

Commit

Permalink
chore: support full resource name in environment experiment run
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 634250315
  • Loading branch information
jaycee-li authored and copybara-github committed May 16, 2024
1 parent d6d371d commit f041f1a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
15 changes: 12 additions & 3 deletions google/cloud/aiplatform/metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,21 @@ def experiment_run(self) -> Optional[experiment_run_resource.ExperimentRun]:
"""Returns the currently set experiment run or experiment run set via env variable AIP_EXPERIMENT_RUN_NAME."""
if self._experiment_run:
return self._experiment_run
if os.getenv(constants.ENV_EXPERIMENT_RUN_KEY):
self._experiment_run = experiment_run_resource.ExperimentRun.get(
os.getenv(constants.ENV_EXPERIMENT_RUN_KEY),

env_experiment_run = os.getenv(constants.ENV_EXPERIMENT_RUN_KEY)
if env_experiment_run and self.experiment:
# The run could be run name or full resource name,
# so we remove the experiment resource prefix if necessary.
env_experiment_run = env_experiment_run.replace(
f"{self.experiment.resource_name}-",
"",
)
self._experiment_run = experiment_run_resource.ExperimentRun(
env_experiment_run,
experiment=self.experiment,
)
return self._experiment_run

return None

def set_experiment(
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/aiplatform/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ def test_start_run_from_env_experiment(
"get_tensorboard_run_artifact_not_found_mock",
"get_or_create_default_tb_none_mock",
)
def test_init_experiment_run_from_env(self):
def test_init_experiment_run_from_env_run_name(self):
os.environ["AIP_EXPERIMENT_RUN_NAME"] = _TEST_RUN

aiplatform.init(
Expand All @@ -1501,6 +1501,26 @@ def test_init_experiment_run_from_env(self):

del os.environ["AIP_EXPERIMENT_RUN_NAME"]

@pytest.mark.usefixtures(
"get_metadata_store_mock",
"get_experiment_run_mock",
"get_tensorboard_run_artifact_not_found_mock",
"get_or_create_default_tb_none_mock",
)
def test_init_experiment_run_from_env_run_resource_name(self):
os.environ["AIP_EXPERIMENT_RUN_NAME"] = _TEST_EXPERIMENT_RUN_CONTEXT_NAME

aiplatform.init(
project=_TEST_PROJECT,
location=_TEST_LOCATION,
experiment=_TEST_EXPERIMENT,
)

run = metadata._experiment_tracker.experiment_run
assert run.name == _TEST_RUN

del os.environ["AIP_EXPERIMENT_RUN_NAME"]

def test_get_experiment(self, get_experiment_mock):
aiplatform.init(
project=_TEST_PROJECT,
Expand Down

0 comments on commit f041f1a

Please sign in to comment.