# Visualize: Dense vs MoE architecture
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
# --- Left: Dense FFN ---
ax = ax1
ax.set_xlim(-1, 11)
ax.set_ylim(-1, 10)
ax.axis('off')
ax.set_title('Dense Transformer Block', fontsize=15, fontweight='bold')
def box(ax, x, y, w, h, label, color, sublabel=None, fontsize=11):
rect = mpatches.FancyBboxPatch((x, y), w, h, boxstyle='round,pad=0.12',
facecolor=color, edgecolor='#333', linewidth=1.5)
ax.add_patch(rect)
ax.text(x + w/2, y + h/2 + (0.15 if sublabel else 0), label,
ha='center', va='center', fontsize=fontsize, fontweight='bold')
if sublabel:
ax.text(x + w/2, y + h/2 - 0.25, sublabel,
ha='center', va='center', fontsize=8, color='#555')
def arrow(ax, x1, y1, x2, y2):
ax.annotate('', xy=(x2, y2), xytext=(x1, y1),
arrowprops=dict(arrowstyle='->', color='#333', lw=1.5))
# Input tokens
for i, tok in enumerate(['the', 'cat', 'sat']):
box(ax, i*3.2 + 0.5, 8, 2.2, 0.7, tok, '#4ECDC4')
# Attention
arrow(ax, 5, 8, 5, 7.4)
box(ax, 1.5, 6.5, 7, 0.8, 'Multi-Head Attention', '#FF6B6B')
# Single FFN β all tokens go through same weights
arrow(ax, 5, 6.5, 5, 5.9)
box(ax, 1.5, 5, 7, 0.8, 'SwiGLU FFN (single)', '#95E1D3', 'ALL tokens use these same weights')
# Arrows from each token to FFN
for i in range(3):
arrow(ax, i*3.2 + 1.6, 8, 5, 7.4)
ax.text(5, 4.0, f'Total params: N\nActive params: N\nEvery token uses 100% of FFN',
ha='center', fontsize=11, color='#666',
bbox=dict(boxstyle='round,pad=0.4', facecolor='#F1F5F9', edgecolor='#CBD5E1'))
# --- Right: MoE ---
ax = ax2
ax.set_xlim(-1, 11)
ax.set_ylim(-1, 10)
ax.axis('off')
ax.set_title('MoE Transformer Block', fontsize=15, fontweight='bold')
# Input tokens
for i, tok in enumerate(['the', 'cat', 'sat']):
box(ax, i*3.2 + 0.5, 8, 2.2, 0.7, tok, '#4ECDC4')
# Attention
arrow(ax, 5, 8, 5, 7.4)
box(ax, 1.5, 6.5, 7, 0.8, 'Multi-Head Attention', '#FF6B6B')
# Router
arrow(ax, 5, 6.5, 5, 5.9)
box(ax, 2.5, 5.2, 5, 0.6, 'Router (learned)', '#FFE66D', 'picks top-k experts per token')
# Expert FFNs
expert_colors = ['#95E1D3', '#A8D8EA', '#B5EAD7', '#C7CEEA', '#E2F0CB', '#FFDAC1', '#FFB7B2', '#FF9AA2']
for i in range(4):
x = i * 2.4 + 0.2
box(ax, x, 3.0, 2.0, 0.7, f'Expert {i}', expert_colors[i], 'SwiGLU')
# Routing arrows β each token goes to 2 experts
routing = [(0, [0, 2]), (1, [1, 3]), (2, [0, 1])] # token β expert indices
route_colors = ['#4ECDC4', '#FF6B6B', '#C4B5FD']
for tok_i, (_, expert_ids) in enumerate(routing):
for exp_i in expert_ids:
x_start = tok_i * 3.2 + 1.6
x_end = exp_i * 2.4 + 1.2
ax.annotate('', xy=(x_end, 3.7), xytext=(x_start, 5.2),
arrowprops=dict(arrowstyle='->', color=route_colors[tok_i],
lw=2, alpha=0.7))
ax.text(5, 1.5, f'Total params: 4N (4 experts)\nActive params: 2N (top-2 routing)\nEach token uses only 50% of FFN params',
ha='center', fontsize=11, color='#666',
bbox=dict(boxstyle='round,pad=0.4', facecolor='#F1F5F9', edgecolor='#CBD5E1'))
# Legend
for i, (tok, color) in enumerate(zip(['the', 'cat', 'sat'], route_colors)):
ax.plot([], [], color=color, linewidth=2, label=f'"{tok}" β experts')
ax.legend(loc='lower right', fontsize=10)
plt.tight_layout()
plt.show()
print('Left: Dense model β every token goes through the SAME FFN.')
print('Right: MoE model β router sends each token to its TOP-2 experts.')
print('4x parameters, but only 2x compute (since each token uses 2 of 4 experts).')