Compress gradients with top-k and error feedback

Why it matters Gradient compression with error feedback is used to train over slow networks, and without the residual it can stall convergence.

Sending only the k largest gradient entries saves bandwidth, but the dropped values would be lost forever. Error feedback keeps a residual of what was not sent and adds it to the next step's gradient.

Write topk_error_feedback(grads, k). grads has shape (steps, W, n): the gradient of each of W workers at each step. Each worker keeps a residual e (starting at zero). At each step, worker w forms c = grad + e, keeps the k entries of c with the largest absolute value (ties to the lower index) as its sent vector s (zeros elsewhere), and sets e = c - s. The applied update at that step is the mean of the workers' sent vectors. Return (updates with shape (steps, n), residuals with shape (W, n) after the last step).

Signaturedef topk_error_feedback(grads, k)

Example

grads [[[3, 1, 1]], [[0, 1, 1]]] (2 steps, 1 worker), k 1 gives updates [[3, 0, 0], [0, 2, 0]] and residuals [[0, 0, 2]].

Understand the idea

Error feedback is like paying a large bill in small instalments: whatever you cannot pay today is carried over and added to tomorrow's amount, so nothing is forgotten. Top-k compression sends only the biggest gradient entries each step; the rest is the carry-over. Worked example: one worker, k = 1. Step 1 gradient [3, 1, 1]: send [3, 0, 0], residual [0, 1, 1]. Step 2 gradient [0, 1, 1] plus residual is [0, 2, 2]; the tie goes to index 1, so send [0, 2, 0] and keep the residual [0, 0, 2] for later. Rule: c = g + e, s = topk(c), e = c - s. With error feedback every coordinate is eventually sent once enough has piled up, which is why compressed training still converges.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: exampletopk_error_feedback([[[3, 1, 1]], [[0, 1, 1]]], 1)
  • Sample 2: two workerstopk_error_feedback(_G, 2)