Skip to content

Commit

Permalink
update naming
Browse files Browse the repository at this point in the history
  • Loading branch information
panh99 committed Dec 1, 2023
1 parent 2d1b983 commit d897fe8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
34 changes: 17 additions & 17 deletions doc/source/how-to-use-middleware.rst
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
Use Built-in Middleware Layers
==============================

In this tutorial, we will learn how to utilize built-in middleware layers to augment the behavior of an application. Middleware allows us to perform operations before and after a task is processed in the application.
In this tutorial, we will learn how to utilize built-in middleware layers to augment the behavior of a flower callable. Middleware allows us to perform operations before and after a task is processed in the flower callable.

What is Middleware?
-------------------

Middleware is a callable that wraps around an application. It can manipulate or inspect incoming tasks (``TaskIns``) in the ``Fwd`` and the resulting tasks (``TaskRes``) in the ``Bwd``. The signature for a middleware layer (``Layer``) is as follows:
Middleware is a callable that wraps around a Flower Callable. It can manipulate or inspect incoming tasks (``TaskIns``) in the ``Fwd`` and the resulting tasks (``TaskRes``) in the ``Bwd``. The signature for a middleware layer (``Layer``) is as follows:

.. code-block:: python
App = Callable[[Fwd], Bwd]
Layer = Callable[[Fwd, App], Bwd]
FlowerCallable = Callable[[Fwd], Bwd]
Layer = Callable[[Fwd, FlowerCallable], Bwd]
A typical middleware function might look something like this:

.. code-block:: python
def example_middleware(fwd: Fwd, app: App) -> Bwd:
# Do something with Fwd before passing to app.
bwd = app(fwd)
def example_middleware(fwd: Fwd, fc: FlowerCallable) -> Bwd:
# Do something with Fwd before passing to the inner flower callable.
bwd = fc(fwd)
# Do something with Bwd before returning.
return bwd
Using Middleware Layers
-----------------------

To use middleware layers in your application, you can follow these steps:
To use middleware layers in your flower callable, you can follow these steps:

1. Import the Required Middleware
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -45,18 +45,18 @@ Define your client function (``client_fn``) that will be wrapped by the middlewa

.. code-block:: python
def client_fn():
def client_fn(cid):
# Your client code goes here.
pass
return # your client
3. Create the Application with Middleware
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3. Create the FlowerCallable with Middleware
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create your application and pass the middleware layers as a list to the ``middleware`` argument. The order in which you provide the middleware layers matters:
Create your flower callable and pass the middleware layers as a list to the ``middleware`` argument. The order in which you provide the middleware layers matters:

.. code-block:: python
app = fl.app.Flower(
flower = fl.app.Flower(
client_fn=client_fn,
middleware=[
example_middleware1, # Middleware layer 1
Expand All @@ -67,7 +67,7 @@ Create your application and pass the middleware layers as a list to the ``middle
Order of Execution
------------------

When the application runs, the middleware layers are executed in the order they are provided in the list:
When the flower callable runs, the middleware layers are executed in the order they are provided in the list:

1. ``example_middleware1`` (outermost layer)
2. ``example_middleware2`` (next layer)
Expand All @@ -80,6 +80,6 @@ Each middleware has a chance to inspect and modify the ``TaskIns`` in the ``Fwd`
Conclusion
----------

By following this guide, you have learned how to effectively use middleware layers to enhance your application's functionality. Remember that the order of middleware is crucial and affects how the input and output are processed.
By following this guide, you have learned how to effectively use middleware layers to enhance your flower callable's functionality. Remember that the order of middleware is crucial and affects how the input and output are processed.

Enjoy building more robust and flexible applications with middleware layers!
Enjoy building more robust and flexible flower callables with middleware layers!
6 changes: 3 additions & 3 deletions src/py/flwr/client/flower.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ def __init__(
middleware: Optional[List[Layer]] = None,
) -> None:
# Create wrapper function for `handle`
def fn(fwd: Fwd) -> Bwd:
def ffn(fwd: Fwd) -> Bwd: # pylint: disable=invalid-name
task_res, state_updated = handle(
client_fn=client_fn,
state=fwd.state,
task_ins=fwd.task_ins,
)
return Bwd(task_res=task_res, state=state_updated)

# Wrap middleware layers around fn
self._call = make_fc(fn, middleware if middleware is not None else [])
# Wrap middleware layers around the wrapped handle function
self._call = make_fc(ffn, middleware if middleware is not None else [])

def __call__(self, fwd: Fwd) -> Bwd:
"""."""
Expand Down
13 changes: 8 additions & 5 deletions src/py/flwr/client/middleware/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
# ==============================================================================
"""Utility functions for middleware layers."""


from typing import List

from flwr.client.typing import Bwd, FlowerCallable, Fwd, Layer


def make_fc(app: FlowerCallable, middleware_layers: List[Layer]) -> FlowerCallable:
def make_fc(
flower_callable: FlowerCallable, middleware_layers: List[Layer]
) -> FlowerCallable:
"""."""

def wrap_app(_app: FlowerCallable, _layer: Layer) -> FlowerCallable:
def wrap_fc(_fc: FlowerCallable, _layer: Layer) -> FlowerCallable:
def new_app(fwd: Fwd) -> Bwd:
return _layer(fwd, _app)
return _layer(fwd, _fc)

return new_app

for layer in reversed(middleware_layers):
app = wrap_app(app, layer)
flower_callable = wrap_fc(flower_callable, layer)

return app
return flower_callable

0 comments on commit d897fe8

Please sign in to comment.