-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.py
484 lines (430 loc) · 23.2 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# -*- coding: utf-8 -*- #
# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for dealing with AI Platform model monitoring jobs API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import copy
from apitools.base.py import encoding
from apitools.base.py import extra_types
from apitools.base.py import list_pager
from googlecloudsdk.api_lib.ai import util as api_util
from googlecloudsdk.api_lib.util import apis
from googlecloudsdk.api_lib.util import messages as messages_util
from googlecloudsdk.command_lib.ai import constants
from googlecloudsdk.command_lib.ai import errors
from googlecloudsdk.command_lib.ai import model_monitoring_jobs_util
from googlecloudsdk.command_lib.ai import validation as common_validation
from googlecloudsdk.core import properties
from googlecloudsdk.core import resources
from googlecloudsdk.core import yaml
import six
def _ParseEndpoint(endpoint_id, region_ref):
"""Parses a endpoint ID into a endpoint resource object."""
region = region_ref.AsDict()['locationsId']
return resources.REGISTRY.Parse(
endpoint_id,
params={
'locationsId': region,
'projectsId': properties.VALUES.core.project.GetOrFail
},
collection='aiplatform.projects.locations.endpoints')
def _ParseDataset(dataset_id, region_ref):
"""Parses a dataset ID into a dataset resource object."""
region = region_ref.AsDict()['locationsId']
return resources.REGISTRY.Parse(
dataset_id,
params={
'locationsId': region,
'projectsId': properties.VALUES.core.project.GetOrFail
},
collection='aiplatform.projects.locations.datasets')
class ModelMonitoringJobsClient(object):
"""High-level client for the AI Platform model deployment monitoring jobs surface."""
def __init__(self, client=None, messages=None, version=None):
self.client = client or apis.GetClientInstance(
constants.AI_PLATFORM_API_NAME,
constants.AI_PLATFORM_API_VERSION[version])
self.messages = messages or self.client.MESSAGES_MODULE
self._service = self.client.projects_locations_modelDeploymentMonitoringJobs
self._version = version
def _ConstructDriftThresholds(self, feature_thresholds,
feature_attribution_thresholds):
"""Construct drift thresholds from user input.
Args:
feature_thresholds: Dict or None, key: feature_name, value: thresholds.
feature_attribution_thresholds: Dict or None, key:feature_name, value:
attribution score thresholds.
Returns:
PredictionDriftDetectionConfig
"""
prediction_drift_detection = api_util.GetMessage(
'ModelMonitoringObjectiveConfigPredictionDriftDetectionConfig',
self._version)()
additional_properties = []
attribution_additional_properties = []
if feature_thresholds:
for key, value in feature_thresholds.items():
threshold = 0.3 if not value else float(value)
additional_properties.append(prediction_drift_detection
.DriftThresholdsValue().AdditionalProperty(
key=key,
value=api_util.GetMessage(
'ThresholdConfig',
self._version)(value=threshold)))
prediction_drift_detection.driftThresholds = prediction_drift_detection.DriftThresholdsValue(
additionalProperties=additional_properties)
if feature_attribution_thresholds:
for key, value in feature_attribution_thresholds.items():
threshold = 0.3 if not value else float(value)
attribution_additional_properties.append(
prediction_drift_detection.AttributionScoreDriftThresholdsValue(
).AdditionalProperty(
key=key,
value=api_util.GetMessage('ThresholdConfig',
self._version)(value=threshold)))
prediction_drift_detection.attributionScoreDriftThresholds = prediction_drift_detection.AttributionScoreDriftThresholdsValue(
additionalProperties=attribution_additional_properties)
return prediction_drift_detection
def _ConstructSkewThresholds(self, feature_thresholds,
feature_attribution_thresholds):
"""Construct skew thresholds from user input.
Args:
feature_thresholds: Dict or None, key: feature_name, value: thresholds.
feature_attribution_thresholds: Dict or None, key:feature_name, value:
attribution score thresholds.
Returns:
TrainingPredictionSkewDetectionConfig
"""
training_prediction_skew_detection = api_util.GetMessage(
'ModelMonitoringObjectiveConfigTrainingPredictionSkewDetectionConfig',
self._version)()
additional_properties = []
attribution_additional_properties = []
if feature_thresholds:
for key, value in feature_thresholds.items():
threshold = 0.3 if not value else float(value)
additional_properties.append(training_prediction_skew_detection
.SkewThresholdsValue().AdditionalProperty(
key=key,
value=api_util.GetMessage(
'ThresholdConfig',
self._version)(value=threshold)))
training_prediction_skew_detection.skewThresholds = training_prediction_skew_detection.SkewThresholdsValue(
additionalProperties=additional_properties)
if feature_attribution_thresholds:
for key, value in feature_attribution_thresholds.items():
threshold = 0.3 if not value else float(value)
attribution_additional_properties.append(
training_prediction_skew_detection
.AttributionScoreSkewThresholdsValue().AdditionalProperty(
key=key,
value=api_util.GetMessage('ThresholdConfig',
self._version)(value=threshold)))
training_prediction_skew_detection.attributionScoreSkewThresholds = training_prediction_skew_detection.AttributionScoreSkewThresholdsValue(
additionalProperties=attribution_additional_properties)
return training_prediction_skew_detection
def _ConstructObjectiveConfigForUpdate(self, existing_monitoring_job,
feature_thresholds,
feature_attribution_thresholds):
"""Construct monitoring objective config.
Update the feature thresholds for skew/drift detection to all the existing
deployed models under the job.
Args:
existing_monitoring_job: Existing monitoring job.
feature_thresholds: Dict or None, key: feature_name, value: thresholds.
feature_attribution_thresholds: Dict or None, key: feature_name, value:
attribution score thresholds.
Returns:
A list of model monitoring objective config.
"""
prediction_drift_detection = self._ConstructDriftThresholds(
feature_thresholds, feature_attribution_thresholds)
training_prediction_skew_detection = self._ConstructSkewThresholds(
feature_thresholds, feature_attribution_thresholds)
objective_configs = []
for objective_config in existing_monitoring_job.modelDeploymentMonitoringObjectiveConfigs:
if objective_config.objectiveConfig.trainingPredictionSkewDetectionConfig:
if training_prediction_skew_detection.skewThresholds:
objective_config.objectiveConfig.trainingPredictionSkewDetectionConfig.skewThresholds = training_prediction_skew_detection.skewThresholds
if training_prediction_skew_detection.attributionScoreSkewThresholds:
objective_config.objectiveConfig.trainingPredictionSkewDetectionConfig.attributionScoreSkewThresholds = training_prediction_skew_detection.attributionScoreSkewThresholds
if objective_config.objectiveConfig.predictionDriftDetectionConfig:
if prediction_drift_detection.driftThresholds:
objective_config.objectiveConfig.predictionDriftDetectionConfig.driftThresholds = prediction_drift_detection.driftThresholds
if prediction_drift_detection.attributionScoreDriftThresholds:
objective_config.objectiveConfig.predictionDriftDetectionConfig.attributionScoreDriftThresholds = prediction_drift_detection.attributionScoreDriftThresholds
if training_prediction_skew_detection.attributionScoreSkewThresholds or prediction_drift_detection.attributionScoreDriftThresholds:
objective_config.objectiveConfig.explanationConfig = api_util.GetMessage(
'ModelMonitoringObjectiveConfigExplanationConfig', self._version)(
enableFeatureAttributes=True)
objective_configs.append(objective_config)
return objective_configs
def _ConstructObjectiveConfigForCreate(self, location_ref, endpoint_name,
feature_thresholds,
feature_attribution_thresholds,
dataset, bigquery_uri, data_format,
gcs_uris, target_field,
training_sampling_rate):
"""Construct monitoring objective config.
Apply the feature thresholds for skew or drift detection to all the deployed
models under the endpoint.
Args:
location_ref: Location reference.
endpoint_name: Endpoint resource name.
feature_thresholds: Dict or None, key: feature_name, value: thresholds.
feature_attribution_thresholds: Dict or None, key: feature_name, value:
attribution score thresholds.
dataset: Vertex AI Dataset Id.
bigquery_uri: The BigQuery table of the unmanaged Dataset used to train
this Model.
data_format: Google Cloud Storage format, supported format: csv,
tf-record.
gcs_uris: The Google Cloud Storage uri of the unmanaged Dataset used to
train this Model.
target_field: The target field name the model is to predict.
training_sampling_rate: Training Dataset sampling rate.
Returns:
A list of model monitoring objective config.
"""
objective_config_template = api_util.GetMessage(
'ModelDeploymentMonitoringObjectiveConfig', self._version)()
if feature_thresholds or feature_attribution_thresholds:
if dataset or bigquery_uri or gcs_uris or data_format:
training_dataset = api_util.GetMessage(
'ModelMonitoringObjectiveConfigTrainingDataset', self._version)()
if target_field is None:
raise errors.ArgumentError(
"Target field must be provided if you'd like to do training-prediction skew detection."
)
training_dataset.targetField = target_field
training_dataset.loggingSamplingStrategy = api_util.GetMessage(
'SamplingStrategy', self._version)(
randomSampleConfig=api_util.GetMessage(
'SamplingStrategyRandomSampleConfig', self._version)(
sampleRate=training_sampling_rate))
if dataset:
training_dataset.dataset = _ParseDataset(dataset,
location_ref).RelativeName()
elif bigquery_uri:
training_dataset.bigquerySource = api_util.GetMessage(
'BigQuerySource', self._version)(
inputUri=bigquery_uri)
elif gcs_uris or data_format:
if gcs_uris is None:
raise errors.ArgumentError(
'Data format is defined but no Google Cloud Storage uris are provided. Please use --gcs-uris to provide training datasets.'
)
if data_format is None:
raise errors.ArgumentError(
'No Data format is defined for Google Cloud Storage training dataset. Please use --data-format to define the Data format.'
)
training_dataset.dataFormat = data_format
training_dataset.gcsSource = api_util.GetMessage(
'GcsSource', self._version)(
uris=gcs_uris)
training_prediction_skew_detection = self._ConstructSkewThresholds(
feature_thresholds, feature_attribution_thresholds)
objective_config_template.objectiveConfig = api_util.GetMessage(
'ModelMonitoringObjectiveConfig', self._version
)(trainingDataset=training_dataset,
trainingPredictionSkewDetectionConfig=training_prediction_skew_detection
)
else:
prediction_drift_detection = self._ConstructDriftThresholds(
feature_thresholds, feature_attribution_thresholds)
objective_config_template.objectiveConfig = api_util.GetMessage(
'ModelMonitoringObjectiveConfig', self._version)(
predictionDriftDetectionConfig=prediction_drift_detection)
if feature_attribution_thresholds:
objective_config_template.objectiveConfig.explanationConfig = api_util.GetMessage(
'ModelMonitoringObjectiveConfigExplanationConfig', self._version)(
enableFeatureAttributes=True)
get_endpoint_req = self.messages.AiplatformProjectsLocationsEndpointsGetRequest(
name=endpoint_name)
endpoint = self.client.projects_locations_endpoints.Get(get_endpoint_req)
objective_configs = []
for deployed_model in endpoint.deployedModels:
objective_config = copy.deepcopy(objective_config_template)
objective_config.deployedModelId = deployed_model.id
objective_configs.append(objective_config)
return objective_configs
def Create(self, location_ref, args):
"""Creates a model deployment monitoring job."""
endpoint_ref = _ParseEndpoint(args.endpoint, location_ref)
job_spec = api_util.GetMessage('ModelDeploymentMonitoringJob',
self._version)()
kms_key_name = common_validation.GetAndValidateKmsKey(args)
if kms_key_name is not None:
job_spec.encryptionSpec = api_util.GetMessage('EncryptionSpec',
self._version)(
kmsKeyName=kms_key_name)
if args.monitoring_config_from_file:
data = yaml.load_path(args.monitoring_config_from_file)
if data:
job_spec = messages_util.DictToMessageWithErrorCheck(
data,
api_util.GetMessage('ModelDeploymentMonitoringJob', self._version))
else:
job_spec.modelDeploymentMonitoringObjectiveConfigs = self._ConstructObjectiveConfigForCreate(
location_ref, endpoint_ref.RelativeName(), args.feature_thresholds,
args.feature_attribution_thresholds, args.dataset, args.bigquery_uri,
args.data_format, args.gcs_uris, args.target_field,
args.training_sampling_rate)
job_spec.endpoint = endpoint_ref.RelativeName()
job_spec.displayName = args.display_name
job_spec.modelMonitoringAlertConfig = api_util.GetMessage(
'ModelMonitoringAlertConfig', self._version)(
emailAlertConfig=api_util.GetMessage(
'ModelMonitoringAlertConfigEmailAlertConfig', self._version)(
userEmails=args.emails))
job_spec.loggingSamplingStrategy = api_util.GetMessage(
'SamplingStrategy', self._version)(
randomSampleConfig=api_util.GetMessage(
'SamplingStrategyRandomSampleConfig', self._version)(
sampleRate=args.prediction_sampling_rate))
job_spec.modelDeploymentMonitoringScheduleConfig = api_util.GetMessage(
'ModelDeploymentMonitoringScheduleConfig', self._version)(
monitorInterval='{}s'.format(
six.text_type(3600 * int(args.monitoring_frequency))))
if args.predict_instance_schema:
job_spec.predictInstanceSchemaUri = args.predict_instance_schema
if args.analysis_instance_schema:
job_spec.analysisInstanceSchemaUri = args.analysis_instance_schema
if args.log_ttl:
job_spec.logTtl = '{}s'.format(six.text_type(86400 * int(args.log_ttl)))
if args.sample_predict_request:
instance_json = model_monitoring_jobs_util.ReadInstanceFromArgs(
args.sample_predict_request)
job_spec.samplePredictInstance = encoding.PyValueToMessage(
extra_types.JsonValue, instance_json)
if self._version == constants.ALPHA_VERSION:
return self._service.Create(
self.messages.
AiplatformProjectsLocationsModelDeploymentMonitoringJobsCreateRequest(
parent=location_ref.RelativeName(),
googleCloudAiplatformV1alpha1ModelDeploymentMonitoringJob=job_spec
))
elif self._version == constants.BETA_VERSION:
return self._service.Create(
self.messages.
AiplatformProjectsLocationsModelDeploymentMonitoringJobsCreateRequest(
parent=location_ref.RelativeName(),
googleCloudAiplatformV1beta1ModelDeploymentMonitoringJob=job_spec
))
else:
return self._service.Create(
self.messages.
AiplatformProjectsLocationsModelDeploymentMonitoringJobsCreateRequest(
parent=location_ref.RelativeName(),
googleCloudAiplatformV1ModelDeploymentMonitoringJob=job_spec))
def Patch(self, model_monitoring_job_ref, args):
"""Update a model deployment monitoring job."""
model_monitoring_job_to_update = api_util.GetMessage(
'ModelDeploymentMonitoringJob', self._version)()
update_mask = []
job_spec = api_util.GetMessage('ModelDeploymentMonitoringJob',
self._version)()
if args.monitoring_config_from_file:
data = yaml.load_path(args.monitoring_config_from_file)
if data:
job_spec = messages_util.DictToMessageWithErrorCheck(
data,
api_util.GetMessage('ModelDeploymentMonitoringJob', self._version))
model_monitoring_job_to_update.modelDeploymentMonitoringObjectiveConfigs = job_spec.modelDeploymentMonitoringObjectiveConfigs
update_mask.append('model_deployment_monitoring_objective_configs')
if args.feature_thresholds or args.feature_attribution_thresholds:
get_monitoring_job_req = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsGetRequest(
name=model_monitoring_job_ref.RelativeName())
model_monitoring_job = self._service.Get(get_monitoring_job_req)
model_monitoring_job_to_update.modelDeploymentMonitoringObjectiveConfigs = self._ConstructObjectiveConfigForUpdate(
model_monitoring_job, args.feature_thresholds,
args.feature_attribution_thresholds)
update_mask.append('model_deployment_monitoring_objective_configs')
if args.display_name:
model_monitoring_job_to_update.displayName = args.display_name
update_mask.append('display_name')
if args.emails:
model_monitoring_job_to_update.modelMonitoringAlertConfig = api_util.GetMessage(
'ModelMonitoringAlertConfig', self._version)(
emailAlertConfig=api_util.GetMessage(
'ModelMonitoringAlertConfigEmailAlertConfig', self._version)(
userEmails=args.emails))
update_mask.append('model_monitoring_alert_config')
# sampling rate
if args.prediction_sampling_rate:
model_monitoring_job_to_update.loggingSamplingStrategy = api_util.GetMessage(
'SamplingStrategy', self._version)(
randomSampleConfig=api_util.GetMessage(
'SamplingStrategyRandomSampleConfig', self._version)(
sampleRate=args.prediction_sampling_rate))
update_mask.append('logging_sampling_strategy')
# schedule
if args.monitoring_frequency:
model_monitoring_job_to_update.modelDeploymentMonitoringScheduleConfig = api_util.GetMessage(
'ModelDeploymentMonitoringScheduleConfig', self._version)(
monitorInterval='{}s'.format(
six.text_type(3600 * int(args.monitoring_frequency))))
update_mask.append('model_deployment_monitoring_schedule_config')
if args.analysis_instance_schema:
model_monitoring_job_to_update.analysisInstanceSchemaUri = args.analysis_instance_schema
update_mask.append('analysis_instance_schema_uri')
if args.log_ttl:
model_monitoring_job_to_update.logTtl = '{}s'.format(
six.text_type(86400 * int(args.log_ttl)))
update_mask.append('log_ttl')
if not update_mask:
raise errors.NoFieldsSpecifiedError('No updates requested.')
if self._version == constants.ALPHA_VERSION:
req = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsPatchRequest(
name=model_monitoring_job_ref.RelativeName(),
googleCloudAiplatformV1alpha1ModelDeploymentMonitoringJob=model_monitoring_job_to_update,
updateMask=','.join(update_mask))
elif self._version == constants.BETA_VERSION:
req = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsPatchRequest(
name=model_monitoring_job_ref.RelativeName(),
googleCloudAiplatformV1beta1ModelDeploymentMonitoringJob=model_monitoring_job_to_update,
updateMask=','.join(update_mask))
else:
req = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsPatchRequest(
name=model_monitoring_job_ref.RelativeName(),
googleCloudAiplatformV1ModelDeploymentMonitoringJob=model_monitoring_job_to_update,
updateMask=','.join(update_mask))
return self._service.Patch(req)
def Get(self, model_monitoring_job_ref):
request = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsGetRequest(
name=model_monitoring_job_ref.RelativeName())
return self._service.Get(request)
def List(self, limit=None, region_ref=None):
return list_pager.YieldFromList(
self._service,
self.messages
.AiplatformProjectsLocationsModelDeploymentMonitoringJobsListRequest(
parent=region_ref.RelativeName()),
field='modelDeploymentMonitoringJobs',
batch_size_attribute='pageSize',
limit=limit)
def Delete(self, model_monitoring_job_ref):
request = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsDeleteRequest(
name=model_monitoring_job_ref.RelativeName())
return self._service.Delete(request)
def Pause(self, model_monitoring_job_ref):
request = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsPauseRequest(
name=model_monitoring_job_ref.RelativeName())
return self._service.Pause(request)
def Resume(self, model_monitoring_job_ref):
request = self.messages.AiplatformProjectsLocationsModelDeploymentMonitoringJobsResumeRequest(
name=model_monitoring_job_ref.RelativeName())
return self._service.Resume(request)