.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto/deployment/customizing_resources.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_deployment_customizing_resources.py: 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. .. GENERATED FROM PYTHON SOURCE LINES 9-32 In this example, the memory required by the function increases as the dataset size increases. Large datasets may not be able to run locally, so we would want to provide hints to the Flyte backend to request for more memory. This is done by decorating the task with the hints as shown in the following code sample. Tasks can have ``requests`` and ``limits`` which mirror the native `equivalents in Kubernetes `__. A task can possibly be allocated more resources than it requests, but never more than its limit. Requests are treated as hints to schedule tasks on nodes with available resources, whereas limits are hard constraints. For either a request or limit, refer to the :py:class:`flytekit:flytekit.Resources` documentation. The following attributes can be specified for a ``Resource``. #. ``cpu`` #. ``mem`` #. ``gpu`` 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 `_. To ensure that tasks that require GPUs get the needed tolerations on their pods, set up FlytePropeller using the following `configuration `_. 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 `_. Let's look at an example to understand how to customize resources. .. GENERATED FROM PYTHON SOURCE LINES 34-35 Import the dependencies. .. GENERATED FROM PYTHON SOURCE LINES 35-40 .. code-block:: default import typing from flytekit import Resources, task, workflow .. GENERATED FROM PYTHON SOURCE LINES 41-42 Define a task and configure the resources to be allocated to it. .. GENERATED FROM PYTHON SOURCE LINES 42-50 .. code-block:: default @task(requests=Resources(cpu="1", mem="100Mi"), limits=Resources(cpu="2", mem="150Mi")) def count_unique_numbers(x: typing.List[int]) -> int: s = set() for i in x: s.add(i) return len(s) .. GENERATED FROM PYTHON SOURCE LINES 51-52 Define a task that computes the square of a number. .. GENERATED FROM PYTHON SOURCE LINES 52-57 .. code-block:: default @task def square(x: int) -> int: return x * x .. GENERATED FROM PYTHON SOURCE LINES 58-59 You can use the tasks decorated with memory and storage hints like regular tasks in a workflow. .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: default @workflow def my_workflow(x: typing.List[int]) -> int: return square(x=count_unique_numbers(x=x)) .. GENERATED FROM PYTHON SOURCE LINES 65-66 You can execute the workflow locally. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: default if __name__ == "__main__": print(count_unique_numbers(x=[1, 1, 2])) print(my_workflow(x=[1, 1, 2])) .. GENERATED FROM PYTHON SOURCE LINES 71-72 .. note:: To alter the limits of the default platform configuration, change the `admin config `_ and `namespace level quota `_ on the cluster. .. GENERATED FROM PYTHON SOURCE LINES 74-80 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. .. GENERATED FROM PYTHON SOURCE LINES 82-83 Import the dependencies. .. GENERATED FROM PYTHON SOURCE LINES 83-88 .. code-block:: default import typing # noqa: E402 from flytekit import Resources, task, workflow # noqa: E402 .. GENERATED FROM PYTHON SOURCE LINES 89-91 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. .. GENERATED FROM PYTHON SOURCE LINES 91-99 .. code-block:: default @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) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Define a task that computes the square of a number. .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. code-block:: default @task def square_1(x: int) -> int: return x * x .. GENERATED FROM PYTHON SOURCE LINES 107-108 The ``with_overrides`` method overrides the old resource allocations. .. GENERATED FROM PYTHON SOURCE LINES 108-115 .. code-block:: default @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") ) .. GENERATED FROM PYTHON SOURCE LINES 116-117 You can execute the workflow locally. .. GENERATED FROM PYTHON SOURCE LINES 117-121 .. code-block:: default if __name__ == "__main__": print(count_unique_numbers_1(x=[1, 1, 2])) print(my_pipeline(x=[1, 1, 2])) .. GENERATED FROM PYTHON SOURCE LINES 122-130 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_deployment_customizing_resources.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: customizing_resources.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: customizing_resources.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_