Skip to main content

Generic Federated Learning Simulator with PyTorch

Project description


GitHub Actions Build Status PyPI Package latest release PyPI Wheel Supported versions Supported implementations Gitter

FedSim is a comprehensive and flexible Federated Learning Simulator! It aims to provide the researchers with an easy to develope/maintain simulator for Federated Learning. See documentation at here!


pip install fedsim

That’s it! You are all set!

Design Architecture


Minimal example

Fedsim provides powerful cli tools that allow you to focus on designing what is truly important. Simply enter the following command to begin federatively training a model.

fedsim-cli fed-learn

The “MNIST” dataset is partitioned on 500 clients by default, and the FedAvg algorithm is used to train a minimal model with two fully connected layers. A text file is made that descibes the configuration for the experiment and a summary of results when it is finished. Additionally, a tensorboard log file is made to monitor the scores/metrics of the training. The directory that these files are stored is (reconfigurable and is) displayed while the experiment is running.

Hooking scores to cli tools

In case you are interested in a certain metric you can make a query for it in your command. For example, lets assume we would like to test and report: * the accuracy score of the global model on global test dataset both every 21 rounds and every 43 rounds. * the average accuracy score of the local models every 15 rounds. Here’s how we modify the above command:

fedsim-cli fed-learn \
    --global-score Accuracy score_name:acc21 split:test log_freq:21 \
    --global-score Accuracy score_name:acc43 split:test log_freq:43 \
    --local-score Accuracy split:train log_freq:15

Check Fedsim Scores Page for the list of all other scores like Accyracy or define your custom score.

Changing the Data

Data partitioning and retrieval is controlled by a DataManager object. This object could be controlled through -d or –data-manager flag in most cli commands. In the following we modify the arguments of the default DataManager such that CIFAR100 is partitioned over 1000 clients.

fedsim-cli fed-learn \
    --data-manger BasicDataManager dataset:cifar100 num_partitions:1000 \
    --num-clients 1000 \
    --model SimpleCNN2 num_classes:100 \
    --global-score Accuracy split:test log_freq:15

Notice that we also changed the model from default to SimpleCNN2 which by default takes 3 input channels. You can learn about existing data managers at data manager documentation and Custom data managers at this guide to make Custom data managers.

Feed CLI with Customized Components

The cli tool can take a locally defined component by ingesting its path. For example, to automatically include your custom algorithm by the a command of the cli tool, you can place your class in a python file and pass the path of the file to -a or –algorithm option (without .py) followed by colon and name of the algorithm definition (class or method). For instance, if you have algorithm CustomFLAlgorithm stored in a foo/bar/, you can pass –algorithm foo/bar/my_custom_alg:CustomFLAlgorithm.

fedsim-cli fed-learn --algorithm foo/bar/my_custom_alg_file:CustomFLAlgorithm mu:0.01 ...

The same is possible for any other component, for instance for a Custom model:

fedsim-cli fed-learn --model foo/bar/my_model_file:CustomModel num_classes:1000 ...

More about cli commands

For help with cli check fedsim-cli documentation or read the output of the following commands:

fedsim-cli --help
fedsim-cli fed-learn --help
fedsim-cli fed-tune --help

Python API

Fedsim is shipped with some of the most well-known Federated Learning algorithms included. However, you will most likely need to quickly develop and test your custom algorithm, model, data manager, or score class. Fedsim has been designed in such a way that doing all of these things takes almost no time and effort. Let’s start by learning how to import and use Fedsim, and then we’ll go over how to easily modify existing modules and classes to your liking. Check the following basic example:

from logall import TensorboardLogger
from import FedAvg
from fedsim.distributed.data_management import BasicDataManager
from fedsim.models import SimpleCNN2
from fedsim.losses import CrossEntropyLoss
from fedsim.scores import Accuracy

n_clients = 1000

dm = BasicDataManager("./data", "cifar100", n_clients)
sw = TensorboardLogger(path=None)

alg = FedAvg(
    model_def=partial(SimpleCNN2, num_channels=3),
    criterion_def=partial(CrossEntropyLoss, log_freq=100),
    partial(Accuracy, log_freq=50),
    partial(Accuracy, log_freq=40),
report_summary = alg.train(rounds=50)

Side Notes

  • Do not use double underscores (__) in argument names of your customized classes.

0.9.0 (2022-09-21)

  • make all user methods in algorithms static (no self argument).

  • better encapsulation.

  • expand definition of storage for read and write protection.

0.8.3 (2022-09-18)

  • add option for valid split on global data in basic dta manager.

0.8.2 (2022-09-12)

  • changed image paths to links from github in readme

0.8.1 (2022-09-12)

  • fix a minor bug with a link in readme

0.8.0 (2022-09-12)

  • some major revision to documentation

  • some enhancement to FedProx compatibility with v0.7+

0.7.0 (2022-09-10)

  • algorithms got more secure with local storage

  • redefined model architectures

  • fixed bug in default step closure’

  • made random seed more consistent

0.6.2 (2022-08-31)

  • fixed some errors in docstring of central FL algorithms

  • add sample balance param to to identifiers of data manager

0.6.1 (2022-08-17)

  • fixed bug in partition_global_data of BasicDataManager

  • some changes in default values for better log storage and aggregation

0.6.0 (2022-08-16)

  • changed the name of cli directory

  • added cli tests

  • added support for pytorch original lr schedulers

  • improved docs

  • added version option to fedsim-cli

0.5.0 (2022-08-15)

  • completed lr schedulers and generalized them for all levels

  • changed some argument names and default values

0.4.1 (2022-08-12)

  • fixed bugs with mismatched loss_fn argument name in cli commands

  • changed all eval_freq arguemnts to unified log_req

0.4.0 (2022-08-12)

  • changed the structure of scores and losses

  • made it possible to hook multiple local and global scores

0.3.1 (2022-08-09)

  • added advanced learning rate schedulers

  • properly tested r2r lr scheduler

0.3.0 (2022-08-09)

  • added fine-tuning to cli, fed-tune

  • cleaner cli

  • made optimizers and schedulers user definable

  • improved logging

0.2.0 (2022-08-01)

  • cleaned the API reference in docs

  • changed cli name to fedsim-cli

  • improved documentation

  • improved importing

  • changed the way custom objects are passed to cli

0.1.4 (2022-07-23)

  • changed FLAlgorithm to CentralFLAlgorithm for more clearity

  • set default device to cuda if available otherwise to cpu in fed-learn cli

  • fix wrong superclass names in demo

  • fix the confusion with save_dir and save_path in DataManager classes

0.1.3 (2022-07-08)

  • the documentation is redesigned and mostly automated.

  • documentation now is available at

  • added code of coduct from github tempalate

0.1.2 (2022-07-05)

  • changed ownership of repo from fedsim-dev to varnio

0.1.1 (2022-06-22)

  • added fedsim.scores which wraps torch loss functions and sklearn scores

  • moved reporting mechanism of distributed algorithm for supporting auto monitor

  • added AppendixAggregator which is used to hold metric scores and report final results

  • apply a patch for wrong pypi supported python versions

0.1.0 (2022-06-21)

  • First major pre-release.

  • The package is restructured

  • docs is updated and checked to pass through tox steps

0.0.4 (2022-06-14)

  • Fourth release on PyPI.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fedsim-0.9.0.tar.gz (17.1 MB view hashes)

Uploaded source

Built Distribution

fedsim-0.9.0-py3-none-any.whl (89.0 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page