Spaces:
Running
Running
Download app.js from thelamapi/Next-Federation: direct link, hf CLI and curl.
- Browser
- Download file 5.43 kB
-
https://huggingface.co/spaces/thelamapi/Next-Federation/resolve/main/app.js
- Command line
-
hf download hf://spaces/thelamapi/Next-Federation/app.js
-
curl -L -o app.js https://huggingface.co/spaces/thelamapi/Next-Federation/resolve/main/app.js
5.43 kB
| /* Next Federation Space: change client skew and dropout, rerun FedAvg / FedProx / local training in the browser. */ | |
| const { $, h, fmt, lineChart, legend, css } = NLUI; | |
| const ALPHAS = [0.05, 0.1, 0.2, 0.3, 0.5, 1, 2, 5, 10]; | |
| const COL = { fedavg: "#2f5fb3", fedprox: "#0b6e4f", local: "#8a8f98" }; | |
| const NAME = { fedavg: "FedAvg", fedprox: "FedProx", local: "Local only" }; | |
| const CLS = ["#4e79a7", "#f28e2b", "#e15759", "#76b7b2", "#59a14f", "#edc948", "#b07aa1", "#ff9da7", "#9c755f", "#bab0ac"]; | |
| let ctx, busy = false, lastRes = null; | |
| const cfg = () => ({ alpha: ALPHAS[Number($("alpha").value)], p_drop: Number($("drop").value), n_clients: Number($("clients").value), | |
| rounds: Number($("rounds").value), seed: Number($("seed").value) }); | |
| function labels() { | |
| const c = cfg(); | |
| $("alpha_v").textContent = c.alpha; $("drop_v").textContent = c.p_drop.toFixed(2); | |
| $("clients_v").textContent = c.n_clients; $("rounds_v").textContent = c.rounds; $("seed_v").textContent = c.seed; | |
| } | |
| function drawDist(parts) { | |
| const cv = $("dist"), W = cv.clientWidth || 500, H = 150, dpr = window.devicePixelRatio || 1; | |
| cv.width = W * dpr; cv.height = H * dpr; cv.style.height = H + "px"; | |
| const g = cv.getContext("2d"); g.scale(dpr, dpr); | |
| const n = parts.length, bw = (W - 30) / n, maxN = Math.max(...parts.map(p => p.train.length + p.test.length), 1); | |
| g.font = "10px system-ui"; g.fillStyle = css("--muted"); | |
| parts.forEach((p, k) => { | |
| const counts = new Array(10).fill(0); for (const i of p.train.concat(p.test)) counts[ctx.y[i]]++; | |
| let y0 = H - 16; | |
| counts.forEach((cnt, c) => { const hh = (H - 26) * cnt / maxN; g.fillStyle = CLS[c]; g.fillRect(30 + k * bw + 2, y0 - hh, bw - 4, hh); y0 -= hh; }); | |
| g.fillStyle = css("--muted"); g.fillText(String(k + 1), 30 + k * bw + bw / 2 - 3, H - 3); | |
| }); | |
| g.fillText(maxN, 2, 12); | |
| } | |
| function drawClients(res) { | |
| const cv = $("bars"), W = cv.clientWidth || 500, H = 170, dpr = window.devicePixelRatio || 1; | |
| cv.width = W * dpr; cv.height = H * dpr; cv.style.height = H + "px"; | |
| const g = cv.getContext("2d"); g.scale(dpr, dpr); | |
| const n = res.fedavg.client_acc.length, gw = (W - 30) / n, ms = ["fedavg", "fedprox", "local"]; | |
| g.strokeStyle = css("--line"); g.fillStyle = css("--muted"); g.font = "10px system-ui"; | |
| for (const v of [0, 0.5, 1]) { const y = H - 16 - (H - 26) * v; g.beginPath(); g.moveTo(28, y); g.lineTo(W, y); g.stroke(); g.fillText(v.toFixed(1), 2, y + 3); } | |
| for (let k = 0; k < n; k++) ms.forEach((m, j) => { | |
| const v = res[m].client_acc[k]; if (v < 0) return; | |
| g.fillStyle = COL[m]; const hh = (H - 26) * v; g.fillRect(30 + k * gw + j * (gw - 4) / 3 + 1, H - 16 - hh, (gw - 4) / 3 - 1, hh); | |
| }); | |
| g.fillStyle = css("--muted"); for (let k = 0; k < n; k++) g.fillText(String(k + 1), 30 + k * gw + gw / 2 - 3, H - 3); | |
| } | |
| function summary(r) { | |
| const acc = r.client_acc.filter(v => v >= 0); | |
| return { global: r.curve[r.curve.length - 1], worst: Math.min(...acc), mean: acc.reduce((a, b) => a + b, 0) / acc.length, mb: r.comm_bytes / 1e6, uploads: r.uploads }; | |
| } | |
| async function runAll() { | |
| if (busy) return lastRes; busy = true; $("run").disabled = true; | |
| const c = cfg(), parts = Core.makeClients(ctx, c.n_clients, c.alpha, c.seed); | |
| drawDist(parts); | |
| const res = {}, curves = {}; | |
| for (const m of ["fedavg", "fedprox", "local"]) { | |
| $("status").textContent = `Training ${NAME[m]} on ${c.n_clients} simulated clients…`; | |
| await new Promise(r => setTimeout(r, 15)); | |
| res[m] = Core.run(ctx.X, ctx.y, parts, ctx.test, m, { ...ctx.hyper, rounds: c.rounds, p_drop: c.p_drop, seed: c.seed }); | |
| curves[m] = res[m].curve; | |
| } | |
| const series = ["fedavg", "fedprox", "local"].map(m => ({ name: NAME[m] + (m === "local" ? " (mean of client models)" : ""), color: COL[m], points: curves[m].map((v, i) => [i + 1, v]) })); | |
| lineChart($("curve"), series, { xlabel: "round", ymin: 0, ymax: 1 }); legend($("cl"), series); | |
| drawClients(res); | |
| const tb = $("tbl"); tb.innerHTML = ""; | |
| tb.append(h("tr", {}, ["Method", "Global test acc", "Worst client", "Mean client", "Traffic (MB)", "Uploads"].map((x, i) => h("th", { class: i ? "num" : "" }, x)))); | |
| for (const m of ["fedavg", "fedprox", "local"]) { | |
| const s = summary(res[m]); | |
| tb.append(h("tr", {}, h("td", {}, NAME[m]), h("td", { class: "num" }, fmt(s.global, 3)), h("td", { class: "num" }, fmt(s.worst, 3)), | |
| h("td", { class: "num" }, fmt(s.mean, 3)), h("td", { class: "num" }, fmt(s.mb, 2)), h("td", { class: "num" }, s.uploads))); | |
| } | |
| $("status").textContent = `Done: α=${c.alpha}, dropout ${c.p_drop}, ${c.n_clients} clients, ${c.rounds} rounds, seed ${c.seed}. Simulation in one browser tab; no real devices.`; | |
| busy = false; $("run").disabled = false; lastRes = res; | |
| return res; | |
| } | |
| window.__liveTest = async function () { | |
| $("alpha").value = "1"; $("drop").value = "0.4"; $("clients").value = "8"; $("rounds").value = "6"; $("seed").value = "9"; labels(); | |
| const res = await runAll(); | |
| return { ok: res.fedavg.curve.length === 6 && res.fedavg.client_acc.length === 8 && $("tbl").rows.length === 4 && res.fedavg.uploads < 6 * 8, | |
| fedavg: summary(res.fedavg), fedprox: summary(res.fedprox), local: summary(res.local) }; | |
| }; | |
| NLUI.boot(Core, async c => { | |
| ctx = c; | |
| for (const id of ["alpha", "drop", "clients", "rounds", "seed"]) $(id).addEventListener("input", labels); | |
| $("run").addEventListener("click", runAll); | |
| labels(); | |
| await runAll(); | |
| }); | |