Fl With Flower

hf-bloghacktoberfestnotebooks

Federated Learning using Hugging Face and Flower

This tutorial will show how to leverage Hugging Face to federate the training of language models over multiple clients using Flower. More specifically, we will fine-tune a pre-trained Transformer model (alBERT) for sequence classification over a dataset of IMDB ratings. The end goal is to detect if a movie rating is positive or negative.

Dependencies

For this tutorial we will need datasets, flwr['simulation'](here we use the extra 'simulation' dependencies from Flower as we will simulated the federated setting inside Google Colab), torch, and transformers.

[ ]

We can now import the relevant modules.

[ ]

Next we will set some global variables and disable some of the logging to clear out our output.

[ ]

Standard Hugging Face workflow

Handling the data

To fetch the IMDB dataset, we will use Hugging Face's datasets library. We then need to tokenize the data and create PyTorch dataloaders, this is all done in the load_data function:

[ ]

Training and testing the model

Once we have a way of creating our trainloader and testloader, we can take care of the training and testing. This is very similar to any PyTorch training or testing loop:

[ ]

Creating the model itself

To create the model itself, we will just load the pre-trained alBERT model using Hugging Face’s AutoModelForSequenceClassification :

[ ]

Federating the example

The idea behind Federated Learning is to train a model between multiple clients and a server without having to share any data. This is done by letting each client train the model locally on its data and send its parameters back to the server, which then aggregates all the clients’ parameters together using a predefined strategy. This process is made very simple by using the Flower framework. If you want a more complete overview, be sure to check out this guide: What is Federated Learning?

Creating the IMDBClient

To federate our example to multiple clients, we first need to write our Flower client class (inheriting from flwr.client.NumPyClient). This is very easy, as our model is a standard PyTorch model:

[ ]

The get_parameters function lets the server get the client's parameters. Inversely, the set_parameters function allows the server to send its parameters to the client. Finally, the fit function trains the model locally for the client, and the evaluate function tests the model locally and returns the relevant metrics.

Generating the clients

In order to simulate the federated setting we need to provide a way to instantiate clients for our simulation. Here, it is very simple as every client will hold the same piece of data (this is not realistic, it is just used here for simplicity sakes).

[ ]

Starting the simulation

We now have all the elements to start our simulation. The weighted_average function is there to provide a way to aggregate the metrics distributed amongst the clients (basically to display a nice average accuracy at the end of the training). We then define our strategy (here FedAvg, which will aggregate the clients weights by doing an average).

Finally, start_simulation is used to start the training.

[ ]

Note that this is a very basic example, and a lot can be added or modified, it was just to showcase how simply we could federate a Hugging Face workflow using Flower. The number of clients and the data samples are intentionally very small in order to quickly run inside Colab, but keep in mind that everything can be tweaked and extended.