@@ -471,6 +471,80 @@ def list(
471471 )
472472
473473
474+ class RolloutOptions (object ):
475+ """RolloutOptions contains configurations for rolling deployments.
476+
477+ Attributes:
478+ previous_deployed_model:
479+ The ID of the previous deployed model.
480+ max_surge_percentage:
481+ Maximum additional replicas to create during the deployment,
482+ specified as a percentage of the current replica count.
483+ max_surge_replicas:
484+ Maximum number of additional replicas to create during the
485+ deployment.
486+ max_unavailable_percentage:
487+ Maximum amount of replicas that can be unavailable during the
488+ deployment, specified as a percentage of the current replica count.
489+ max_unavailable_replicas:
490+ Maximum number of replicas that can be unavailable during the
491+ deployment.
492+ """
493+
494+ def __init__ (
495+ self ,
496+ previous_deployed_model : int ,
497+ max_surge_percentage : Optional [int ] = None ,
498+ max_surge_replicas : Optional [int ] = None ,
499+ max_unavailable_percentage : Optional [int ] = None ,
500+ max_unavailable_replicas : Optional [int ] = None ,
501+ ):
502+ self .previous_deployed_model = previous_deployed_model
503+ self .max_surge_percentage = max_surge_percentage
504+ self .max_surge_replicas = max_surge_replicas
505+ self .max_unavailable_percentage = max_unavailable_percentage
506+ self .max_unavailable_replicas = max_unavailable_replicas
507+
508+ @classmethod
509+ def from_gapic (cls , opts : gca_endpoint_compat .RolloutOptions ) -> "RolloutOptions" :
510+ return cls (
511+ previous_deployed_model = int (opts .previous_deployed_model ),
512+ max_surge_percentage = opts .max_surge_percentage ,
513+ max_surge_replicas = opts .max_surge_replicas ,
514+ max_unavailable_percentage = opts .max_unavailable_percentage ,
515+ max_unavailable_replicas = opts .max_unavailable_replicas ,
516+ )
517+
518+ def to_gapic (self ) -> gca_endpoint_compat .RolloutOptions :
519+ """Converts RolloutOptions class to gapic RolloutOptions proto."""
520+ result = gca_endpoint_compat .RolloutOptions (
521+ previous_deployed_model = str (self .previous_deployed_model ),
522+ )
523+ if self .max_surge_percentage :
524+ if self .max_surge_replicas :
525+ raise ValueError (
526+ "max_surge_percentage and max_surge_replicas cannot both be" " set."
527+ )
528+ result .max_surge_percentage = self .max_surge_percentage
529+ elif self .max_surge_replicas :
530+ result .max_surge_replicas = self .max_surge_replicas
531+ else :
532+ result .max_surge_replicas = 0
533+ if self .max_unavailable_percentage :
534+ if self .max_unavailable_replicas :
535+ raise ValueError (
536+ "max_unavailable_percentage and max_unavailable_replicas"
537+ " cannot both be set."
538+ )
539+ result .max_unavailable_percentage = self .max_unavailable_percentage
540+ elif self .max_unavailable_replicas :
541+ result .max_unavailable_replicas = self .max_unavailable_replicas
542+ else :
543+ result .max_unavailable_replicas = 0
544+
545+ return result
546+
547+
474548class Endpoint (aiplatform .Endpoint ):
475549 @staticmethod
476550 def _validate_deploy_args (
@@ -616,6 +690,7 @@ def deploy(
616690 fast_tryout_enabled : bool = False ,
617691 system_labels : Optional [Dict [str , str ]] = None ,
618692 required_replica_count : Optional [int ] = 0 ,
693+ rollout_options : Optional [RolloutOptions ] = None ,
619694 ) -> None :
620695 """Deploys a Model to the Endpoint.
621696
@@ -712,6 +787,8 @@ def deploy(
712787 set, the model deploy/mutate operation will succeed once
713788 available_replica_count reaches required_replica_count, and the
714789 rest of the replicas will be retried.
790+ rollout_options (RolloutOptions):
791+ Optional. Options to configure a rolling deployment.
715792
716793 """
717794 self ._sync_gca_resource_if_skipped ()
@@ -754,6 +831,7 @@ def deploy(
754831 fast_tryout_enabled = fast_tryout_enabled ,
755832 system_labels = system_labels ,
756833 required_replica_count = required_replica_count ,
834+ rollout_options = rollout_options ,
757835 )
758836
759837 @base .optional_sync ()
@@ -780,6 +858,7 @@ def _deploy(
780858 fast_tryout_enabled : bool = False ,
781859 system_labels : Optional [Dict [str , str ]] = None ,
782860 required_replica_count : Optional [int ] = 0 ,
861+ rollout_options : Optional [RolloutOptions ] = None ,
783862 ) -> None :
784863 """Deploys a Model to the Endpoint.
785864
@@ -870,7 +949,8 @@ def _deploy(
870949 set, the model deploy/mutate operation will succeed once
871950 available_replica_count reaches required_replica_count, and the
872951 rest of the replicas will be retried.
873-
952+ rollout_options (RolloutOptions): Optional.
953+ Options to configure a rolling deployment.
874954 """
875955 _LOGGER .log_action_start_against_resource (
876956 f"Deploying Model { model .resource_name } to" , "" , self
@@ -901,6 +981,7 @@ def _deploy(
901981 fast_tryout_enabled = fast_tryout_enabled ,
902982 system_labels = system_labels ,
903983 required_replica_count = required_replica_count ,
984+ rollout_options = rollout_options ,
904985 )
905986
906987 _LOGGER .log_action_completed_against_resource ("model" , "deployed" , self )
@@ -934,6 +1015,7 @@ def _deploy_call(
9341015 fast_tryout_enabled : bool = False ,
9351016 system_labels : Optional [Dict [str , str ]] = None ,
9361017 required_replica_count : Optional [int ] = 0 ,
1018+ rollout_options : Optional [RolloutOptions ] = None ,
9371019 ) -> None :
9381020 """Helper method to deploy model to endpoint.
9391021
@@ -1031,6 +1113,8 @@ def _deploy_call(
10311113 set, the model deploy/mutate operation will succeed once
10321114 available_replica_count reaches required_replica_count, and the
10331115 rest of the replicas will be retried.
1116+ rollout_options (RolloutOptions): Optional. Options to configure a
1117+ rolling deployment.
10341118
10351119 Raises:
10361120 ValueError: If only `accelerator_type` or `accelerator_count` is
@@ -1103,7 +1187,7 @@ def _deploy_call(
11031187 machine_type = _DEFAULT_MACHINE_TYPE
11041188 _LOGGER .info (f"Using default machine_type: { machine_type } " )
11051189
1106- if use_dedicated_resources :
1190+ if use_dedicated_resources and not rollout_options :
11071191 dedicated_resources = gca_machine_resources_compat .DedicatedResources (
11081192 min_replica_count = min_replica_count ,
11091193 max_replica_count = max_replica_count ,
@@ -1146,6 +1230,15 @@ def _deploy_call(
11461230 )
11471231 )
11481232 deployed_model .dedicated_resources = dedicated_resources
1233+ elif rollout_options :
1234+ deployed_model .rollout_options = rollout_options .to_gapic ()
1235+ elif supports_automatic_resources :
1236+ deployed_model .automatic_resources = (
1237+ gca_machine_resources_compat .AutomaticResources (
1238+ min_replica_count = min_replica_count ,
1239+ max_replica_count = max_replica_count ,
1240+ )
1241+ )
11491242 else :
11501243 deployed_model = gca_endpoint_compat .DeployedModel (
11511244 model = model .versioned_resource_name ,
@@ -1444,6 +1537,7 @@ def deploy(
14441537 fast_tryout_enabled : bool = False ,
14451538 system_labels : Optional [Dict [str , str ]] = None ,
14461539 required_replica_count : Optional [int ] = 0 ,
1540+ rollout_options : Optional [RolloutOptions ] = None ,
14471541 ) -> Union [Endpoint , models .PrivateEndpoint ]:
14481542 """Deploys model to endpoint.
14491543
@@ -1561,6 +1655,8 @@ def deploy(
15611655 set, the model deploy/mutate operation will succeed once
15621656 available_replica_count reaches required_replica_count, and the
15631657 rest of the replicas will be retried.
1658+ rollout_options (RolloutOptions):
1659+ Optional. Options to configure a rolling deployment.
15641660
15651661 Returns:
15661662 endpoint (Union[Endpoint, models.PrivateEndpoint]):
@@ -1620,6 +1716,7 @@ def deploy(
16201716 fast_tryout_enabled = fast_tryout_enabled ,
16211717 system_labels = system_labels ,
16221718 required_replica_count = required_replica_count ,
1719+ rollout_options = rollout_options ,
16231720 )
16241721
16251722 def _should_enable_dedicated_endpoint (self , fast_tryout_enabled : bool ) -> bool :
@@ -1655,6 +1752,7 @@ def _deploy(
16551752 fast_tryout_enabled : bool = False ,
16561753 system_labels : Optional [Dict [str , str ]] = None ,
16571754 required_replica_count : Optional [int ] = 0 ,
1755+ rollout_options : Optional [RolloutOptions ] = None ,
16581756 ) -> Union [Endpoint , models .PrivateEndpoint ]:
16591757 """Deploys model to endpoint.
16601758
@@ -1763,6 +1861,8 @@ def _deploy(
17631861 set, the model deploy/mutate operation will succeed once
17641862 available_replica_count reaches required_replica_count, and the
17651863 rest of the replicas will be retried.
1864+ rollout_options (RolloutOptions):
1865+ Optional. Options to configure a rolling deployment.
17661866
17671867 Returns:
17681868 endpoint (Union[Endpoint, models.PrivateEndpoint]):
@@ -1771,6 +1871,10 @@ def _deploy(
17711871
17721872 if endpoint is None :
17731873 display_name = self .display_name [:118 ] + "_endpoint"
1874+ if rollout_options is not None :
1875+ raise ValueError (
1876+ "Rollout options may only be used when deploying to an existing endpoint."
1877+ )
17741878
17751879 if not network :
17761880 endpoint = Endpoint .create (
@@ -1792,6 +1896,10 @@ def _deploy(
17921896 credentials = self .credentials ,
17931897 encryption_spec_key_name = encryption_spec_key_name ,
17941898 )
1899+ if isinstance (endpoint , Endpoint ):
1900+ preview_kwargs = {"rollout_options" : rollout_options }
1901+ else :
1902+ preview_kwargs = {}
17951903
17961904 _LOGGER .log_action_start_against_resource ("Deploying model to" , "" , endpoint )
17971905
@@ -1820,6 +1928,7 @@ def _deploy(
18201928 fast_tryout_enabled = fast_tryout_enabled ,
18211929 system_labels = system_labels ,
18221930 required_replica_count = required_replica_count ,
1931+ ** preview_kwargs ,
18231932 )
18241933
18251934 _LOGGER .log_action_completed_against_resource ("model" , "deployed" , endpoint )
0 commit comments