Skip to content

Commit f8052b8

Browse files
sararobcopybara-github
authored andcommitted
docs(samples): add samples for autologging
PiperOrigin-RevId: 511202564
1 parent 5850b0e commit f8052b8

5 files changed

+182
-0
lines changed

samples/model-builder/conftest.py

+14
Original file line numberDiff line numberDiff line change
@@ -1091,3 +1091,17 @@ def mock_remove_version_aliases(mock_model_registry):
10911091
) as mock_remove_version_aliases:
10921092
mock_remove_version_aliases.return_value = None
10931093
yield mock_remove_version_aliases
1094+
1095+
1096+
"""
1097+
----------------------------------------------------------------------------
1098+
Autologging Fixtures
1099+
----------------------------------------------------------------------------
1100+
"""
1101+
1102+
1103+
@pytest.fixture
1104+
def mock_autolog():
1105+
with patch.object(aiplatform, "autolog") as mock_autolog_method:
1106+
mock_autolog_method.return_value = None
1107+
yield mock_autolog_method
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Union
16+
17+
from google.cloud import aiplatform
18+
19+
20+
# [START aiplatform_sdk_autologging_with_auto_run_creation_sample]
21+
def autologging_with_auto_run_creation_sample(
22+
experiment_name: str,
23+
experiment_tensorboard: Union[str, aiplatform.Tensorboard],
24+
project: str,
25+
location: str,
26+
):
27+
aiplatform.init(
28+
experiment_name=experiment_name,
29+
project=project,
30+
location=location,
31+
experiment_tensorboard=experiment_tensorboard,
32+
)
33+
34+
aiplatform.autolog()
35+
36+
# Your model training code goes here
37+
38+
aiplatform.autolog(disable=True)
39+
40+
41+
# [END aiplatform_sdk_autologging_with_auto_run_creation_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from experiment_tracking import autologging_with_auto_run_creation_sample
16+
17+
import test_constants as constants
18+
19+
20+
def test_autologging_with_auto_run_creation_sample(mock_sdk_init, mock_autolog):
21+
22+
autologging_with_auto_run_creation_sample.autologging_with_auto_run_creation_sample(
23+
experiment_name=constants.EXPERIMENT_NAME,
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
experiment_tensorboard=constants.TENSORBOARD_NAME,
27+
)
28+
29+
mock_sdk_init.assert_called_with(
30+
experiment_name=constants.EXPERIMENT_NAME,
31+
project=constants.PROJECT,
32+
location=constants.LOCATION,
33+
experiment_tensorboard=constants.TENSORBOARD_NAME,
34+
)
35+
36+
assert mock_autolog.call_count == 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Union
16+
17+
from google.cloud import aiplatform
18+
19+
20+
# [START aiplatform_sdk_autologging_with_manual_run_creation_sample]
21+
def autologging_with_manual_run_creation_sample(
22+
experiment_name: str,
23+
run_name: str,
24+
experiment_tensorboard: Union[str, aiplatform.Tensorboard],
25+
project: str,
26+
location: str,
27+
):
28+
aiplatform.init(
29+
experiment_name=experiment_name,
30+
project=project,
31+
location=location,
32+
experiment_tensorboard=experiment_tensorboard,
33+
)
34+
35+
aiplatform.autolog()
36+
37+
aiplatform.start_run(run=run_name)
38+
39+
# Your model training code goes here
40+
41+
aiplatform.end_run()
42+
43+
aiplatform.autolog(disable=True)
44+
45+
46+
# [END aiplatform_sdk_autologging_with_manual_run_creation_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from experiment_tracking import autologging_with_manual_run_creation_sample
16+
17+
import test_constants as constants
18+
19+
20+
def test_autologging_with_manual_run_creation_sample(
21+
mock_sdk_init, mock_start_run, mock_end_run, mock_autolog
22+
):
23+
24+
autologging_with_manual_run_creation_sample.autologging_with_manual_run_creation_sample(
25+
experiment_name=constants.EXPERIMENT_NAME,
26+
run_name=constants.EXPERIMENT_RUN_NAME,
27+
project=constants.PROJECT,
28+
location=constants.LOCATION,
29+
experiment_tensorboard=constants.TENSORBOARD_NAME,
30+
)
31+
32+
mock_sdk_init.assert_called_with(
33+
experiment_name=constants.EXPERIMENT_NAME,
34+
project=constants.PROJECT,
35+
location=constants.LOCATION,
36+
experiment_tensorboard=constants.TENSORBOARD_NAME,
37+
)
38+
39+
mock_start_run.assert_called_with(
40+
run=constants.EXPERIMENT_RUN_NAME,
41+
)
42+
43+
mock_end_run.assert_called_with()
44+
45+
assert mock_autolog.call_count == 2

0 commit comments

Comments
 (0)