APPFL Client Agent#
Functionalities#
APPFL client agent acts on behalf of the FL client to fulfill various tasks related to local training, including
Load general configurations shared among all clients from the server.
Load model parameters received from the server.
Perform the local training job according to the configuration.
Prepare local model parameters and other optional metadata for communication with the server.
Other tasks that the client agent needs to perform for certain FL algorithms (e.g. get the size of the local dataset).
Specifically, the current client agent has the following functionalities.
Note
User can also define their functionalities by either inheriting the ClientAgent
class or directly adding new methods to the current client agent. Additionally, if you think your added functionalities are useful for other users, please consider contributing to the APPFL package by submitting a pull request.
class ClientAgent:
def __init__(
self,
client_agent_config: ClientAgentConfig = ClientAgentConfig()
) -> None:
"""
Initialize the client agent with configurations.
"""
def load_config(self, config: DictConfig) -> None:
"""
Load additional configurations provided by the server.
"""
def get_id(self) -> str:
"""
Return a unique client id for server to distinguish clients.
"""
def get_sample_size(self) -> int:
"""
Return the size of the local dataset.
"""
def train(self) -> None:
"""
Train the model locally.
"""
def get_parameters(self) -> Union[Dict, OrderedDict, bytes, Tuple[Union[Dict, OrderedDict, bytes], Dict]]:
"""
Return parameters for communication.
:return parameters: The parameters to be sent to the server,
can be of type Dict, OrderedDict, bytes (if compressed), or
Tuple[Union[Dict, OrderedDict, bytes], Dict] with optional
metadata in Dict type.
"""
def load_parameters(self, params) -> None:
"""
Load parameters from the server.
"""
Configurations#
As shown above, to create a client agent, you need to provide the configurations for the client agent. The configurations for the client agent are defined in the appfl.config.ClientAgentConfig
class, which can be loaded from a YAML file. The following shows an example configuration YAML file for the client agent,
Note
It should be noted that the configurations in client YAML file only contain the configurations that are specific for one client. The general configurations shared among all clients are sent from the server.
train_configs:
# Device
device: "cpu"
# Logging and outputs
logging_id: "Client1"
logging_output_dirname: "./output"
logging_output_filename: "result"
# Local dataset
data_configs:
dataset_path: "./dataset/mnist_dataset.py"
dataset_name: "get_mnist"
dataset_kwargs:
num_clients: 2
client_id: 0
partition_strategy: "class_noniid"
visualization: True
output_dirname: "./output"
output_filename: "visualization.pdf"
comm_configs:
grpc_configs:
server_uri: localhost:50051
max_message_size: 1048576
use_ssl: False
The above configuration file contains serveral client-specific cnofigurations, such as the device to use and the way to load the private local dataset:
data_configs
: This is the most important component in the client configuration file. It contains the path in client’s local machine to the file which defines how to load the local dataset (dataset_path
), the name of the function in the file to load the dataset (dataset_name
), and any keyword arguments if needed (dataset_kwargs
).train_configs
: This contains the device to use in training and some logging configurations.comm_configs
: The client may also need to specify some comminucation settings in order to connect to the server. For example, if the experiment uses gRPC as the communication method, then the client needs to specify theserver_uri
,max_message_size
, anduse_ssl
to establish the connection to the server.