-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(components): Add v1.model.ModelGetOp components
PiperOrigin-RevId: 581081482
- Loading branch information
Googler
committed
Nov 28, 2023
1 parent
1e089e6
commit 14a26f1
Showing
7 changed files
with
224 additions
and
0 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
14 changes: 14 additions & 0 deletions
14
...ts/google-cloud/google_cloud_pipeline_components/container/v1/model/get_model/__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 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. | ||
"""Google Cloud Pipeline Components - Get Model Launcher and Remote Runner.""" |
70 changes: 70 additions & 0 deletions
70
...ts/google-cloud/google_cloud_pipeline_components/container/v1/model/get_model/launcher.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,70 @@ | ||
# 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. | ||
"""GCP launcher for Get Model based on the AI Platform SDK.""" | ||
|
||
import argparse | ||
import logging | ||
import sys | ||
|
||
from google_cloud_pipeline_components.container.v1.model.get_model import remote_runner | ||
|
||
|
||
def _parse_args(args): | ||
"""Parse command line arguments.""" | ||
parser = argparse.ArgumentParser( | ||
prog='Vertex Pipelines get model launcher', description='' | ||
) | ||
parser.add_argument( | ||
'--executor_input', | ||
dest='executor_input', | ||
type=str, | ||
# executor_input is only needed for components that emit output artifacts. | ||
required=True, | ||
default=argparse.SUPPRESS, | ||
) | ||
parser.add_argument('--project', dest='project', type=str, default=None) | ||
parser.add_argument('--location', dest='location', type=str, default=None) | ||
|
||
group_args = parser.add_mutually_exclusive_group(required=True) | ||
group_args.add_argument( | ||
'--model_name', dest='model_name', type=str, default=None | ||
) | ||
parsed_args, _ = parser.parse_known_args(args) | ||
return vars(parsed_args) | ||
|
||
|
||
def main(argv): | ||
"""Main entry. | ||
Expected input args are as follows: | ||
model_name - Required. Provided string resource name to create a model | ||
artifact. | ||
project - Required. Project to get this Model from. | ||
location - Required. Location to get this Model from. | ||
Args: | ||
argv: A list of system arguments. | ||
""" | ||
parsed_args = _parse_args(argv) | ||
print(parsed_args.model_name) | ||
logging.info('model_name: %s', parsed_args.model_name) | ||
print(parsed_args.project) | ||
logging.info('project: %s', parsed_args.project) | ||
print(parsed_args.location) | ||
logging.info('location: %s', parsed_args.location) | ||
remote_runner.get_model(**parsed_args) | ||
|
||
|
||
if __name__ == '__main__': | ||
main(sys.argv[1:]) |
60 changes: 60 additions & 0 deletions
60
...ogle-cloud/google_cloud_pipeline_components/container/v1/model/get_model/remote_runner.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,60 @@ | ||
# 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. | ||
"""Remote runner for Get Model based on the AI Platform SDK.""" | ||
|
||
|
||
from google.api_core import gapic_v1 | ||
from google.cloud import aiplatform | ||
from google_cloud_pipeline_components.container.utils import artifact_utils | ||
from google_cloud_pipeline_components.types import artifact_types | ||
|
||
|
||
def get_model( | ||
executor_input, | ||
model_name: str, | ||
project: str, | ||
location: str, | ||
): | ||
"""Get model.""" | ||
if not location or not project: | ||
raise ValueError( | ||
'Model resource name must be in the format' | ||
' projects/{project}/locations/{location}/models/{model_name}' | ||
) | ||
api_endpoint = location + '-aiplatform.googleapis.com' | ||
vertex_uri_prefix = f'https://{api_endpoint}/v1/' | ||
model_resource_name = ( | ||
f'projects/{project}/locations/{location}/models/{model_name}' | ||
) | ||
|
||
client = aiplatform.gapic.ModelServiceClient( | ||
client_info=gapic_v1.client_info.ClientInfo( | ||
user_agent='googles-cloud-pipeline-components' | ||
), | ||
client_options={ | ||
'api_endpoint': api_endpoint, | ||
}, | ||
) | ||
get_model_response = client.get_model(name=model_resource_name) | ||
|
||
resp_model_name_without_version = get_model_response.name.split('@', 1)[0] | ||
model_resource_name = ( | ||
f'{resp_model_name_without_version}@{get_model_response.version_id}' | ||
) | ||
|
||
vertex_model = artifact_types.VertexModel.create( | ||
'model', vertex_uri_prefix + model_resource_name, model_resource_name | ||
) | ||
# TODO(b/266848949): Output Artifact should use correct MLMD artifact. | ||
artifact_utils.update_output_artifacts(executor_input, [vertex_model]) |
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
14 changes: 14 additions & 0 deletions
14
components/google-cloud/google_cloud_pipeline_components/v1/model/get_model/__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 2023 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 Get Vertex Model Component.""" |
63 changes: 63 additions & 0 deletions
63
components/google-cloud/google_cloud_pipeline_components/v1/model/get_model/component.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,63 @@ | ||
# Copyright 2023 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. | ||
from google_cloud_pipeline_components import _image | ||
from google_cloud_pipeline_components import _placeholders | ||
from google_cloud_pipeline_components.types.artifact_types import VertexModel | ||
from kfp.dsl import container_component | ||
from kfp.dsl import ContainerSpec | ||
from kfp.dsl import Output | ||
from kfp.dsl import OutputPath | ||
|
||
|
||
@container_component | ||
def model_get( | ||
model: Output[VertexModel], | ||
model_name: str, | ||
project: str = _placeholders.PROJECT_ID_PLACEHOLDER, | ||
location: str = 'us-central1', | ||
): | ||
# fmt: off | ||
"""Gets a model artifact or string inputs to an existing Vertex model. | ||
Args: | ||
project: Project to get this Model from. Defaults to the project in which the PipelineJob is run. | ||
model_name: Vertex model resource name in the format of | ||
projects/{project}/locations/{location}/models/{model} or | ||
projects/{project}/locations/{location}/models/{model}@{model_version_id | ||
or model_version_alias}.If no version ID or alias is specified, the "default" version will be returned. | ||
location: Optional location to get this Model from. If not set, defaults to `us-central1`. | ||
Returns: | ||
model: Artifact of the Vertex Model. | ||
""" | ||
# fmt: on | ||
return ContainerSpec( | ||
image=_image.GCPC_IMAGE_TAG, | ||
command=[ | ||
'python3', | ||
'-u', | ||
'-m', | ||
'google_cloud_pipeline_components.container.v1.model.get_model.launcher', | ||
], | ||
args=[ | ||
'--project', | ||
project, | ||
'--location', | ||
location, | ||
'--model_name', | ||
model_name, | ||
'--executor_input', | ||
'{{$}}', | ||
], | ||
) |