Federated Learning
Training without centralizing data.
Your phone's keyboard can learn from what you type without your words leaving the device: the model travels to the data, and only updates travel back. That changes the optimization problem, because clients hold different data and communicate rarely, and the threat model, because updates can leak the data they came from. This lesson analyzes FedAvg and client drift, the cryptography of secure aggregation, and the attacks that make federated learning private only in combination with other tools.
What you’ll learn
- Formulate federated optimization, implement FedAvg, and explain when local steps save communication.
- Derive client drift on heterogeneous quadratics, and show how SCAFFOLD's control variates remove it.
- Explain secure aggregation by pairwise masking, its recovery from dropouts, and what it does and does not hide.
- Reconstruct inputs from gradients (exactly for a linear layer, by optimization in general), and combine federated learning with user-level differential privacy.
Federated optimization
Federated learning minimizes a weighted average of client objectives without collecting the data:
The setting differs from datacenter training in four ways (Kairouz et al., 2021). Data are non-IID, since each reflects one user, and unbalanced. Clients are many (up to millions of phones), and only a small, changing fraction takes part in each round. Communication, especially upload, is slow and costly relative to computation. Cross-device deployments involve phones; cross-silo ones a few reliable, data-rich institutions such as hospitals.
FEDAVG (McMahan et al., 2017)
server: w ← w₀
for round t = 1 … T:
S ← random set of m clients (a fraction of all K)
send w to every k ∈ S; receive w_k
Δ ← Σ_{k∈S} (n_k / n_S) · (w − w_k) # averaged local progress
w ← w − η_g · Δ # η_g = 1 is plain averaging
client k: y ← w
for E local epochs, for each mini-batch b of the local data:
y ← y − η · ∇ℓ(y; b)
return yWith the server simply averages the client models. Treating as a pseudo-gradient and applying momentum or Adam on the server gives the adaptive federated optimizers of Reddi et al. (2021).
Local steps buy communication. With IID data, local SGD reaches a given accuracy with far fewer rounds than synchronizing after every step, with convergence rates matching minibatch SGD under suitable step sizes (Stich, 2019), and McMahan et al. reported 10 to 100 times fewer rounds than one-step-per-round training on their benchmarks. Heterogeneity changes the picture: Woodworth, Patel & Srebro (2020) showed that with differing client objectives, local SGD can do worse than minibatch SGD with the same communication.
Think first If every client ran local SGD to convergence before each average (), what would FedAvg compute?
Each client would return its own minimizer, so FedAvg would converge to the weighted average of the client optima. That is generally not the minimizer of the average objective: averaging minimizers ignores how sharply each client's loss curves around its minimum. The next step makes this exact.
Heterogeneity and client drift
Take equally weighted clients with . The global objective is minimized at the curvature-weighted mean:
local gradient steps with rate (where ) shrink the distance to by each time:
The server's average is a contraction in , with a unique fixed point:
With , and : one local step is plain gradient descent. As , and , the unweighted average of client optima. In between, FedAvg converges to the wrong point whenever curvatures differ: client drift.
Two clients have and , so . FedAvg uses and local steps. Where does it converge?
Show the answer
0.6
and , so the weights are 0.651 and 0.972, and . The flat client pulls the answer toward its optimum far more than its curvature deserves; with the limit would be 0.5.
Three remedies. Decaying learning rates make FedAvg converge to the true optimum, at rates such as for smooth, strongly convex objectives, with constants that grow with heterogeneity (X. Li et al., 2020). FedProx (T. Li et al., 2020) adds to each local objective, limiting how far a client can wander from the server model. SCAFFOLD (Karimireddy et al., 2020) corrects every local step with control variates, kept by the server and kept by client , using the update ; its convergence guarantees do not depend on how different the clients are.
Use exact control variates evaluated at the round's starting point , namely and . The local step becomes
With , starting from , the recursion is , a geometric sum:
The server's average is a gradient step on the global objective, with an effective step size; its fixed point is , the true optimum , for any .
Secure aggregation
The server needs only the sum of the clients' update vectors . Secure aggregation (Bonawitz et al., 2017) lets it learn and nothing else about any single , even when clients drop out partway through, using only cheap cryptography: key agreement, a pseudorandom generator (PRG) and secret sharing.
Every pair of clients agrees on a seed by Diffie–Hellman key agreement; the server relays the public keys but cannot compute the seeds. Client sends its update hidden under pseudorandom masks, with all arithmetic modulo :
Summing over clients, each pair's mask appears once with each sign:
Each on its own is masked by PRG outputs the server cannot predict, so it looks uniformly random: it reveals nothing about to a computationally bounded server.
Dropouts. If client vanishes after the others have masked with , unmatched masks remain in the sum. So each client splits its Diffie–Hellman secret into -of- Shamir shares held by the others; the survivors reveal the shares belonging to dropped clients, and the server removes their masks. A lying server could then claim that a live client had dropped and unmask its . To prevent this, each client adds a second, self-generated mask , also secret-shared, and for each client the honest parties reveal shares of either (if it survived) or its key (if it dropped), never both.
Cost and limits. For a thousand clients with million-entry vectors the protocol cost less than twice the communication of sending the updates in the clear, though the original server computation grows quadratically with the number of clients; later versions talk to only a logarithmic number of neighbors (Bell et al., 2020). Secure aggregation does not hide the sum itself, which can expose a client through differencing across rounds with different participants. It does not stop malicious clients from poisoning the model, and it is in tension with robust aggregation rules (coordinate-wise median, trimmed mean, Krum), which need to inspect individual updates.
Which statement about secure aggregation is correct?
- It makes the trained model differentially private
- It protects the model against poisoned updates from malicious clients
- The server learns the sum of the participating clients' updates and nothing else about any individual update
- It hides even the sum of the updates from the server
Show the answer
C. The server learns the sum of the participating clients' updates and nothing else about any individual update
Pairwise masks cancel only in the sum, and secret sharing handles dropouts. Everything the sum itself reveals is still revealed, which is why secure aggregation is combined with differential privacy.
What updates leak, and user-level privacy
For a fully connected layer applied to one example, the chain rule gives the weight and bias gradients:
Row of the weight gradient is the input scaled by the -th bias gradient, so any row with a nonzero bias gradient returns the input exactly:
For a batch, both gradients are averages over examples, and the same ratio returns a weighted combination of the batch's inputs, with weights that vary from row to row. Recovery is exact for a single example; larger batches and several local steps blur the leak without removing it.
For general architectures, attacks optimize. Deep leakage from gradients (Zhu, Liu & Han, 2019) fits dummy inputs and labels by gradient descent so that their gradient matches the shared one, , and recovered images pixel by pixel and text token by token for small batches; the label can be read off the signs of the last layer's gradient (Zhao, Mopuri & Bilen, 2020). Geiping et al. (2020), matching gradient directions under an image prior, recovered recognizable ImageNet-sized images from trained networks, even from updates averaged over several local steps. Larger batches and more local steps help only partly, and compressing or sparsifying updates helps little; secure aggregation hides individual updates from the server, and differential privacy bounds what any observer can learn.
User-level differential privacy. DP-FedAvg (McMahan, Ramage, Talwar & Zhang, 2018) samples clients with probability per round, clips each client's entire update to norm , and adds to the sum. The unit of privacy is a user's whole dataset, however many examples it holds, and the accounting is DP-SGD's with clients in the role of examples. Because the noise is divided among the clients of a round (per-coordinate noise about in the average), thousands of clients per round are needed for good accuracy. With secure aggregation each client can add a share of the noise (the distributed discrete Gaussian; Kairouz, Liu & Steinke, 2021), so no party ever sees an un-noised sum. Gboard's production language models have been trained with federated learning under formal user-level DP guarantees (Xu et al., 2023).
Key takeaways
- Federated optimization minimizes with non-IID, unbalanced clients, partial participation and costly communication; FedAvg trades communication rounds for local computation.
- On heterogeneous quadratics FedAvg converges to , the optimum only for ; SCAFFOLD's control variates turn each round into a global gradient step and remove the drift.
- Secure aggregation reveals only the sum: pairwise masks cancel, Shamir shares repair dropouts and self-masks stop a lying server; it prevents neither poisoning nor leakage from the sum itself.
- Gradients leak: one example's input can be read exactly off a linear layer's gradient, and optimization attacks reconstruct images and text from small batches.
- User-level differential privacy (clip whole client updates, add Gaussian noise, account as in DP-SGD) is what makes federated training formally private, and it needs many clients per round.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why does FedAvg with many local epochs on non-IID data converge to the wrong point, even with exact gradients?
- Because clients are sampled at random
- Because each client drifts toward its own optimum, and an average of client optima differs from the minimizer of the average objective when curvatures differ
- Because the learning rate is too small
- Because averaging weights is a nonlinear operation
Show the answer
B. Because each client drifts toward its own optimum, and an average of client optima differs from the minimizer of the average objective when curvatures differ
On quadratics the fixed point weights each client's optimum by instead of by its curvature ; only (or a decaying step size, or a correction such as SCAFFOLD's) recovers .
Three clients use pairwise masks: client 1 sends , client 2 sends , and client 3 sends . What does the server obtain by adding the three messages?
- Nothing useful until the clients reveal their masks
- alone
Show the answer
C.
Each mask is added by one member of its pair and subtracted by the other, so all three cancel. Only the sum survives, and each individual message still looks random.
A single example passes through . An eavesdropper sees and . What was ?
- It cannot be determined from gradients
Show the answer
B.
, so , exactly.
Why does DP-FedAvg clip each client's entire model update, rather than each training example's gradient?
- To bound the influence of a whole user, however many examples they hold, which gives user-level privacy
- Because per-example gradients cannot be computed on phones
- To reduce communication cost
- Because clipping per example would give no privacy at all
Show the answer
A. To bound the influence of a whole user, however many examples they hold, which gives user-level privacy
The unit of privacy is whatever has bounded influence. Clipping the whole update bounds one user's total contribution to the sum, so the Gaussian noise protects everything that user contributed.
Further reading
- H. B. McMahan, E. Moore, D. Ramage, S. Hampson & B. Agüera y Arcas, "Communication-efficient learning of deep networks from decentralized data," AISTATS (2017).
- P. Kairouz, H. B. McMahan et al., "Advances and open problems in federated learning," Foundations and Trends in Machine Learning 14 (2021).
- K. Bonawitz et al., "Practical secure aggregation for privacy-preserving machine learning," ACM CCS (2017).
- S. P. Karimireddy, S. Kale, M. Mohri, S. Reddi, S. Stich & A. T. Suresh, "SCAFFOLD: Stochastic controlled averaging for federated learning," ICML (2020).
- L. Zhu, Z. Liu & S. Han, "Deep leakage from gradients," NeurIPS (2019).