figures / unit6-capture-proxy.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
14.4 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The capture proxy</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: The capture proxy -->
<div class="fig-capture"></div>
<style>
.fig-capture { position: relative; color: var(--text-color); font-size: 14px; }
.fig-capture svg { display: block; width: 100%; overflow: visible; }
.fig-capture .fig-controls { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 10px; }
.fig-capture 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-capture button[aria-pressed="true"] { border-color: var(--primary-color); }
.fig-capture button:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
.fig-capture .cp-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 8px; font-size: 14px; color: var(--muted-color); }
.fig-capture .cp-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-capture .fig-note { margin: 8px 0 0; min-height: 4.4em; line-height: 1.45; color: var(--muted-color); }
</style>
<script>
Embed.mount('fig-capture', (ctx) => {
const NS = 'http://www.w3.org/2000/svg';
// One run of the agent, three model calls. Token counts are illustrative, the roles and the mask are not.
const SEGS = [
{ role: 'system', n: 6, call: 1 },
{ role: 'user', n: 4, call: 1 },
{ role: 'assistant', n: 5, call: 1 },
{ role: 'tool', n: 4, call: 2 },
{ role: 'assistant', n: 4, call: 2 },
{ role: 'tool', n: 3, call: 3 },
{ role: 'assistant', n: 3, call: 3 },
];
const SHIFT = 2; // tokens that differ at the start of each model turn when the messages are rendered again
const MAX_CELLS = SEGS.reduce((a, s) => a + s.n, 0) + SHIFT * 3;
let call = 0, rerender = false, width = 640;
const controls = document.createElement('div');
controls.className = 'fig-controls';
const next = document.createElement('button');
next.type = 'button';
next.addEventListener('click', () => { call = call >= 3 ? 0 : call + 1; draw(); }, { signal: ctx.signal });
const toggle = document.createElement('button');
toggle.type = 'button';
toggle.textContent = 'Re-render from messages';
toggle.addEventListener('click', () => { rerender = !rerender; if (rerender && call === 0) call = 3; draw(); }, { signal: ctx.signal });
controls.append(next, toggle);
const svg = document.createElementNS(NS, 'svg');
const legend = document.createElement('div');
legend.className = 'cp-legend';
const note = document.createElement('p');
note.className = 'fig-note';
note.setAttribute('aria-live', 'polite');
ctx.root.append(controls, svg, legend, note);
ctx.describe('An agent such as OpenCode runs in a sandbox and sends each model call to a capture proxy, which forwards it to the vLLM server the trainer controls. vLLM returns the token ids it saw and sampled, with their log-probabilities. The proxy keeps them and gives the agent its reply as text. Step through three calls: each call extends the same token sequence, the model\'s tokens get loss mask 1 and the system prompt, task and tool results get 0. The task\'s test gives the reward when the run ends, and its advantage goes to every token with mask 1. A toggle shows what happens when the saved messages are rendered again with the chat template: two tokens differ where each model turn starts, so the mask and log-probabilities land on the wrong positions.');
function tokens() {
// [{ role, mask, bad }] for the row as captured, or as rendered again from the messages
const out = [];
SEGS.filter((s) => s.call <= call).forEach((s) => {
if (rerender && s.role === 'assistant') for (let i = 0; i < SHIFT; i++) out.push({ role: 'template', extra: true });
for (let i = 0; i < s.n; i++) out.push({ role: s.role });
});
// the mask comes from what vLLM sampled, at the positions vLLM used
const mask = [];
SEGS.filter((s) => s.call <= call).forEach((s) => { for (let i = 0; i < s.n; i++) mask.push(s.role === 'assistant' ? 1 : 0); });
return { cells: out, mask };
}
function draw() {
const C = Hand.colors(ctx.root);
next.textContent = call >= 3 ? 'Start over' : call === 0 ? 'First call' : 'Next call';
toggle.setAttribute('aria-pressed', String(rerender));
svg.replaceChildren();
const h = Hand.create(svg, { seed: 31 });
const narrow = width < 560;
const sd = (k) => 4000 + k;
// --- the three boxes
const boxH = narrow ? 70 : 78, top = 6;
const gap = narrow ? 34 : 70;
const wA = narrow ? Math.round(width * 0.36) : Math.round(width * 0.34);
const wC = narrow ? Math.round(width * 0.24) : Math.round(width * 0.2);
const wB = width - wA - wC - 2 * gap - 4;
const xA = 2, xB = xA + wA + gap, xC = xB + wB + gap;
const active = call > 0 && !rerender;
h.rect(xA, top, wA, boxH, { stroke: C.grey, width: 1.3, dashed: true, seed: sd(1) });
h.text(xA + 8, top + 18, 'sandbox', { size: 14, color: C.muted });
h.rect(xA + 8, top + 26, wA - 16, boxH - 34, { fill: C.greySoft, stroke: C.grey, width: 1.3, seed: sd(2) });
h.text(xA + wA / 2, top + 26 + (boxH - 34) / 2 + 5, 'OpenCode', { size: 14, anchor: 'middle', color: C.ink });
// the proxy is neutral plumbing (grey), vLLM serves the model being trained (purple)
h.rect(xB, top + 10, wB, boxH - 10, { fill: C.greySoft, stroke: C.ink, width: active ? 2 : 1.3, seed: sd(3) });
h.text(xB + wB / 2, top + 10 + (boxH - 10) / 2 - (narrow ? 2 : 3), narrow ? 'capture\nproxy' : 'capture proxy', { size: 14, anchor: 'middle', color: C.ink });
if (!narrow) h.text(xB + wB / 2, top + 10 + (boxH - 10) / 2 + 17, 'keeps the token ids', { size: 14, anchor: 'middle', color: C.muted });
h.rect(xC, top + 10, wC, boxH - 10, { fill: C.purpleSoft, stroke: C.purple, width: 1.3, seed: sd(4) });
h.text(xC + wC / 2, top + 10 + (boxH - 10) / 2 + 5, 'vLLM', { size: 14, anchor: 'middle', color: C.ink });
const yReq = top + 30, yRep = top + boxH - 14;
const arrowCol = active ? C.purple : C.muted, aw = active ? 1.8 : 1.2;
h.arrow([[xA + wA + 4, yReq], [xB - 4, yReq]], { stroke: arrowCol, width: aw, head: 7, seed: sd(10) });
h.arrow([[xB - 4, yRep], [xA + wA + 4, yRep]], { stroke: arrowCol, width: aw, head: 7, seed: sd(11) });
h.arrow([[xB + wB + 4, yReq], [xC - 4, yReq]], { stroke: arrowCol, width: aw, head: 7, seed: sd(12) });
h.arrow([[xC - 4, yRep], [xB + wB + 4, yRep]], { stroke: arrowCol, width: aw, head: 7, seed: sd(13) });
if (!narrow) {
h.text(xA + wA + gap / 2, yReq - 6, 'messages', { size: 13, anchor: 'middle', color: C.muted });
h.text(xA + wA + gap / 2, yRep + 17, 'text', { size: 13, anchor: 'middle', color: C.muted });
h.text(xB + wB + gap / 2, yReq - 6, 'messages', { size: 13, anchor: 'middle', color: C.muted });
h.text(xB + wB + gap / 2, yRep + 17, 'token ids', { size: 13, anchor: 'middle', color: C.muted });
}
// --- the captured row
const { cells, mask } = tokens();
const titleY = top + boxH + (narrow ? 30 : 46);
const rowTop = titleY + (narrow ? 14 : 14);
const labelW = narrow ? 0 : 92;
const cw = Math.min(20, Math.floor((width - labelW - 4) / MAX_CELLS));
const ch = 20, x0 = labelW;
const yTok = rowTop + (narrow ? 18 : 0), yMask = yTok + ch + (narrow ? 22 : 8);
const lab = (y, txt) => narrow ? h.text(x0, y - 6, txt, { size: 14, color: C.muted }) : h.text(0, y + 15, txt, { size: 14, color: C.muted });
lab(yTok, rerender ? 'tokens, again' : 'tokens');
lab(yMask, 'loss mask');
h.text(0, titleY, call === 0 ? 'Nothing captured yet' : `${rerender ? 'Rendered again from the messages' : 'Captured by the proxy'}, after call ${call}`,
{ size: 14, color: C.ink });
if (call === 0) {
[yTok, yMask].forEach((y, k) => h.rect(x0, y, cw * MAX_CELLS, ch, { stroke: C.border, width: 1, dashed: true, seed: sd(20 + k) }));
}
cells.forEach((c, i) => {
const x = x0 + i * cw;
const col = c.extra ? { stroke: C.red, fill: C.redSoft } : Hand.role(C, c.role);
h.rect(x + 1, yTok, cw - 2, ch, { fill: col.fill, stroke: col.stroke, width: c.extra ? 1.6 : 1, roughness: 0.5, radius: 3, seed: sd(100 + i) });
});
mask.forEach((m, i) => {
const x = x0 + i * cw;
const under = cells[i];
const wrong = rerender && under && ((m === 1 && under.role !== 'assistant') || (m === 0 && under.role === 'assistant'));
h.rect(x + 1, yMask, cw - 2, ch, {
fill: m ? C.purpleSoft : 'none', stroke: wrong ? C.red : m ? C.purple : C.border, width: wrong ? 2 : 1,
roughness: 0.5, radius: 3, seed: sd(300 + i) });
if (cw >= 11) h.text(x + cw / 2, yMask + 15, String(m), { size: cw >= 15 ? 13 : 11, anchor: 'middle', color: wrong ? C.redText : C.ink });
});
// the model's tokens that the shifted mask no longer covers
if (rerender) cells.forEach((c, i) => {
if (c.role === 'assistant' && mask[i] !== 1) {
h.rect(x0 + i * cw + 1, yTok, cw - 2, ch, { stroke: C.red, width: 2, roughness: 0.5, radius: 3, seed: sd(600 + i) });
}
});
let H = yMask + ch + 10;
if (call === 3) {
const y = H + 18;
h.text(0, y, rerender ? (narrow ? 'reward 1, advantage on wrong positions' : 'reward from the task\'s test: 1, but its advantage lands on the wrong positions')
: (narrow ? 'reward 1, advantage on every mask 1 token' : 'reward from the task\'s test: 1, and its advantage goes to every token with mask 1'),
{ size: 14, color: rerender ? C.redText : C.greenText });
H = y + 10;
}
svg.setAttribute('viewBox', `0 0 ${width} ${H}`);
svg.setAttribute('height', H);
legend.replaceChildren();
[['system prompt', 'system'], ['task', 'user'], ['model', 'assistant'], ['tool result', 'tool']].forEach(([txt, r]) => {
const col = Hand.role(C, r);
const s = document.createElement('span');
s.style.setProperty('--sw', col.fill);
s.style.setProperty('--sb', col.stroke);
s.textContent = txt;
legend.appendChild(s);
});
if (rerender) {
const s = document.createElement('span');
s.style.setProperty('--sw', C.redSoft);
s.style.setProperty('--sb', C.red);
s.textContent = 'differs from what vLLM saw';
legend.appendChild(s);
}
const NOTES = [
'OpenCode runs in its sandbox and sends each model call to the URL it was given, which is the proxy. Press First call.',
'Call 1. The proxy forwards the messages to vLLM, which renders them with the chat template, samples a tool call and returns the token ids with their log-probabilities. The proxy keeps them and gives OpenCode the reply as text.',
'Call 2. The tool ran in the sandbox and its output is now in the prompt. The prompt vLLM returns starts with every token of call 1, so the sequence grows. Tool output is context, with loss mask 0.',
'Call 3, the last. The task\'s test scores the sandbox. The other runs of the same task, each in its own sandbox, make up the GRPO group, and the advantage computed from this run\'s reward goes to every token with mask 1.',
];
note.textContent = rerender
? `Rendering the saved messages again gives ${SHIFT} different tokens where each model turn starts (red, token counts illustrative). The mask and log-probabilities from vLLM now sit on the wrong positions. OpenEnv measured this on Qwen3.5-4B: 0 of 28 turns matched.`
: NOTES[call];
}
return ({ width: w }) => { width = w; draw(); };
}, { deps: [] });
</script>
</div>
</body>
</html>