Skip to content

Commit 75655af

Browse files
KCFindstrcopybara-github
authored andcommitted
feat: Expose system_labels field to model deployment APIs in Vertex Python SDK
PiperOrigin-RevId: 697852093
1 parent 13cede4 commit 75655af

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

google/cloud/aiplatform/models.py

+35
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@ def deploy(
12911291
reservation_affinity_values: Optional[List[str]] = None,
12921292
spot: bool = False,
12931293
fast_tryout_enabled: bool = False,
1294+
system_labels: Optional[Dict[str, str]] = None,
12941295
) -> None:
12951296
"""Deploys a Model to the Endpoint.
12961297
@@ -1403,6 +1404,9 @@ def deploy(
14031404
If True, model will be deployed using faster deployment path.
14041405
Useful for quick experiments. Not for production workloads. Only
14051406
available for most popular models with certain machine types.
1407+
system_labels (Dict[str, str]):
1408+
Optional. System labels to apply to Model Garden deployments.
1409+
System labels are managed by Google for internal use only.
14061410
"""
14071411
self._sync_gca_resource_if_skipped()
14081412

@@ -1447,6 +1451,7 @@ def deploy(
14471451
disable_container_logging=disable_container_logging,
14481452
deployment_resource_pool=deployment_resource_pool,
14491453
fast_tryout_enabled=fast_tryout_enabled,
1454+
system_labels=system_labels,
14501455
)
14511456

14521457
@base.optional_sync()
@@ -1477,6 +1482,7 @@ def _deploy(
14771482
disable_container_logging: bool = False,
14781483
deployment_resource_pool: Optional[DeploymentResourcePool] = None,
14791484
fast_tryout_enabled: bool = False,
1485+
system_labels: Optional[Dict[str, str]] = None,
14801486
) -> None:
14811487
"""Deploys a Model to the Endpoint.
14821488
@@ -1583,6 +1589,9 @@ def _deploy(
15831589
If True, model will be deployed using faster deployment path.
15841590
Useful for quick experiments. Not for production workloads. Only
15851591
available for most popular models with certain machine types.
1592+
system_labels (Dict[str, str]):
1593+
Optional. System labels to apply to Model Garden deployments.
1594+
System labels are managed by Google for internal use only.
15861595
"""
15871596
_LOGGER.log_action_start_against_resource(
15881597
f"Deploying Model {model.resource_name} to", "", self
@@ -1617,6 +1626,7 @@ def _deploy(
16171626
disable_container_logging=disable_container_logging,
16181627
deployment_resource_pool=deployment_resource_pool,
16191628
fast_tryout_enabled=fast_tryout_enabled,
1629+
system_labels=system_labels,
16201630
)
16211631

16221632
_LOGGER.log_action_completed_against_resource("model", "deployed", self)
@@ -1654,6 +1664,7 @@ def _deploy_call(
16541664
disable_container_logging: bool = False,
16551665
deployment_resource_pool: Optional[DeploymentResourcePool] = None,
16561666
fast_tryout_enabled: bool = False,
1667+
system_labels: Optional[Dict[str, str]] = None,
16571668
) -> None:
16581669
"""Helper method to deploy model to endpoint.
16591670
@@ -1767,6 +1778,9 @@ def _deploy_call(
17671778
If True, model will be deployed using faster deployment path.
17681779
Useful for quick experiments. Not for production workloads. Only
17691780
available for most popular models with certain machine types.
1781+
system_labels (Dict[str, str]):
1782+
Optional. System labels to apply to Model Garden deployments.
1783+
System labels are managed by Google for internal use only.
17701784
17711785
Raises:
17721786
ValueError: If only `accelerator_type` or `accelerator_count` is specified.
@@ -1788,6 +1802,9 @@ def _deploy_call(
17881802
disable_container_logging=disable_container_logging,
17891803
)
17901804

1805+
if system_labels:
1806+
deployed_model.system_labels = system_labels
1807+
17911808
supports_shared_resources = (
17921809
gca_model_compat.Model.DeploymentResourcesType.SHARED_RESOURCES
17931810
in model.supported_deployment_resources_types
@@ -1847,6 +1864,9 @@ def _deploy_call(
18471864
disable_container_logging=disable_container_logging,
18481865
)
18491866

1867+
if system_labels:
1868+
deployed_model.system_labels = system_labels
1869+
18501870
supports_automatic_resources = (
18511871
gca_model_compat.Model.DeploymentResourcesType.AUTOMATIC_RESOURCES
18521872
in model.supported_deployment_resources_types
@@ -3911,6 +3931,7 @@ def deploy(
39113931
reservation_affinity_key: Optional[str] = None,
39123932
reservation_affinity_values: Optional[List[str]] = None,
39133933
spot: bool = False,
3934+
system_labels: Optional[Dict[str, str]] = None,
39143935
) -> None:
39153936
"""Deploys a Model to the PrivateEndpoint.
39163937
@@ -4026,6 +4047,9 @@ def deploy(
40264047
Format: 'projects/{project_id_or_number}/zones/{zone}/reservations/{reservation_name}'
40274048
spot (bool):
40284049
Optional. Whether to schedule the deployment workload on spot VMs.
4050+
system_labels (Dict[str, str]):
4051+
Optional. System labels to apply to Model Garden deployments.
4052+
System labels are managed by Google for internal use only.
40294053
"""
40304054

40314055
if self.network:
@@ -4070,6 +4094,7 @@ def deploy(
40704094
sync=sync,
40714095
spot=spot,
40724096
disable_container_logging=disable_container_logging,
4097+
system_labels=system_labels,
40734098
)
40744099

40754100
def update(
@@ -5133,6 +5158,7 @@ def deploy(
51335158
reservation_affinity_values: Optional[List[str]] = None,
51345159
spot: bool = False,
51355160
fast_tryout_enabled: bool = False,
5161+
system_labels: Optional[Dict[str, str]] = None,
51365162
) -> Union[Endpoint, PrivateEndpoint]:
51375163
"""Deploys model to endpoint. Endpoint will be created if unspecified.
51385164
@@ -5267,6 +5293,9 @@ def deploy(
52675293
If True, model will be deployed using faster deployment path.
52685294
Useful for quick experiments. Not for production workloads. Only
52695295
available for most popular models with certain machine types.
5296+
system_labels (Dict[str, str]):
5297+
Optional. System labels to apply to Model Garden deployments.
5298+
System labels are managed by Google for internal use only.
52705299
52715300
Returns:
52725301
endpoint (Union[Endpoint, PrivateEndpoint]):
@@ -5336,6 +5365,7 @@ def deploy(
53365365
private_service_connect_config=private_service_connect_config,
53375366
deployment_resource_pool=deployment_resource_pool,
53385367
fast_tryout_enabled=fast_tryout_enabled,
5368+
system_labels=system_labels,
53395369
)
53405370

53415371
@base.optional_sync(return_input_arg="endpoint", bind_future_to_self=False)
@@ -5371,6 +5401,7 @@ def _deploy(
53715401
] = None,
53725402
deployment_resource_pool: Optional[DeploymentResourcePool] = None,
53735403
fast_tryout_enabled: bool = False,
5404+
system_labels: Optional[Dict[str, str]] = None,
53745405
) -> Union[Endpoint, PrivateEndpoint]:
53755406
"""Deploys model to endpoint. Endpoint will be created if unspecified.
53765407
@@ -5498,6 +5529,9 @@ def _deploy(
54985529
If True, model will be deployed using faster deployment path.
54995530
Useful for quick experiments. Not for production workloads. Only
55005531
available for most popular models with certain machine types.
5532+
system_labels (Dict[str, str]):
5533+
Optional. System labels to apply to Model Garden deployments.
5534+
System labels are managed by Google for internal use only.
55015535
55025536
Returns:
55035537
endpoint (Union[Endpoint, PrivateEndpoint]):
@@ -5557,6 +5591,7 @@ def _deploy(
55575591
disable_container_logging=disable_container_logging,
55585592
deployment_resource_pool=deployment_resource_pool,
55595593
fast_tryout_enabled=fast_tryout_enabled,
5594+
system_labels=system_labels,
55605595
)
55615596

55625597
_LOGGER.log_action_completed_against_resource("model", "deployed", endpoint)

tests/unit/aiplatform/test_endpoints.py

+45
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,51 @@ def test_preview_deploy_with_system_labels(self, preview_deploy_model_mock, sync
22082208
timeout=None,
22092209
)
22102210

2211+
@pytest.mark.usefixtures("get_endpoint_mock", "get_model_mock")
2212+
@pytest.mark.parametrize("sync", [True, False])
2213+
def test_deploy_with_system_labels(self, deploy_model_mock, sync):
2214+
test_endpoint = models.Endpoint(_TEST_ENDPOINT_NAME)
2215+
test_model = models.Model(_TEST_ID)
2216+
test_model._gca_resource.supported_deployment_resources_types.append(
2217+
aiplatform.gapic.Model.DeploymentResourcesType.DEDICATED_RESOURCES,
2218+
)
2219+
2220+
test_endpoint.deploy(
2221+
model=test_model,
2222+
sync=sync,
2223+
deploy_request_timeout=None,
2224+
machine_type=_TEST_MACHINE_TYPE,
2225+
accelerator_type=_TEST_ACCELERATOR_TYPE,
2226+
accelerator_count=_TEST_ACCELERATOR_COUNT,
2227+
system_labels=_TEST_LABELS,
2228+
)
2229+
if not sync:
2230+
test_endpoint.wait()
2231+
2232+
expected_machine_spec = gca_machine_resources.MachineSpec(
2233+
machine_type=_TEST_MACHINE_TYPE,
2234+
accelerator_type=_TEST_ACCELERATOR_TYPE,
2235+
accelerator_count=_TEST_ACCELERATOR_COUNT,
2236+
)
2237+
expected_dedicated_resources = gca_machine_resources.DedicatedResources(
2238+
machine_spec=expected_machine_spec,
2239+
min_replica_count=1,
2240+
max_replica_count=1,
2241+
)
2242+
expected_deployed_model = gca_endpoint.DeployedModel(
2243+
dedicated_resources=expected_dedicated_resources,
2244+
model=test_model.resource_name,
2245+
display_name=None,
2246+
system_labels=_TEST_LABELS,
2247+
)
2248+
deploy_model_mock.assert_called_once_with(
2249+
endpoint=test_endpoint.resource_name,
2250+
deployed_model=expected_deployed_model,
2251+
traffic_split={"0": 100},
2252+
metadata=(),
2253+
timeout=None,
2254+
)
2255+
22112256
@pytest.mark.usefixtures("get_endpoint_mock", "get_model_mock", "get_drp_mock")
22122257
@pytest.mark.parametrize("sync", [True, False])
22132258
def test_deploy_with_deployment_resource_pool(self, deploy_model_mock, sync):

tests/unit/aiplatform/test_models.py

+48
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,54 @@ def test_preview_deploy_with_system_labels(self, preview_deploy_model_mock, sync
26792679
timeout=None,
26802680
)
26812681

2682+
@pytest.mark.usefixtures(
2683+
"get_model_mock",
2684+
"create_endpoint_mock",
2685+
"get_endpoint_mock",
2686+
)
2687+
@pytest.mark.parametrize("sync", [True, False])
2688+
def test_deploy_with_system_labels(self, deploy_model_mock, sync):
2689+
test_model = models.Model(_TEST_ID)
2690+
test_model._gca_resource.supported_deployment_resources_types.append(
2691+
aiplatform.gapic.Model.DeploymentResourcesType.DEDICATED_RESOURCES
2692+
)
2693+
2694+
test_endpoint = test_model.deploy(
2695+
machine_type=_TEST_MACHINE_TYPE,
2696+
accelerator_type=_TEST_ACCELERATOR_TYPE,
2697+
accelerator_count=_TEST_ACCELERATOR_COUNT,
2698+
sync=sync,
2699+
deploy_request_timeout=None,
2700+
system_labels=_TEST_LABELS,
2701+
)
2702+
2703+
if not sync:
2704+
test_endpoint.wait()
2705+
2706+
expected_machine_spec = gca_machine_resources.MachineSpec(
2707+
machine_type=_TEST_MACHINE_TYPE,
2708+
accelerator_type=_TEST_ACCELERATOR_TYPE,
2709+
accelerator_count=_TEST_ACCELERATOR_COUNT,
2710+
)
2711+
expected_dedicated_resources = gca_machine_resources.DedicatedResources(
2712+
machine_spec=expected_machine_spec,
2713+
min_replica_count=1,
2714+
max_replica_count=1,
2715+
)
2716+
expected_deployed_model = gca_endpoint.DeployedModel(
2717+
dedicated_resources=expected_dedicated_resources,
2718+
model=test_model.resource_name,
2719+
display_name=None,
2720+
system_labels=_TEST_LABELS,
2721+
)
2722+
deploy_model_mock.assert_called_once_with(
2723+
endpoint=test_endpoint.resource_name,
2724+
deployed_model=expected_deployed_model,
2725+
traffic_split={"0": 100},
2726+
metadata=(),
2727+
timeout=None,
2728+
)
2729+
26822730
@pytest.mark.usefixtures(
26832731
"get_model_mock",
26842732
"preview_get_drp_mock",

0 commit comments

Comments
 (0)