GeezerVison is showing github.com — chore: add sample for TensorBoard uploader · Russ76/python-aiplatform@b0a7dd3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/conftest.py +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -451,13 +451,42 @@ def mock_tensorboard(): | 451 | 451 | yield mock | 452 | 452 |
| 453 | 453 |
| | 454 | +@pytest.fixture | | 455 | +def mock_TensorBoard_uploaderTracker(): | | 456 | + mock = MagicMock(aiplatform.uploader_tracker) | | 457 | + yield mock | | 458 | + | | 459 | + | 454 | 460 | @pytest.fixture | 455 | 461 | def mock_create_tensorboard(mock_tensorboard): | 456 | 462 | with patch.object(aiplatform.Tensorboard, "create") as mock: | 457 | 463 | mock.return_value = mock_tensorboard | 458 | 464 | yield mock | 459 | 465 |
| 460 | 466 |
| | 467 | +@pytest.fixture | | 468 | +def mock_tensorboard_uploader_onetime(): | | 469 | + with patch.object(aiplatform, "upload_tb_log") as mock_tensorboard_uploader_onetime: | | 470 | + mock_tensorboard_uploader_onetime.return_value = None | | 471 | + yield mock_tensorboard_uploader_onetime | | 472 | + | | 473 | + | | 474 | +@pytest.fixture | | 475 | +def mock_tensorboard_uploader_start(): | | 476 | + with patch.object( | | 477 | + aiplatform, "start_upload_tb_log" | | 478 | + ) as mock_tensorboard_uploader_start: | | 479 | + mock_tensorboard_uploader_start.return_value = None | | 480 | + yield mock_tensorboard_uploader_start | | 481 | + | | 482 | + | | 483 | +@pytest.fixture | | 484 | +def mock_tensorboard_uploader_end(): | | 485 | + with patch.object(aiplatform, "end_upload_tb_log") as mock_tensorboard_uploader_end: | | 486 | + mock_tensorboard_uploader_end.return_value = None | | 487 | + yield mock_tensorboard_uploader_end | | 488 | + | | 489 | + | 461 | 490 | """ | 462 | 491 | ---------------------------------------------------------------------------- | 463 | 492 | Endpoint Fixtures | |
Collapse file Copy file name to clipboard +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Optional | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_upload_tensorboard_log_sample] | | 21 | +def upload_tensorboard_log_continuously_sample( | | 22 | + tensorboard_experiment_name: str, | | 23 | + logdir: str, | | 24 | + tensorboard_id: str, | | 25 | + project: str, | | 26 | + location: str, | | 27 | + experiment_display_name: Optional[str] = None, | | 28 | + run_name_prefix: Optional[str] = None, | | 29 | + description: Optional[str] = None, | | 30 | +) -> None: | | 31 | + | | 32 | + aiplatform.init(project=project, location=location) | | 33 | + | | 34 | + # Continuous monitoring | | 35 | + aiplatform.start_upload_tb_log( | | 36 | + tensorboard_id=tensorboard_id, | | 37 | + tensorboard_experiment_name=tensorboard_experiment_name, | | 38 | + logdir=logdir, | | 39 | + experiment_display_name=experiment_display_name, | | 40 | + run_name_prefix=run_name_prefix, | | 41 | + description=description, | | 42 | + ) | | 43 | + aiplatform.end_upload_tb_log() | | 44 | + | | 45 | + | | 46 | +# [END aiplatform_sdk_upload_tensorboard_log_sample] |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + | | 16 | +from experiment_tracking import upload_tensorboard_log_continuously_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_upload_tensorboard_log_continuously_sample( | | 21 | + mock_sdk_init, | | 22 | + mock_tensorboard_uploader_start, | | 23 | + mock_tensorboard_uploader_end, | | 24 | +): | | 25 | + upload_tensorboard_log_continuously_sample.upload_tensorboard_log_continuously_sample( | | 26 | + project=constants.PROJECT, | | 27 | + location=constants.LOCATION, | | 28 | + logdir=constants.TENSORBOARD_LOG_DIR, | | 29 | + tensorboard_id=constants.TENSORBOARD_ID, | | 30 | + tensorboard_experiment_name=constants.TENSORBOARD_EXPERIMENT_NAME, | | 31 | + experiment_display_name=constants.EXPERIMENT_NAME, | | 32 | + run_name_prefix=constants.EXPERIMENT_RUN_NAME, | | 33 | + description=constants.DESCRIPTION, | | 34 | + ) | | 35 | + | | 36 | + mock_sdk_init.assert_called_once_with( | | 37 | + project=constants.PROJECT, | | 38 | + location=constants.LOCATION, | | 39 | + ) | | 40 | + | | 41 | + mock_tensorboard_uploader_start.assert_called_once_with( | | 42 | + logdir=constants.TENSORBOARD_LOG_DIR, | | 43 | + tensorboard_id=constants.TENSORBOARD_ID, | | 44 | + tensorboard_experiment_name=constants.TENSORBOARD_EXPERIMENT_NAME, | | 45 | + experiment_display_name=constants.EXPERIMENT_NAME, | | 46 | + run_name_prefix=constants.EXPERIMENT_RUN_NAME, | | 47 | + description=constants.DESCRIPTION, | | 48 | + ) | | 49 | + | | 50 | + mock_tensorboard_uploader_end.assert_called_once_with() |
Collapse file Copy file name to clipboard +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Optional | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_upload_tensorboard_log_one_time_sample] | | 21 | +def upload_tensorboard_log_one_time_sample( | | 22 | + tensorboard_experiment_name: str, | | 23 | + logdir: str, | | 24 | + tensorboard_id: str, | | 25 | + project: str, | | 26 | + location: str, | | 27 | + experiment_display_name: Optional[str] = None, | | 28 | + run_name_prefix: Optional[str] = None, | | 29 | + description: Optional[str] = None, | | 30 | + verbosity: Optional[int] = 1, | | 31 | +) -> None: | | 32 | + | | 33 | + aiplatform.init(project=project, location=location) | | 34 | + | | 35 | + # one time upload | | 36 | + aiplatform.upload_tb_log( | | 37 | + tensorboard_id=tensorboard_id, | | 38 | + tensorboard_experiment_name=tensorboard_experiment_name, | | 39 | + logdir=logdir, | | 40 | + experiment_display_name=experiment_display_name, | | 41 | + run_name_prefix=run_name_prefix, | | 42 | + description=description, | | 43 | + ) | | 44 | + | | 45 | + | | 46 | +# [END aiplatform_sdk_upload_tensorboard_log_one_time_sample] |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + | | 16 | +from experiment_tracking import upload_tensorboard_log_one_time_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_upload_tensorboard_log_one_time_sample( | | 21 | + mock_sdk_init, | | 22 | + mock_tensorboard_uploader_onetime, | | 23 | +): | | 24 | + upload_tensorboard_log_one_time_sample.upload_tensorboard_log_one_time_sample( | | 25 | + project=constants.PROJECT, | | 26 | + location=constants.LOCATION, | | 27 | + logdir=constants.TENSORBOARD_LOG_DIR, | | 28 | + tensorboard_id=constants.TENSORBOARD_ID, | | 29 | + tensorboard_experiment_name=constants.TENSORBOARD_EXPERIMENT_NAME, | | 30 | + experiment_display_name=constants.EXPERIMENT_NAME, | | 31 | + run_name_prefix=constants.EXPERIMENT_RUN_NAME, | | 32 | + description=constants.DESCRIPTION, | | 33 | + ) | | 34 | + | | 35 | + mock_sdk_init.assert_called_once_with( | | 36 | + project=constants.PROJECT, | | 37 | + location=constants.LOCATION, | | 38 | + ) | | 39 | + | | 40 | + mock_tensorboard_uploader_onetime.assert_called_once_with( | | 41 | + logdir=constants.TENSORBOARD_LOG_DIR, | | 42 | + tensorboard_id=constants.TENSORBOARD_ID, | | 43 | + tensorboard_experiment_name=constants.TENSORBOARD_EXPERIMENT_NAME, | | 44 | + experiment_display_name=constants.EXPERIMENT_NAME, | | 45 | + run_name_prefix=constants.EXPERIMENT_RUN_NAME, | | 46 | + description=constants.DESCRIPTION, | | 47 | + ) |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/test_constants.py +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 272 | 272 | VALIDATION_OPTIONS = "fail-pipeline" | 273 | 273 | PREDEFINED_SPLIT_COLUMN_NAME = "predefined" | 274 | 274 |
| 275 | | -TENSORBOARD_NAME = ( | 276 | | - f"projects/{PROJECT}/locations/{LOCATION}/tensorboards/my-tensorboard" | 277 | | -) | 278 | | - | 279 | 275 | SCHEMA_TITLE = "system.Schema" | 280 | 276 | SCHEMA_VERSION = "0.0.1" | 281 | 277 | METADATA = {} | | 332 | 328 | IS_DEFAULT_VERSION = False | 333 | 329 | VERSION_ALIASES = ["test-version-alias"] | 334 | 330 | VERSION_DESCRIPTION = "test-version-description" | | 331 | + | | 332 | +# TensorBoard | | 333 | +TENSORBOARD_LOG_DIR = "gs://fake-dir" | | 334 | +TENSORBOARD_ID = "8888888888888888888" | | 335 | +TENSORBOARD_NAME = ( | | 336 | + f"projects/{PROJECT}/locations/{LOCATION}/tensorboards/my-tensorboard" | | 337 | +) | | 338 | +TENSORBOARD_EXPERIMENT_NAME = "my-tensorboard-experiment" |
|