Group gradients into all-reduce buckets in backward order
Why it matters Bucketed all-reduce is how data-parallel trainers hide communication time behind backward computation, and the bucket cap is a common tuning knob.
Data-parallel training overlaps gradient communication with the backward pass. Gradients become ready in reverse layer order, so they are packed into buckets in that order, and each bucket is all-reduced as soon as it is full.
Write make_buckets(params, cap_bytes, bytes_per_elem=4). params is a list of (name, numel) in forward (model definition) order. Walk them in reverse order. Add each parameter to the current bucket unless that would push the bucket above cap_bytes, in which case close the current bucket and start a new one with it. A parameter larger than the cap gets a bucket of its own. Return a list of buckets, each a list of names in the order they were added.
def make_buckets(params, cap_bytes, bytes_per_elem=4)Example
params [("emb", 100), ("w1", 50), ("b1", 10), ("w2", 50)], cap 240 bytes, 4 bytes each gives [["w2", "b1"], ["w1"], ["emb"]].
Understand the idea
Bucketing is like a courier who leaves with a van as soon as it is full instead of waiting for every parcel of the day. Gradients are the parcels and they arrive in reverse layer order during backward. Small gradients would each pay a fixed startup cost if sent alone, so they are grouped; big groups would wait too long, so there is a cap. Worked example: cap 240 bytes, 4 bytes per value. Reverse order is w2 (200 bytes), b1 (40), w1 (200), emb (400). w2 + b1 = 240 fits. Adding w1 would make 440, so close [w2, b1]. w1 starts a bucket; emb would overflow, so [w1] closes and [emb] stands alone. Rule: walk in reverse, close when size + next > cap. Each closed bucket is all-reduced while backward keeps computing earlier layers.