Skip to content

Commit

Permalink
Add example for resource overrides (flyteorg#834)
Browse files Browse the repository at this point in the history
* Add example for resource overrides

Signed-off-by: Kevin Su <[email protected]>

Co-authored-by: SmritiSatyanV <[email protected]>
  • Loading branch information
pingsutw and SmritiSatyanV authored Jul 8, 2022
1 parent d78a5dc commit 070a99c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ on:
- master
paths-ignore:
- 'cookbook/docs/**'
- 'cookbook/deployment/**'
pull_request:
paths-ignore:
- 'cookbook/docs/**'
- 'cookbook/deployment/**'
jobs:

# Lists all directories that have Dockerfile in them...
Expand Down Expand Up @@ -146,4 +144,4 @@ jobs:
--data-raw '{
"tag_name": "'${{ needs.bump-version.outputs.version }}'",
"prerelease": false
}'
}'
81 changes: 70 additions & 11 deletions cookbook/deployment/customizing_resources.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""
Customizing Task Resources
---------------------------
One of the reasons to use a hosted Flyte environment is the potential of leveraging CPU, memory and storage resources, far greater than what's available locally.
Flytekit makes it possible to specify these requirements declaratively and close to where the task itself is declared.
"""

# %%
Expand All @@ -25,20 +23,22 @@
# #. ``mem``
# #. ``gpu``
#
# To ensure regular tasks that do not require GPUs are not scheduled on GPU nodes, a separate node group for GPU nodes can be configured with `taints <https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/>`_.
#
# To ensure tasks that do require GPUs get the needed tolerations on their pods, set up flytepropeller using the following `configuration <https://github.com/flyteorg/flytepropeller/blob/v0.10.5/config.yaml#L51,L56>`_. Make sure that this toleration config matches the taint config that you have configured to protect your gpu providing nodes from having to deal with regular non-gpu workloads (pods).
# To ensure that regular tasks that don't require GPUs are not scheduled on GPU nodes, a separate node group for GPU nodes can be configured with `taints <https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/>`_.
#
# The ``storage`` resources option is not yet supported, but coming soon.
# To ensure that tasks that require GPUs get the needed tolerations on their pods, set up FlytePropeller using the following `configuration <https://github.com/flyteorg/flytepropeller/blob/v0.10.5/config.yaml#L51,L56>`_. Ensure that this toleration config matches the taint config you have configured to protect your GPU providing nodes from dealing with regular non-GPU workloads (pods).
#
# The actual values follow the `kubernetes convention <https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes>`_.

# The actual values follow the `Kubernetes convention <https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes>`_.
# Let's look at an example to understand how to customize resources.

# %%
# Import the dependencies.
import typing

from flytekit import Resources, task, workflow


# %%
# Define a task and configure the resources to be allocated to it.
@task(requests=Resources(cpu="1", mem="100Mi"), limits=Resources(cpu="2", mem="150Mi"))
def count_unique_numbers(x: typing.List[int]) -> int:
s = set()
Expand All @@ -48,21 +48,80 @@ def count_unique_numbers(x: typing.List[int]) -> int:


# %%
# Let's create a dummy task that determines the square of a number.
# Define a task that computes the square of a number.
@task
def square(x: int) -> int:
return x * x


# %%
# The tasks decorated with memory and storage hints can be used like regular tasks in a workflow.
# You can use the tasks decorated with memory and storage hints like regular tasks in a workflow.
@workflow
def my_workflow(x: typing.List[int]) -> int:
return square(x=count_unique_numbers(x=x))


# %%
# The workflow and task can be executed locally.
# You can execute the workflow locally.
if __name__ == "__main__":
print(count_unique_numbers(x=[1, 1, 2]))
print(my_workflow(x=[1, 1, 2]))

# %%
# Using ``with_overrides``
# ^^^^^^^^^^^^^^^^^^^^^^^^
#
# You can use the ``with_overrides`` method to override the resources allocated to the tasks dynamically.
# Let's understand how the resources can be initialized with an example.
#

# %%
# Import the dependencies.
import typing # noqa: E402

from flytekit import Resources, task, workflow # noqa: E402


# %%
# Define a task and configure the resources to be allocated to it.
# You can use tasks decorated with memory and storage hints like regular tasks in a workflow.
@task(requests=Resources(cpu="2", mem="200Mi"), limits=Resources(cpu="3", mem="350Mi"))
def count_unique_numbers_1(x: typing.List[int]) -> int:
s = set()
for i in x:
s.add(i)
return len(s)


# %%
# Define a task that computes the square of a number.
@task
def square_1(x: int) -> int:
return x * x


# %%
# The ``with_overrides`` method overrides the old resource allocations.
@workflow
def my_pipeline(x: typing.List[int]) -> int:
return square_1(x=count_unique_numbers_1(x=x)).with_overrides(
limits=Resources(cpu="6", mem="500Mi")
)


# %%
# You can execute the workflow locally.
if __name__ == "__main__":
print(count_unique_numbers_1(x=[1, 1, 2]))
print(my_pipeline(x=[1, 1, 2]))

# %%
# You can see the memory allocation below. The memory limit is ``500Mi`` rather than ``350Mi``, and the
# CPU limit is 4, whereas it should have been 6 as specified using ``with_overrides``.
# This is because the default platform CPU quota for every pod is 4.
#
# .. figure:: https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/core/resource_allocation.png
# :alt: Resource allocated using "with_overrides" method
#
# Resource allocated using "with_overrides" method

0 comments on commit 070a99c

Please sign in to comment.