Skip to content

fl_server_ai.notification.training.round_start

Classes:

Name Description
TrainingRoundStartNotification

Notification for the start of a training round.

Functions:

Name Description
training_notification_callback_failure

Callback that logs a failed training notification.

training_notification_callback_success

Callback that logs a successful training notification.

Attributes

Classes

TrainingRoundStartNotification dataclass

Bases: TrainingNotification['TrainingRoundStartNotification.Body']


              flowchart TD
              fl_server_ai.notification.training.round_start.TrainingRoundStartNotification[TrainingRoundStartNotification]
              fl_server_ai.notification.training.training.TrainingNotification[TrainingNotification]
              fl_server_ai.notification.notification.Notification[Notification]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.training.training.TrainingNotification --> fl_server_ai.notification.training.round_start.TrainingRoundStartNotification
                                fl_server_ai.notification.notification.Notification --> fl_server_ai.notification.training.training.TrainingNotification
                                fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.notification.Notification
                




              click fl_server_ai.notification.training.round_start.TrainingRoundStartNotification href "" "fl_server_ai.notification.training.round_start.TrainingRoundStartNotification"
              click fl_server_ai.notification.training.training.TrainingNotification href "" "fl_server_ai.notification.training.training.TrainingNotification"
              click fl_server_ai.notification.notification.Notification href "" "fl_server_ai.notification.notification.Notification"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Notification for the start of a training round.

Classes:

Name Description
Body

Inner class representing the body of the notification.

Methods:

Name Description
from_training

Create a TrainingRoundStartNotification instance from a training object.

Attributes:

Name Type Description
callback_error Signature | None
callback_success Signature | None
type NotificationType

The type of the notification.

Source code in fl_server_ai/notification/training/round_start.py
class TrainingRoundStartNotification(TrainingNotification["TrainingRoundStartNotification.Body"]):
    """
    Notification for the start of a training round.
    """

    type: NotificationType = NotificationType.UPDATE_ROUND_START
    """The type of the notification."""

    @property
    def callback_success(self) -> Optional[Signature]:
        return training_notification_callback_success.s(training_uuid=self.training_uuid)

    @property
    def callback_error(self) -> Optional[Signature]:
        return training_notification_callback_failure.s(training_uuid=self.training_uuid)

    @dataclass
    class Body(Serializable):
        """
        Inner class representing the body of the notification.
        """
        round: int
        """The round number."""
        global_model_uuid: UUID
        """The UUID of the global model."""

    @classmethod
    def from_training(cls, training: TrainingDB):
        """
        Create a `TrainingRoundStartNotification` instance from a training object.

        Args:
            training (TrainingDB): The training object to create the notification from.

        Returns:
            TrainingRoundStartNotification: The created notification.
        """
        return cls(
            receivers=training.participants.all(),
            body=cls.Body(
                round=training.model.round,
                global_model_uuid=training.model.id
            ),
            training_uuid=training.id
        )

Attributes

callback_error property
callback_error: Signature | None
callback_success property
callback_success: Signature | None
type class-attribute instance-attribute

The type of the notification.

Classes

Body dataclass

Bases: Serializable


              flowchart TD
              fl_server_ai.notification.training.round_start.TrainingRoundStartNotification.Body[Body]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.training.round_start.TrainingRoundStartNotification.Body
                


              click fl_server_ai.notification.training.round_start.TrainingRoundStartNotification.Body href "" "fl_server_ai.notification.training.round_start.TrainingRoundStartNotification.Body"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Inner class representing the body of the notification.

Methods:

Name Description
__init__

Attributes:

Name Type Description
global_model_uuid UUID

The UUID of the global model.

round int

The round number.

Source code in fl_server_ai/notification/training/round_start.py
@dataclass
class Body(Serializable):
    """
    Inner class representing the body of the notification.
    """
    round: int
    """The round number."""
    global_model_uuid: UUID
    """The UUID of the global model."""
Attributes
global_model_uuid instance-attribute
global_model_uuid: UUID

The UUID of the global model.

round instance-attribute
round: int

The round number.

Functions
__init__
__init__(round: int, global_model_uuid: UUID) -> None

Functions

from_training classmethod
from_training(training: Training)

Create a TrainingRoundStartNotification instance from a training object.

Parameters:

Name Type Description Default
training
Training

The training object to create the notification from.

required

Returns:

Name Type Description
TrainingRoundStartNotification

The created notification.

Source code in fl_server_ai/notification/training/round_start.py
@classmethod
def from_training(cls, training: TrainingDB):
    """
    Create a `TrainingRoundStartNotification` instance from a training object.

    Args:
        training (TrainingDB): The training object to create the notification from.

    Returns:
        TrainingRoundStartNotification: The created notification.
    """
    return cls(
        receivers=training.participants.all(),
        body=cls.Body(
            round=training.model.round,
            global_model_uuid=training.model.id
        ),
        training_uuid=training.id
    )

Functions

training_notification_callback_failure

training_notification_callback_failure(exception: NotificationException, training_uuid: UUID)

Callback that logs a failed training notification.

Also sets the training to an error state if the exception is not a client rejection.

Parameters:

Name Type Description Default

exception

NotificationException

The exception that occurred.

required

training_uuid

UUID

The UUID of the training.

required
Source code in fl_server_ai/notification/training/round_start.py
@app.task(bind=False, ignore_result=True)
def training_notification_callback_failure(exception: NotificationException, training_uuid: UUID):
    """
    Callback that logs a failed training notification.

    Also sets the training to an error state if the exception is not a client rejection.

    Args:
        exception (NotificationException): The exception that occurred.
        training_uuid (UUID): The UUID of the training.
    """
    logger = get_task_logger("fl.celery")
    if isinstance(exception, ClientNotificationRejectionException):
        # client sent error response, remove
        receiver: NotificationReceiver = exception.notification_return_object
        logger.warn(
            f"Training {training_uuid}: User {receiver.id} sent error response: {exception.status_code}."
            "User will be removed from training!"
        )
        user = receiver if isinstance(receiver, UserDB) else UserDB.objects.get(id=receiver.id)
        TrainingDB.objects.get(id=training_uuid).participants.remove(user)
    else:
        # set training to error state
        e = exception.inner_exception if exception.inner_exception else exception
        logger.error(f"Training {training_uuid}: Exception occurred during sending: {e}")
        logger.error(f"Training {training_uuid} is in error state!")
        training = TrainingDB.objects.get(id=training_uuid)
        training.state = TrainingState.ERROR
        training.save()

training_notification_callback_success

training_notification_callback_success(receiver: NotificationReceiver, training_uuid: UUID)

Callback that logs a successful training notification.

Parameters:

Name Type Description Default

receiver

NotificationReceiver

The receiver of the notification.

required

training_uuid

UUID

The UUID of the training.

required
Source code in fl_server_ai/notification/training/round_start.py
@app.task(bind=False, ignore_result=True)
def training_notification_callback_success(receiver: NotificationReceiver, training_uuid: UUID):
    """
    Callback that logs a successful training notification.

    Args:
        receiver (NotificationReceiver): The receiver of the notification.
        training_uuid (UUID): The UUID of the training.
    """
    logger = get_task_logger("fl.celery")
    logger.debug(f"Training {training_uuid}: User {receiver.id} accepted notification")