Skip to content

fl_server_core.models.training

Classes:

Name Description
AggregationMethod

Aggregation method choices for a Training.

Training

Training model class.

TrainingState

Training state choices for a Training.

UncertaintyMethod

Uncertainty method choices for a Training.

Functions:

Name Description
post_save_training

Ensure that the correct target_num_updates is set for every new training.

Classes

AggregationMethod

Bases: TextChoices


              flowchart TD
              fl_server_core.models.training.AggregationMethod[AggregationMethod]

              

              click fl_server_core.models.training.AggregationMethod href "" "fl_server_core.models.training.AggregationMethod"
            

Aggregation method choices for a Training.

Attributes:

Name Type Description
FED_AVG
FED_DC
FED_PROX
Source code in fl_server_core/models/training.py
class AggregationMethod(models.TextChoices):
    """
    Aggregation method choices for a Training.
    """
    FED_AVG = "FedAvg", _("FedAvg")
    FED_DC = "FedDC", _("FedDC")
    FED_PROX = "FedProx", _("FedProx")

Attributes

FED_AVG class-attribute instance-attribute
FED_AVG = ('FedAvg', gettext_lazy('FedAvg'))
FED_DC class-attribute instance-attribute
FED_DC = ('FedDC', gettext_lazy('FedDC'))
FED_PROX class-attribute instance-attribute
FED_PROX = ('FedProx', gettext_lazy('FedProx'))

Training

Bases: Model


              flowchart TD
              fl_server_core.models.training.Training[Training]

              

              click fl_server_core.models.training.Training href "" "fl_server_core.models.training.Training"
            

Training model class.

Attributes:

Name Type Description
actor ForeignKey

User who is the actor of the training.

aggregation_method CharField

Aggregation method used in the training.

id UUIDField

Unique identifier for the training.

last_update

Time of the last update.

locked BooleanField

Flag indicating whether the training is locked.

model OneToOneField

Model used in the training.

options JSONField

Options for the training.

participants ManyToManyField

Users who are the participants of the training.

state CharField

State of the training.

target_num_updates IntegerField

Target number of updates for the training.

uncertainty_method CharField

Uncertainty method used in the training.

Source code in fl_server_core/models/training.py
class Training(models.Model):
    """
    Training model class.
    """

    id: UUIDField = UUIDField(primary_key=True, editable=False, default=uuid4)
    """Unique identifier for the training."""
    model: OneToOneField = OneToOneField(Model, on_delete=CASCADE)
    """Model used in the training."""
    actor: ForeignKey = ForeignKey(User, on_delete=CASCADE, related_name="actors")
    """User who is the actor of the training."""
    participants: ManyToManyField = ManyToManyField(User)
    """Users who are the participants of the training."""
    state: CharField = CharField(max_length=1, choices=TrainingState.choices)
    """State of the training."""
    target_num_updates: IntegerField = IntegerField()
    """Target number of updates for the training."""
    last_update = models.DateTimeField(auto_now=True)
    """Time of the last update."""
    uncertainty_method: CharField = CharField(
        max_length=32, choices=UncertaintyMethod.choices, default=UncertaintyMethod.NONE
    )
    """Uncertainty method used in the training."""
    aggregation_method: CharField = CharField(
        max_length=32, choices=AggregationMethod.choices, default=AggregationMethod.FED_AVG
    )
    """Aggregation method used in the training."""
    # HINT: https://docs.djangoproject.com/en/4.2/topics/db/queries/#querying-jsonfield
    options: JSONField = JSONField(default=dict, encoder=DjangoJSONEncoder)
    """Options for the training."""
    locked: BooleanField = BooleanField(default=False)
    """Flag indicating whether the training is locked."""

Attributes

actor class-attribute instance-attribute
actor: ForeignKey = ForeignKey(User, on_delete=CASCADE, related_name='actors')

User who is the actor of the training.

aggregation_method class-attribute instance-attribute
aggregation_method: CharField = CharField(max_length=32, choices=choices, default=FED_AVG)

Aggregation method used in the training.

id class-attribute instance-attribute
id: UUIDField = UUIDField(primary_key=True, editable=False, default=uuid4)

Unique identifier for the training.

last_update class-attribute instance-attribute
last_update = DateTimeField(auto_now=True)

Time of the last update.

locked class-attribute instance-attribute
locked: BooleanField = BooleanField(default=False)

Flag indicating whether the training is locked.

model class-attribute instance-attribute
model: OneToOneField = OneToOneField(Model, on_delete=CASCADE)

Model used in the training.

options class-attribute instance-attribute
options: JSONField = JSONField(default=dict, encoder=DjangoJSONEncoder)

Options for the training.

participants class-attribute instance-attribute
participants: ManyToManyField = ManyToManyField(User)

Users who are the participants of the training.

state class-attribute instance-attribute
state: CharField = CharField(max_length=1, choices=choices)

State of the training.

target_num_updates class-attribute instance-attribute
target_num_updates: IntegerField = IntegerField()

Target number of updates for the training.

uncertainty_method class-attribute instance-attribute
uncertainty_method: CharField = CharField(max_length=32, choices=choices, default=NONE)

Uncertainty method used in the training.

TrainingState

Bases: TextChoices


              flowchart TD
              fl_server_core.models.training.TrainingState[TrainingState]

              

              click fl_server_core.models.training.TrainingState href "" "fl_server_core.models.training.TrainingState"
            

Training state choices for a Training.

Attributes:

Name Type Description
COMPLETED
ERROR
INITIAL
ONGOING
SWAG_ROUND
Source code in fl_server_core/models/training.py
class TrainingState(models.TextChoices):
    """
    Training state choices for a Training.
    """
    INITIAL = "I", _("Initial")
    ONGOING = "O", _("Ongoing")
    COMPLETED = "C", _("Completed")
    ERROR = "E", _("Error")
    SWAG_ROUND = "S", _("SwagRound")

Attributes

COMPLETED class-attribute instance-attribute
COMPLETED = ('C', gettext_lazy('Completed'))
ERROR class-attribute instance-attribute
ERROR = ('E', gettext_lazy('Error'))
INITIAL class-attribute instance-attribute
INITIAL = ('I', gettext_lazy('Initial'))
ONGOING class-attribute instance-attribute
ONGOING = ('O', gettext_lazy('Ongoing'))
SWAG_ROUND class-attribute instance-attribute
SWAG_ROUND = ('S', gettext_lazy('SwagRound'))

UncertaintyMethod

Bases: TextChoices


              flowchart TD
              fl_server_core.models.training.UncertaintyMethod[UncertaintyMethod]

              

              click fl_server_core.models.training.UncertaintyMethod href "" "fl_server_core.models.training.UncertaintyMethod"
            

Uncertainty method choices for a Training.

Attributes:

Name Type Description
ENSEMBLE
MC_DROPOUT
NONE
SWAG
Source code in fl_server_core/models/training.py
class UncertaintyMethod(models.TextChoices):
    """
    Uncertainty method choices for a Training.
    """
    NONE = "NONE", _("None")
    ENSEMBLE = "ENSEMBLE", _("Ensemble")
    MC_DROPOUT = "MC_DROPOUT", _("MC Dropout")
    SWAG = "SWAG", _("SWAG")

Attributes

ENSEMBLE class-attribute instance-attribute
ENSEMBLE = ('ENSEMBLE', gettext_lazy('Ensemble'))
MC_DROPOUT class-attribute instance-attribute
MC_DROPOUT = ('MC_DROPOUT', gettext_lazy('MC Dropout'))
SWAG class-attribute instance-attribute
SWAG = ('SWAG', gettext_lazy('SWAG'))

Functions

post_save_training

post_save_training(sender, instance=None, created=False, *args, **kwargs)

Ensure that the correct target_num_updates is set for every new training.

This method is called after saving a training instance. It is used to set the target_num_updates to the correct value if this training instance is newly created and daisy chaining is enabled.

Parameters:

Name Type Description Default

sender

The model class.

required

instance

Training

The actual instance being saved. Defaults to None.

None

created

bool

A boolean; True if a new record was created. Defaults to False.

False

*args

Additional arguments.

()

**kwargs

Arbitrary keyword arguments.

{}
Source code in fl_server_core/models/training.py
@receiver(post_save, sender=Training)
def post_save_training(sender, instance=None, created=False, *args, **kwargs):
    """
    Ensure that the correct `target_num_updates` is set for every new training.

    This method is called after saving a training instance.
    It is used to set the `target_num_updates` to the correct value if this training instance is newly created
    and daisy chaining is enabled.

    Args:
        sender: The model class.
        instance (Training, optional): The actual instance being saved. Defaults to None.
        created (bool, optional): A boolean; True if a new record was created. Defaults to False.
        *args: Additional arguments.
        **kwargs: Arbitrary keyword arguments.
    """
    if not created:
        return
    daisy_chain_period = instance.options.get("daisy_chain_period", 0)
    if daisy_chain_period <= 0:
        return
    instance.target_num_updates = instance.target_num_updates * daisy_chain_period
    instance.save()