advanced27 min studyLesson 556 in Engineering

Vocabulary shards: look up locally and normalize globally

Trace masked embedding ownership and a stable distributed softmax/cross-entropy without gathering every logit.

On this page

What you’ll understand

  • Compute a vocabulary-row-sharded embedding lookup.
  • Reduce a global logit maximum and exponential denominator.
  • Preserve target ownership, padding masks, and gradient scaling.

A large vocabulary creates a large embedding table and output-logit dimension. Splitting vocabulary rows reduces each rank’s parameter storage, but a token ID still refers to one global row and a probability still normalizes over the full valid vocabulary. A local lookup or local softmax cannot simply stand in for that global meaning. The same ownership discipline applies to loss targets, tied embedding/output weights, and backward accumulation.

One global token ID has one owning row shard

For an even vocabulary split, each rank owns a contiguous or explicitly mapped subset of rows in an embedding table shaped[V,H]. A rank tests whether each global token ID is in its subset, converts owned IDs to valid local indices, and contributes the corresponding H-vector. Nonowners contribute a neutral zero vector in one common reduction strategy. Summing those masked contributions reconstructs the embedding because exactly one rank owns a valid ID. Another supported layout may return sequence shards instead, changing the collective but not the row identity.

Do not index a local table with an unmasked global ID and then try to discard the result afterward; the invalid access has already happened. Repeated occurrences of one token accumulate gradients into the same owned row. Padding IDs, out-of-range IDs, and a vocabulary padded for divisibility need explicit rules. If input and output embeddings share one logical parameter, the implementation must preserve that tying and combine all intended gradient contributions rather than accidentally create two unrelated copies.

e(t)=∑r1[t∈Vr]Er[t−start⁡r]e(t)=\sum_r\mathbf1[t\in V_r]E_r[t-\operatorname{start}_r]
V_r are disjoint valid vocabulary ranges, E_r stores owned rows, and t is a valid global token ID. The local lookup is performed only for an owned ID; nonowners contribute zero without an invalid lookup. The sum yields one H-dimensional embedding.↔ Scroll wide equations horizontally.

A probability needs the whole vocabulary denominator

For one token’s output logits, first find the global maximum over valid vocabulary entries. Every rank subtracts that maximum from its owned logits, sums their exponentials locally, and combines the local sums globally. The target logit is obtained from its unique owner or an equivalent distributed selection. Cross-entropy then uses the same global denominator. Local softmax would assign too much probability to the local shard because it ignores competing tokens elsewhere.

Subtracting the global maximum improves numerical stability without changing probabilities. A padded vocabulary entry that is not a real class must be excluded consistently; otherwise it adds false denominator mass. Ignored target tokens must follow the loss’s mask and normalization, not trigger a bogus target owner. Gradients for valid logits are their global probabilities minus the target indicator, with whatever loss scaling was defined. Fused loss implementations can use different reduction schedules, so describe the mathematical quantities before claiming a fixed count of all-reduces.

m=max⁡r,j∈Vrzj,D=∑r∑j∈Vrezj−m,L=log⁡D+m−zym=\max_{r,j\in V_r}z_j,\quad D=\sum_r\sum_{j\in V_r}e^{z_j-m},\quad L=\log D+m-z_y
z are dimensionless logits over the valid vocabulary, y is a valid nonignored target, and D>0. This is one-token cross-entropy with natural logarithms. Masked classes and ignored tokens require explicit handling outside this ordinary case.↔ Scroll wide equations horizontally.

Measure what is avoided and what is still communicated

Distributed vocabulary loss can avoid materializing every full logit vector on every rank, but it still needs global normalization information and correct backward ownership. Parameter savings do not imply zero communication or a globally sharded hidden activation at every boundary. Check the selected library’s input/output placements and tied-weight options. For testing, choose logits where the largest value and target live on different shards, as well as targets at shard boundaries and near a padded vocabulary tail.

The sampling lab illustrates a normalized distribution only. Reset, set p=0.5 and n=4, predict binomial masses proportional to 1,4,6,4,1 across five outcomes, then Step. Their common denominator is 16. The model is an exact binomial distribution, not vocabulary softmax, embeddings, logits, or a collective. Its limited point is that probabilities share one normalization across the defined outcome space; the worked example supplies the different exponential denominator used by language-model loss.

Four vocabulary entries across two ranks

One token has logits[0,ln 2,ln 3,ln 4], split two per rank. The target is the fourth global entry, index 3. Every entry is a valid class.

  1. The global maximum is ln 4. Scaled exponentials are[1/4,2/4,3/4,1]. Rank 0 contributes 0.75 and rank 1 contributes 1.75 to the denominator.

  2. The global denominator is 2.5. Target probability is 1/2.5=0.4, so cross-entropy is−ln 0.4=ln 2.5≈0.91629.

  3. A softmax restricted to rank 1 would give the target 4/(3+4)=4/7≈0.57143, which is wrong for the full vocabulary. Global valid-logit gradients are[0.1,0.2,0.3,−0.6] under a one-token sum loss.

The result

The target owner alone cannot determine its probability. The correct denominator includes every valid vocabulary shard.

Explore the model

Interactive lab

Interactive lab · sampling

One distribution has one normalization

Reset, set p=0.5 and n=4, predict masses[1,4,6,4,1]/16, then Step. This exact binomial law is not a vocabulary softmax, embedding lookup, or distributed reduction; playback highlights outcomes rather than sampling training tokens.

One distribution has one normalizationExact binomial masses for the mean of n independent Bernoulli trials, compared with normal mass between adjacent half-count boundaries; normal tails outside the finite support remain outside the bars. Playback highlights outcomes, not random samples. The Berry–Esseen bound applies to standardized CDFs, not individual bars or continuity-corrected errors. At p = 0 or 1, normal comparison and that bound are disabled. Normal-CDF evaluation uses a numerical approximation with absolute error below 7.5×10⁻⁸ (at most 1.5×10⁻⁷ per interval, before rounding). Current readings: Mean sample proportion: 0.5; Sample-mean variance: 0.0125; Uniform CDF error bound: ≤ 0.1062. All parameters are adjustable with the labeled controls below.Exact distribution of the mean of 20 independent Bernoulli variables with success probability 0.5; playback highlights outcome 0.00.250.50.75100.0580.1150.1730.23Sample mean · k / nProbability massNormal bin masses are an approximationk = 0 · exact probability = 9.54e-7
Exact binomial massNormal interval massHighlighted outcome / mean
Mean sample proportion
0.5
Sample-mean variance
0.0125
Uniform CDF error bound
≤ 0.1062
t = 0 s

Exact binomial masses for the mean of n independent Bernoulli trials, compared with normal mass between adjacent half-count boundaries; normal tails outside the finite support remain outside the bars. Playback highlights outcomes, not random samples. The Berry–Esseen bound applies to standardized CDFs, not individual bars or continuity-corrected errors. At p = 0 or 1, normal comparison and that bound are disabled. Normal-CDF evaluation uses a numerical approximation with absolute error below 7.5×10⁻⁸ (at most 1.5×10⁻⁷ per interval, before rounding). Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forChanging the outcome space changes normalization, so a local subset cannot silently replace the global definition.

Check your understanding

Why is local softmax on the target’s vocabulary shard generally insufficient?

Try it yourself

An embedding table has rows[1,0],[0,1],[2,3],[4,5], split two rows per rank. For token ID 2, give each masked contribution and the reconstructed embedding.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Only the owner performs the local lookup; the other contributes a zero vector.

Compare with the solution

Rank 0 owns IDs 0 and 1, so it contributes[0,0] without indexing ID 2 locally. Rank 1 owns IDs 2 and 3, so ID 2 maps to local row 0 and contributes[2,3]. Their sum is[2,3]. Repeated occurrences of ID 2 would later accumulate gradients into that same owned row.

Take it with you

  • Global token identity determines valid local embedding lookup.
  • Vocabulary probabilities require global maximum and denominator information.
  • Masks, target ownership, tied weights, and loss scaling are part of equivalence.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.