From 81cc08ed68d8fa24920faea254ccf529e13af888 Mon Sep 17 00:00:00 2001 From: Javier Date: Tue, 30 Jan 2024 10:30:10 +0000 Subject: [PATCH] Stop using `start_numpy_client` in e2e tests (#2869) Co-authored-by: Daniel J. Beutel --- ...mple-jax-from-centralized-to-federated.rst | 2 +- ...-pytorch-from-centralized-to-federated.rst | 4 +-- doc/source/how-to-enable-ssl-connections.rst | 4 +-- doc/source/how-to-run-simulations.rst | 2 +- doc/source/ref-changelog.md | 4 +++ .../tutorial-quickstart-huggingface.rst | 4 +-- doc/source/tutorial-quickstart-jax.rst | 2 +- doc/source/tutorial-quickstart-pytorch.rst | 4 +-- .../tutorial-quickstart-scikitlearn.rst | 4 +-- doc/source/tutorial-quickstart-tensorflow.rst | 4 +-- e2e/bare-https/client.py | 4 +-- e2e/bare/client.py | 2 +- e2e/fastai/client.py | 4 +-- e2e/jax/client.py | 2 +- e2e/opacus/client.py | 4 +-- e2e/pandas/client.py | 4 +-- e2e/pytorch-lightning/client.py | 4 +-- e2e/pytorch/client.py | 4 +-- e2e/scikit-learn/client.py | 2 +- e2e/strategies/client.py | 2 +- e2e/tabnet/client.py | 2 +- e2e/tensorflow/client.py | 2 +- src/py/flwr/client/app.py | 35 +++++++++++-------- src/py/flwr/simulation/ray_transport/utils.py | 21 +++++------ 24 files changed, 69 insertions(+), 57 deletions(-) diff --git a/doc/source/example-jax-from-centralized-to-federated.rst b/doc/source/example-jax-from-centralized-to-federated.rst index 2b1823e9d408..6b06a288a67a 100644 --- a/doc/source/example-jax-from-centralized-to-federated.rst +++ b/doc/source/example-jax-from-centralized-to-federated.rst @@ -259,7 +259,7 @@ Having defined the federation process, we can run it. # Start Flower client client = FlowerClient(params, grad_fn, train_x, train_y, test_x, test_y) - fl.client.start_numpy_client(server_address="0.0.0.0:8080", client) + fl.client.start_client(server_address="0.0.0.0:8080", client.to_client()) if __name__ == "__main__": main() diff --git a/doc/source/example-pytorch-from-centralized-to-federated.rst b/doc/source/example-pytorch-from-centralized-to-federated.rst index d649658667da..58101135b8c0 100644 --- a/doc/source/example-pytorch-from-centralized-to-federated.rst +++ b/doc/source/example-pytorch-from-centralized-to-federated.rst @@ -278,7 +278,7 @@ We included type annotations to give you a better understanding of the data type return float(loss), self.num_examples["testset"], {"accuracy": float(accuracy)} All that's left to do it to define a function that loads both model and data, creates a :code:`CifarClient`, and starts this client. -You load your data and model by using :code:`cifar.py`. Start :code:`CifarClient` with the function :code:`fl.client.start_numpy_client()` by pointing it at the same IP adress we used in :code:`server.py`: +You load your data and model by using :code:`cifar.py`. Start :code:`CifarClient` with the function :code:`fl.client.start_client()` by pointing it at the same IP adress we used in :code:`server.py`: .. code-block:: python @@ -292,7 +292,7 @@ You load your data and model by using :code:`cifar.py`. Start :code:`CifarClient # Start client client = CifarClient(model, trainloader, testloader, num_examples) - fl.client.start_numpy_client(server_address="0.0.0.0:8080", client) + fl.client.start_client(server_address="0.0.0.0:8080", client.to_client()) if __name__ == "__main__": diff --git a/doc/source/how-to-enable-ssl-connections.rst b/doc/source/how-to-enable-ssl-connections.rst index fa59d4423c5a..051dd5711497 100644 --- a/doc/source/how-to-enable-ssl-connections.rst +++ b/doc/source/how-to-enable-ssl-connections.rst @@ -75,9 +75,9 @@ We are now going to show how to write a client which uses the previously generat client = MyFlowerClient() # Start client - fl.client.start_numpy_client( + fl.client.start_client( "localhost:8080", - client=client, + client=client.to_client(), root_certificates=Path(".cache/certificates/ca.crt").read_bytes(), ) diff --git a/doc/source/how-to-run-simulations.rst b/doc/source/how-to-run-simulations.rst index 707e3d3ffe84..6e0520a79bf5 100644 --- a/doc/source/how-to-run-simulations.rst +++ b/doc/source/how-to-run-simulations.rst @@ -7,7 +7,7 @@ Run simulations Simulating Federated Learning workloads is useful for a multitude of use-cases: you might want to run your workload on a large cohort of clients but without having to source, configure and mange a large number of physical devices; you might want to run your FL workloads as fast as possible on the compute systems you have access to without having to go through a complex setup process; you might want to validate your algorithm on different scenarios at varying levels of data and system heterogeneity, client availability, privacy budgets, etc. These are among some of the use-cases where simulating FL workloads makes sense. Flower can accommodate these scenarios by means of its `VirtualClientEngine `_ or VCE. -The :code:`VirtualClientEngine` schedules, launches and manages `virtual` clients. These clients are identical to `non-virtual` clients (i.e. the ones you launch via the command `flwr.client.start_numpy_client `_) in the sense that they can be configure by creating a class inheriting, for example, from `flwr.client.NumPyClient `_ and therefore behave in an identical way. In addition to that, clients managed by the :code:`VirtualClientEngine` are: +The :code:`VirtualClientEngine` schedules, launches and manages `virtual` clients. These clients are identical to `non-virtual` clients (i.e. the ones you launch via the command `flwr.client.start_client `_) in the sense that they can be configure by creating a class inheriting, for example, from `flwr.client.NumPyClient `_ and therefore behave in an identical way. In addition to that, clients managed by the :code:`VirtualClientEngine` are: * resource-aware: this means that each client gets assigned a portion of the compute and memory on your system. You as a user can control this at the beginning of the simulation and allows you to control the degree of parallelism of your Flower FL simulation. The fewer the resources per client, the more clients can run concurrently on the same hardware. * self-managed: this means that you as a user do not need to launch clients manually, instead this gets delegated to :code:`VirtualClientEngine`'s internals. diff --git a/doc/source/ref-changelog.md b/doc/source/ref-changelog.md index 5f323bc80baa..f1af015ac24c 100644 --- a/doc/source/ref-changelog.md +++ b/doc/source/ref-changelog.md @@ -8,6 +8,10 @@ - **Retiring MXNet examples** The development of the MXNet fremework has ended and the project is now [archived on GitHub](https://github.com/apache/mxnet). Existing MXNet examples won't receive updates [#2724](https://github.com/adap/flower/pull/2724) +- **Deprecated `start_numpy_client`**. ([#2563](https://github.com/adap/flower/pull/2563)) + + Until now, clients of type `NumPyClient` needed to be started via `start_numpy_client`. In our efforts to consolidate the core framework, we have introduced changes, and now all client types should start via `start_client`. To continue using `NumPyClient` clients, you simply need to first call the `.to_client()` method and then pass returned `Client` object to `start_client`. The examples and the documentation have been updated accordingly. + - **Update Flower Baselines** - HFedXGBoost [#2226](https://github.com/adap/flower/pull/2226) diff --git a/doc/source/tutorial-quickstart-huggingface.rst b/doc/source/tutorial-quickstart-huggingface.rst index 7718e6558456..1e06120b452f 100644 --- a/doc/source/tutorial-quickstart-huggingface.rst +++ b/doc/source/tutorial-quickstart-huggingface.rst @@ -212,9 +212,9 @@ We can now start client instances using: .. code-block:: python - fl.client.start_numpy_client( + fl.client.start_client( server_address="127.0.0.1:8080", - client=IMDBClient() + client=IMDBClient().to_client() ) diff --git a/doc/source/tutorial-quickstart-jax.rst b/doc/source/tutorial-quickstart-jax.rst index 945f231e112e..d2b9243e2bb3 100644 --- a/doc/source/tutorial-quickstart-jax.rst +++ b/doc/source/tutorial-quickstart-jax.rst @@ -265,7 +265,7 @@ Having defined the federation process, we can run it. # Start Flower client client = FlowerClient(params, grad_fn, train_x, train_y, test_x, test_y) - fl.client.start_numpy_client(server_address="0.0.0.0:8080", client) + fl.client.start_client(server_address="0.0.0.0:8080", client=client.to_client()) if __name__ == "__main__": main() diff --git a/doc/source/tutorial-quickstart-pytorch.rst b/doc/source/tutorial-quickstart-pytorch.rst index fb77d107b63f..f15a4a93114e 100644 --- a/doc/source/tutorial-quickstart-pytorch.rst +++ b/doc/source/tutorial-quickstart-pytorch.rst @@ -191,10 +191,10 @@ to actually run this client: .. code-block:: python - fl.client.start_numpy_client(server_address="[::]:8080", client=CifarClient()) + fl.client.start_client(server_address="[::]:8080", client=CifarClient().to_client()) That's it for the client. We only have to implement :code:`Client` or -:code:`NumPyClient` and call :code:`fl.client.start_client()` or :code:`fl.client.start_numpy_client()`. The string :code:`"[::]:8080"` tells the client which server to connect to. In our case we can run the server and the client on the same machine, therefore we use +:code:`NumPyClient` and call :code:`fl.client.start_client()`. If you implement a client of type :code:`NumPyClient` you'll need to first call its :code:`to_client()` method. The string :code:`"[::]:8080"` tells the client which server to connect to. In our case we can run the server and the client on the same machine, therefore we use :code:`"[::]:8080"`. If we run a truly federated workload with the server and clients running on different machines, all that needs to change is the :code:`server_address` we point the client at. diff --git a/doc/source/tutorial-quickstart-scikitlearn.rst b/doc/source/tutorial-quickstart-scikitlearn.rst index b33068e975fa..4921f63bab2c 100644 --- a/doc/source/tutorial-quickstart-scikitlearn.rst +++ b/doc/source/tutorial-quickstart-scikitlearn.rst @@ -145,10 +145,10 @@ to actually run this client: .. code-block:: python - fl.client.start_numpy_client("0.0.0.0:8080", client=MnistClient()) + fl.client.start_client("0.0.0.0:8080", client=MnistClient().to_client()) That's it for the client. We only have to implement :code:`Client` or -:code:`NumPyClient` and call :code:`fl.client.start_client()` or :code:`fl.client.start_numpy_client()`. The string :code:`"0.0.0.0:8080"` tells the client which server to connect to. In our case we can run the server and the client on the same machine, therefore we use +:code:`NumPyClient` and call :code:`fl.client.start_client()`. If you implement a client of type :code:`NumPyClient` you'll need to first call its :code:`to_client()` method. The string :code:`"0.0.0.0:8080"` tells the client which server to connect to. In our case we can run the server and the client on the same machine, therefore we use :code:`"0.0.0.0:8080"`. If we run a truly federated workload with the server and clients running on different machines, all that needs to change is the :code:`server_address` we pass to the client. diff --git a/doc/source/tutorial-quickstart-tensorflow.rst b/doc/source/tutorial-quickstart-tensorflow.rst index 64b2255a9ac6..bd63eb461d21 100644 --- a/doc/source/tutorial-quickstart-tensorflow.rst +++ b/doc/source/tutorial-quickstart-tensorflow.rst @@ -84,11 +84,11 @@ to actually run this client: .. code-block:: python - fl.client.start_numpy_client(server_address="[::]:8080", client=CifarClient()) + fl.client.start_client(server_address="[::]:8080", client=CifarClient().to_client()) That's it for the client. We only have to implement :code:`Client` or -:code:`NumPyClient` and call :code:`fl.client.start_client()` or :code:`fl.client.start_numpy_client()`. The string :code:`"[::]:8080"` tells the client which server to connect to. In our case we can run the server and the client on the same machine, therefore we use +:code:`NumPyClient` and call :code:`fl.client.start_client()`. If you implement a client of type :code:`NumPyClient` you'll need to first call its :code:`to_client()` method. The string :code:`"[::]:8080"` tells the client which server to connect to. In our case we can run the server and the client on the same machine, therefore we use :code:`"[::]:8080"`. If we run a truly federated workload with the server and clients running on different machines, all that needs to change is the :code:`server_address` we point the client at. diff --git a/e2e/bare-https/client.py b/e2e/bare-https/client.py index 20a5b4875ddf..04563acdd73f 100644 --- a/e2e/bare-https/client.py +++ b/e2e/bare-https/client.py @@ -31,9 +31,9 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client( + fl.client.start_client( server_address="127.0.0.1:8080", - client=FlowerClient(), + client=FlowerClient().to_client(), root_certificates=Path("certificates/ca.crt").read_bytes(), insecure=False, ) diff --git a/e2e/bare/client.py b/e2e/bare/client.py index 59ef2e4248ee..1068bd7da6d1 100644 --- a/e2e/bare/client.py +++ b/e2e/bare/client.py @@ -52,4 +52,4 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=FlowerClient()) + fl.client.start_client(server_address="127.0.0.1:8080", client=FlowerClient().to_client()) diff --git a/e2e/fastai/client.py b/e2e/fastai/client.py index 4425fed25277..69b20f7e26c3 100644 --- a/e2e/fastai/client.py +++ b/e2e/fastai/client.py @@ -60,7 +60,7 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client( + fl.client.start_client( server_address="127.0.0.1:8080", - client=FlowerClient(), + client=FlowerClient().to_client(), ) diff --git a/e2e/jax/client.py b/e2e/jax/client.py index 495d6a671981..1536487dd466 100644 --- a/e2e/jax/client.py +++ b/e2e/jax/client.py @@ -59,4 +59,4 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=FlowerClient()) + fl.client.start_client(server_address="127.0.0.1:8080", client=FlowerClient().to_client()) diff --git a/e2e/opacus/client.py b/e2e/opacus/client.py index 2e5c363381fa..205bc39adbac 100644 --- a/e2e/opacus/client.py +++ b/e2e/opacus/client.py @@ -142,7 +142,7 @@ def client_fn(cid): ) if __name__ == "__main__": - fl.client.start_numpy_client( + fl.client.start_client( server_address="127.0.0.1:8080", - client=FlowerClient(model) + client=FlowerClient(model).to_client() ) diff --git a/e2e/pandas/client.py b/e2e/pandas/client.py index 5b8670091cb3..efc2eb763f53 100644 --- a/e2e/pandas/client.py +++ b/e2e/pandas/client.py @@ -42,7 +42,7 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client( + fl.client.start_client( server_address="127.0.0.1:8080", - client=FlowerClient(), + client=FlowerClient().to_client(), ) diff --git a/e2e/pytorch-lightning/client.py b/e2e/pytorch-lightning/client.py index 71b178eca8c3..632683e8ec01 100644 --- a/e2e/pytorch-lightning/client.py +++ b/e2e/pytorch-lightning/client.py @@ -65,8 +65,8 @@ def main() -> None: train_loader, val_loader, test_loader = mnist.load_data() # Flower client - client = FlowerClient(model, train_loader, val_loader, test_loader) - fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=client) + client = FlowerClient(model, train_loader, val_loader, test_loader).to_client() + fl.client.start_client(server_address="127.0.0.1:8080", client=client) if __name__ == "__main__": diff --git a/e2e/pytorch/client.py b/e2e/pytorch/client.py index ccd36f47d22a..cce4701ff6e6 100644 --- a/e2e/pytorch/client.py +++ b/e2e/pytorch/client.py @@ -133,7 +133,7 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client( + fl.client.start_client( server_address="127.0.0.1:8080", - client=FlowerClient(), + client=FlowerClient().to_client(), ) diff --git a/e2e/scikit-learn/client.py b/e2e/scikit-learn/client.py index fdca96c1697a..e137a28c356b 100644 --- a/e2e/scikit-learn/client.py +++ b/e2e/scikit-learn/client.py @@ -52,4 +52,4 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client(server_address="0.0.0.0:8080", client=FlowerClient()) + fl.client.start_client(server_address="0.0.0.0:8080", client=FlowerClient().to_client()) diff --git a/e2e/strategies/client.py b/e2e/strategies/client.py index eb4598cb5439..5a341d041f12 100644 --- a/e2e/strategies/client.py +++ b/e2e/strategies/client.py @@ -54,4 +54,4 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=FlowerClient()) + fl.client.start_client(server_address="127.0.0.1:8080", client=FlowerClient().to_client()) diff --git a/e2e/tabnet/client.py b/e2e/tabnet/client.py index 3c10df0c79f1..5e3a5e52680b 100644 --- a/e2e/tabnet/client.py +++ b/e2e/tabnet/client.py @@ -87,4 +87,4 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=FlowerClient()) + fl.client.start_client(server_address="127.0.0.1:8080", client=FlowerClient().to_client()) diff --git a/e2e/tensorflow/client.py b/e2e/tensorflow/client.py index 4ad2d5ebda57..fc5eb5ab39b3 100644 --- a/e2e/tensorflow/client.py +++ b/e2e/tensorflow/client.py @@ -40,4 +40,4 @@ def client_fn(cid): if __name__ == "__main__": # Start Flower client - fl.client.start_numpy_client(server_address="127.0.0.1:8080", client=FlowerClient()) + fl.client.start_client(server_address="127.0.0.1:8080", client=FlowerClient().to_client()) diff --git a/src/py/flwr/client/app.py b/src/py/flwr/client/app.py index dcea3b91da83..2f02e753d63a 100644 --- a/src/py/flwr/client/app.py +++ b/src/py/flwr/client/app.py @@ -34,7 +34,7 @@ TRANSPORT_TYPE_REST, TRANSPORT_TYPES, ) -from flwr.common.logger import log, warn_experimental_feature +from flwr.common.logger import log, warn_deprecated_feature, warn_experimental_feature from flwr.common.message import Message from .flower import load_flower_callable @@ -399,6 +399,12 @@ def start_numpy_client( ) -> None: """Start a Flower NumPyClient which connects to a gRPC server. + Warning + ------- + This function is deprecated since 1.7.0. Use :code:`flwr.client.start_client` + instead and first convert your :code:`NumPyClient` to type + :code:`flwr.client.Client` by executing its :code:`to_client()` method. + Parameters ---------- server_address : str @@ -454,21 +460,22 @@ def start_numpy_client( >>> root_certificates=Path("/crts/root.pem").read_bytes(), >>> ) """ - # warnings.warn( - # "flwr.client.start_numpy_client() is deprecated and will " - # "be removed in a future version of Flower. Instead, pass " - # "your client to `flwr.client.start_client()` by calling " - # "first the `.to_client()` method as shown below: \n" - # "\tflwr.client.start_client(\n" - # "\t\tserver_address=':',\n" - # "\t\tclient=FlowerClient().to_client()\n" - # "\t)", - # DeprecationWarning, - # stacklevel=2, - # ) + mssg = ( + "flwr.client.start_numpy_client() is deprecated. \n\tInstead, use " + "`flwr.client.start_client()` by ensuring you first call " + "the `.to_client()` method as shown below: \n" + "\tflwr.client.start_client(\n" + "\t\tserver_address=':',\n" + "\t\tclient=FlowerClient().to_client()," + " # <-- where FlowerClient is of type flwr.client.NumPyClient object\n" + "\t)\n" + "\tUsing `start_numpy_client()` is deprecated." + ) + + warn_deprecated_feature(name=mssg) # Calling this function is deprecated. A warning is thrown. - # We first need to convert either the supplied client to `Client.` + # We first need to convert the supplied client to `Client.` wrp_client = client.to_client() diff --git a/src/py/flwr/simulation/ray_transport/utils.py b/src/py/flwr/simulation/ray_transport/utils.py index 41aa8049eaf0..dd9fb6b2aa85 100644 --- a/src/py/flwr/simulation/ray_transport/utils.py +++ b/src/py/flwr/simulation/ray_transport/utils.py @@ -15,6 +15,7 @@ """Utilities for Actors in the Virtual Client Engine.""" import traceback +import warnings from logging import ERROR from flwr.client import Client @@ -26,7 +27,7 @@ TF = None # Display Deprecation warning once -# warnings.filterwarnings("once", category=DeprecationWarning) +warnings.filterwarnings("once", category=DeprecationWarning) def enable_tf_gpu_growth() -> None: @@ -69,15 +70,15 @@ def check_clientfn_returns_client(client: Client) -> Client: the client internally to `Client` by calling `.to_client()`. """ if not isinstance(client, Client): - # mssg = ( - # " Ensure your client is of type `Client`. Please convert it" - # " using the `.to_client()` method before returning it" - # " in the `client_fn` you pass to `start_simulation`." - # " We have applied this conversion on your behalf." - # " Not returning a `Client` might trigger an error in future" - # " versions of Flower." - # ) + mssg = ( + " Ensure your client is of type `flwr.client.Client`. Please convert it" + " using the `.to_client()` method before returning it" + " in the `client_fn` you pass to `start_simulation`." + " We have applied this conversion on your behalf." + " Not returning a `Client` might trigger an error in future" + " versions of Flower." + ) - # warnings.warn(mssg, DeprecationWarning, stacklevel=2) + warnings.warn(mssg, DeprecationWarning, stacklevel=2) client = client.to_client() return client