forked from opendatahub-io/data-science-pipelines
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend compiler to support parallelFor with loop static params (#67)
- Loading branch information
Showing
7 changed files
with
513 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2020 kubeflow.org | ||
# | ||
# 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 kfp.dsl as dsl | ||
|
||
|
||
@dsl.pipeline(name='my-pipeline') | ||
def pipeline(my_pipe_param='10'): | ||
loop_args = [{'A_a': 1, 'B_b': 2}, {'A_a': 10, 'B_b': 20}] | ||
with dsl.ParallelFor(loop_args) as item: | ||
op1 = dsl.ContainerOp( | ||
name="my-in-coop1", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo op1 %s %s" % (item.A_a, my_pipe_param)], | ||
) | ||
|
||
op2 = dsl.ContainerOp( | ||
name="my-in-coop2", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo op2 %s" % item.B_b], | ||
) | ||
|
||
op_out = dsl.ContainerOp( | ||
name="my-out-cop", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo %s" % my_pipe_param], | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
# don't use top-level import of TektonCompiler to prevent monkey-patching KFP compiler when using KFP's dsl-compile | ||
from kfp_tekton.compiler import TektonCompiler | ||
TektonCompiler().compile(pipeline, __file__.replace('.py', '.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,109 @@ | ||
# Copyright 2020 kubeflow.org | ||
# | ||
# 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. | ||
|
||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: my-in-coop1 | ||
spec: | ||
params: | ||
- name: loop-item-param-subvar-A_a | ||
- name: my_pipe_param | ||
steps: | ||
- args: | ||
- echo op1 $(inputs.params.loop-item-param-subvar-A_a) $(inputs.params.my_pipe_param) | ||
command: | ||
- sh | ||
- -c | ||
image: library/bash:4.4.23 | ||
name: my-in-coop1 | ||
--- | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: my-in-coop2 | ||
spec: | ||
params: | ||
- name: loop-item-param-subvar-B_b | ||
steps: | ||
- args: | ||
- echo op2 $(inputs.params.loop-item-param-subvar-B_b) | ||
command: | ||
- sh | ||
- -c | ||
image: library/bash:4.4.23 | ||
name: my-in-coop2 | ||
--- | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: my-out-cop | ||
spec: | ||
params: | ||
- name: my_pipe_param | ||
steps: | ||
- args: | ||
- echo $(inputs.params.my_pipe_param) | ||
command: | ||
- sh | ||
- -c | ||
image: library/bash:4.4.23 | ||
name: my-out-cop | ||
--- | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Pipeline | ||
metadata: | ||
annotations: | ||
pipelines.kubeflow.org/pipeline_spec: '{"inputs": [{"default": "10", "name": "my_pipe_param", | ||
"optional": true}], "name": "my-pipeline"}' | ||
name: my-pipeline | ||
spec: | ||
params: | ||
- default: '10' | ||
name: my_pipe_param | ||
tasks: | ||
- name: my-in-coop1-loop-items-0 | ||
params: | ||
- name: loop-item-param-subvar-A_a | ||
value: '1' | ||
- name: my_pipe_param | ||
value: $(params.my_pipe_param) | ||
taskRef: | ||
name: my-in-coop1 | ||
- name: my-in-coop1-loop-items-1 | ||
params: | ||
- name: loop-item-param-subvar-A_a | ||
value: '10' | ||
- name: my_pipe_param | ||
value: $(params.my_pipe_param) | ||
taskRef: | ||
name: my-in-coop1 | ||
- name: my-in-coop2-loop-items-0 | ||
params: | ||
- name: loop-item-param-subvar-B_b | ||
value: '2' | ||
taskRef: | ||
name: my-in-coop2 | ||
- name: my-in-coop2-loop-items-1 | ||
params: | ||
- name: loop-item-param-subvar-B_b | ||
value: '20' | ||
taskRef: | ||
name: my-in-coop2 | ||
- name: my-out-cop-loop-items-0 | ||
params: | ||
- name: my_pipe_param | ||
value: $(params.my_pipe_param) | ||
taskRef: | ||
name: my-out-cop |
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 2020 kubeflow.org | ||
# | ||
# 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 kfp.dsl as dsl | ||
from kfp.dsl import _for_loop | ||
|
||
class Coder: | ||
def __init__(self, ): | ||
self._code_id = 0 | ||
|
||
def get_code(self, ): | ||
self._code_id += 1 | ||
return '{code:0{num_chars:}d}'.format(code=self._code_id, num_chars=_for_loop.LoopArguments.NUM_CODE_CHARS) | ||
|
||
|
||
dsl.ParallelFor._get_unique_id_code = Coder().get_code | ||
|
||
|
||
@dsl.pipeline(name='my-pipeline') | ||
def pipeline(my_pipe_param: int = 10): | ||
loop_args = [{'a': 1, 'b': 2}, {'a': 10, 'b': 20}] | ||
with dsl.ParallelFor(loop_args) as item: | ||
op1 = dsl.ContainerOp( | ||
name="my-in-coop1", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo op1 %s %s" % (item.a, my_pipe_param)], | ||
) | ||
|
||
with dsl.ParallelFor([100, 200, 300]) as inner_item: | ||
op11 = dsl.ContainerOp( | ||
name="my-inner-inner-coop", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo op1 %s %s %s" % (item.a, inner_item, my_pipe_param)], | ||
) | ||
|
||
op2 = dsl.ContainerOp( | ||
name="my-in-coop2", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo op2 %s" % item.b], | ||
) | ||
|
||
op_out = dsl.ContainerOp( | ||
name="my-out-cop", | ||
image="library/bash:4.4.23", | ||
command=["sh", "-c"], | ||
arguments=["echo %s" % my_pipe_param], | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
# don't use top-level import of TektonCompiler to prevent monkey-patching KFP compiler when using KFP's dsl-compile | ||
from kfp_tekton.compiler import TektonCompiler | ||
TektonCompiler().compile(pipeline, __file__.replace('.py', '.yaml')) |
Oops, something went wrong.