BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Distributed training8 of 22 in topic

702. Simulate ring all-reduce step by step

MediumDistributed training Pro

This is a NumPy simulation of NCCL's ring all-reduce; no network is involved.

p workers each hold a vector split into p chunks. Ring all-reduce has two phases of p - 1 steps each:
1. Reduce-scatter: at step k, worker i sends chunk (i - k) mod p to worker (i + 1) mod p, which adds it into its own copy of that chunk. All sends in a step happen at the same time.
2. All-gather: at step k, worker i sends chunk (i + 1 - k) mod p (now fully reduced) to worker (i + 1) mod p, which overwrites its copy.

Write ring_allreduce(vectors) where vectors is a list of p equal-length arrays whose length is divisible by p. Return (result_per_worker, elements_sent_per_worker), where the first is a list of arrays (all equal to the sum) and the second is how many elements one worker sent in total.

Functiondef ring_allreduce(vectors)

Example 1

Input: ring_allreduce([[1.0, 2.0], [10.0, 20.0]])

Output: ([[11.0, 22.0], [11.0, 22.0]], 2)

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Split each worker's data into p chunks.

Hint 2

In each step, first collect every send from a snapshot of the current chunks, then apply them.

Common mistakes
  • The buggy version starts one chunk off, circulates partial sums, and forgets to count the all-gather traffic.
Understand the idea

Ring all-reduce sums a vector across p workers arranged in a circle, without any central server. Each vector is cut into p chunks. Phase 1 (reduce-scatter, p - 1 steps): each worker passes one chunk to its right neighbour, who adds it in; after this, worker i holds the full sum of chunk (i + 1) mod p. Phase 2 (all-gather, p - 1 steps): the finished chunks travel around the ring and overwrite stale copies. All sends in a step use the values from before that step.

Worked example

p = 3, w0 = [1, 2, 3], w1 = [10, 20, 30], w2 = [100, 200, 300], one element per chunk. Step 0: w1 chunk 0 becomes 11, w2 chunk 1 becomes 220, w0 chunk 2 becomes 303. Step 1: w1 chunk 2 = 30 + 303 = 333, w2 chunk 0 = 100 + 11 = 111, w0 chunk 1 = 2 + 220 = 222. Now each worker owns one full sum. Each worker sends 2 (p - 1) chunks of size n / p, here 4 elements. NCCL uses rings for multi-GPU gradient sync.

Topics
Distributed training
Similar questions
  • Estimate per-GPU memory under ZeRO stages 0 to 3Medium
  • Estimate per-step communication for DP, ZeRO and TPMedium
  • Shard flattened parameters across ranksMedium
  • Group gradients into all-reduce buckets in backward orderMedium
All Distributed training problems
Report
702 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def ring_allreduce(vectors):
# your code here
pass
​
ring_allreduce(_v)[0]

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS