We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Top-K Gating
An expert gate chooses which experts should process each token and how much weight each selected expert receives. Implement top_k_gating(logits, k) to turn scores into sparse routing weights.
Inputs and output
logits is a floating-point tensor of shape (batch, n_experts), with finite values. Each row represents one token and each column identifies an expert. k is an integer with 1 <= k <= n_experts.
Return a tensor of the same shape, dtype and device. Keep the original expert-column order and leave logits unchanged.
Select and normalize
For each row independently:
- Select the k largest logits. Break equal-score ties by choosing lower expert indices first.
- Apply softmax over the selected scores only.
- Put those probabilities in their original columns. Every unselected expert receives exactly zero.
For logits [[5, 3, 1]] and k=2, select experts 0 and 1. Their normalized weights are approximately [[0.880797, 0.119203, 0]]. For [[2, 2, 2]] and k=2, the tie rule gives [[0.5, 0.5, 0]].
Each row sums to one. At k=1, the chosen expert gets weight one; at k=n_experts, the result is ordinary softmax over the full row.
One approach is to replace unselected scores with negative infinity and then apply a numerically stable softmax. Subtract the row maximum before exponentiating. Do not use a finite mask value such as -1e9, which can outrank valid, more negative scores. Do not apply full-row softmax and simply zero entries afterward without renormalizing.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Choose which experts process each token
A mixture-of-experts layer contains several networks, called experts, and a router. For each token, the router scores the experts, selects some of them, and weights their outputs. Sparse routing lets a large model use only a few experts for each token.
For scores [3, 1, 0], top-1 routing selects expert 0. If selected weights are
normalized to sum to one, its weight is 1. Top-2 routing selects experts 0
and 1, with weights determined by the task's rule.
Follow the weighting convention
Two common choices are softmax over selected logits and full softmax followed by selection. Full softmax followed by selection gives the same selected weights as the first choice if they are renormalized. Without renormalization, their sum is generally less than one and the output differs.
Setting excluded logits to zero is not a valid way to remove them before
softmax: exp(0) = 1, so they would still get positive weight. Excluded experts
must contribute zero probability to the combination.
Put each expert result back at the right token
When grouping tokens for expert computation, keep enough indexing information to restore their original order. If several experts process the same token, add their weighted outputs; do not overwrite one contribution with another.
Real systems also limit expert capacity and encourage balanced workloads so one expert does not receive nearly all tokens. Implement those policies only when the exercise includes them; they can change which outputs are returned.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.