p2pfl.learning.frameworks.p2pfl_model moduleΒΆ

P2PFL model abstraction.

class p2pfl.learning.frameworks.p2pfl_model.P2PFLModel(model, params=None, num_samples=None, contributors=None, additional_info=None, compression=None)[source]ΒΆ

Bases: ABC

Abstract base class for all P2PFL models.

This class encapsulates the different models across all the possible frameworks. The key concept is the extraction of the model weights in a common format for all the frameworks.

Important

Inherit from WeightBasedModel or TreeBasedModel instead, which handle neural networks and tree ensembles respectively with proper aggregator compatibility.

Parameters:

model (Any) – The model to encapsulate.

Note

The model type is ANY because different model types have different parameter formats.

Parameters:
  • params (Any)

  • num_samples (int | None)

  • contributors (list[str] | None)

  • additional_info (dict[str, Any] | None)

  • compression (dict[str, dict[str, Any]] | None)

add_info(callback, info)[source]ΒΆ

Add additional information to the learner state.

Parameters:
  • callback (str) – The callback to add the information

  • info (Any) – The information for the callback.

Return type:

None

build_copy(**kwargs)[source]ΒΆ

Build a copy of the model.

Parameters:

**kwargs – Parameters of the model initialization.

Return type:

P2PFLModel

Returns:

A copy of the model.

decode_parameters(data)[source]ΒΆ

Decode the parameters of the model.

Parameters:

data (bytes) – The serialized parameters.

Return type:

tuple[Any, dict[str, Any]]

encode_parameters(params=None)[source]ΒΆ

Encode the parameters of the model.

Parameters:

params (Any) – The parameters of the model.

Return type:

bytes

get_contributors()[source]ΒΆ

Get the contributors of the model.

Return type:

list[str]

abstract get_framework()[source]ΒΆ

Retrieve the model framework name.

Return type:

str

Returns:

The name of the model framework.

get_info(callback=None)[source]ΒΆ

Get additional information from the learner state.

Parameters:
  • callback (str | None) – The callback to add the information

  • key – The key of the information.

Return type:

Any

get_model()[source]ΒΆ

Get the model.

Return type:

Any

get_num_samples()[source]ΒΆ

Get the number of samples used to train this model.

Return type:

int

abstract get_parameters()[source]ΒΆ

Get the parameters of the model.

Return type:

Any

Returns:

The parameters of the model.

set_contribution(contributors, num_samples)[source]ΒΆ

Set the contribution of the model.

Parameters:
  • contributors (list[str]) – The contributors of the model.

  • num_samples (int) – The number of samples used to train this model.

Return type:

None

abstract set_parameters(params)[source]ΒΆ

Set the parameters of the model.

Parameters:

params (Any) – The parameters of the model.

Raises:

ModelNotMatchingError – If parameters don’t match the model.

Return type:

None

class p2pfl.learning.frameworks.p2pfl_model.TreeBasedModel(model, params=None, num_samples=None, contributors=None, additional_info=None, compression=None)[source]ΒΆ

Bases: P2PFLModel

Base class for tree ensemble models (XGBoost).

Returns parameters as dict[str, Any] tree structures. Compatible with TreeAggregator.

Parameters:
  • model (Any)

  • params (Any)

  • num_samples (int | None)

  • contributors (list[str] | None)

  • additional_info (dict[str, Any] | None)

  • compression (dict[str, dict[str, Any]] | None)

decode_parameters(data)[source]ΒΆ

Decode the parameters of the model.

Parameters:

data (bytes) – The serialized parameters.

Return type:

tuple[dict[str, Any], dict[str, Any]]

Returns:

Tuple of (tree structure dict, additional info).

abstract get_parameters()[source]ΒΆ

Get the parameters of the model.

Return type:

dict[str, Any]

Returns:

Parsed tree structure as a dictionary (XGBoost JSON format).

class p2pfl.learning.frameworks.p2pfl_model.WeightBasedModel(model, params=None, num_samples=None, contributors=None, additional_info=None, compression=None)[source]ΒΆ

Bases: P2PFLModel

Base class for neural network models (PyTorch, TensorFlow, Flax).

Returns parameters as list[np.ndarray] weight tensors. Compatible with WeightAggregator.

Parameters:
  • model (Any)

  • params (Any)

  • num_samples (int | None)

  • contributors (list[str] | None)

  • additional_info (dict[str, Any] | None)

  • compression (dict[str, dict[str, Any]] | None)

decode_parameters(data)[source]ΒΆ

Decode the parameters of the model.

Parameters:

data (bytes) – The serialized parameters.

Return type:

tuple[list[ndarray], dict[str, Any]]

Returns:

Tuple of (weight arrays, additional info).

abstract get_parameters()[source]ΒΆ

Get the parameters of the model.

Return type:

list[ndarray]

Returns:

List of numpy arrays, one per layer.