File size: 13,482 Bytes
c0eb006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39824c1
c0eb006
 
 
39824c1
 
c0eb006
 
39824c1
 
c0eb006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fadc379
e7f17af
c0eb006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39824c1
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How the pieces fit together during training</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: How the pieces fit together during training -->
<div class="fig-training-loop"></div>
<style>
  .fig-training-loop { position: relative; color: var(--text-color); font-size: 14px; }
  .fig-training-loop svg { display: block; width: 100%; overflow: visible; }
  .fig-training-loop .fig-note { margin: 8px 0 0; color: var(--muted-color); line-height: 1.45; }
  .fig-training-loop .fig-controls { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
  .fig-training-loop 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-training-loop button[aria-pressed="true"] { border-color: var(--primary-color); }
  .fig-training-loop button:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
</style>
<script>
  Embed.mount('fig-training-loop', (ctx) => {
    const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    const note = document.createElement('p');
    note.className = 'fig-note';
    const NOTES = {
      sft: 'SFT (Unit 2): before training, a stronger teacher runs the tasks once in the harness, and the tests keep only the traces that pass. The trainer then teaches the student to write those recorded traces. The student never runs during collection.',
      distill: "On-policy distillation (Unit 3): the same loop as RL, with a different signal. The student writes an attempt and the teacher gives its probability for every token the student wrote. Unit 3's hands-on trains the first turn only. Distilling whole episodes in the live Python session scored the same on MBPP, at more cost.",
      rl: 'RL (Units 4 to 6): the model being trained writes its own attempts and a check turns each one into a single number, the reward. In Unit 4 the environment only runs the code and the tests to score it. From Unit 5 on, it runs the tool calls during the rollout and grades the answer.',
    };
    let mode = ['sft', 'distill', 'rl'].includes(new URLSearchParams(location.search).get('mode')) ? new URLSearchParams(location.search).get('mode') : 'rl';
    const controls = document.createElement('div');
    controls.className = 'fig-controls';
    const buttons = {};
    [['sft', 'SFT'], ['distill', 'Distillation'], ['rl', 'RL']].forEach(([key, label]) => {
      const b = document.createElement('button');
      b.type = 'button';
      b.textContent = label;
      b.addEventListener('click', () => { mode = key; ctx.rerender(); }, { signal: ctx.signal });
      buttons[key] = b;
      controls.appendChild(b);
    });
    ctx.root.append(controls, svg, note);
    ctx.describe('The training loop. Inside a rollout, run by the harness, the policy (the model being trained) sends a tool call to the environment (a Python session) and gets a result back. The rollouts (trajectories) get a reward (tests passed). The trainer (TRL) uses them to update the weights of the policy, and the loop starts again. Buttons switch between SFT, distillation and RL to show what changes.');
    if (document.fonts) document.fonts.load(`14px ${Hand.FONT}`).then(() => ctx.rerender());

    return ({ width: fullW }) => {
      const W = fullW - 4;
      const C = Hand.colors(ctx.root);
      Object.entries(buttons).forEach(([k, b]) => b.setAttribute('aria-pressed', String(k === mode)));
      note.textContent = NOTES[mode];
      svg.replaceChildren();
      const h = Hand.create(svg, { seed: 31 });
      const narrow = W < 560;
      const size = narrow ? 14 : 15;
      const plain = { fill: C.greySoft, stroke: C.ink, width: 1.2 };
      const policy = { fill: C.purpleSoft, stroke: C.purple, width: 1.2 };
      const tool = { fill: C.yellowSoft, stroke: C.yellow, width: 1.2 };
      const reward = { fill: C.greenSoft, stroke: C.green, width: 1.2 };
      const arrowO = { stroke: C.ink, width: 1.3, head: 9 };
      const update = { stroke: C.purple, width: 2.6, head: 12 };
      const small = { size: 13, color: C.muted };
      const box = (x, y, w, bh, label, o, color = C.ink) => {
        h.rect(x, y, w, bh, o);
        const lines = label.split('\n').length, lh = size * 1.25;
        h.text(x + w / 2, y + bh / 2 - (lines - 1) * lh / 2 + size * 0.35, label, { size, anchor: 'middle', color });
      };
      const teacher = { fill: C.tealSoft, stroke: C.teal, width: 1.2 };
      const L = {
        policy: 'Policy\n(the model\nbeing trained)', env: 'Environment\n(Python session)',
        rollouts: 'Rollouts\n(trajectories)', reward: 'Reward: one\nnumber per rollout', trainer: 'GRPOTrainer\n(TRL)',
      };
      let policyStyle = policy, signalStyle = reward;
      if (mode === 'sft') {
        Object.assign(L, { policy: 'Teacher\n(a stronger\nmodel)', rollouts: 'Recorded\ntraces', reward: 'Filter: keep\nif tests pass', trainer: 'SFTTrainer\n(TRL)' });
        policyStyle = teacher; signalStyle = plain;
      } else if (mode === 'distill') {
        Object.assign(L, { policy: 'Policy\n(the student)', rollouts: 'Student\nattempts', reward: 'Teacher: a score\nfor every token', trainer: 'Distillation\nTrainer (TRL)' });
        signalStyle = teacher;
      }
      const sft = mode === 'sft';
      // In Unit 3 only the first turn is trained: no environment, no tool runs.
      const unused = mode === 'distill';
      const updateLabel = sft ? "train the student" : 'update the weights';
      const REGION = { sft: "Before training, once: the teacher's rollouts, in our loop", distill: "The student's attempt (its first turn, in Unit 3)", rl: 'Rollout, run by our loop (a white-box harness)' };
      const REGION_NARROW = { sft: 'Before training, once', distill: "Student's first turn", rl: 'Our loop (white box)' };
      let H;

      if (!narrow) {
        const pad = 20, gIn = 90, gOut = 64, bw = Math.min(190, (W - 2 * pad - gIn - gOut) / 3), bh = 80;
        const regionW = 2 * pad + 2 * bw + gIn;
        const xP = pad, xE = pad + bw + gIn, xR = regionW + gOut;
        const yTop = 46, regionH = yTop + bh + pad;
        h.rect(0, 0, regionW, regionH, { stroke: C.muted, width: 1.2, dashed: true });
        h.text(14, 26, REGION[mode], { size: 15, color: C.muted });
        box(xP, yTop, bw, bh, L.policy, policyStyle);
        if (unused) {
          // Same slot as the environment in the other views, so the three views line up when you switch.
          box(xE, yTop, bw, bh, 'No tool runs\n(first turn only)', { fill: 'none', stroke: C.muted, width: 1.1, dashed: true }, C.muted);
          h.arrow([[xP + bw + 6, yTop + bh / 2], [xE - 6, yTop + bh / 2]], { stroke: C.muted, width: 1.1, head: 8, dashed: true });
        } else {
          box(xE, yTop, bw, bh, L.env, tool);
          const midIn = xP + bw + gIn / 2;
          h.arrow([[xP + bw + 6, yTop + 24], [xE - 6, yTop + 24]], arrowO);
          h.text(midIn, yTop + 14, 'tool call', { ...small, anchor: 'middle' });
          h.arrow([[xE - 6, yTop + bh - 24], [xP + bw + 6, yTop + bh - 24]], arrowO);
          h.text(midIn, yTop + bh - 2, 'result', { ...small, anchor: 'middle' });
        }
        box(xR, yTop, bw, bh, L.rollouts, plain);
        h.arrow([[regionW + 6, yTop + bh / 2], [xR - 6, yTop + bh / 2]], arrowO);
        const y2 = regionH + 56;
        box(xR, y2, bw, bh, L.reward, signalStyle);
        h.arrow([[xR + bw / 2, yTop + bh + 6], [xR + bw / 2, y2 - 6]], arrowO);
        const xT = xE - 20;
        box(xT, y2, bw, bh, L.trainer, plain);
        h.arrow([[xR - 6, y2 + bh / 2], [xT + bw + 6, y2 + bh / 2]], update);
        if (sft) h.text((xR + xT + bw) / 2, y2 + bh / 2 - 10, 'fixed dataset', { size: 13, color: C.muted, anchor: 'middle' });
        if (sft) {
          const sw = Math.min(bw, xT - 90 - xP);
          box(xP, y2, sw, bh, 'Student\n(the model\nbeing trained)', policy);
          h.arrow([[xT - 6, y2 + bh / 2], [xP + sw + 6, y2 + bh / 2]], update);
          h.text((xT + xP + sw) / 2, y2 + bh / 2 + 24, 'train', { size: 13, color: C.purpleText, anchor: 'middle' });
          H = y2 + bh + 10;
        } else {
          const yLoop = y2 + bh + 40, tx = xT + bw / 2, px = xP + bw / 2;
          h.line(tx, y2 + bh + 6, tx, yLoop, update);
          h.line(tx, yLoop, px, yLoop, update);
          h.arrow([[px, yLoop], [px, yTop + bh + 6]], update);
          h.text((tx + px) / 2, yLoop + 22, updateLabel, { size: 14, color: C.purpleText, anchor: 'middle' });
          H = yLoop + 30;
        }
      } else {
        const pad = 12, gIn = Math.max(64, W * 0.17), bw = (W - 2 * pad - gIn) / 2, bh = 76;
        const xP = pad, xE = pad + bw + gIn;
        const yTop = 40, regionH = yTop + bh + pad + 4;
        h.rect(0, 0, W, regionH, { stroke: C.muted, width: 1.2, dashed: true });
        h.text(12, 24, REGION_NARROW[mode], { size: 14, color: C.muted });
        box(xP, yTop, bw, bh, L.policy, policyStyle);
        if (unused) {
          box(xE, yTop, bw, bh, 'No tool runs\n(first turn\nonly)', { fill: 'none', stroke: C.muted, width: 1.1, dashed: true }, C.muted);
          h.arrow([[xP + bw + 4, yTop + bh / 2], [xE - 4, yTop + bh / 2]], { stroke: C.muted, width: 1.1, head: 8, dashed: true });
        } else {
          box(xE, yTop, bw, bw < 150 ? bh : bh, bw < 150 ? 'Environment\n(Python\nsession)' : L.env, tool);
          const midIn = xP + bw + gIn / 2;
          h.arrow([[xP + bw + 4, yTop + 24], [xE - 4, yTop + 24]], { ...arrowO, head: 8 });
          h.text(midIn, yTop + 14, 'tool call', { ...small, anchor: 'middle' });
          h.arrow([[xE - 4, yTop + bh - 24], [xP + bw + 4, yTop + bh - 24]], { ...arrowO, head: 8 });
          h.text(midIn, yTop + bh - 2, 'result', { ...small, anchor: 'middle' });
        }
        // Right column: rollouts, then reward. Left column: trainer, which feeds back up to the policy.
        const yR = regionH + 44, bh2 = 62, ex = xE + bw / 2, px = xP + bw / 2;
        h.arrow([[ex, regionH + 5], [ex, yR - 5]], arrowO);
        box(xE, yR, bw, bh2, L.rollouts, plain);
        const yW = yR + bh2 + 40;
        h.arrow([[ex, yR + bh2 + 5], [ex, yW - 5]], arrowO);
        box(xE, yW, bw, bh2, L.reward, signalStyle);
        box(xP, yW, bw, bh2, L.trainer, plain);
        h.arrow([[xE - 5, yW + bh2 / 2], [xP + bw + 5, yW + bh2 / 2]], update);
        if (sft) h.text((xE + xP + bw) / 2, yW + bh2 / 2 - 8, 'dataset', { size: 12, color: C.muted, anchor: 'middle' });
        if (sft) {
          const yS = yW + bh2 + 44;
          h.arrow([[px, yW + bh2 + 5], [px, yS - 5]], update);
          h.text(px + 12, yW + bh2 + 28, updateLabel, { size: 13, color: C.purpleText });
          box(xP, yS, bw, bh2, 'Student\n(being trained)', policy);
          H = yS + bh2 + 4;
        } else {
          h.arrow([[px, yW - 5], [px, yTop + bh + 5]], update);
          h.text(px + 12, (yTop + bh + yW) / 2 + 14, 'update\nthe weights', { size: 13, color: C.purpleText });
          H = yW + bh2 + 4;
        }
      }
      svg.setAttribute('viewBox', `-2 0 ${fullW} ${H}`);
      svg.setAttribute('height', H);
    };
  }, { deps: [] });
</script>

</div>
</body>
</html>