1*14675a02SAndroid Build Coastguard Worker# Cross-Device Federated Computations Demo 2*14675a02SAndroid Build Coastguard Worker 3*14675a02SAndroid Build Coastguard WorkerThis directory contains an example 4*14675a02SAndroid Build Coastguard Worker[Federated Program platform](https://github.com/tensorflow/federated/blob/main/tensorflow_federated/python/program/README.md#platform-specific-components) 5*14675a02SAndroid Build Coastguard Workerimplementation that's compatible with the Federated Compute client. 6*14675a02SAndroid Build Coastguard Worker 7*14675a02SAndroid Build Coastguard WorkerThe code in this directory prioritizes understandability over production 8*14675a02SAndroid Build Coastguard Workerscalability because many of the frameworks used to create robust servers are 9*14675a02SAndroid Build Coastguard Workerdependent on the intended deployment environment. Comments throughout the code 10*14675a02SAndroid Build Coastguard Workerand documentation call out where changes should be made for a production 11*14675a02SAndroid Build Coastguard Workerimplementation. Unless otherwise noted, the libraries in other directories are 12*14675a02SAndroid Build Coastguard Workerproduction quality. 13*14675a02SAndroid Build Coastguard Worker 14*14675a02SAndroid Build Coastguard WorkerSee 15*14675a02SAndroid Build Coastguard Worker[Towards Federated Learning at Scale: System Design](https://arxiv.org/abs/1902.01046) 16*14675a02SAndroid Build Coastguard Worker(TFLaS) for additional information on scaling Federated Learning. 17*14675a02SAndroid Build Coastguard Worker 18*14675a02SAndroid Build Coastguard Worker## Example Usage 19*14675a02SAndroid Build Coastguard Worker 20*14675a02SAndroid Build Coastguard Worker> See `federated_program_test.py` for a working example of configuring and 21*14675a02SAndroid Build Coastguard Worker> running a Federated Program using this package. 22*14675a02SAndroid Build Coastguard Worker 23*14675a02SAndroid Build Coastguard WorkerThe following example program is based on the example in the 24*14675a02SAndroid Build Coastguard Worker[TFF documentation](https://github.com/tensorflow/federated/blob/main/tensorflow_federated/python/program/README.md#program): 25*14675a02SAndroid Build Coastguard Worker 26*14675a02SAndroid Build Coastguard Worker```python 27*14675a02SAndroid Build Coastguard Workerfrom fcp import demo 28*14675a02SAndroid Build Coastguard Workerfrom fcp.protos import plan_pb2 29*14675a02SAndroid Build Coastguard Worker 30*14675a02SAndroid Build Coastguard Worker# Parameters set by the customer. 31*14675a02SAndroid Build Coastguard Worker_OUTPUT_DIR = flags.DEFINE_string('output_dir', None, 'The output path.') 32*14675a02SAndroid Build Coastguard Worker_POPULATION_NAME = flags.DEFINE_string( 33*14675a02SAndroid Build Coastguard Worker 'population_name', None, 'The identifier for the client population.') 34*14675a02SAndroid Build Coastguard Worker_COLLECTION_URI = flags.DEFINE_string( 35*14675a02SAndroid Build Coastguard Worker 'collection_uri', None, 36*14675a02SAndroid Build Coastguard Worker 'A URI identifying the example collection to read from.') 37*14675a02SAndroid Build Coastguard Worker 38*14675a02SAndroid Build Coastguard Worker 39*14675a02SAndroid Build Coastguard Workerdef main(): 40*14675a02SAndroid Build Coastguard Worker # Parameters set by the program. 41*14675a02SAndroid Build Coastguard Worker total_rounds = 10 42*14675a02SAndroid Build Coastguard Worker num_clients = 3 43*14675a02SAndroid Build Coastguard Worker 44*14675a02SAndroid Build Coastguard Worker # Configure the platform-specific components. 45*14675a02SAndroid Build Coastguard Worker with demo.FederatedContext( 46*14675a02SAndroid Build Coastguard Worker _POPULATION_NAME.value, 47*14675a02SAndroid Build Coastguard Worker base_context=tff.framework.get_context_stack().current) as context: 48*14675a02SAndroid Build Coastguard Worker data_source = demo.FederatedDataSource( 49*14675a02SAndroid Build Coastguard Worker _POPULATION_NAME.value, 50*14675a02SAndroid Build Coastguard Worker plan_pb2.ExampleSelector(collection_uri=_COLLECTION_URI.value)) 51*14675a02SAndroid Build Coastguard Worker 52*14675a02SAndroid Build Coastguard Worker # Configure the platform-agnostic components. 53*14675a02SAndroid Build Coastguard Worker summary_dir = os.path.join(_OUTPUT_DIR.value, 'summary') 54*14675a02SAndroid Build Coastguard Worker output_managers = [ 55*14675a02SAndroid Build Coastguard Worker tff.program.LoggingReleaseManager(), 56*14675a02SAndroid Build Coastguard Worker tensorboard_manager = tff.program.TensorBoardReleaseManager(summary_dir), 57*14675a02SAndroid Build Coastguard Worker ] 58*14675a02SAndroid Build Coastguard Worker program_state_dir = os.path.join(..., 'program_state') 59*14675a02SAndroid Build Coastguard Worker program_state_manager = tff.program.FileProgramStateManager( 60*14675a02SAndroid Build Coastguard Worker program_state_dir) 61*14675a02SAndroid Build Coastguard Worker 62*14675a02SAndroid Build Coastguard Worker # Define the computations. 63*14675a02SAndroid Build Coastguard Worker initialize = ... 64*14675a02SAndroid Build Coastguard Worker train = ... 65*14675a02SAndroid Build Coastguard Worker 66*14675a02SAndroid Build Coastguard Worker # Execute the computations using program logic. 67*14675a02SAndroid Build Coastguard Worker tff.framework.set_default_context(context) 68*14675a02SAndroid Build Coastguard Worker train_federated_model( 69*14675a02SAndroid Build Coastguard Worker initialize=initialize, 70*14675a02SAndroid Build Coastguard Worker train=train, 71*14675a02SAndroid Build Coastguard Worker data_source=data_source, 72*14675a02SAndroid Build Coastguard Worker total_rounds=total_rounds, 73*14675a02SAndroid Build Coastguard Worker num_clients=num_clients, 74*14675a02SAndroid Build Coastguard Worker output_managers=output_managers, 75*14675a02SAndroid Build Coastguard Worker program_state_manager=program_state_manager) 76*14675a02SAndroid Build Coastguard Worker``` 77*14675a02SAndroid Build Coastguard Worker 78*14675a02SAndroid Build Coastguard Worker## Code Structure 79*14675a02SAndroid Build Coastguard Worker 80*14675a02SAndroid Build Coastguard Worker```mermaid 81*14675a02SAndroid Build Coastguard Workerflowchart 82*14675a02SAndroid Build Coastguard Worker client(Client) 83*14675a02SAndroid Build Coastguard Worker 84*14675a02SAndroid Build Coastguard Worker subgraph FP[Federated Program Process] 85*14675a02SAndroid Build Coastguard Worker federated_program(Federated Program) 86*14675a02SAndroid Build Coastguard Worker style federated_program color:#333,fill:#bbb,stroke:#666,stroke-width:3px; 87*14675a02SAndroid Build Coastguard Worker 88*14675a02SAndroid Build Coastguard Worker subgraph Server[In-Process Server] 89*14675a02SAndroid Build Coastguard Worker server(server.py) 90*14675a02SAndroid Build Coastguard Worker http_actions(http_actions.py) 91*14675a02SAndroid Build Coastguard Worker plan_utils(plan_utils.py) 92*14675a02SAndroid Build Coastguard Worker 93*14675a02SAndroid Build Coastguard Worker subgraph Handlers[HTTP Handlers] 94*14675a02SAndroid Build Coastguard Worker aggregations(aggregations.py) 95*14675a02SAndroid Build Coastguard Worker eligibility_eval_tasks(eligibility_eval_tasks.py) 96*14675a02SAndroid Build Coastguard Worker media(media.py) 97*14675a02SAndroid Build Coastguard Worker task_assignments(task_assignments.py) 98*14675a02SAndroid Build Coastguard Worker end 99*14675a02SAndroid Build Coastguard Worker end 100*14675a02SAndroid Build Coastguard Worker 101*14675a02SAndroid Build Coastguard Worker subgraph FP_Platform[Federated Program Platform] 102*14675a02SAndroid Build Coastguard Worker federated_context(federated_context.py) 103*14675a02SAndroid Build Coastguard Worker federated_computation(federated_computation.py) 104*14675a02SAndroid Build Coastguard Worker federated_data_source(federated_data_source.py) 105*14675a02SAndroid Build Coastguard Worker checkpoint_tensor_reference(checkpoint_tensor_reference.py) 106*14675a02SAndroid Build Coastguard Worker end 107*14675a02SAndroid Build Coastguard Worker end 108*14675a02SAndroid Build Coastguard Worker 109*14675a02SAndroid Build Coastguard Worker client & server --> Handlers 110*14675a02SAndroid Build Coastguard Worker server --> http_actions & plan_utils 111*14675a02SAndroid Build Coastguard Worker Handlers --> http_actions 112*14675a02SAndroid Build Coastguard Worker federated_program --> federated_context & federated_computation & federated_data_source 113*14675a02SAndroid Build Coastguard Worker federated_context --> checkpoint_tensor_reference & server 114*14675a02SAndroid Build Coastguard Worker``` 115*14675a02SAndroid Build Coastguard Worker 116*14675a02SAndroid Build Coastguard Worker### Client 117*14675a02SAndroid Build Coastguard Worker 118*14675a02SAndroid Build Coastguard WorkerThe [Federated Computations Client](../client) 119*14675a02SAndroid Build Coastguard Workerlibrary is used by applications running on end-user devices to run 120*14675a02SAndroid Build Coastguard Workerserver-defined computations over on-device data and report back results (such as 121*14675a02SAndroid Build Coastguard Workerupdated model weights) to be aggregated by the server. 122*14675a02SAndroid Build Coastguard Worker 123*14675a02SAndroid Build Coastguard Worker> See `federated_program_test.py` for command-line flags that should be used 124*14675a02SAndroid Build Coastguard Worker> when running `//fcp/client:client_runner_main`. 125*14675a02SAndroid Build Coastguard Worker 126*14675a02SAndroid Build Coastguard Worker> ⚠️ The client requires TLS when connecting to any host other than `localhost`. 127*14675a02SAndroid Build Coastguard Worker> The server's public and private keys will need to be provided to the 128*14675a02SAndroid Build Coastguard Worker> `demo.FederatedContext` constructor, and the corresponding CA certificate will 129*14675a02SAndroid Build Coastguard Worker> need to be passed to the client library (e.g., via `--test_cert` for 130*14675a02SAndroid Build Coastguard Worker> `client_runner_main`). 131*14675a02SAndroid Build Coastguard Worker 132*14675a02SAndroid Build Coastguard Worker### Federated Program Platform 133*14675a02SAndroid Build Coastguard Worker 134*14675a02SAndroid Build Coastguard WorkerThe demo Federated Computations platform is a 135*14675a02SAndroid Build Coastguard Worker[Federated Program platform](https://github.com/tensorflow/federated/blob/main/tensorflow_federated/python/program/README.md#platform-specific-components) 136*14675a02SAndroid Build Coastguard Workerimplementation that allows TFF computations to be run using Federated 137*14675a02SAndroid Build Coastguard WorkerComputations Clients. 138*14675a02SAndroid Build Coastguard Worker 139*14675a02SAndroid Build Coastguard WorkerA production implementation could reuse much of this code as-is, though 140*14675a02SAndroid Build Coastguard Worker`federated_context.py` would need to be updated to communicate with remote 141*14675a02SAndroid Build Coastguard Workerserver(s) instead of an in-process server. 142*14675a02SAndroid Build Coastguard Worker 143*14675a02SAndroid Build Coastguard Worker#### `federated_context.py` 144*14675a02SAndroid Build Coastguard Worker 145*14675a02SAndroid Build Coastguard WorkerContains a 146*14675a02SAndroid Build Coastguard Worker[`tff.program.FederatedContext`](https://www.tensorflow.org/federated/api_docs/python/tff/program/FederatedContext) 147*14675a02SAndroid Build Coastguard Workerimplementation for running computations on the demo Federated Computations 148*14675a02SAndroid Build Coastguard Workerplatform. 149*14675a02SAndroid Build Coastguard Worker 150*14675a02SAndroid Build Coastguard WorkerThis module uses libraries in 151*14675a02SAndroid Build Coastguard Worker[`fcp/artifact_building`](../artifact_building) to 152*14675a02SAndroid Build Coastguard Workerconvert TFF computations to the format expected by the 153*14675a02SAndroid Build Coastguard Worker[in-process server](#in-process-server) and [client](#client). 154*14675a02SAndroid Build Coastguard Worker 155*14675a02SAndroid Build Coastguard Worker#### `federated_computation.py` 156*14675a02SAndroid Build Coastguard Worker 157*14675a02SAndroid Build Coastguard WorkerContains a 158*14675a02SAndroid Build Coastguard Worker[`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation) 159*14675a02SAndroid Build Coastguard Workersubclass for computations that will be run by the demo Federated Computations 160*14675a02SAndroid Build Coastguard Workerplatform. 161*14675a02SAndroid Build Coastguard Worker 162*14675a02SAndroid Build Coastguard Worker#### `federated_data_source.py` 163*14675a02SAndroid Build Coastguard Worker 164*14675a02SAndroid Build Coastguard WorkerContains a 165*14675a02SAndroid Build Coastguard Worker[`tff.program.FederatedDataSource`](https://www.tensorflow.org/federated/api_docs/python/tff/program/FederatedDataSource) 166*14675a02SAndroid Build Coastguard Workerimplementation for representing on-device data sources. 167*14675a02SAndroid Build Coastguard Worker 168*14675a02SAndroid Build Coastguard Worker#### `checkpoint_tensor_reference.py` 169*14675a02SAndroid Build Coastguard Worker 170*14675a02SAndroid Build Coastguard WorkerContains a 171*14675a02SAndroid Build Coastguard Worker[`tff.program.MaterializableValueReference`](https://www.tensorflow.org/federated/api_docs/python/tff/program/MaterializableValueReference) 172*14675a02SAndroid Build Coastguard Workerimplementation that reads values from a TensorFlow checkpoint. 173*14675a02SAndroid Build Coastguard Worker 174*14675a02SAndroid Build Coastguard Worker### In-Process Server 175*14675a02SAndroid Build Coastguard Worker 176*14675a02SAndroid Build Coastguard WorkerAn in-process HTTP(S) server that implements the 177*14675a02SAndroid Build Coastguard Worker[Federated Compute protocol](../protos/federatedcompute). 178*14675a02SAndroid Build Coastguard WorkerThis server is responsible for selecting which clients will contribute to each 179*14675a02SAndroid Build Coastguard Workercomputation invocation (**task**), broadcasting computations and state to 180*14675a02SAndroid Build Coastguard Workerclients, aggregating the results of on-device computation, and incorporating 181*14675a02SAndroid Build Coastguard Workerthat aggregate information back into the model or metrics. 182*14675a02SAndroid Build Coastguard Worker 183*14675a02SAndroid Build Coastguard WorkerIn a production implementation, each Federated Compute protocol service would 184*14675a02SAndroid Build Coastguard Workerlikely be handled by a separate replicated microservice, not a Python module. 185*14675a02SAndroid Build Coastguard Worker 186*14675a02SAndroid Build Coastguard Worker#### `server.py` 187*14675a02SAndroid Build Coastguard Worker 188*14675a02SAndroid Build Coastguard WorkerProvides the interface for setting up and stopping the in-process HTTP(S) server 189*14675a02SAndroid Build Coastguard Workerand running computations provided by the `FederatedContext`. This module is 190*14675a02SAndroid Build Coastguard Workerresponsible for notifying the various Federated Compute protocol service 191*14675a02SAndroid Build Coastguard Workerimplementations when a new task has been added and then managing the lifecycle 192*14675a02SAndroid Build Coastguard Workerof that task. 193*14675a02SAndroid Build Coastguard Worker 194*14675a02SAndroid Build Coastguard Worker#### `eligibility_eval_tasks.py` 195*14675a02SAndroid Build Coastguard Worker 196*14675a02SAndroid Build Coastguard WorkerContains handlers for the Federated Compute protocol's 197*14675a02SAndroid Build Coastguard Worker[EligibilityEvalTasks](../protos/federatedcompute/eligibility_eval_tasks.proto) 198*14675a02SAndroid Build Coastguard Workerservice. This service is responsible for serving optional pre-task-assignment 199*14675a02SAndroid Build Coastguard Workercomputations that determines to which tasks each client is eligible to 200*14675a02SAndroid Build Coastguard Workercontribute. The demo platform does not currently support configuring Eligibility 201*14675a02SAndroid Build Coastguard WorkerEval tasks; clients are considered to be eligible for all tasks. 202*14675a02SAndroid Build Coastguard Worker 203*14675a02SAndroid Build Coastguard Worker#### `task_assignments.py` 204*14675a02SAndroid Build Coastguard Worker 205*14675a02SAndroid Build Coastguard WorkerContains handlers for the Federated Compute protocol's 206*14675a02SAndroid Build Coastguard Worker[TaskAssignments](../protos/federatedcompute/task_assignments.proto) 207*14675a02SAndroid Build Coastguard Workerservice. This service is responsible for either assigning each client to a 208*14675a02SAndroid Build Coastguard Workertask -- or rejecting the client. 209*14675a02SAndroid Build Coastguard Worker 210*14675a02SAndroid Build Coastguard Worker#### `aggregations.py` 211*14675a02SAndroid Build Coastguard Worker 212*14675a02SAndroid Build Coastguard WorkerContains handlers for the Federated Compute protocol's 213*14675a02SAndroid Build Coastguard Worker[Aggregations](../protos/federatedcompute/aggregations.proto) 214*14675a02SAndroid Build Coastguard Workerservice. This service is responsible for aggregating client-reported data using 215*14675a02SAndroid Build Coastguard Workerthe 216*14675a02SAndroid Build Coastguard Worker[simple Aggregation Protocol](../aggregation/protocol/simple_aggregation) 217*14675a02SAndroid Build Coastguard Workerlibrary. 218*14675a02SAndroid Build Coastguard Worker 219*14675a02SAndroid Build Coastguard WorkerNote that the demo platform does not currently contain an implementation of the 220*14675a02SAndroid Build Coastguard Worker[SecureAggregations](../protos/federatedcompute/secure_aggregations.proto) 221*14675a02SAndroid Build Coastguard Workerservice. 222*14675a02SAndroid Build Coastguard Worker 223*14675a02SAndroid Build Coastguard Worker#### `media.py` 224*14675a02SAndroid Build Coastguard Worker 225*14675a02SAndroid Build Coastguard WorkerContains handlers for HTTP uploads and downloads using `PUT` and `GET` requests. 226*14675a02SAndroid Build Coastguard Worker 227*14675a02SAndroid Build Coastguard WorkerA production implementation will likely replace this module with a 228*14675a02SAndroid Build Coastguard Workerdeployment-environment-specific download service; a custom upload service 229*14675a02SAndroid Build Coastguard Workerimplementation may be needed since it should not persistently store 230*14675a02SAndroid Build Coastguard Workerclient-uploaded data. 231*14675a02SAndroid Build Coastguard Worker 232*14675a02SAndroid Build Coastguard Worker#### `http_actions.py` 233*14675a02SAndroid Build Coastguard Worker 234*14675a02SAndroid Build Coastguard WorkerContains helper functions for converting proto-based handlers into HTTP 235*14675a02SAndroid Build Coastguard Workerhandlers. This conversion mimics the Cloud Endpoints 236*14675a02SAndroid Build Coastguard Worker[HTTP to gRPC transcoding](https://cloud.google.com/endpoints/docs/grpc/transcoding). 237*14675a02SAndroid Build Coastguard Worker 238*14675a02SAndroid Build Coastguard Worker#### `plan_utils.py` 239*14675a02SAndroid Build Coastguard Worker 240*14675a02SAndroid Build Coastguard WorkerContains helper functions for constructing the TensorFlow graph and input 241*14675a02SAndroid Build Coastguard Workercheckpoint used by the client and running TensorFlow-based post-processing on 242*14675a02SAndroid Build Coastguard Workeraggregated results. 243