-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sdk): enable dynamic importer metadata #7660
Merged
google-oss-prow
merged 13 commits into
kubeflow:master
from
connor-mccarthy:fix-importer-metadata-not-supporting-pipeline-parameter
Sep 19, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cd50f3d
support placeholder in metadata dict
connor-mccarthy d9b57b9
add unit tests
connor-mccarthy 3c9c479
add compilation test
connor-mccarthy 5720786
fix tests
connor-mccarthy c0f62fe
update compiler test
connor-mccarthy 35ff77c
use 'metadata' as input key
connor-mccarthy 69bfe6e
update compiler test and golden snapshot
connor-mccarthy 4cb6e2f
add int input to compiler test
connor-mccarthy 5bc0b35
change constant name
connor-mccarthy 4b670df
use util function
connor-mccarthy 6cd08e8
support f-strings in dynamic import metadata; add and update tests
connor-mccarthy 2b98dd3
update method name after rebase
connor-mccarthy 0210fe4
make compile consistent across python versions
connor-mccarthy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
# Copyright 2022 The Kubeflow Authors | ||
# | ||
# 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. | ||
import unittest | ||
|
||
from kfp import dsl | ||
from kfp.components import importer_node | ||
from kfp.components.types.artifact_types import Dataset | ||
|
||
|
||
class TestImporterSupportsDynamicMetadata(unittest.TestCase): | ||
|
||
def test_dynamic_dict_element_from_pipeline_param(self): | ||
|
||
@dsl.pipeline() | ||
def my_pipeline(meta_inp: str): | ||
|
||
dataset = importer_node.importer( | ||
'gs://my_bucket', | ||
Dataset, | ||
reimport=True, | ||
metadata={ | ||
'string': meta_inp, | ||
'string-2': meta_inp | ||
}) | ||
|
||
pipeline_spec = my_pipeline.pipeline_spec | ||
input_keys = list(pipeline_spec.components['comp-importer'] | ||
.input_definitions.parameters.keys()) | ||
self.assertIn('metadata', input_keys) | ||
deployment_spec = pipeline_spec.deployment_spec.fields[ | ||
'executors'].struct_value.fields['exec-importer'] | ||
metadata = deployment_spec.struct_value.fields[ | ||
'importer'].struct_value.fields['metadata'] | ||
self.assertEqual(metadata.struct_value.fields['string'].string_value, | ||
"{{$.inputs.parameters['metadata']}}") | ||
self.assertEqual(metadata.struct_value.fields['string-2'].string_value, | ||
"{{$.inputs.parameters['metadata']}}") | ||
|
||
def test_dynamic_list_element_from_pipeline_param(self): | ||
|
||
@dsl.pipeline() | ||
def my_pipeline(meta_inp1: str, meta_inp2: int): | ||
|
||
dataset = importer_node.importer( | ||
'gs://my_bucket', | ||
Dataset, | ||
reimport=True, | ||
metadata={ | ||
'outer_key': [meta_inp1, meta_inp2], | ||
meta_inp1: meta_inp1 | ||
}) | ||
|
||
pipeline_spec = my_pipeline.pipeline_spec | ||
input_keys = list(pipeline_spec.components['comp-importer'] | ||
.input_definitions.parameters.keys()) | ||
self.assertIn('metadata', input_keys) | ||
deployment_spec = pipeline_spec.deployment_spec.fields[ | ||
'executors'].struct_value.fields['exec-importer'] | ||
metadata = deployment_spec.struct_value.fields[ | ||
'importer'].struct_value.fields['metadata'] | ||
self.assertEqual( | ||
metadata.struct_value.fields['outer_key'].list_value.values[0] | ||
.string_value, "{{$.inputs.parameters['metadata']}}") | ||
self.assertEqual( | ||
metadata.struct_value.fields['outer_key'].list_value.values[1] | ||
.string_value, "{{$.inputs.parameters['metadata-2']}}") | ||
self.assertEqual( | ||
metadata.struct_value.fields["{{$.inputs.parameters['metadata']}}"] | ||
.string_value, "{{$.inputs.parameters['metadata']}}") | ||
|
||
def test_dynamic_dict_element_from_task_output(self): | ||
|
||
@dsl.component | ||
def string_task(string: str) -> str: | ||
return 'string' | ||
|
||
@dsl.pipeline() | ||
def my_pipeline(): | ||
task1 = string_task(string='string1') | ||
task2 = string_task(string='string2') | ||
dataset = importer_node.importer( | ||
'gs://my_bucket', | ||
Dataset, | ||
reimport=True, | ||
metadata={ | ||
'string-1': task1.output, | ||
'string-2': task2.output | ||
}) | ||
|
||
pipeline_spec = my_pipeline.pipeline_spec | ||
input_keys = list(pipeline_spec.components['comp-importer'] | ||
.input_definitions.parameters.keys()) | ||
self.assertIn('metadata', input_keys) | ||
self.assertIn('metadata-2', input_keys) | ||
|
||
deployment_spec = pipeline_spec.deployment_spec.fields[ | ||
'executors'].struct_value.fields['exec-importer'] | ||
metadata = deployment_spec.struct_value.fields[ | ||
'importer'].struct_value.fields['metadata'] | ||
self.assertEqual(metadata.struct_value.fields['string-1'].string_value, | ||
"{{$.inputs.parameters['metadata']}}") | ||
self.assertEqual(metadata.struct_value.fields['string-2'].string_value, | ||
"{{$.inputs.parameters['metadata-2']}}") | ||
|
||
def test_dynamic_list_element_from_task_output(self): | ||
|
||
@dsl.component | ||
def string_task() -> str: | ||
return 'string' | ||
|
||
@dsl.pipeline() | ||
def my_pipeline(): | ||
task = string_task() | ||
dataset = importer_node.importer( | ||
'gs://my_bucket', | ||
Dataset, | ||
reimport=True, | ||
metadata={ | ||
'outer_key': [task.output, task.output], | ||
task.output: task.output | ||
}) | ||
|
||
pipeline_spec = my_pipeline.pipeline_spec | ||
input_keys = list(pipeline_spec.components['comp-importer'] | ||
.input_definitions.parameters.keys()) | ||
self.assertIn('metadata', input_keys) | ||
self.assertNotIn('metadata-2', input_keys) | ||
deployment_spec = pipeline_spec.deployment_spec.fields[ | ||
'executors'].struct_value.fields['exec-importer'] | ||
metadata = deployment_spec.struct_value.fields[ | ||
'importer'].struct_value.fields['metadata'] | ||
self.assertEqual( | ||
metadata.struct_value.fields['outer_key'].list_value.values[0] | ||
.string_value, "{{$.inputs.parameters['metadata']}}") | ||
self.assertEqual( | ||
metadata.struct_value.fields['outer_key'].list_value.values[1] | ||
.string_value, "{{$.inputs.parameters['metadata']}}") | ||
self.assertEqual( | ||
metadata.struct_value.fields["{{$.inputs.parameters['metadata']}}"] | ||
.string_value, "{{$.inputs.parameters['metadata']}}") | ||
|
||
def test_dynamic_fstring_in_metadata(self): | ||
|
||
@dsl.component | ||
def string_task() -> str: | ||
return 'string' | ||
|
||
@dsl.pipeline() | ||
def my_pipeline(integer: int = 1): | ||
task = string_task() | ||
dataset = importer_node.importer( | ||
'gs://my_bucket', | ||
Dataset, | ||
reimport=True, | ||
metadata={ | ||
f'prefix1-{integer}': f'prefix2-{task.output}', | ||
'key': 'value' | ||
}) | ||
|
||
pipeline_spec = my_pipeline.pipeline_spec | ||
input_keys = list(pipeline_spec.components['comp-importer'] | ||
.input_definitions.parameters.keys()) | ||
self.assertIn('metadata', input_keys) | ||
self.assertIn('metadata-2', input_keys) | ||
self.assertNotIn('metadata-3', input_keys) | ||
deployment_spec = pipeline_spec.deployment_spec.fields[ | ||
'executors'].struct_value.fields['exec-importer'] | ||
metadata = deployment_spec.struct_value.fields[ | ||
'importer'].struct_value.fields['metadata'] | ||
self.assertEqual( | ||
metadata.struct_value.fields[ | ||
"prefix1-{{$.inputs.parameters[\'metadata\']}}"].string_value, | ||
"prefix2-{{$.inputs.parameters[\'metadata-2\']}}") | ||
self.assertEqual(metadata.struct_value.fields['key'].string_value, | ||
'value') |
67 changes: 67 additions & 0 deletions
67
sdk/python/test_data/pipelines/pipeline_with_dynamic_importer_metadata.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,67 @@ | ||
# Copyright 2022 The Kubeflow Authors | ||
# | ||
# 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. | ||
"""Pipeline using dsl.importer, with dynamic metadata.""" | ||
|
||
from kfp import compiler | ||
from kfp import dsl | ||
from kfp.dsl import Dataset | ||
from kfp.dsl import importer | ||
|
||
DEFAULT_ARTIFACT_URI = 'gs://ml-pipeline-playground/shakespeare1.txt' | ||
DEFAULT_IMAGE_URI = 'us-docker.pkg.dev/vertex-ai/prediction/tf2-gpu.2-5:latest' | ||
|
||
|
||
@dsl.component | ||
def make_name(name: str) -> str: | ||
return name | ||
|
||
|
||
@dsl.pipeline(name='pipeline-with-importer', pipeline_root='dummy_root') | ||
def my_pipeline(name: str = 'default-name', | ||
int_input: int = 1, | ||
pipeline_input_artifact_uri: str = DEFAULT_ARTIFACT_URI, | ||
pipeline_input_image_uri: str = DEFAULT_IMAGE_URI): | ||
|
||
importer1 = importer( | ||
artifact_uri=pipeline_input_artifact_uri, | ||
artifact_class=Dataset, | ||
reimport=False, | ||
metadata={ | ||
'name': [name, 'alias-name'], | ||
'containerSpec': { | ||
'imageUri': pipeline_input_image_uri | ||
} | ||
}) | ||
|
||
make_name_op = make_name(name='a-different-name') | ||
|
||
importer2 = importer( | ||
artifact_uri=DEFAULT_ARTIFACT_URI, | ||
artifact_class=Dataset, | ||
reimport=False, | ||
metadata={ | ||
'name': f'prefix-{make_name_op.output}', | ||
'list-of-data': [make_name_op.output, name, int_input], | ||
make_name_op.output: make_name_op.output, | ||
name: DEFAULT_IMAGE_URI, | ||
'containerSpec': { | ||
'imageUri': DEFAULT_IMAGE_URI | ||
} | ||
}) | ||
|
||
|
||
if __name__ == '__main__': | ||
compiler.Compiler().compile( | ||
pipeline_func=my_pipeline, | ||
package_path=__file__.replace('.py', '.yaml')) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker for this PR, as I still don't quite follow the practical usage of an importer node when pipeline accepts artifact inputs. But I think there's a bug here in case
isinstance(d, str)
, users may embed aPipelineChannel
into a string, e.g.:f'{task1.output}-{name}'
. and I think we are missing that parse-and-find logic here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed -- let's discuss offline about where we can raise this consideration.
Updated