|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import RoutingPanel from './RoutingPanel'; |
| 3 | +import GatingPanel from './GatingPanel'; |
| 4 | +import LoadBalancingPanel from './LoadBalancingPanel'; |
| 5 | + |
| 6 | +export default function App() { |
| 7 | + const [numExperts, setNumExperts] = useState(8); |
| 8 | + const [topK, setTopK] = useState(2); |
| 9 | + const [batchSize, setBatchSize] = useState(10); |
| 10 | + const [expertLoads, setExpertLoads] = useState(Array(8).fill(0)); |
| 11 | + |
| 12 | + const handleGenerate = () => { |
| 13 | + // Simulate load updates |
| 14 | + const newLoads = [...expertLoads]; |
| 15 | + for (let i = 0; i < batchSize; i++) { |
| 16 | + // Randomly assign to topK experts |
| 17 | + for (let k = 0; k < topK; k++) { |
| 18 | + const idx = Math.floor(Math.random() * numExperts); |
| 19 | + newLoads[idx]++; |
| 20 | + } |
| 21 | + } |
| 22 | + setExpertLoads(newLoads); |
| 23 | + }; |
| 24 | + |
| 25 | + // Reset loads when expert count changes |
| 26 | + React.useEffect(() => { |
| 27 | + setExpertLoads(Array(numExperts).fill(0)); |
| 28 | + }, [numExperts]); |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="min-h-screen bg-slate-950 p-8 font-sans text-slate-200"> |
| 32 | + <header className="max-w-7xl mx-auto mb-8 flex justify-between items-end"> |
| 33 | + <div> |
| 34 | + <h1 className="text-4xl font-black text-transparent bg-clip-text bg-gradient-to-r from-neon-blue to-neon-purple mb-2"> |
| 35 | + Mixture of Experts |
| 36 | + </h1> |
| 37 | + <p className="text-slate-400 text-lg"> |
| 38 | + Visualizing Sparse Gating & Expert Routing |
| 39 | + </p> |
| 40 | + </div> |
| 41 | + <div className="flex gap-4"> |
| 42 | + <div className="bg-slate-900 p-4 rounded-lg border border-slate-800"> |
| 43 | + <label className="block text-xs font-bold text-slate-500 uppercase mb-1">Experts</label> |
| 44 | + <div className="flex gap-2"> |
| 45 | + {[4, 8, 16].map(n => ( |
| 46 | + <button |
| 47 | + key={n} |
| 48 | + onClick={() => setNumExperts(n)} |
| 49 | + className={`px-3 py-1 rounded text-sm font-bold transition-colors ${numExperts === n ? 'bg-neon-blue text-black' : 'bg-slate-800 hover:bg-slate-700' |
| 50 | + }`} |
| 51 | + > |
| 52 | + {n} |
| 53 | + </button> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + |
| 58 | + <div className="bg-slate-900 p-4 rounded-lg border border-slate-800"> |
| 59 | + <label className="block text-xs font-bold text-slate-500 uppercase mb-1">Top-K</label> |
| 60 | + <div className="flex gap-2"> |
| 61 | + {[1, 2].map(k => ( |
| 62 | + <button |
| 63 | + key={k} |
| 64 | + onClick={() => setTopK(k)} |
| 65 | + className={`px-3 py-1 rounded text-sm font-bold transition-colors ${topK === k ? 'bg-neon-pink text-black' : 'bg-slate-800 hover:bg-slate-700' |
| 66 | + }`} |
| 67 | + > |
| 68 | + {k} |
| 69 | + </button> |
| 70 | + ))} |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + </header> |
| 75 | + |
| 76 | + <main className="max-w-7xl mx-auto grid grid-cols-1 lg:grid-cols-3 gap-8"> |
| 77 | + {/* Main Visualization */} |
| 78 | + <div className="lg:col-span-2 aspect-video"> |
| 79 | + <RoutingPanel |
| 80 | + numExperts={numExperts} |
| 81 | + topK={topK} |
| 82 | + batchSize={batchSize} |
| 83 | + onGenerate={handleGenerate} |
| 84 | + /> |
| 85 | + </div> |
| 86 | + |
| 87 | + {/* Stats / Info Panel */} |
| 88 | + <div className="space-y-6"> |
| 89 | + <div className="bg-slate-900 p-6 rounded-xl border border-slate-800"> |
| 90 | + <h2 className="text-xl font-bold text-white mb-4 flex items-center gap-2"> |
| 91 | + <span className="w-2 h-8 bg-neon-green rounded-full"></span> |
| 92 | + How it Works |
| 93 | + </h2> |
| 94 | + <div className="space-y-4 text-sm text-slate-400"> |
| 95 | + <p> |
| 96 | + <strong className="text-white">1. The Router:</strong> A learned gating network that predicts which experts are best suited for each token. |
| 97 | + </p> |
| 98 | + <p> |
| 99 | + <strong className="text-white">2. Sparse Activation:</strong> Instead of using all parameters (Dense), we only use the Top-{topK} experts. This saves massive compute. |
| 100 | + </p> |
| 101 | + <p> |
| 102 | + <strong className="text-white">3. Load Balancing:</strong> Ideally, tokens are spread evenly. If one expert gets too many, it becomes a bottleneck (Expert Collapse). |
| 103 | + </p> |
| 104 | + </div> |
| 105 | + |
| 106 | + <div className="mt-6"> |
| 107 | + <GatingPanel numExperts={numExperts} topK={topK} /> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + |
| 111 | + <LoadBalancingPanel numExperts={numExperts} loads={expertLoads} /> |
| 112 | + </div> |
| 113 | + </main> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
0 commit comments