We want, for each token embedding x∈Rd, to (1) pick which experts, and (2) blend their outputs.
Step 1 — Score every expert. Give the router a learnable weight matrix Wg∈RN×d. The raw score (logit) for expert i is the dot product:
hi=(Wgx)iWhy this step? A dot product measures alignment between the token and each expert's learned "preference vector" (row of Wg). High alignment → this expert is relevant.
Step 2 — Turn scores into probabilities. Apply softmax so scores are comparable and sum to 1:
pi=∑j=1NehjehiWhy this step? We need normalized weights to combine expert outputs, and softmax is the smooth, differentiable way to convert "who is best" into weights.
Step 3 — Keep only the Top-k. Let T be the set of indices of the k largest pi. Zero out the rest:
gi=⎩⎨⎧∑j∈Tpjpi0i∈TotherwiseWhy this step? Setting non-selected gates to 0 means those experts do zero computation — that's where the FLOP savings come from. Re-normalizing over T keeps the used weights summing to 1.
Step 4 — Combine expert outputs.y=∑i∈TgiEi(x)Why this step? We only evaluate the k chosen experts Ei(x) and take a gate-weighted sum. This is the layer's output.
Fix: an auxiliary load-balancing loss. For a batch, define:
fi = fraction of tokens routed to expert i (dispatch fraction).
Pi = average router probability assigned to expert i.
Laux=αN∑i=1NfiPi
Why this exact form? It is minimized when both fi and Pi are uniform (=1/N), i.e. every expert gets an equal share. Using Pi (soft, differentiable) instead of only fi (from a hard argmax, non-differentiable) lets gradients actually flow to fix imbalance. The factor N keeps the loss scale independent of expert count; α (e.g. 0.01) keeps it a gentle nudge, not the main objective.
Imagine a hospital with 64 specialist doctors. A dense model is like making every patient see all 64 doctors — accurate but insanely slow and expensive. MoE puts a smart receptionist (the router) at the door who looks at your problem and sends you to just the 2 best-matched doctors. The hospital still has all that expertise, but each patient only uses a tiny bit of it. To keep it fair, there's a rule so one popular doctor doesn't get all the patients while others sit idle (load balancing), and each doctor can only see so many patients per hour (capacity).
Dekho, dense model mein har token ko poore network se guzarna padta hai — matlab saare parameters har token ke liye compute hote hain. Yeh accurate to hai par bahut mehenga. MoE ka jugaad simple hai: hum bahut saare chhote "expert" networks rakhte hain, par har token ke liye sirf 2 (ya 1) best experts ko chalate hain. Ek chhota sa "router" decide karta hai ki yeh token kis expert ko jaana chahiye — bilkul receptionist ki tarah jo patient ko sahi doctor ke paas bhejta hai.
Router kaam kaise karta hai? Pehle woh har expert ke liye ek score nikalta hai (Wgx ka dot product), phir softmax se usko probability banata hai, phir Top-k experts choose karta hai aur baaki ko gate = 0 de deta hai. Gate 0 ka matlab woh expert compute hi nahi hota — yahi se FLOPs ki bachat aati hai. Selected experts ke outputs ko gate weights se multiply karke jod dete hain, bas ho gaya output.
Ek important point: total parameters badhte hain N (experts ki sankhya) ke saath, par compute badhta hai k (active experts) ke saath. Isliye MoE mein aap knowledge/capacity ko sasta mein badha sakte ho bina compute badhaye. Lekin ek problem hai — "rich get richer": ek popular expert ko saare tokens milne lagte hain aur baaki experts bekaar ho jaate hain. Iske liye ek load-balancing loss (αN∑fiPi) lagate hain jo sabko barabar kaam dilata hai. Aur har expert ki ek capacity hoti hai — usse zyada tokens aayein to extra tokens drop ho jaate hain.
Exam/interview ke liye yaad rakho: Route → Softmax → Top-k → Combine, aur "compute = k, capacity = N". Bas itna clear ho gaya to MoE tumhara pakka concept hai.