File size: 12,036 Bytes
c0eb006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39824c1
c0eb006
 
 
39824c1
 
c0eb006
 
39824c1
 
c0eb006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!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>