figures / unit6-sync-async.html
sergiopaniego's picture
sergiopaniego HF Staff
Brand teal accent, readable text colors, U4 nudge redesign, U7 figures
39824c1 verified
Raw History Blame Contribute Delete
12 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Synchronous vs asynchronous RL</title>
<!-- Generated by build.py from fragments/. Runtime and theme tokens come from the research-article template. -->
<link rel="stylesheet" href="runtime/variables.css">
<script>
// The iframe can't read the course page's theme, so follow ?theme= if given, else the OS setting.
(function () {
const forced = new URLSearchParams(location.search).get('theme');
const mq = matchMedia('(prefers-color-scheme: dark)');
const apply = () => document.documentElement.setAttribute('data-theme', forced || (mq.matches ? 'dark' : 'light'));
apply();
if (!forced) mq.addEventListener('change', apply);
})();
</script>
<script src="runtime/color-palettes.js"></script>
<script src="runtime/rough.js"></script>
<script src="runtime/hand.js"></script>
<script src="runtime/embed-runtime.js"></script>
<style>
html, body { margin: 0; padding: 0; }
/* An opaque card with its own background, so it stays readable whatever theme the course page uses */
body { background: transparent; }
.figure-card { font-family: 'Source Sans 3', var(--default-font-family); color: var(--text-color); background: var(--page-bg);
border: 1px solid var(--border-color); border-radius: 10px; padding: 12px 14px 10px; }
</style>
<script>
// Clean look by default, in Source Sans. ?style=hand brings back the old hand-drawn look for comparison:
// it loads Excalifont and uses it for the whole card.
if (new URLSearchParams(location.search).get('style') === 'hand') {
const st = document.createElement('style');
st.textContent = "@font-face { font-family: 'Excalifont'; src: url('runtime/fonts/Excalifont-Regular.woff2') format('woff2'); font-display: swap; }" +
".figure-card { font-family: 'Excalifont', 'Source Sans 3', var(--default-font-family); }";
document.head.appendChild(st);
} else {
document.documentElement.classList.add('style-clean');
const l = document.createElement('link'); l.rel = 'stylesheet';
l.href = 'https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@400;600;700&display=swap';
document.head.appendChild(l);
}
</script>
</head>
<body>
<div class="figure-card">
<!-- title: Synchronous vs asynchronous RL -->
<div class="fig-sync-async"></div>
<style>
.fig-sync-async { position: relative; color: var(--text-color); font-size: 14px; }
.fig-sync-async svg { display: block; width: 100%; overflow: visible; }
.fig-sync-async .sa-head { margin: 0 0 6px; line-height: 1.4; font-size: 15px; }
.fig-sync-async .sa-head span { color: var(--muted-color); }
.fig-sync-async .sa-section + .sa-section { margin-top: 14px; }
.fig-sync-async .sa-foot { display: flex; align-items: flex-start; gap: 12px; margin-top: 10px; }
.fig-sync-async .sa-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; flex: 1; font-size: 14px; color: var(--muted-color); }
.fig-sync-async .sa-legend span::before {
content: ""; display: inline-block; width: 14px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px;
background: var(--sw); border: 1.5px solid var(--sb, transparent);
}
.fig-sync-async .sa-legend span.sa-dash::before { border-style: dashed; }
.fig-sync-async .sa-legend span.sa-arrow::before { content: "\2191"; width: auto; height: auto; border: 0; background: none; color: var(--sw); margin-right: 6px; }
.fig-sync-async button {
font: inherit; font-size: 14px; height: 30px; padding: 0 12px; border-radius: 6px; cursor: pointer;
color: var(--text-color); background: var(--surface-bg); border: 1px solid var(--border-color);
}
.fig-sync-async button:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
</style>
<script>
Embed.mount('fig-sync-async', (ctx) => {
const NS = 'http://www.w3.org/2000/svg';
// Time is normalized to [0, 1]. Synchronous: generate (8 episodes, the slowest decides), then train.
const GEN = 0.242, TRAIN = 0.137;
const LENGTHS = [0.42, 0.55, 0.3, 0.95, 0.48, 1.0, 0.36, 0.62];
const SYNC_GENS = [0, GEN + TRAIN, 2 * (GEN + TRAIN)];
const SYNC_TRAINS = [GEN, 2 * GEN + TRAIN];
// Asynchronous: each of the 8 slots starts a new episode as soon as the previous one ends.
let seed = 7;
const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const ASYNC_BARS = [];
for (let row = 0; row < 8; row++) {
let x = rand() * 0.032;
while (x < 1) { const len = 0.056 + rand() * 0.17; ASYNC_BARS.push({ row, x0: x, x1: Math.min(1, x + len) }); x += len + 0.005; }
}
// async: the first step starts once the first episodes are done, then 7 steps fill the lane to the end
const firstDone = Math.min(...ASYNC_BARS.filter((b) => b.x0 < 0.04).map((b) => b.x1)) + 0.01;
const STEP_GAP = 0.0097, ASYNC_TRAIN_W = (1 - firstDone - 6 * STEP_GAP) / 7;
const ASYNC_TRAINS = Array.from({ length: 7 }, (_, i) => firstDone + i * (ASYNC_TRAIN_W + STEP_GAP));
const SWEEP_MS = 9000, HOLD_MS = 6500;
const sections = [
{ key: 'sync', title: 'Synchronous.', sub: '8 episodes start together, and the trainer waits for the longest one.' },
{ key: 'async', title: 'Asynchronous.', sub: 'A new episode starts as soon as one ends, and the trainer takes what is ready.' },
].map((s) => {
const div = document.createElement('div');
div.className = 'sa-section';
const p = document.createElement('p');
p.className = 'sa-head';
const b = document.createElement('strong');
b.textContent = s.title;
const sp = document.createElement('span');
sp.textContent = ' ' + s.sub;
p.append(b, sp);
const svg = document.createElementNS(NS, 'svg');
div.append(p, svg);
ctx.root.appendChild(div);
return { ...s, svg };
});
const legend = document.createElement('div');
legend.className = 'sa-legend';
const play = document.createElement('button');
play.type = 'button';
play.addEventListener('click', () => setPlaying(!playing), { signal: ctx.signal });
const foot = document.createElement('div');
foot.className = 'sa-foot';
foot.append(legend, play);
ctx.root.appendChild(foot);
ctx.describe('Two timelines of the same work. In synchronous RL, the trainer GPUs wait while 8 episodes are generated and the vLLM GPUs wait while the trainer trains. In asynchronous RL, episodes stream continuously and training steps run back to back, so neither side sits idle.');
let width = 600, now = ctx.reducedMotion ? SWEEP_MS + HOLD_MS : 0, playing = !ctx.reducedMotion, clock = 0, last = null, C = null;
function setPlaying(p) { playing = p; play.textContent = p ? 'Pause' : 'Play'; }
setPlaying(playing);
if (ctx.reducedMotion) play.style.display = 'none';
function draw() {
if (!C) return;
const t = Math.min(1, now / SWEEP_MS);
const markOp = Math.max(0, Math.min(1, (now - SWEEP_MS) / 600));
const narrow = width < 560;
const labelW = narrow ? 0 : 100;
const x0 = labelW, x1 = width - 2, X = (u) => x0 + u * (x1 - x0);
const above = narrow ? 20 : 0; // on phones: vLLM label above its lane, trainer label below its lane
const VH = 66, TH = 36, GAP = 12;
const vY = above, tY = vY + VH + GAP, below = narrow ? 20 : 0;
const grown = (a, b) => Math.max(0, Math.min(b, t) - a);
sections.forEach((s, si) => {
const lastSec = si === sections.length - 1;
const H = tY + TH + below + (lastSec ? 34 : 4);
const svg = s.svg;
svg.replaceChildren();
svg.setAttribute('viewBox', `0 0 ${width} ${H}`);
svg.setAttribute('height', H);
// every shape gets a fixed seed (by its role and index), so frames don't jitter as shapes appear
const h = Hand.create(svg, { seed: 11 + si });
const sd = (k) => (si + 1) * 100000 + k;
// lanes
[['vLLM GPUs', vY, VH], ['trainer GPUs', tY, TH]].forEach(([label, y, lh], k) => {
h.rect(x0, y, x1 - x0, lh, { stroke: C.border, width: 1.2, seed: sd(k) });
if (narrow) h.text(x0 + 2, y === vY ? y - 6 : y + lh + 16, label, { size: 14, color: C.muted });
else h.text(0, y + lh / 2 + 5, label, { size: 14, color: C.muted });
});
// idle GPUs (drawn under the work): the trainer during generation, vLLM during training
if (s.key === 'sync' && markOp > 0) {
const idle = (a, b, y, lh, k) => h.rect(X(a) + 2, y + 2, X(b) - X(a) - 4, lh - 4, {
fill: C.redSoft, stroke: C.red, width: 1.4, dashed: true, seed: sd(10 + k) }).setAttribute('opacity', markOp);
SYNC_GENS.forEach((g, k) => idle(g, g + GEN, tY, TH, k));
SYNC_TRAINS.forEach((a, k) => idle(a, a + TRAIN, vY, VH, 5 + k));
}
const barGap = (VH - 8 * 4) / 9;
const bar = (a, b, row, k) => {
const w = grown(a, b);
if (w <= 0) return;
const y = vY + barGap + row * (4 + barGap) + 2;
h.line(X(a) + 1, y, Math.max(X(a) + 2, X(a + w) - 1), y, { stroke: C.purple, width: 3.4, roughness: 0.6, bowing: 0.3, seed: sd(1000 + k) });
};
const train = (a, b, k) => {
const w = grown(a, b);
if (w <= 0) return;
h.rect(X(a) + 3, tY + 5, Math.max(2, X(a + w) - X(a) - 6), TH - 10, { fill: C.greySoft, stroke: C.grey, width: 1.3, seed: sd(500 + k) });
if (t >= b && X(b) - X(a) > 44) h.text((X(a) + X(b)) / 2, tY + TH / 2 + 5, 'train', { size: 14, anchor: 'middle', color: C.ink });
};
if (s.key === 'sync') {
SYNC_GENS.forEach((g, gi) => LENGTHS.forEach((l, i) => bar(g, g + GEN * l, i, gi * 8 + i)));
SYNC_TRAINS.forEach((a, k) => train(a, a + TRAIN, k));
} else {
ASYNC_BARS.forEach((b, k) => bar(b.x0, b.x1, b.row, k));
ASYNC_TRAINS.forEach((a, k) => {
train(a, a + ASYNC_TRAIN_W, k);
if (t >= a + ASYNC_TRAIN_W) {
const ax = X(a + ASYNC_TRAIN_W) - 6;
h.arrow([[ax, tY + 3], [ax, vY + VH + 1]], { stroke: C.purple, width: 1.6, head: 6, seed: sd(700 + k) });
}
});
}
// the "now" cursor, drawn only inside the lanes so it never crosses a label
if (t < 1) [[vY, VH], [tY, TH]].forEach(([y, lh]) => {
const l = document.createElementNS(NS, 'line');
[['x1', X(t)], ['x2', X(t)], ['y1', y], ['y2', y + lh], ['stroke', C.muted], ['stroke-width', 1.2], ['stroke-dasharray', '3 3']]
.forEach(([k, v]) => l.setAttribute(k, v));
svg.appendChild(l);
});
if (lastSec) {
const ay = tY + TH + below + 14;
h.arrow([[x0, ay], [x1 - 2, ay]], { stroke: C.muted, width: 1.3, head: 9, seed: sd(900) });
h.text(x1 - 14, ay + 18, 'time', { size: 14, anchor: 'end', color: C.muted });
}
});
}
function drawLegend() {
legend.replaceChildren();
[['one episode being generated', C.purple, C.purple], ['one training step', C.greySoft, C.grey],
['GPUs sitting idle', C.redSoft, C.red, 'sa-dash'], ['new weights sent to vLLM (async)', C.purple, null, 'sa-arrow']]
.forEach(([txt, sw, sb, cls]) => {
const s = document.createElement('span');
if (cls) s.className = cls;
s.style.setProperty('--sw', sw);
if (sb) s.style.setProperty('--sb', sb);
s.textContent = txt;
legend.appendChild(s);
});
}
ctx.animate((time) => {
// own clock, so Pause freezes the timeline where it is
if (last !== null && playing) clock += time - last;
last = time;
if (!ctx.reducedMotion) now = clock % (SWEEP_MS + HOLD_MS);
draw();
});
return ({ width: w }) => { width = w; C = Hand.colors(ctx.root); drawLegend(); draw(); };
}, { deps: [] });
</script>
</div>
</body>
</html>