47
47
from google .cloud .aiplatform import models
48
48
from google .cloud .aiplatform import utils
49
49
from google .cloud .aiplatform .utils import gcs_utils
50
+ from google .cloud .aiplatform .utils import _explanation_utils
50
51
from google .cloud .aiplatform import model_evaluation
51
-
52
52
from google .cloud .aiplatform .compat .services import endpoint_service_client
53
53
54
54
from google .cloud .aiplatform .compat .types import (
@@ -617,10 +617,6 @@ def _validate_deploy_args(
617
617
deployed_model_display_name : Optional [str ],
618
618
traffic_split : Optional [Dict [str , int ]],
619
619
traffic_percentage : Optional [int ],
620
- explanation_metadata : Optional [aiplatform .explain .ExplanationMetadata ] = None ,
621
- explanation_parameters : Optional [
622
- aiplatform .explain .ExplanationParameters
623
- ] = None ,
624
620
):
625
621
"""Helper method to validate deploy arguments.
626
622
@@ -663,20 +659,10 @@ def _validate_deploy_args(
663
659
not be provided. Traffic of previously deployed models at the endpoint
664
660
will be scaled down to accommodate new deployed model's traffic.
665
661
Should not be provided if traffic_split is provided.
666
- explanation_metadata (aiplatform.explain.ExplanationMetadata):
667
- Optional. Metadata describing the Model's input and output for explanation.
668
- `explanation_metadata` is optional while `explanation_parameters` must be
669
- specified when used.
670
- For more details, see `Ref docs <http://tinyurl.com/1igh60kt>`
671
- explanation_parameters (aiplatform.explain.ExplanationParameters):
672
- Optional. Parameters to configure explaining for Model's predictions.
673
- For more details, see `Ref docs <http://tinyurl.com/1an4zake>`
674
662
675
663
Raises:
676
664
ValueError: if Min or Max replica is negative. Traffic percentage > 100 or
677
665
< 0. Or if traffic_split does not sum to 100.
678
- ValueError: if explanation_metadata is specified while explanation_parameters
679
- is not.
680
666
"""
681
667
if min_replica_count < 0 :
682
668
raise ValueError ("Min replica cannot be negative." )
@@ -697,11 +683,6 @@ def _validate_deploy_args(
697
683
"Sum of all traffic within traffic split needs to be 100."
698
684
)
699
685
700
- if bool (explanation_metadata ) and not bool (explanation_parameters ):
701
- raise ValueError (
702
- "To get model explanation, `explanation_parameters` must be specified."
703
- )
704
-
705
686
# Raises ValueError if invalid accelerator
706
687
if accelerator_type :
707
688
utils .validate_accelerator_type (accelerator_type )
@@ -817,6 +798,9 @@ def deploy(
817
798
deployed_model_display_name = deployed_model_display_name ,
818
799
traffic_split = traffic_split ,
819
800
traffic_percentage = traffic_percentage ,
801
+ )
802
+
803
+ explanation_spec = _explanation_utils .create_and_validate_explanation_spec (
820
804
explanation_metadata = explanation_metadata ,
821
805
explanation_parameters = explanation_parameters ,
822
806
)
@@ -832,8 +816,7 @@ def deploy(
832
816
accelerator_type = accelerator_type ,
833
817
accelerator_count = accelerator_count ,
834
818
service_account = service_account ,
835
- explanation_metadata = explanation_metadata ,
836
- explanation_parameters = explanation_parameters ,
819
+ explanation_spec = explanation_spec ,
837
820
metadata = metadata ,
838
821
sync = sync ,
839
822
deploy_request_timeout = deploy_request_timeout ,
@@ -854,10 +837,7 @@ def _deploy(
854
837
accelerator_type : Optional [str ] = None ,
855
838
accelerator_count : Optional [int ] = None ,
856
839
service_account : Optional [str ] = None ,
857
- explanation_metadata : Optional [aiplatform .explain .ExplanationMetadata ] = None ,
858
- explanation_parameters : Optional [
859
- aiplatform .explain .ExplanationParameters
860
- ] = None ,
840
+ explanation_spec : Optional [aiplatform .explain .ExplanationSpec ] = None ,
861
841
metadata : Optional [Sequence [Tuple [str , str ]]] = (),
862
842
sync = True ,
863
843
deploy_request_timeout : Optional [float ] = None ,
@@ -919,14 +899,8 @@ def _deploy(
919
899
to the resource project.
920
900
Users deploying the Model must have the `iam.serviceAccounts.actAs`
921
901
permission on this service account.
922
- explanation_metadata (aiplatform.explain.ExplanationMetadata):
923
- Optional. Metadata describing the Model's input and output for explanation.
924
- `explanation_metadata` is optional while `explanation_parameters` must be
925
- specified when used.
926
- For more details, see `Ref docs <http://tinyurl.com/1igh60kt>`
927
- explanation_parameters (aiplatform.explain.ExplanationParameters):
928
- Optional. Parameters to configure explaining for Model's predictions.
929
- For more details, see `Ref docs <http://tinyurl.com/1an4zake>`
902
+ explanation_spec (aiplatform.explain.ExplanationSpec):
903
+ Optional. Specification of Model explanation.
930
904
metadata (Sequence[Tuple[str, str]]):
931
905
Optional. Strings which should be sent along with the request as
932
906
metadata.
@@ -963,8 +937,7 @@ def _deploy(
963
937
accelerator_type = accelerator_type ,
964
938
accelerator_count = accelerator_count ,
965
939
service_account = service_account ,
966
- explanation_metadata = explanation_metadata ,
967
- explanation_parameters = explanation_parameters ,
940
+ explanation_spec = explanation_spec ,
968
941
metadata = metadata ,
969
942
deploy_request_timeout = deploy_request_timeout ,
970
943
autoscaling_target_cpu_utilization = autoscaling_target_cpu_utilization ,
@@ -992,10 +965,7 @@ def _deploy_call(
992
965
accelerator_type : Optional [str ] = None ,
993
966
accelerator_count : Optional [int ] = None ,
994
967
service_account : Optional [str ] = None ,
995
- explanation_metadata : Optional [aiplatform .explain .ExplanationMetadata ] = None ,
996
- explanation_parameters : Optional [
997
- aiplatform .explain .ExplanationParameters
998
- ] = None ,
968
+ explanation_spec : Optional [aiplatform .explain .ExplanationSpec ] = None ,
999
969
metadata : Optional [Sequence [Tuple [str , str ]]] = (),
1000
970
deploy_request_timeout : Optional [float ] = None ,
1001
971
autoscaling_target_cpu_utilization : Optional [int ] = None ,
@@ -1066,14 +1036,8 @@ def _deploy_call(
1066
1036
to the resource project.
1067
1037
Users deploying the Model must have the `iam.serviceAccounts.actAs`
1068
1038
permission on this service account.
1069
- explanation_metadata (aiplatform.explain.ExplanationMetadata):
1070
- Optional. Metadata describing the Model's input and output for explanation.
1071
- `explanation_metadata` is optional while `explanation_parameters` must be
1072
- specified when used.
1073
- For more details, see `Ref docs <http://tinyurl.com/1igh60kt>`
1074
- explanation_parameters (aiplatform.explain.ExplanationParameters):
1075
- Optional. Parameters to configure explaining for Model's predictions.
1076
- For more details, see `Ref docs <http://tinyurl.com/1an4zake>`
1039
+ explanation_spec (aiplatform.explain.ExplanationSpec):
1040
+ Optional. Specification of Model explanation.
1077
1041
metadata (Sequence[Tuple[str, str]]):
1078
1042
Optional. Strings which should be sent along with the request as
1079
1043
metadata.
@@ -1199,13 +1163,7 @@ def _deploy_call(
1199
1163
"See https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1#google.cloud.aiplatform.v1.Model.FIELDS.repeated.google.cloud.aiplatform.v1.Model.DeploymentResourcesType.google.cloud.aiplatform.v1.Model.supported_deployment_resources_types"
1200
1164
)
1201
1165
1202
- # Service will throw error if explanation_parameters is not provided
1203
- if explanation_parameters :
1204
- explanation_spec = gca_endpoint_compat .explanation .ExplanationSpec ()
1205
- explanation_spec .parameters = explanation_parameters
1206
- if explanation_metadata :
1207
- explanation_spec .metadata = explanation_metadata
1208
- deployed_model .explanation_spec = explanation_spec
1166
+ deployed_model .explanation_spec = explanation_spec
1209
1167
1210
1168
# Checking if traffic percentage is valid
1211
1169
# TODO(b/221059294) PrivateEndpoint should support traffic split
@@ -2332,6 +2290,9 @@ def deploy(
2332
2290
deployed_model_display_name = deployed_model_display_name ,
2333
2291
traffic_split = None ,
2334
2292
traffic_percentage = 100 ,
2293
+ )
2294
+
2295
+ explanation_spec = _explanation_utils .create_and_validate_explanation_spec (
2335
2296
explanation_metadata = explanation_metadata ,
2336
2297
explanation_parameters = explanation_parameters ,
2337
2298
)
@@ -2347,8 +2308,7 @@ def deploy(
2347
2308
accelerator_type = accelerator_type ,
2348
2309
accelerator_count = accelerator_count ,
2349
2310
service_account = service_account ,
2350
- explanation_metadata = explanation_metadata ,
2351
- explanation_parameters = explanation_parameters ,
2311
+ explanation_spec = explanation_spec ,
2352
2312
metadata = metadata ,
2353
2313
sync = sync ,
2354
2314
)
@@ -3004,11 +2964,6 @@ def upload(
3004
2964
if labels :
3005
2965
utils .validate_labels (labels )
3006
2966
3007
- if bool (explanation_metadata ) and not bool (explanation_parameters ):
3008
- raise ValueError (
3009
- "To get model explanation, `explanation_parameters` must be specified."
3010
- )
3011
-
3012
2967
appended_user_agent = None
3013
2968
if local_model :
3014
2969
container_spec = local_model .get_serving_container_spec ()
@@ -3109,13 +3064,12 @@ def upload(
3109
3064
if artifact_uri :
3110
3065
managed_model .artifact_uri = artifact_uri
3111
3066
3112
- # Override explanation_spec if required field is provided
3113
- if explanation_parameters :
3114
- explanation_spec = gca_endpoint_compat .explanation .ExplanationSpec ()
3115
- explanation_spec .parameters = explanation_parameters
3116
- if explanation_metadata :
3117
- explanation_spec .metadata = explanation_metadata
3118
- managed_model .explanation_spec = explanation_spec
3067
+ managed_model .explanation_spec = (
3068
+ _explanation_utils .create_and_validate_explanation_spec (
3069
+ explanation_metadata = explanation_metadata ,
3070
+ explanation_parameters = explanation_parameters ,
3071
+ )
3072
+ )
3119
3073
3120
3074
request = gca_model_service_compat .UploadModelRequest (
3121
3075
parent = initializer .global_config .common_location_path (project , location ),
@@ -3283,8 +3237,6 @@ def deploy(
3283
3237
deployed_model_display_name = deployed_model_display_name ,
3284
3238
traffic_split = traffic_split ,
3285
3239
traffic_percentage = traffic_percentage ,
3286
- explanation_metadata = explanation_metadata ,
3287
- explanation_parameters = explanation_parameters ,
3288
3240
)
3289
3241
3290
3242
if isinstance (endpoint , PrivateEndpoint ):
@@ -3295,6 +3247,11 @@ def deploy(
3295
3247
"A maximum of one model can be deployed to each private Endpoint."
3296
3248
)
3297
3249
3250
+ explanation_spec = _explanation_utils .create_and_validate_explanation_spec (
3251
+ explanation_metadata = explanation_metadata ,
3252
+ explanation_parameters = explanation_parameters ,
3253
+ )
3254
+
3298
3255
return self ._deploy (
3299
3256
endpoint = endpoint ,
3300
3257
deployed_model_display_name = deployed_model_display_name ,
@@ -3306,8 +3263,7 @@ def deploy(
3306
3263
accelerator_type = accelerator_type ,
3307
3264
accelerator_count = accelerator_count ,
3308
3265
service_account = service_account ,
3309
- explanation_metadata = explanation_metadata ,
3310
- explanation_parameters = explanation_parameters ,
3266
+ explanation_spec = explanation_spec ,
3311
3267
metadata = metadata ,
3312
3268
encryption_spec_key_name = encryption_spec_key_name
3313
3269
or initializer .global_config .encryption_spec_key_name ,
@@ -3331,10 +3287,7 @@ def _deploy(
3331
3287
accelerator_type : Optional [str ] = None ,
3332
3288
accelerator_count : Optional [int ] = None ,
3333
3289
service_account : Optional [str ] = None ,
3334
- explanation_metadata : Optional [aiplatform .explain .ExplanationMetadata ] = None ,
3335
- explanation_parameters : Optional [
3336
- aiplatform .explain .ExplanationParameters
3337
- ] = None ,
3290
+ explanation_spec : Optional [aiplatform .explain .ExplanationSpec ] = None ,
3338
3291
metadata : Optional [Sequence [Tuple [str , str ]]] = (),
3339
3292
encryption_spec_key_name : Optional [str ] = None ,
3340
3293
network : Optional [str ] = None ,
@@ -3398,14 +3351,8 @@ def _deploy(
3398
3351
to the resource project.
3399
3352
Users deploying the Model must have the `iam.serviceAccounts.actAs`
3400
3353
permission on this service account.
3401
- explanation_metadata (aiplatform.explain.ExplanationMetadata):
3402
- Optional. Metadata describing the Model's input and output for explanation.
3403
- `explanation_metadata` is optional while `explanation_parameters` must be
3404
- specified when used.
3405
- For more details, see `Ref docs <http://tinyurl.com/1igh60kt>`
3406
- explanation_parameters (aiplatform.explain.ExplanationParameters):
3407
- Optional. Parameters to configure explaining for Model's predictions.
3408
- For more details, see `Ref docs <http://tinyurl.com/1an4zake>`
3354
+ explanation_spec (aiplatform.explain.ExplanationSpec):
3355
+ Optional. Specification of Model explanation.
3409
3356
metadata (Sequence[Tuple[str, str]]):
3410
3357
Optional. Strings which should be sent along with the request as
3411
3358
metadata.
@@ -3483,8 +3430,7 @@ def _deploy(
3483
3430
accelerator_type = accelerator_type ,
3484
3431
accelerator_count = accelerator_count ,
3485
3432
service_account = service_account ,
3486
- explanation_metadata = explanation_metadata ,
3487
- explanation_parameters = explanation_parameters ,
3433
+ explanation_spec = explanation_spec ,
3488
3434
metadata = metadata ,
3489
3435
deploy_request_timeout = deploy_request_timeout ,
3490
3436
autoscaling_target_cpu_utilization = autoscaling_target_cpu_utilization ,
0 commit comments