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

709. Split an MLP across GPUs with tensor parallelism

HardDistributed training Pro

This is a NumPy simulation of Megatron-style tensor parallelism.

A transformer MLP is y = relu(x @ W1) @ W2. Megatron splits W1 by COLUMNS and W2 by ROWS across p GPUs. Each GPU computes its slice of the hidden layer with no communication, multiplies by its rows of W2, and one all-reduce (a sum) at the end gives y.

Write tp_mlp(x, W1, W2, p) that simulates this: split W1 into p column blocks and W2 into p matching row blocks (np.array_split), compute each GPU's partial output, and return (y, partials) where partials is the list of per-GPU outputs and y is their sum.

Functiondef tp_mlp(x, W1, W2, p)

Example 1

Input: tp_mlp([[1.0, -1.0]], [[1.0, 0.0], [0.0, 1.0]], [[1.0], [2.0]], 2)

Output: ([[1.0]], [[[1.0]], [[0.0]]])

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

W1 blocks: np.array_split(W1, p, axis=1).

Hint 2

W2 blocks: np.array_split(W2, p, axis=0).

Hint 3

Partial_i = relu(x @ W1_i) @ W2_i.

Common mistakes
  • Splitting W1 by rows instead (the buggy version) makes each GPU hold a partial pre-activation; ReLU of a partial sum is not the partial of the ReLU, so the result is wrong.
Understand the idea

Tensor parallelism splits one layer's weights across GPUs. For an MLP y = relu(x W1) W2, cut W1 by columns so each GPU owns whole hidden units, and cut W2 by the matching rows. Each GPU computes its part of the hidden layer, applies ReLU locally, multiplies by its rows of W2, and a single sum at the end gives y. Splitting W1 by rows would give partial pre-activations, and ReLU of a partial sum is not the sum of ReLUs: relu(1) + relu(-2) = 1 but relu(1 - 2) = 0.

Worked example

x = [[1, 1]], W1 = [[1, -1], [1, -2]], W2 = [[3], [4]], p = 2. GPU 0 takes column 0 of W1: hidden 1 + 1 = 2, relu 2, times row 0 of W2 (3) = 6. GPU 1 takes column 1: hidden -1 - 2 = -3, relu 0, times 4 = 0. Sum = 6, matching the single-GPU answer.

split W1 columns and W2 rows with np.array_split, partial_k = relu(x W1_k) W2_k, y = sum of partials

Megatron-LM trains large LLMs this way.

Topics
Distributed training
Similar questions
  • Simulate a GPipe-style schedule with uneven stage timesHard
  • Generate a 1F1B pipeline scheduleHard
  • Split attention heads across GPUs with tensor parallelismHard
  • Compute cross-entropy when the vocabulary is split across GPUsHard
All Distributed training problems
Report
709 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def tp_mlp(x, W1, W2, p):
# your code here
pass
​
tp_mlp(_x, _W1, _W2, 2)[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