I have run into issues with dynamic "fanning-out" and then "fanning-in" with Kubeflow Pipelines as well. Maybe a little heavy-handed but I used a mounted PVC claim to get over this.
Kubeflow allows you to mount a known PVC or create a new one on the fly using VolumeOp
(link here). This snippet shows how to use a known PVC.
pvc_name = '<available-pvc-name>'
pvc_volume_name = '<pvc-uuid>' # pass the pvc uuid here
# Op 1 creates a list to iterate over
op_1 = dsl.ContainerOp(
name='echo',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=['echo "[1,2,3]"> /tmp/output.txt'],
file_outputs={'output': '/tmp/output.txt'})
# Using withParam here to iterate over the results from op1
# and writing the results of each step to its own PVC
with dsl.ParallelFor(op_1.output) as item:
op_2 = dsl.ContainerOp(
name='iterate',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=[f"echo item-{item} > /tmp/output.txt; " # <- write to output
f"mkdir -p /mnt/{{workflow.uid}}; " # <- make a dir under /mnt
f"echo item-{item}\n >> /mnt/{{workflow.uid}}"], # <- append results from each step to the PVC
file_outputs={'output': '/tmp/output.txt'},
# mount the PVC
pvolumes={"/mnt": dsl.PipelineVolume(pvc=pvc_name, name=pvc_volume_name)})
op_3 = dsl.ContainerOp(
name='echo',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=[f"echo /mnt/{{workflow.uid}} > /tmp/output.txt"],
# mount the PVC again to use
pvolumes={"/mnt": dsl.PipelineVolume(pvc=pvc_name, name=pvc_volume_name)},
file_outputs={'output': '/tmp/output_2.txt'}).after(op_2)
Ensure that op_3
runs after the loops from op_2
using after(op_2)
in the end.
Note: This might be a heavy-handed approach and there might be better solutions if KFP allows this as part of the KF compiler but I couldn't get it to work. If it's easy to create a PVC in the env this might work for your case.