fluke.algorithms.decentralized

Submodules

client

This module implements clients for decentralized federated learning (DFL) algorithms.

Classes included in fluke.algorithms.decentralized

Topology

This class represents the topology of the decentralized FL algorithm.

DecentralizedFL

Decentralized Federated Learning (DFL) is a class of federated learning algorithms where clients communicate directly with each other without a central server.

GossipDFL

Gossip Decentralized Federated Learning (GossipDFL) is a specific implementation of a decentralized FL algorithm where clients communicate with their neighbors in a gossip-like manner.

class fluke.algorithms.decentralized.Topology

class fluke.algorithms.decentralized.Topology(graph: Graph)[source]

This class represents the topology of the decentralized FL algorithm. It is essentially a wrapper around a networkx graph, with some additional methods to facilitate the creation and manipulation of the graph. The nodes of the graph represent the clients, and the edges represent the communication links between the clients. The graph can be directed or undirected, depending on the use case.

static fully_connected(num_nodes: int) Topology[source]

Create a fully connected (complete) graph with num_nodes nodes.

Parameters:

num_nodes (int) – The number of nodes in the graph.

Returns:

A fully connected graph.

Return type:

Topology

static ring(num_nodes: int) Topology[source]

Create a ring topology graph with num_nodes nodes.

Parameters:

num_nodes (int) – The number of nodes in the graph.

Returns:

A ring topology graph.

Return type:

Topology

static random(num_nodes: int, p: float = 0.5) Topology[source]

Create a random d-regular graph with num_nodes nodes and edge probability p.

Parameters:
  • num_nodes (int) – The number of nodes in the graph.

  • p (float) – The probability of creating an edge between any two nodes.

Returns:

A random d-regular graph.

Return type:

Topology

property num_nodes: int

Get the number of nodes in the topology graph.

Returns:

The number of nodes.

Return type:

int

add_node(node: int) None[source]

Add a node to the topology graph.

Parameters:

node (int) – The node to add.

add_edge(node1: int, node2: int, weight: float = None) None[source]

Add an edge between two nodes in the topology graph.

Parameters:
  • node1 (int) – The first node.

  • node2 (int) – The second node.

  • weight (float, optional) – The weight of the edge. Defaults to None.

has_node(node: int) bool[source]

Check if a node exists in the topology graph.

Parameters:

node (int) – The node to check.

Returns:

True if the node exists, False otherwise.

Return type:

bool

has_edge(node1: int, node2: int) bool[source]

Check if an edge exists between two nodes in the topology graph.

Parameters:
  • node1 (int) – The first node.

  • node2 (int) – The second node.

Returns:

True if the edge exists, False otherwise.

Return type:

bool

nodes() list[int][source]

Get a list of all nodes in the topology graph.

Returns:

A list of all nodes.

Return type:

list[int]

edges() list[tuple[int, int]][source]

Get a list of all edges in the topology graph.

Returns:

A list of all edges.

Return type:

list[tuple[int, int]]

draw(**kwargs) None[source]

Draw the topology graph using networkx’s draw function.

Parameters:

**kwargs – Additional keyword arguments passed to the networkx draw function.

abstract class fluke.algorithms.decentralized.DecentralizedFL

class fluke.algorithms.decentralized.DecentralizedFL(n_clients: int, data_splitter: DataSplitter, hyper_params: DDict | dict[str, Any], topology: Topology = None, clients: list[AbstractDFLClient] | None = None, test_data: FastDataLoader | DataLoader | None = None, **kwargs)[source]

Decentralized Federated Learning (DFL) is a class of federated learning algorithms where clients communicate directly with each other without a central server. This class represents the base implementation of a decentralized FL algorithm, which can be extended to implement specific algorithms like GossipFL, etc.

Parameters:
  • n_clients (int) – The number of clients in the federation.

  • data_splitter (DataSplitter) – The data splitter to use for splitting the dataset among the clients.

  • hyper_params (DDict | dict[str, Any]) – The hyperparameters of the algorithm.

  • topology (Topology, optional) – The topology of the communication graph. If not provided, a fully connected topology will be used by default.

  • clients (list[AbstractDFLClient], optional) – A list of client instances to use in the federation. If not provided, clients will be initialized based on the provided hyperparameters and topology.

  • test_data (FastDataLoader | DataLoader, optional) – The test dataset to be used for evaluation. If not provided, the test dataset will be generated by the data splitter along with the training datasets for the clients.

  • **kwargs – Additional keyword arguments passed to the parent class.

property id: str

Get the unique identifier of this instance of the algorithm.

Returns:

Unique identifier of the instance of the algorithm.

Return type:

str

get_client_class() type[AbstractDFLClient][source]

Get the class of the client to be used in the decentralized FL algorithm.

Returns:

The class of the client.

Return type:

type[AbstractDFLClient]

set_callbacks(callbacks: callable | Collection[Callable]) None[source]

Set the callbacks for the server the clients and the channel.

The callbacks are expected to be instances of the fluke.server.ServerObserver, fluke.client.ClientObserver or fluke.comm.ChannelObserver classes. Each callback will be attached to the corresponding entity.

Parameters:

callbacks (Union[callable, Collection[callable]]) – Callbacks to attach to the algorithm.

save(path: str, round: int | None = None, *args, **kwargs) str[source]

Save the algorithm state into files in the specified directory.

Note

To avoid overwriting previous saved states, the folder name will be suffixed with the unique (randomly generated) identifier of the algorithm.

Parameters:
  • path (str) – Path to the folder where the algorithm state will be saved.

  • round (int, optional) – Round number. Defaults to None.

Returns:

Path to the folder where the algorithm state was saved.

Return type:

str

load(path: str, round: int | None = None) None[source]

Load the algorithm state from the specified folder

Parameters:
  • path (str) – Path to the folder where the algorithm state is saved.

  • round (int, optional) – Round number. Defaults to None.

abstract class fluke.algorithms.decentralized.GossipDFL

class fluke.algorithms.decentralized.GossipDFL(n_clients: int, data_splitter: DataSplitter, hyper_params: DDict | dict[str, Any], topology: Topology = None, clients: list[AbstractDFLClient] | None = None, test_data: FastDataLoader | DataLoader | None = None, **kwargs)[source]

Gossip Decentralized Federated Learning (GossipDFL) is a specific implementation of a decentralized FL algorithm where clients communicate with their neighbors in a gossip-like manner. In each round, active clients send their model to a randomly chosen neighbor, and then perform local updates based on the received models.