-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Linchin/ling-exit-handler
Ling exit handler
- Loading branch information
Showing
33 changed files
with
530 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
components/google-cloud/google_cloud_pipeline_components/aiplatform/evaluation/import.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: import_model_evaluation | ||
description: | | ||
Calls ModelService.ImportModelEvaluation to import a model evaluation file to Vertex | ||
Args: | ||
metrics (system.Metrics): | ||
Path of metrics generated from an evaluation component. | ||
explanation (system.Metrics): | ||
Path for model explanation metrics generated from an evaluation comonent. | ||
model (google.VertexModel): | ||
Vertex model resource that will be the parent resource of the uploaded evaluation. | ||
metrics_schema_uri (str): | ||
GCS link to the schema URI for model evaluation metrics. | ||
inputs: | ||
- {name: metrics, type: Metrics} | ||
- {name: explanation, type: Metrics, optional: True} | ||
- {name: model, type: google.VertexModel} | ||
- {name: metrics_schema_uri, type: String} | ||
implementation: | ||
container: | ||
image: gcr.io/ml-pipeline/google-cloud-pipeline-components:latest | ||
command: [python3, -u, -m, google_cloud_pipeline_components.container.experimental.evaluation.import_model_evaluation] | ||
args: | ||
- --metrics | ||
- {inputPath: metrics} | ||
- if: | ||
cond: {isPresent: explanation} | ||
then: | ||
- --explanation | ||
- "{{$.inputs.artifacts['explanation'].metadata['explanation_gcs_path']}}" | ||
- --metrics_schema_uri | ||
- {inputValue: metrics_schema_uri} | ||
- --model_name | ||
- "{{$.inputs.artifacts['model'].metadata['resourceName']}}" |
14 changes: 14 additions & 0 deletions
14
...ogle-cloud/google_cloud_pipeline_components/container/experimental/evaluation/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright 2021 The Kubeflow Authors. 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. | ||
"""Google Cloud Pipeline Evaluation Components root.""" |
112 changes: 112 additions & 0 deletions
112
...le_cloud_pipeline_components/container/experimental/evaluation/import_model_evaluation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright 2022 The Kubeflow Authors. 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. | ||
"""Module for importing a model evaluation to an existing Vertex model resource.""" | ||
|
||
import sys | ||
import argparse | ||
import json | ||
import six | ||
|
||
from google.cloud import aiplatform | ||
from google.api_core import gapic_v1 | ||
from google.protobuf.struct_pb2 import Value, Struct, NULL_VALUE, ListValue | ||
|
||
|
||
def main(argv): | ||
"""Calls ModelService.ImportModelEvaluation""" | ||
parser = argparse.ArgumentParser( | ||
prog='Vertex Model Service evaluation importer', description='') | ||
parser.add_argument( | ||
'--metrics', | ||
dest='metrics', | ||
type=str, | ||
required=True, | ||
default=argparse.SUPPRESS) | ||
parser.add_argument( | ||
'--explanation', dest='explanation', type=str, default=None) | ||
parser.add_argument( | ||
'--metrics_schema_uri', | ||
dest='metrics_schema_uri', | ||
type=str, | ||
required=True, | ||
default=argparse.SUPPRESS) | ||
parser.add_argument( | ||
'--model_name', | ||
dest='model_name', | ||
type=str, | ||
required=True, | ||
default=argparse.SUPPRESS) | ||
|
||
parsed_args, _ = parser.parse_known_args(argv) | ||
|
||
_, project_id, _, location, _, model_id = parsed_args.model_name.split('/') | ||
|
||
with open(parsed_args.metrics) as metrics_file: | ||
model_evaluation = { | ||
'metrics': | ||
to_value( | ||
next( | ||
iter( | ||
json.loads(metrics_file.read())['slicedMetrics'][0] | ||
['metrics'].values()))), | ||
'metrics_schema_uri': | ||
parsed_args.metrics_schema_uri, | ||
} | ||
|
||
if parsed_args.explanation: | ||
with open('/gcs' + parsed_args.explanation[4:]) as explanation_file: | ||
model_evaluation['model_explanation'] = { | ||
'mean_attributions': [{ | ||
'feature_attributions': | ||
to_value( | ||
json.loads(explanation_file.read())['explanation'] | ||
['attributions'][0]['featureAttributions']) | ||
}] | ||
} | ||
print(model_evaluation) | ||
aiplatform.gapic.ModelServiceClient( | ||
client_info=gapic_v1.client_info.ClientInfo( | ||
user_agent='google-cloud-pipeline-components',), | ||
client_options={ | ||
'api_endpoint': location + '-aiplatform.googleapis.com', | ||
}).import_model_evaluation( | ||
parent=parsed_args.model_name, | ||
model_evaluation=model_evaluation, | ||
) | ||
|
||
|
||
def to_value(value): | ||
if value is None: | ||
return Value(null_value=NULL_VALUE) | ||
elif isinstance(value, bool): | ||
# This check needs to happen before isinstance(value, int), | ||
# isinstance(value, int) returns True when value is bool. | ||
return Value(bool_value=value) | ||
elif isinstance(value, six.integer_types) or isinstance(value, float): | ||
return Value(number_value=value) | ||
elif isinstance(value, six.string_types) or isinstance(value, six.text_type): | ||
return Value(string_value=value) | ||
elif isinstance(value, dict): | ||
return Value( | ||
struct_value=Struct(fields={k: to_value(v) for k, v in value.items()})) | ||
elif isinstance(value, list): | ||
return Value( | ||
list_value=ListValue(values=[to_value(item) for item in value])) | ||
else: | ||
raise ValueError('Unsupported data type: {}'.format(type(value))) | ||
|
||
|
||
if __name__ == '__main__': | ||
print(sys.argv) | ||
main(sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.