Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Adding most suggestions with a few that need to be handled locally!

Co-authored-by: Nikki Everett <[email protected]>
Signed-off-by: pryce-turner <[email protected]>
  • Loading branch information
pryce-turner and neverett authored Mar 11, 2024
1 parent 8ec5a04 commit 035e4ec
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions docs/user_guide/advanced_composition/nested_parallelization.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,34 @@ kernelspec:
name: python3
---

+++ {"lines_to_next_cell": 0}

(nested_parallelization)=

# Nested Parallelization
# Nested parallelization

```{eval-rst}
.. tags:: Advanced
```

For exceptionally large or complicated workflows where dynamic workflows or map tasks aren't enough, it can be benficial to have multiple levels of parallelization.
For exceptionally large or complicated workflows that can't be adequately implemented as dynamic workflows or map tasks, it can be beneficial to have multiple levels of workflow parallelization.

This is useful for multiple reasons:
Nested parallelization enables:
1. Better code organization
2. Better code reuse
3. Better testing
4. Better debugging
5. Better monitoring - each subworkflow can be run independently and monitored independently
6. Better performance and scale - each subworkflow is executed as a separate workflow and thus can land on different flytepropeller workers and shards. This allows for better parallelism and scale.

## Nested Dynamics
## Nested dynamic workflows

This example shows how to break down a large workflow into smaller workflows and then compose them together to form a hierarchy.
You can use nested dynamic workflows to break down a large workflow into smaller workflows and then compose them together to form a hierarchy. In this example, a top-level workflow uses two levels of dynamic workflows to process a list through some simple addition tasks and then flatten the list again.

### Example code

```python
"""
The structure for 6 items and a chunk size of 2, the workflow will be broken down as follows:
A core workflow parallelized as six items with a chunk size of two will be structured as follows:
multi_wf -> level1 -> level2 -> core_wf -> step1 -> step2
-> core_wf -> step1 -> step2
Expand Down Expand Up @@ -99,36 +100,39 @@ def multi_wf(l: typing.List[int], chunk: int) -> typing.List[int]:
return level1(l=l, chunk=chunk)
```

+++ {"lines_to_next_cell": 0}

This shows a top-level workflow which uses 2 levels of dynamic workflows to process a list through some simple addition tasks and then flatten it again. Here is a visual representation of the execution in a Flyte console:
### Flyte console

Here is a visual representation of the execution of nested dynamic workflows in the Flyte console:

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/nested_parallel_top_level.png?raw=true
:alt: Nested Parallelization UI View
:class: with-shadow
:::

For each node in that top-level we can see 2 sub-workflows being run in parallel.
In each level2 node at the top level, two core workflows are run in parallel:

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/nested_parallel_inner_dynamic.png?raw=true
:alt: Inner Dynamic
:alt: Inner dynamic workflow
:class: with-shadow
:::

Finally, drilling into each sub-workflow, we'll see both those tasks being executed in series.
Finally, in each core workflow, the two tasks are executed in series:

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/nested_parallel_subworkflow.png?raw=true
:alt: Sub-Workflow
:alt: Core workflow
:class: with-shadow
:::

## Mixed Parallelism
## Mixed parallelism

This example is similar to the above but instead of using a dynamic to parallelize a 2-task serial workflow, we're using that workflow to call a map task which processes both inputs in parallel. This workflow has one fewer layers of parallelism so the outputs won't be the same, but it does still demonstrate how you can mix these different approaches to achieving concurrency.
This example is similar to nested dynamic workflows, but instead of using a dynamic workflow to parallelize a core workflow with serial tasks, we use a core workflow to call a map task, which processes both inputs in parallel. This workflow has one less layer of parallelism, so the outputs won't be the same as those of the nested parallelization example, but it does still demonstrate how you can mix these different approaches to achieve concurrency.

### Example code

```python
"""
The structure for 6 items and a chunk size of 2, the workflow will be broken down as follows:
A core workflow parallelized as six items with a chunk size of two will be structured as follows:
multi_wf -> level1 -> level2 -> mappable
-> mappable
Expand Down Expand Up @@ -172,19 +176,20 @@ def multi_wf(l: typing.List[int], chunk: int) -> typing.List[int]:
return level1(l=l, chunk=chunk)

```
+++ {"lines_to_next_cell": 0}

While the top-level dynamic will be exactly the same as the previous example, here you can see the inner map task nodes as well as links in the sidebar.
### Flyte console

While the top-level dynamic workflow will be exactly the same as the nested dynamic workflows example, the inner map task nodes will be visible as links in the sidebar:

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/nested_parallel_inner_map.png?raw=true
:alt: Inner Map Task
:class: with-shadow
:::

## Design Considerations
## Design considerations

You can nest even further if needed, or incorporate map tasks if your inputs are all the same type. The design of your workflow should of course be informed by the actual data you're processing. For example if you have a big library of music that you'd like to extract the lyrics for, the first level can loop through all the albums and the second level can go through each song.
While you can nest even further if needed, or incorporate map tasks if your inputs are all the same type, the design of your workflow should be informed by the actual data you're processing. For example, if you have a big library of music from which you'd like to extract the lyrics, the first level could loop through all the albums, and the second level could process each song.

If you're just churning through an enormous list of the same input, it's typically best to keep the code simple and let the scheduler handle optimizing the execution. Additionally, unless you need the features of a dynamic workflow like mixing and matching inputs and outputs, it's usually most efficient to use a map task. This has the added benefit of keeping the UI clean.
If you're just processing an enormous list of the same input, it's best to keep your code simple and let the scheduler handle optimizing the execution. Additionally, unless you need dynamic workflow features like mixing and matching inputs and outputs, it's usually most efficient to use a map task, which has the added benefit of keeping the UI clean.

You can also choose to limit the scale of parallel execution at a couple levels. The `max_parallelism` attribute can be applied at the workflow level and will limit the number of parallel tasks being executed, this is set to 25 by default. Within map tasks specifically, you can indicate a `concurrency` argument which will limit the number of mapped tasks within running at any given time.
You can also choose to limit the scale of parallel execution at a few levels. The `max_parallelism` attribute can be applied at the workflow level and will limit the number of parallel tasks being executed. (This is set to 25 by default.) Within map tasks, you can specify a `concurrency` argument, which will limit the number of mapped tasks that can run in parallel at any given time.

0 comments on commit 035e4ec

Please sign in to comment.