fluke

The fluke module is the entry module of the fluke framework. Here are defined generic classes used by the other modules.

Submodules

algorithms

This module contains (as submodules) the implementation of several the federated learning algorithms.

data

This module contains the data utilities for fluke.

comm

This module contains the classes for the communication between the clients and the server.

client

The module fluke.client provides the base classes for the clients in fluke.

evaluation

This module contains the definition of the evaluation classes used to perform the evaluation of the model client-side and server-side.

nets

This module contains the definition of several neural networks used in state-of-the-art federated learning papers.

server

The module fluke.server provides the base classes for the servers in fluke.

utils

This module contains utility functions and classes used in fluke.

Classes included in fluke

DDict

A dictionary that can be accessed with dot notation recursively.

Singleton

This metaclass is used to create singleton classes.

GlobalSettings

Global settings for fluke.

ObserverSubject

Subject class for the observer pattern.

Classes

class fluke.DDict

class fluke.DDict(*args: dict, **kwargs: dict[str, Any])[source]

Bases: dict

A dictionary that can be accessed with dot notation recursively.

Example

1d = DDict(a=1, b=2, c={'d': 3, 'e': 4})
2print(d.a)  # 1
3print(d.b)  # 2
4print(d.c.d)  # 3
5print(d.c.e)  # 4
exclude(*keys: str)[source]

Create a new DDict excluding the specified keys.

Parameters:

*keys – The keys to be excluded.

Returns:

The new DDict.

Return type:

DDict

Example

1d = DDict(a=1, b=2, c=3)
2e = d.exclude('b', 'c')
3print(e) # {'a': 1}
update(*args: dict, **kwargs: dict[str, Any])[source]

Update the DDict with the specified key-value pairs.

Parameters:
  • *args (dict) – Dictionary with the key-value pairs.

  • **kwargs – The key-value pairs.

Example

1d = DDict(a=1)
2print(d) # {'a': 1}
3d.update(b=2, c=3)
4print(d) # {'a': 1, 'b': 2, 'c': 3}

metaclass fluke.Singleton

class fluke.Singleton[source]

Bases: type

This metaclass is used to create singleton classes. A singleton class is a class that can have only one instance. If the instance does not exist, it is created; otherwise, the existing instance is returned.

Example

1class MyClass(metaclass=Singleton):
2    pass
3a = MyClass()
4b = MyClass()
5print(a is b)  # True

class fluke.GlobalSettings

class fluke.GlobalSettings(*args, **kwargs: dict[str, Any])[source]

Bases: object

Global settings for fluke. This class is a singleton that holds the global settings for fluke. The settings include:

  • The device ("cpu", "cuda[:N]", "auto", "mps");

  • The seed for reproducibility;

  • The evaluation configuration;

  • The progress bars for the federated learning process, clients and the server;

  • The live renderer, which is used to render the progress bars.

auto_device() device[source]

Set device to cuda or mps if available, otherwise cpu.

Returns:

The device.

Return type:

torch.device

get_device() device[source]

Get the current device.

Returns:

The device.

Return type:

torch.device

get_eval_cfg() DDict[source]

Get the evaluation configuration.

Returns:

The evaluation configuration.

Return type:

DDict

get_evaluator() Evaluator[source]

Get the evaluator.

Returns:

The evaluator.

Return type:

Evaluator

get_live_renderer() Live[source]

Get the live renderer. The live renderer is used to render the progress bars.

Returns:

The live renderer.

Return type:

Live

get_progress_bar(progress_type: str) Progress[source]

Get the progress bar. The possible progress bar types are:

  • FL: The progress bar for the federated learning process.

  • clients: The progress bar for the clients.

  • server: The progress bar for the server.

Parameters:

progress_type (str) – The type of progress bar.

Returns:

The progress bar.

Return type:

Progress

Raises:

ValueError – If the progress bar type is invalid.

get_seed() int[source]

Get the seed.

Returns:

The seed.

Return type:

int

set_device(device: str) device[source]

Set the device. The device can be cpu, auto, mps, cuda or cuda:N, where N is the GPU index.

Parameters:

device (str) – The device as string.

Returns:

The selected device as torch.device.

Return type:

torch.device

set_eval_cfg(cfg: DDict) None[source]

Set the evaluation configuration.

Parameters:

cfg (DDict) – The evaluation configuration.

set_evaluator(evaluator: Evaluator) None[source]

Set the evaluator.

Parameters:

evaluator (Evaluator) – The evaluator.

set_seed(seed: int) None[source]

Set seed for reproducibility. The seed is used to set the random seed for the following libraries: torch, torch.cuda, numpy, random.

Parameters:

seed (int) – The seed.

interface fluke.ObserverSubject

class fluke.ObserverSubject[source]

Bases: object

Subject class for the observer pattern. The subject is the class that is observed and thus it holds the observers.

Example

 1class MySubject(ObserverSubject):
 2    def __init__(self):
 3        super().__init__()
 4        self._data = 0
 5
 6    @property
 7    def data(self):
 8        return self._data
 9
10    @data.setter
11    def data(self, value):
12        self._data = value
13        self.notify()
14
15class MyObserver:
16    def __init__(self, subject):
17        subject.attach(self)
18
19    def update(self):
20        print("Data changed.")
21
22subject = MySubject()
23observer = MyObserver(subject)
24subject.data = 1  # "Data changed."
attach(observer: Any | Iterable[Any])[source]

Attach one or more observers.

Parameters:

observer (Union[Any, Iterable[Any]]) – The observer or a list of observers.

detach(observer: Any)[source]

Detach an observer.

Parameters:

observer (Any) – The observer to be detached.