FedAvg: one round

Problem: train one model on data spread over $K$ devices. Data can't leave them, and every communication round is expensive.

1/4

McMahan et al., AISTATS 2017 · gray refs = where in the paper · + = my explanation, not the paper's

Server sends w_t to sampled clients; each trains locally and returns its model; the server averages them weighted by data size

1The algorithm Alg. 1

Server
initialise $w_0$; for each round $t = 0, 1, 2, \dots$
$m \leftarrow \max(C\cdot K,\,1)$; $\;S_t \leftarrow$ random set of $m$ clients
for $k \in S_t$ in parallel: $\;w^k_{t+1} \leftarrow \text{ClientUpdate}(k, w_t)$
$w_{t+1} \leftarrow \sum_{k \in S_t} \frac{n_k}{n_S}\, w^k_{t+1}, \quad n_S = \sum_{k\in S_t} n_k$
ClientUpdate$(k, w)$ — runs on device $k$
split local data $\mathcal P_k$ into batches of size $B$
for each local epoch $1 \dots E$: for each batch $b$:
$w \leftarrow w - \eta\, \nabla \ell(w; b)$
return $w$ to the server

+ the paper's Alg. 1 writes the sum as $\sum_{k=1}^{K}\frac{n_k}{n}$; with a sample $S_t$, renormalise by $n_S$ so the weights sum to 1.

2Three knobs

$C$fraction of clients per round
$E$local epochs per round
$B$local minibatch ($B=\infty$: whole $\mathcal P_k$)
$$u_k = \frac{E\, n_k}{B} \;\;\text{local steps per round}$$

FedSGD is the case $E=1,\ B=\infty$: one full-batch gradient per client per round. The baseline. §2

What travels? $w_t$ down, $w^k_{t+1}$ up (and $n_k$). Never data.

Important assumption: every client starts the round from the same $w_t$. Averaging nets trained from different inits fails Fig. 1

more local work ($E\uparrow$, $B\downarrow$) → more progress per round → fewer rounds (paper: 10–100× fewer than FedSGD in its experiments) → but on non-IID data, more local work = more drift (pp. 2–4)

Key idea: trade cheap local computation for expensive communication: many local SGD steps, then one data-size-weighted average.

Why averaging models optimises $f$

Problem: the server averages weights. When is that the same as descending the global loss?

2/4

1One local step, then average

2 clients, $n_1 : n_2 = 1 : 3$. The average sits ¾ of the way to $w^2$: the bigger client pulls harder.

Numbers: $n_1 = 100,\ n_2 = 300$, $w^1 = 2,\ w^2 = 6$
$\Rightarrow w_{t+1} = \tfrac14\cdot 2 + \tfrac34\cdot 6 = 5$ (plain mean: 4)

2Objective → FedSGD = averaging

$$f(w) = \tfrac1n \textstyle\sum_{i=1}^{n} f_i(w) = \sum_{k=1}^K \underbrace{\tfrac{n_k}{n}}_{p_k} F_k(w)$$
group the $n$ examples by client
$$F_k(w) = \tfrac{1}{n_k}\textstyle\sum_{i\in\mathcal P_k} f_i(w)$$
client $k$'s own average loss
$$\nabla f(w_t) = \textstyle\sum_k p_k\, \nabla F_k(w_t)$$
$\nabla$ is linear
$$w_{t+1} = w_t - \eta \textstyle\sum_k p_k \nabla F_k(w_t)$$
one GD step on $f$ (FedSGD)
$$\phantom{w_{t+1}} = \textstyle\sum_k p_k \big(w_t - \eta \nabla F_k(w_t)\big)$$
uses $\sum_k p_k = 1$
$$\phantom{w_{t+1}} = \color{#2c7a3f}{\textstyle\sum_k p_k\, w^k_{t+1}}$$
= average of one-step local models. Exact.

3FedAvg: $u_k$ local steps before averaging

$$w^k_{t,s+1} = w^k_{t,s} - \eta\, g_k(w^k_{t,s}), \qquad w^k_{t,0} = w_t$$
$$w_{t+1} = w_t \;-\; \eta \sum_k p_k \underbrace{\sum_{s=0}^{u_k-1} g_k\big(\color{#c0392b}{w^k_{t,s}}\big)}_{\text{gradients along client } k\text{'s own path}}$$

$g_k$: minibatch gradient of $F_k$. For $s \ge 1$ the point $\color{#c0392b}{w^k_{t,s}} \ne w_t$, and it moves toward client k's optimum.

→ only the first local step is a GD step on $f$. Small $\eta u_k$: ≈ $u_k$ steps on $f$; large: drift.

4Implementing it

  • flatten all parameters into one vector $w$
  • or send updates $\Delta_k = w^k - w_t$: then $w_{t+1} = w_t + \sum_k p_k \Delta_k$
  • clients must report $n_k$; server never sees $\mathcal P_k$
  • local optimiser: plain SGD, same $\eta$ everywhere, restarted from $w_t$ each round
  • to test drift: split data by label (non-IID) vs shuffle (IID)

Common mistake: uniform $\frac{1}{|S_t|}$ average minimises $\frac1K\sum_k F_k$: every client counts equally, not every example. Differs when $n_k$ are unequal.

5Where the bias comes from: expand to 2nd order +

$$g_k(w^k_{t,s}) \approx \nabla F_k(w_t) + H_k\,(w^k_{t,s} - w_t), \qquad w^k_{t,s} - w_t \approx -\eta\, s\, \nabla F_k(w_t)$$
Taylor, $H_k = \nabla^2 F_k(w_t)$; 1st-order path; full-batch
$$\sum_k p_k \Delta_k \approx \underbrace{-\eta u\, \nabla f(w_t)}_{u \text{ GD steps on } f} \;+\; \underbrace{\color{#c0392b}{\eta^2 \tfrac{u(u-1)}{2} \textstyle\sum_k p_k H_k \nabla F_k(w_t)}}_{\text{drift: each client's curvature} \times \text{its own gradient}}$$
sum over $s = 0..u{-}1$, all $u_k = u$

at $w^\star$: $\nabla f = 0$, but the red term $\ne 0$ unless all $H_k$ are equal or all $\nabla F_k(w^\star) = 0$ → FedAvg steps away from $w^\star$. Check p. 4, $u = 2$: $0.01\cdot(\tfrac12\cdot 1\cdot 3 + \tfrac12\cdot 3\cdot(-3)) = -0.03$, so $3 \to 2.97$ ✓

Key idea: weights $p_k = n_k/n$ make the average exactly a GD step on $f$, but only for one local step. More steps evaluate gradients away from $w_t$.

Client data and client drift

Problem: we minimise $f = \sum_k p_k F_k$, but client $k$ only ever sees $F_k$. If $F_k \ne f$, its local steps head somewhere else.

3/4

1IID split

$$\mathbb E_{\mathcal P_k}\big[F_k(w)\big] = f(w)$$

every client sees a shuffled sample → $F_k \approx f$ → both paths follow $f$; $w_{t+1}$ lands on the end of the gray path. §2

2Non-IID split

$$w^\star_k = \arg\min F_k \;\ne\; w^\star = \arg\min f$$

each path bends toward its own $w^\star_k$. The average misses where $u$ steps on $f$ would go.

Same $f$ (gray rings), same $w_t$, $\eta = 0.1$, $u = 5$ local GD steps in both panels; only the split differs. Dashed gray = 5 GD steps on $f$ itself. + 2-D quadratic cartoon: real losses are non-convex, the mechanism is the same.

3Why drift happens +

non-IID → $\nabla F_k(w) \ne \nabla f(w)$ → $u_k$ steps follow $F_k$, curve toward $w_k^\star$ → $\Delta_k$ biased → average lands off the path of $f$, even when started at $w^\star$ (p. 4)

$$\zeta^2(w) = \sum_k p_k \,\big\| \nabla F_k(w) - \nabla f(w) \big\|^2$$

heterogeneity: how much clients disagree about the downhill direction. $\zeta = 0$ at every $w$ → no drift. Drift grows with $\zeta$ and with $\eta u_k$.

"client drift" is the later name (Karimireddy et al. 2020, SCAFFOLD); the 2017 paper doesn't use it.

4Kinds of non-IID

label skew: client sees few classes. Paper's "pathological" MNIST: sort by digit, 200 shards of 300, 2 shards per client. §3

quantity skew: unbalanced $n_k$ → weights $p_k$ matter.

+ feature skew: same labels, different inputs (handwriting, cameras).

Notice: the paper reports FedAvg still trains well on its pathological split. Drift slows and biases it; it need not break it. Very large $E$ can plateau or diverge; the authors suggest decaying local work. §3

Key idea: drift = each client descends its own $F_k$ for many steps; averaging the end points is not descending $f$.

Worked example: the average misses $w^\star$

Problem: two clients, different optima. Does FedAvg converge to the global optimum? +

4/4
at $w^\star$: $F_1'(3) = +3$, $F_2'(3) = -3$. They cancel only in the average; each client alone still wants to move.

1Setup ($n_1 = n_2$, so $p_k = \frac12$)

$$F_1 = \tfrac{1}{2}w^2, \quad F_2 = \tfrac{3}{2}(w-4)^2$$

optima $c = (0, 4)$, curvatures $a = (1, 3)$

$$f'(w) = \tfrac12 w + \tfrac32(w-4) = 2w - 6$$

$w^\star = 3$, $f(3) = 3$. The steep client pulls $w^\star$ toward 4.

$$w^k = c_k + \rho_k^{\,u}\,(w_t - c_k), \quad \rho_k = 1 - \eta a_k$$

each local GD step shrinks the gap to $c_k$ by $\rho_k$. $\;\eta = 0.1$: $\rho = (0.9,\ 0.7)$

2One round, starting at $w_t = w^\star = 3$

$u$$w^1 = 0.9^u\cdot 3$$w^2 = 4 - 0.7^u$$w_{t+1}$
12.73.33.0 stays
22.433.512.97 leaves
$\infty$042.0

$u \to \infty$: $f(2) = 4 \gt f(3)$, and $f'(2) = -2 \ne 0$: not even stationary.

3Where it settles (fixed point)

$$\bar w = \textstyle\sum_k p_k\big[c_k + \rho_k^{u}(\bar w - c_k)\big]$$
set $w_{t+1} = w_t$
$$\bar w = \frac{\sum_k p_k (1-\rho_k^{u})\, c_k}{\sum_k p_k (1-\rho_k^{u})}$$
vs $w^\star = \frac{\sum p_k a_k c_k}{\sum p_k a_k}$

$u = 1$: $1-\rho_k = \eta a_k$ → $\bar w = w^\star = 3$ ✓

$u = 2$: $\frac{0.51\cdot 4}{0.19 + 0.51} = 2.91$ · $u = 5$: 2.68 · $u = 10$: 2.39

$u \to \infty$: $\bar w \to \sum_k p_k c_k = 2$ (curvature forgotten)

What changes? $E\uparrow$ or $B\downarrow$ → $u\uparrow$ → fewer rounds, but $\bar w$ slides from $w^\star$ toward $\sum_k p_k c_k$.
$\eta\downarrow$ → drift term $\propto \eta^2$ shrinks faster than progress $\propto \eta$ → less bias, slower.

Assumes full-batch local GD, quadratic $F_k$, $\eta a_k \lt 2$. In this toy the bias needs different curvatures: with $a_1 = a_2$ it vanishes.

?Test your understanding

  1. With $E=1,\ B=\infty$, show FedAvg is one GD step on $f$. Which property of $p_k$ did you use?
  2. Set $a_1 = a_2$ in the example. Where is $\bar w$ for every $u$, and why?
  3. You average with $\frac{1}{|S_t|}$ instead of $\frac{n_k}{n_S}$. Which objective do you minimise? When is it $f$?
  4. Why does raising $E$ save rounds on IID data but bias the result under label skew? Use $u \to \infty$.

!Remember

  1. FedAvg: sampled clients run $E$ epochs of SGD from $w_t$; the server takes the $n_k$-weighted average.
  2. One local step + averaging is exactly GD on $f$; more local steps buy fewer rounds with bias.
  3. Non-IID data → clients descend different $F_k$ → the average drifts off $w^\star$, more with larger $\eta u$ and heterogeneity.