GeezerVison is showing github.com — feat: Added create_training_pipeline_custom_package_job_sample and cr… · aditdn/python-aiplatform@7abf8ef
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 +43-5Lines changed: 43 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -147,6 +147,18 @@ def mock_custom_training_job(): | 147 | 147 | yield mock | 148 | 148 |
| 149 | 149 |
| | 150 | +@pytest.fixture | | 151 | +def mock_custom_container_training_job(): | | 152 | + mock = MagicMock(aiplatform.training_jobs.CustomContainerTrainingJob) | | 153 | + yield mock | | 154 | + | | 155 | + | | 156 | +@pytest.fixture | | 157 | +def mock_custom_package_training_job(): | | 158 | + mock = MagicMock(aiplatform.training_jobs.CustomPythonPackageTrainingJob) | | 159 | + yield mock | | 160 | + | | 161 | + | 150 | 162 | @pytest.fixture | 151 | 163 | def mock_image_training_job(): | 152 | 164 | mock = MagicMock(aiplatform.training_jobs.AutoMLImageTrainingJob) | @@ -198,15 +210,41 @@ def mock_run_automl_image_training_job(mock_image_training_job): | 198 | 210 |
| 199 | 211 |
| 200 | 212 | @pytest.fixture | 201 | | -def mock_init_custom_training_job(): | 202 | | - with patch.object(aiplatform.training_jobs.CustomTrainingJob, "__init__") as mock: | 203 | | - mock.return_value = None | | 213 | +def mock_get_custom_training_job(mock_custom_training_job): | | 214 | + with patch.object(aiplatform, "CustomTrainingJob") as mock: | | 215 | + mock.return_value = mock_custom_training_job | | 216 | + yield mock | | 217 | + | | 218 | + | | 219 | +@pytest.fixture | | 220 | +def mock_get_custom_container_training_job(mock_custom_container_training_job): | | 221 | + with patch.object(aiplatform, "CustomContainerTrainingJob") as mock: | | 222 | + mock.return_value = mock_custom_container_training_job | | 223 | + yield mock | | 224 | + | | 225 | + | | 226 | +@pytest.fixture | | 227 | +def mock_get_custom_package_training_job(mock_custom_package_training_job): | | 228 | + with patch.object(aiplatform, "CustomPythonPackageTrainingJob") as mock: | | 229 | + mock.return_value = mock_custom_package_training_job | | 230 | + yield mock | | 231 | + | | 232 | + | | 233 | +@pytest.fixture | | 234 | +def mock_run_custom_training_job(mock_custom_training_job): | | 235 | + with patch.object(mock_custom_training_job, "run") as mock: | | 236 | + yield mock | | 237 | + | | 238 | + | | 239 | +@pytest.fixture | | 240 | +def mock_run_custom_container_training_job(mock_custom_container_training_job): | | 241 | + with patch.object(mock_custom_container_training_job, "run") as mock: | 204 | 242 | yield mock | 205 | 243 |
| 206 | 244 |
| 207 | 245 | @pytest.fixture | 208 | | -def mock_run_custom_training_job(): | 209 | | - with patch.object(aiplatform.training_jobs.CustomTrainingJob, "run") as mock: | | 246 | +def mock_run_custom_package_training_job(mock_custom_package_training_job): | | 247 | + with patch.object(mock_custom_package_training_job, "run") as mock: | 210 | 248 | yield mock | 211 | 249 |
| 212 | 250 |
| |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/create_training_pipeline_custom_container_job_sample.py +11-11Lines changed: 11 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | from google.cloud import aiplatform | 18 | 18 |
| 19 | 19 |
| 20 | | -# [START aiplatform_sdk_create_training_pipeline_custom_job_sample] | 21 | | -def create_training_pipeline_custom_training_managed_dataset_sample( | | 20 | +# [START aiplatform_sdk_create_training_pipeline_custom_container_job_sample] | | 21 | +def create_training_pipeline_custom_container_job_sample( | 22 | 22 | project: str, | 23 | 23 | location: str, | | 24 | + staging_bucket: str, | 24 | 25 | display_name: str, | 25 | | - script_path: str, | 26 | 26 | container_uri: str, | 27 | 27 | model_serving_container_image_uri: str, | 28 | | - dataset_id: int, | | 28 | + dataset_id: Optional[str] = None, | 29 | 29 | model_display_name: Optional[str] = None, | 30 | 30 | args: Optional[List[Union[str, float, int]]] = None, | 31 | | - replica_count: int = 0, | | 31 | + replica_count: int = 1, | 32 | 32 | machine_type: str = "n1-standard-4", | 33 | 33 | accelerator_type: str = "ACCELERATOR_TYPE_UNSPECIFIED", | 34 | 34 | accelerator_count: int = 0, | @@ -37,19 +37,19 @@ def create_training_pipeline_custom_training_managed_dataset_sample( | 37 | 37 | test_fraction_split: float = 0.1, | 38 | 38 | sync: bool = True, | 39 | 39 | ): | 40 | | - aiplatform.init(project=project, location=location) | | 40 | + aiplatform.init(project=project, location=location, staging_bucket=staging_bucket) | 41 | 41 |
| 42 | | - job = aiplatform.CustomTrainingJob( | | 42 | + job = aiplatform.CustomContainerTrainingJob( | 43 | 43 | display_name=display_name, | 44 | | - script_path=script_path, | 45 | 44 | container_uri=container_uri, | 46 | 45 | model_serving_container_image_uri=model_serving_container_image_uri, | 47 | 46 | ) | 48 | 47 |
| 49 | | - my_image_ds = aiplatform.ImageDataset(dataset_id) | | 48 | + # This example uses an ImageDataset, but you can use another type | | 49 | + dataset = aiplatform.ImageDataset(dataset_id) if dataset_id else None | 50 | 50 |
| 51 | 51 | model = job.run( | 52 | | - dataset=my_image_ds, | | 52 | + dataset=dataset, | 53 | 53 | model_display_name=model_display_name, | 54 | 54 | args=args, | 55 | 55 | replica_count=replica_count, | @@ -70,4 +70,4 @@ def create_training_pipeline_custom_training_managed_dataset_sample( | 70 | 70 | return model | 71 | 71 |
| 72 | 72 |
| 73 | | -# [END aiplatform_sdk_create_training_pipeline_custom_job_sample] | | 73 | +# [END aiplatform_sdk_create_training_pipeline_custom_container_job_sample] |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/create_training_pipeline_custom_container_job_test.py +13-13Lines changed: 13 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | # limitations under the License. | 14 | 14 |
| 15 | 15 |
| 16 | | -import create_training_pipeline_custom_training_managed_dataset_sample | | 16 | +import create_training_pipeline_custom_container_job_sample | 17 | 17 | import test_constants as constants | 18 | 18 |
| 19 | 19 |
| 20 | | -def test_create_training_pipeline_custom_job_sample( | | 20 | +def test_create_training_pipeline_custom_container_job_sample( | 21 | 21 | mock_sdk_init, | 22 | 22 | mock_image_dataset, | 23 | | - mock_init_custom_training_job, | 24 | | - mock_run_custom_training_job, | 25 | 23 | mock_get_image_dataset, | | 24 | + mock_get_custom_container_training_job, | | 25 | + mock_run_custom_container_training_job, | 26 | 26 | ): | 27 | 27 |
| 28 | | - create_training_pipeline_custom_training_managed_dataset_sample.create_training_pipeline_custom_training_managed_dataset_sample( | | 28 | + create_training_pipeline_custom_container_job_sample.create_training_pipeline_custom_container_job_sample( | 29 | 29 | project=constants.PROJECT, | 30 | 30 | location=constants.LOCATION, | | 31 | + staging_bucket=constants.STAGING_BUCKET, | 31 | 32 | display_name=constants.DISPLAY_NAME, | 32 | 33 | args=constants.ARGS, | 33 | | - script_path=constants.SCRIPT_PATH, | 34 | 34 | container_uri=constants.CONTAINER_URI, | 35 | 35 | model_serving_container_image_uri=constants.CONTAINER_URI, | 36 | 36 | dataset_id=constants.RESOURCE_ID, | @@ -44,25 +44,25 @@ def test_create_training_pipeline_custom_job_sample( | 44 | 44 | test_fraction_split=constants.TEST_FRACTION_SPLIT, | 45 | 45 | ) | 46 | 46 |
| 47 | | - mock_get_image_dataset.assert_called_once_with(constants.RESOURCE_ID) | 48 | | - | 49 | 47 | mock_sdk_init.assert_called_once_with( | 50 | | - project=constants.PROJECT, location=constants.LOCATION | | 48 | + project=constants.PROJECT, | | 49 | + location=constants.LOCATION, | | 50 | + staging_bucket=constants.STAGING_BUCKET, | 51 | 51 | ) | 52 | | - mock_init_custom_training_job.assert_called_once_with( | | 52 | + | | 53 | + mock_get_custom_container_training_job.assert_called_once_with( | 53 | 54 | display_name=constants.DISPLAY_NAME, | 54 | | - script_path=constants.SCRIPT_PATH, | 55 | 55 | container_uri=constants.CONTAINER_URI, | 56 | 56 | model_serving_container_image_uri=constants.CONTAINER_URI, | 57 | 57 | ) | 58 | | - mock_run_custom_training_job.assert_called_once_with( | | 58 | + mock_run_custom_container_training_job.assert_called_once_with( | 59 | 59 | dataset=mock_image_dataset, | 60 | 60 | model_display_name=constants.DISPLAY_NAME_2, | 61 | | - args=constants.ARGS, | 62 | 61 | replica_count=constants.REPLICA_COUNT, | 63 | 62 | machine_type=constants.MACHINE_TYPE, | 64 | 63 | accelerator_type=constants.ACCELERATOR_TYPE, | 65 | 64 | accelerator_count=constants.ACCELERATOR_COUNT, | | 65 | + args=constants.ARGS, | 66 | 66 | training_fraction_split=constants.TRAINING_FRACTION_SPLIT, | 67 | 67 | validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT, | 68 | 68 | test_fraction_split=constants.TEST_FRACTION_SPLIT, | |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/create_training_pipeline_custom_job_sample.py +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | def create_training_pipeline_custom_job_sample( | 22 | 22 | project: str, | 23 | 23 | location: str, | | 24 | + staging_bucket: str, | 24 | 25 | display_name: str, | 25 | 26 | script_path: str, | 26 | 27 | container_uri: str, | 27 | 28 | model_serving_container_image_uri: str, | | 29 | + dataset_id: Optional[str] = None, | 28 | 30 | model_display_name: Optional[str] = None, | 29 | 31 | args: Optional[List[Union[str, float, int]]] = None, | 30 | 32 | replica_count: int = 0, | @@ -36,7 +38,7 @@ def create_training_pipeline_custom_job_sample( | 36 | 38 | test_fraction_split: float = 0.1, | 37 | 39 | sync: bool = True, | 38 | 40 | ): | 39 | | - aiplatform.init(project=project, location=location) | | 41 | + aiplatform.init(project=project, location=location, staging_bucket=staging_bucket) | 40 | 42 |
| 41 | 43 | job = aiplatform.CustomTrainingJob( | 42 | 44 | display_name=display_name, | @@ -45,7 +47,11 @@ def create_training_pipeline_custom_job_sample( | 45 | 47 | model_serving_container_image_uri=model_serving_container_image_uri, | 46 | 48 | ) | 47 | 49 |
| | 50 | + # This example uses an ImageDataset, but you can use another type | | 51 | + dataset = aiplatform.ImageDataset(dataset_id) if dataset_id else None | | 52 | + | 48 | 53 | model = job.run( | | 54 | + dataset=dataset, | 49 | 55 | model_display_name=model_display_name, | 50 | 56 | args=args, | 51 | 57 | replica_count=replica_count, | |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/create_training_pipeline_custom_job_test.py +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 |
| 19 | 19 |
| 20 | 20 | def test_create_training_pipeline_custom_job_sample( | 21 | | - mock_sdk_init, mock_init_custom_training_job, mock_run_custom_training_job, | | 21 | + mock_sdk_init, | | 22 | + mock_image_dataset, | | 23 | + mock_get_image_dataset, | | 24 | + mock_get_custom_training_job, | | 25 | + mock_run_custom_training_job, | 22 | 26 | ): | 23 | 27 |
| 24 | 28 | create_training_pipeline_custom_job_sample.create_training_pipeline_custom_job_sample( | 25 | 29 | project=constants.PROJECT, | 26 | 30 | location=constants.LOCATION, | | 31 | + staging_bucket=constants.STAGING_BUCKET, | 27 | 32 | display_name=constants.DISPLAY_NAME, | 28 | 33 | args=constants.ARGS, | 29 | 34 | script_path=constants.SCRIPT_PATH, | 30 | 35 | container_uri=constants.CONTAINER_URI, | 31 | 36 | model_serving_container_image_uri=constants.CONTAINER_URI, | | 37 | + dataset_id=constants.RESOURCE_ID, | 32 | 38 | model_display_name=constants.DISPLAY_NAME_2, | 33 | 39 | replica_count=constants.REPLICA_COUNT, | 34 | 40 | machine_type=constants.MACHINE_TYPE, | @@ -40,15 +46,18 @@ def test_create_training_pipeline_custom_job_sample( | 40 | 46 | ) | 41 | 47 |
| 42 | 48 | mock_sdk_init.assert_called_once_with( | 43 | | - project=constants.PROJECT, location=constants.LOCATION | | 49 | + project=constants.PROJECT, | | 50 | + location=constants.LOCATION, | | 51 | + staging_bucket=constants.STAGING_BUCKET, | 44 | 52 | ) | 45 | | - mock_init_custom_training_job.assert_called_once_with( | | 53 | + mock_get_custom_training_job.assert_called_once_with( | 46 | 54 | display_name=constants.DISPLAY_NAME, | 47 | 55 | script_path=constants.SCRIPT_PATH, | 48 | 56 | container_uri=constants.CONTAINER_URI, | 49 | 57 | model_serving_container_image_uri=constants.CONTAINER_URI, | 50 | 58 | ) | 51 | 59 | mock_run_custom_training_job.assert_called_once_with( | | 60 | + dataset=mock_image_dataset, | 52 | 61 | model_display_name=constants.DISPLAY_NAME_2, | 53 | 62 | replica_count=constants.REPLICA_COUNT, | 54 | 63 | machine_type=constants.MACHINE_TYPE, | |
Collapse file Copy file name to clipboard +77Lines changed: 77 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 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 List, Optional, Union | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_create_training_pipeline_custom_package_job_sample] | | 21 | +def create_training_pipeline_custom_package_job_sample( | | 22 | + project: str, | | 23 | + location: str, | | 24 | + staging_bucket: str, | | 25 | + display_name: str, | | 26 | + python_package_gcs_uri: str, | | 27 | + python_module_name: str, | | 28 | + container_uri: str, | | 29 | + model_serving_container_image_uri: str, | | 30 | + dataset_id: Optional[str] = None, | | 31 | + model_display_name: Optional[str] = None, | | 32 | + args: Optional[List[Union[str, float, int]]] = None, | | 33 | + replica_count: int = 1, | | 34 | + machine_type: str = "n1-standard-4", | | 35 | + accelerator_type: str = "ACCELERATOR_TYPE_UNSPECIFIED", | | 36 | + accelerator_count: int = 0, | | 37 | + training_fraction_split: float = 0.8, | | 38 | + validation_fraction_split: float = 0.1, | | 39 | + test_fraction_split: float = 0.1, | | 40 | + sync: bool = True, | | 41 | +): | | 42 | + aiplatform.init(project=project, location=location, staging_bucket=staging_bucket) | | 43 | + | | 44 | + job = aiplatform.CustomPythonPackageTrainingJob( | | 45 | + display_name=display_name, | | 46 | + python_package_gcs_uri=python_package_gcs_uri, | | 47 | + python_module_name=python_module_name, | | 48 | + container_uri=container_uri, | | 49 | + model_serving_container_image_uri=model_serving_container_image_uri, | | 50 | + ) | | 51 | + | | 52 | + # This example uses an ImageDataset, but you can use another type | | 53 | + dataset = aiplatform.ImageDataset(dataset_id) if dataset_id else None | | 54 | + | | 55 | + model = job.run( | | 56 | + dataset=dataset, | | 57 | + model_display_name=model_display_name, | | 58 | + args=args, | | 59 | + replica_count=replica_count, | | 60 | + machine_type=machine_type, | | 61 | + accelerator_type=accelerator_type, | | 62 | + accelerator_count=accelerator_count, | | 63 | + training_fraction_split=training_fraction_split, | | 64 | + validation_fraction_split=validation_fraction_split, | | 65 | + test_fraction_split=test_fraction_split, | | 66 | + sync=sync, | | 67 | + ) | | 68 | + | | 69 | + model.wait() | | 70 | + | | 71 | + print(model.display_name) | | 72 | + print(model.resource_name) | | 73 | + print(model.uri) | | 74 | + return model | | 75 | + | | 76 | + | | 77 | +# [END aiplatform_sdk_create_training_pipeline_custom_package_job_sample] |
|