Spaces:
Running
Running
File size: 17,955 Bytes
411dd91 | 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Client, server and sessions</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: Client, server and sessions -->
<div class="fig-client-server"></div>
<style>
.fig-client-server { position: relative; color: var(--text-color); font-size: 14px; }
.fig-client-server svg { display: block; width: 100%; overflow: visible; }
.fig-client-server svg .cs-hit { cursor: pointer; outline: none; }
.fig-client-server .fig-note { margin: 10px 0 0; line-height: 1.45; font-size: 15px; }
</style>
<script>
Embed.mount('fig-client-server', (ctx) => {
const NS = 'http://www.w3.org/2000/svg';
// The layers, with the names and order of the list on the OpenEnv page ("What runs the code"),
// from the strongest. Checked against envs/data_analysis_env/server/sandbox.py at 0c9f9cca and
// the server block of scripts/unit5/train_grpo_data.py (server as root, uids from 61000).
const LAYERS = [
{ name: 'A user per session', note: "The code can't read the trainer's or the server's environment or memory, files only root can read, or another session's files." },
{ name: 'No sockets', note: "The code can't open a connection to anything, on the internet or on the same machine." },
{ name: 'Limits and a clean environment', note: "No call runs past 30 seconds or past the session's memory cap, and no token in the server's environment reaches the code." },
{ name: 'An audit hook', note: "Python itself refuses reads outside the task's files, sockets and other known escapes, as an extra check on top of the others." },
];
const UID = 61000;
let active = 0, width = 640, C = null;
const svg = document.createElementNS(NS, 'svg');
const note = document.createElement('p');
note.className = 'fig-note';
note.setAttribute('aria-live', 'polite');
ctx.root.append(svg, note);
ctx.describe('An HF Job with two processes. On the left, the training process, with the TRL trainer and vLLM, is the only process that holds HF_TOKEN. It opens one client per episode. Each client has its own WebSocket connection to the environment server, and each connection gets its own session. On the right, the environment server, started inside the same job and running as root, has three sessions. Each session runs in its own worker process as its own user, uid 61000, 61001 and 61002, with its Python session and the task\'s files. The third session shows the four safety layers from the page, from the strongest. 1, a user per session: the code can\'t read the trainer\'s or the server\'s environment or memory, files only root can read, or another session\'s files. 2, no sockets: the code can\'t open a connection to anything. 3, limits and a clean environment: a 30-second timeout per call, a memory cap per session, and no token from the server\'s environment. 4, an audit hook: Python refuses reads outside the task\'s files, sockets and other escapes, as an extra check. Below the job, a note says the same client code can also connect to a Space on the Hub by URL.');
const measure = document.createElement('canvas').getContext('2d');
const textW = (s, size, weight = 400) => { measure.font = `${weight} ${size}px ${Hand.FONT}`; return measure.measureText(s).width; };
function wrap(text, maxW, size, weight) {
const out = [];
text.split(' ').forEach((w) => {
const last = out[out.length - 1];
if (last && textW(last + ' ' + w, size, weight) <= maxW) out[out.length - 1] = last + ' ' + w;
else out.push(w);
});
return out;
}
function setActive(k) {
if (k === active) return;
active = k;
paintActive();
}
function writeNote(k) {
note.replaceChildren();
const strong = document.createElement('strong');
strong.textContent = `${k + 1}. ${LAYERS[k].name}.`;
note.append(strong, ' ' + LAYERS[k].note);
}
let hiBack = null, hiFront = null, shells = [];
function paintActive() {
if (!hiBack) return;
hiBack.replaceChildren();
hiFront.replaceChildren();
const s = shells[active];
Hand.create(hiBack, { seed: 90 }).rect(s.x, s.y, s.w, s.h, { fill: C.yellowSoft, stroke: 'none', seed: 9001 });
Hand.create(hiFront, { seed: 91 }).rect(s.x, s.y, s.w, s.h, { stroke: C.ink, width: 2, seed: 9002 });
shells.forEach((sh, k) => sh.label.setAttribute('font-weight', k === active ? 700 : 400));
writeNote(active);
}
function draw() {
if (!C) return;
const narrow = width < 600;
const pad = narrow ? 8 : 12; // job frame to the boxes inside it
const inset = 10; // box edge to its content
const lh = 17;
const tool = Hand.role(C, 'tool'), model = Hand.role(C, 'assistant');
const y0 = 30; // under the job label
// --- geometry -------------------------------------------------------------------------
let T, S, gapW = 0; // trainer box, server box
if (narrow) {
T = { x: pad, w: width - 2 * pad };
S = { x: pad, w: width - 2 * pad };
} else {
const inner = width - 2 * pad;
T = { x: pad, w: Math.round(inner * 0.25) };
gapW = Math.round(inner * 0.16);
S = { x: T.x + T.w + gapW, w: inner - T.w - gapW };
}
const tLines = wrap('TRL trainer and vLLM', T.w - 2 * inset, 13);
const tokenW = Math.min(T.w - 2 * inset, textW('HF_TOKEN, only here', 12.5, 600) + 16);
const tHeadH = 26 + tLines.length * lh + 8 + 24; // title, lines, token chip
const sLines = wrap('started inside the same job, as root', S.w - 2 * inset, 13);
const sHeadH = 26 + sLines.length * lh + 6;
// sessions: two compact ones and one with its layers
const sx = S.x + inset, sw = S.w - 2 * inset;
const compactH = 14 + 3 * lh + 4;
const shellTop = 22, shellSide = 9, shellBottom = 9;
const shellW1 = sw - 2 * 8;
const coreW = shellW1 - 2 * 3 * shellSide - 16;
const stack = coreW < 300;
const coreH = stack ? 2 * 28 + 6 + 4 : 28 + 4;
const shellH1 = 4 * shellTop + 3 * shellBottom + coreH + shellBottom;
const detailH = 46 + shellH1 + 8;
const clientH = 40;
let tTop, tH, sTop, sH, comp = [], det, clients = [];
if (narrow) {
tTop = y0;
const colW = (T.w - 2 * inset - 2 * 8) / 3;
const cy = tTop + tHeadH + 4;
for (let k = 0; k < 3; k++) clients.push({ x: T.x + inset + k * (colW + 8), y: cy, w: colW, h: clientH });
tH = tHeadH + 4 + clientH + inset;
sTop = tTop + tH + 54;
// the connections come in from the top, so the server's name goes at the bottom of its box
const rowY = sTop + inset;
for (let k = 0; k < 2; k++) comp.push({ x: clients[k].x, y: rowY, w: colW, h: compactH });
det = { x: sx, y: rowY + compactH + 10, w: sw, h: detailH };
sH = det.y + det.h + 4 + sHeadH - sTop;
} else {
tTop = sTop = y0;
const first = sTop + Math.max(sHeadH, tHeadH - 4);
for (let k = 0; k < 2; k++) comp.push({ x: sx, y: first + k * (compactH + 10), w: sw, h: compactH });
det = { x: sx, y: first + 2 * (compactH + 10), w: sw, h: detailH };
sH = tH = det.y + det.h + inset - sTop;
const cx = T.x + inset, cw = T.w - 2 * inset;
comp.forEach((c) => clients.push({ x: cx, y: c.y + c.h / 2 - clientH / 2, w: cw, h: clientH }));
clients.push({ x: cx, y: det.y + 4, w: cw, h: clientH });
}
const jobBottom = (narrow ? sTop + sH : tTop + tH) + pad;
const spaceLines = narrow ? wrap('same client code, connected by URL', width - 2 * pad - 2 * inset, 13) : ['same client code, connected by URL'];
const spaceY = jobBottom + 16, spaceH = 26 + spaceLines.length * lh + 4;
const H = spaceY + spaceH + 4;
svg.replaceChildren();
svg.setAttribute('viewBox', `0 0 ${width} ${H}`);
svg.setAttribute('height', H);
hiBack = document.createElementNS(NS, 'g');
const base = document.createElementNS(NS, 'g'), over = document.createElementNS(NS, 'g');
hiFront = document.createElementNS(NS, 'g');
const hits = document.createElementNS(NS, 'g');
// the highlight's fill goes between the session's background and its layers
svg.append(base, hiBack, over, hiFront, hits);
const h = Hand.create(base, { seed: 57 }), ho = Hand.create(over, { seed: 58 });
// --- the job ------------------------------------------------------------------------
h.rect(1, 1, width - 2, jobBottom - 1, { stroke: C.muted, width: 1.2, dashed: true });
h.text(pad, 20, 'HF Job', { size: 15, weight: 600, color: C.ink });
// --- the training process -------------------------------------------------------------
h.rect(T.x, tTop, T.w, tH, { fill: model.fill, stroke: model.stroke, width: 1.2 });
h.text(T.x + inset, tTop + 22, 'Training process', { size: 15, weight: 600, color: C.ink });
tLines.forEach((l, k) => h.text(T.x + inset, tTop + 22 + (k + 1) * lh + 2, l, { size: 13, color: C.ink }));
const chipY = tTop + 26 + tLines.length * lh + 8;
h.rect(T.x + inset, chipY, tokenW, 22, { fill: C.redSoft, stroke: C.red, width: 1.1 });
h.text(T.x + inset + 8, chipY + 15.5, 'HF_TOKEN, only here', { size: 12.5, weight: 600, color: C.redText });
clients.forEach((c, k) => {
h.rect(c.x, c.y, c.w, c.h, { fill: C.bg, stroke: model.stroke, width: 1.1 });
h.text(c.x + 8, c.y + 17, 'client', { size: 13.5, weight: 600, color: C.ink });
h.text(c.x + 8, c.y + 33, `episode ${k + 1}`, { size: 12.5, color: C.muted });
});
// --- the environment server -----------------------------------------------------------
h.rect(S.x, sTop, S.w, sH, { fill: tool.fill, stroke: tool.stroke, width: 1.2 });
const sHeadY = narrow ? det.y + det.h + 4 : sTop;
h.text(S.x + inset, sHeadY + 22, 'Environment server', { size: 15, weight: 600, color: C.ink });
sLines.forEach((l, k) => h.text(S.x + inset, sHeadY + 22 + (k + 1) * lh + 2, l, { size: 13, color: C.muted }));
comp.forEach((c, k) => {
h.rect(c.x, c.y, c.w, c.h, { fill: C.bg, stroke: tool.stroke, width: 1.1 });
h.text(c.x + 8, c.y + 19, `session ${k + 1}`, { size: 13.5, weight: 600, color: C.ink });
h.text(c.x + 8, c.y + 19 + lh, 'worker process', { size: 12.5, color: C.muted });
h.text(c.x + 8, c.y + 19 + 2 * lh, `uid ${UID + k}`, { size: 12.5, color: C.muted });
if (!narrow) h.text(c.x + c.w - 8, c.y + 19 + lh, 'same layers as session 3', { size: 12.5, color: C.muted, anchor: 'end' });
});
// the session with its layers, nested from the strongest (outside) to the audit hook (inside)
h.rect(det.x, det.y, det.w, det.h, { fill: C.bg, stroke: tool.stroke, width: 1.1 });
h.text(det.x + 8, det.y + 19, 'session 3', { size: 13.5, weight: 600, color: C.ink });
h.text(det.x + 8, det.y + 19 + lh, `worker process, uid ${UID + 2}`, { size: 12.5, color: C.muted });
shells = LAYERS.map((L, k) => {
const x = det.x + 8 + k * shellSide, y = det.y + 46 + k * shellTop;
const w = shellW1 - 2 * k * shellSide, hh = shellH1 - k * (shellTop + shellBottom);
ho.rect(x, y, w, hh, { stroke: C.grey, width: 1.1 });
const label = ho.text(x + 8, y + 15.5, `${k + 1} ${L.name}`, { size: 12.5, color: C.ink });
return { x, y, w, h: hh, label };
});
const core = shells[3];
const cx = core.x + 8, cy = core.y + shellTop, cw = core.w - 16;
const chips = stack
? [[cx, cy, cw], [cx, cy + 34, cw]]
: [[cx, cy, (cw - 8) / 2], [cx + (cw + 8) / 2, cy, (cw - 8) / 2]];
[['Python session', tool], ["the task's files", { fill: C.greySoft, stroke: C.grey }]].forEach(([lab, col], k) => {
const [x, y, w] = chips[k];
ho.rect(x, y, w, 28, { fill: col.fill, stroke: col.stroke, width: 1.1 });
ho.text(x + w / 2, y + 18.5, lab, { size: 13, color: C.ink, anchor: 'middle' });
});
// --- the WebSocket connections, one session per connection --------------------------------
const targets = [...comp, det];
const ws = (x1, y1, x2, y2) => {
h.arrow([[x1, y1], [x2, y2]], { stroke: C.ink, width: 1.3, head: 7 });
h.arrow([[x2, y2], [x1, y1]], { stroke: C.ink, width: 1.3, head: 7 });
};
if (narrow) {
clients.forEach((c, k) => {
const x = c.x + c.w / 2;
ws(x, c.y + c.h + 2, x, targets[k].y - 2);
});
const lab = 'WebSocket, one session per connection';
const lw = textW(lab, 13) + 14, mx = width / 2, my = tTop + tH + 27;
h.rect(mx - lw / 2, my - 11, lw, 20, { fill: C.bg, stroke: 'none' });
h.text(mx, my + 4, lab, { size: 13, color: C.ink, anchor: 'middle' });
} else {
clients.forEach((c, k) => {
const t = targets[k], y = c.y + c.h / 2;
ws(c.x + c.w + 2, y, t.x - 2, k < 2 ? y : t.y + 4 + clientH / 2);
});
const gx = T.x + T.w + gapW / 2;
const gl = [['WebSocket', C.ink, 600], ...wrap('one session per connection', gapW - 8, 12.5).map((l) => [l, C.muted, 400])];
gl.forEach(([l, col, wt], k) => h.text(gx, sTop + 22 + k * 16, l, { size: k ? 12.5 : 13.5, color: col, weight: wt, anchor: 'middle' }));
}
// --- the other place the same client code can connect to ------------------------------------
const spX = narrow ? pad : S.x, spW = narrow ? width - 2 * pad : S.w;
h.rect(spX, spaceY, spW, spaceH, { stroke: C.muted, width: 1.1, dashed: true });
h.text(spX + inset, spaceY + 20, 'Or a Space on the Hub', { size: 14, weight: 600, color: C.ink });
spaceLines.forEach((l, k) => h.text(spX + inset, spaceY + 20 + (k + 1) * lh, l, { size: 13, color: C.muted }));
if (!narrow) {
const ax = T.x + T.w / 2;
const ay = spaceY + spaceH / 2;
h.line(ax, tTop + tH + 2, ax, ay, { stroke: C.muted, width: 1.2, dashed: true });
h.arrow([[ax, ay], [spX - 4, ay]], { stroke: C.muted, width: 1.2, dashed: true, head: 7 });
}
// --- hover, tap and keyboard targets, innermost on top --------------------------------------
shells.forEach((s, k) => {
const r = document.createElementNS(NS, 'rect');
r.setAttribute('x', s.x); r.setAttribute('y', s.y); r.setAttribute('width', s.w); r.setAttribute('height', s.h);
r.setAttribute('fill', 'transparent');
r.setAttribute('class', 'cs-hit');
r.setAttribute('tabindex', '0');
r.setAttribute('role', 'button');
r.setAttribute('aria-label', `Layer ${k + 1}: ${LAYERS[k].name}`);
r.addEventListener('pointerover', () => setActive(k), { signal: ctx.signal });
r.addEventListener('click', () => setActive(k), { signal: ctx.signal });
r.addEventListener('focus', () => setActive(k), { signal: ctx.signal });
hits.appendChild(r);
});
paintActive();
}
// Keep the note as tall as its longest text, so the iframe height doesn't change on hover.
function layout() {
draw();
let tallest = 0;
LAYERS.forEach((_, k) => { note.style.minHeight = ''; writeNote(k); tallest = Math.max(tallest, note.offsetHeight); });
writeNote(active);
note.style.minHeight = tallest + 'px';
}
if (document.fonts && document.fonts.ready) document.fonts.ready.then(() => { if (!ctx.signal.aborted) layout(); });
return ({ width: w }) => { width = w; C = Hand.colors(ctx.root); layout(); };
}, { deps: [] });
</script>
</div>
</body>
</html>
|