File size: 15,449 Bytes
4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 234ef1f 4db51d5 | 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | // ββ Output Example Demo renderer βββββββββββββββββββββββββββββββββββββββββββββ
// Supports multiple clips with left/right arrow navigation, device selector,
// and streaming text effect at real TPS speed.
const DEMO_DEVICE_LABELS = {
orin_nano_super: "Jetson Orin Nano Super",
agx_orin: "Jetson AGX Orin",
agx_thor: "Jetson AGX Thor",
};
const DEMO_DEFAULT_DEVICE = "agx_orin";
const DEMO_TOKENS_PER_WORD = 1.33;
// ββ CSV parser (minimal, demo-only) βββββββββββββββββββββββββββββββββββββββββ
function demoParseCsv(text) {
const lines = text.replace(/\r/g, "").trim().split("\n");
const headers = lines[0].split(",");
return lines.slice(1).map(line => {
const vals = line.split(",");
const row = {};
headers.forEach((h, i) => { row[h] = (vals[i] || "").trim(); });
return row;
});
}
// ββ Look up TPS from benchmark data βββββββββββββββββββββββββββββββββββββββββ
function demoLookupTps(csvRows, modelName, device, matchCriteria) {
const row = csvRows.find(r =>
r.model === modelName &&
r.device === device &&
Object.entries(matchCriteria).every(([col, val]) => r[col] === val)
);
if (row) {
if (!row.tps || row.tps.toUpperCase() === "OOM") return { found: true, tps: null };
return { found: true, tps: parseFloat(row.tps) };
}
const oomRow = csvRows.find(r =>
r.model === modelName &&
r.device === device &&
r.tps && r.tps.toUpperCase() === "OOM"
);
if (oomRow) return { found: true, tps: null };
return { found: false, tps: null };
}
// ββ Determine available devices from benchmark data βββββββββββββββββββββββββ
function demoAvailableDevices(csvRows, models, matchCriteria) {
const deviceSet = new Set();
csvRows.forEach(r => {
if (!models.includes(r.model)) return;
if (!Object.entries(matchCriteria).every(([col, val]) => r[col] === val)) return;
if (r.tps && r.tps.toUpperCase() !== "OOM") deviceSet.add(r.device);
});
return Object.keys(DEMO_DEVICE_LABELS).filter(d => deviceSet.has(d));
}
// ββ Streaming text animation ββββββββββββββββββββββββββββββββββββββββββββββββ
function demoStreamText(element, fullText, tps) {
const handle = { _tid: null, cancel() { clearTimeout(this._tid); } };
element.textContent = "";
if (!tps || tps <= 0) {
element.textContent = fullText;
return handle;
}
const words = fullText.split(/(\s+)/);
const delayMs = 1000 / (tps / DEMO_TOKENS_PER_WORD);
let idx = 0;
function tick() {
if (idx < words.length) {
element.textContent += words[idx];
idx++;
handle._tid = setTimeout(tick, delayMs);
}
}
tick();
return handle;
}
// ββ Pre-calculate height for an output box to prevent layout shift ββββββββββ
function demoPreCalcHeight(box, textEl, fullText) {
textEl.textContent = fullText;
const h = box.offsetHeight;
box.style.minHeight = h + "px";
textEl.textContent = "";
}
// ββ Main render function ββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function renderDemo(data, container, optimizedOrg, dataFile, modelColors) {
if (!data) return;
// Normalise: support both old single-clip format and new clips array
let clips = data.clips;
if (!clips) {
clips = [{
video: data.video,
label: data.title,
prompt: data.prompt,
inference_setup: data.inference_setup,
citation: data.citation,
citation_url: data.citation_url,
outputs: data.outputs,
}];
}
if (!clips.length) return;
const matchCriteria = data.benchmark_match || {};
// Collect all model names across clips
const allModelNames = [...new Set(clips.flatMap(c => (c.outputs || []).map(o => o.model)))];
// Load benchmark CSV
let csvRows = [];
if (dataFile) {
try {
const csvResp = await fetch(dataFile);
if (csvResp.ok) csvRows = demoParseCsv(await csvResp.text());
} catch { /* proceed without */ }
}
const devices = csvRows.length
? demoAvailableDevices(csvRows, allModelNames, matchCriteria)
: [];
let selectedDevice = devices.includes(DEMO_DEFAULT_DEVICE)
? DEMO_DEFAULT_DEVICE
: (devices[0] || DEMO_DEFAULT_DEVICE);
let currentClipIdx = 0;
let activeStreams = [];
// ββ Build DOM skeleton ββββββββββββββββββββββββββββββββββββββββββββββ
const section = document.createElement("div");
section.className = "demo-section";
// Toggle button
const toggle = document.createElement("button");
toggle.className = "demo-toggle";
toggle.innerHTML = '<span class="demo-toggle-arrow">▶</span> Show Demo';
// Collapsible wrapper
const content = document.createElement("div");
content.className = "demo-content";
const inner = document.createElement("div");
inner.className = "demo-content-inner";
const card = document.createElement("div");
card.className = "demo-card";
// Title row: h3 + device selector
const titleRow = document.createElement("div");
titleRow.className = "demo-title-row";
const titleLeft = document.createElement("div");
titleLeft.className = "demo-title-left";
const h3 = document.createElement("h3");
titleLeft.appendChild(h3);
const clipSetup = document.createElement("span");
clipSetup.className = "demo-clip-setup";
titleLeft.appendChild(clipSetup);
titleRow.appendChild(titleLeft);
const deviceSelector = document.createElement("div");
deviceSelector.className = "demo-device-selector";
if (devices.length > 1) {
const deviceGroup = document.createElement("div");
deviceGroup.className = "btn-group";
function renderDeviceButtons() {
deviceGroup.innerHTML = "";
devices.forEach(dev => {
const btn = document.createElement("button");
btn.className = "btn" + (dev === selectedDevice ? " active" : "");
btn.dataset.value = dev;
btn.textContent = DEMO_DEVICE_LABELS[dev] || dev;
deviceGroup.appendChild(btn);
});
}
renderDeviceButtons();
deviceGroup.addEventListener("click", e => {
const btn = e.target.closest(".btn");
if (!btn) return;
const newDevice = btn.dataset.value;
if (newDevice === selectedDevice) return;
selectedDevice = newDevice;
renderDeviceButtons();
renderOutputs();
});
deviceSelector.appendChild(deviceGroup);
}
card.appendChild(titleRow);
// Video with overlay arrows
const videoWrap = document.createElement("div");
videoWrap.className = "demo-video-wrap";
// Pre-create and buffer a <video> element for every clip
const clipVideos = clips.map(clip => {
if (!clip.video) return null;
const v = document.createElement("video");
v.loop = true;
v.muted = true;
v.playsInline = true;
v.preload = "auto";
v.className = "demo-video";
v.style.opacity = "0";
v.src = clip.video;
videoWrap.appendChild(v);
return v;
});
let activeVideoIdx = -1;
const arrowLeft = document.createElement("button");
arrowLeft.className = "demo-arrow demo-arrow-left";
arrowLeft.innerHTML = "◀";
arrowLeft.setAttribute("aria-label", "Previous clip");
const arrowRight = document.createElement("button");
arrowRight.className = "demo-arrow demo-arrow-right";
arrowRight.innerHTML = "▶";
arrowRight.setAttribute("aria-label", "Next clip");
videoWrap.appendChild(arrowLeft);
videoWrap.appendChild(arrowRight);
card.appendChild(videoWrap);
// Device selector (centered under video)
card.appendChild(deviceSelector);
// Prompt (full width)
const promptEl = document.createElement("div");
promptEl.className = "demo-prompt";
card.appendChild(promptEl);
// Outputs grid
const grid = document.createElement("div");
grid.className = "demo-outputs";
card.appendChild(grid);
// Citation
const citeEl = document.createElement("p");
citeEl.className = "demo-citation";
card.appendChild(citeEl);
// ββ Arrow visibility ββββββββββββββββββββββββββββββββββββββββββββββββ
function updateArrows() {
const showArrows = clips.length > 1;
arrowLeft.style.display = showArrows ? "" : "none";
arrowRight.style.display = showArrows ? "" : "none";
arrowLeft.disabled = false;
arrowRight.disabled = false;
}
arrowLeft.addEventListener("click", () => {
currentClipIdx = (currentClipIdx - 1 + clips.length) % clips.length;
renderClip();
});
arrowRight.addEventListener("click", () => {
currentClipIdx = (currentClipIdx + 1) % clips.length;
renderClip();
});
// ββ Render current clip βββββββββββββββββββββββββββββββββββββββββββββ
function cancelStreams() {
activeStreams.forEach(s => s.cancel());
activeStreams = [];
}
function renderOutputs() {
cancelStreams();
grid.innerHTML = "";
const clip = clips[currentClipIdx];
const outputs = clip.outputs || [];
outputs.forEach(out => {
const color = modelColors && modelColors[out.model];
const borderColor = color ? color.border : null;
const box = document.createElement("div");
box.className = "demo-output";
if (borderColor) box.style.borderColor = borderColor;
const modelEl = document.createElement("span");
modelEl.className = "demo-output-model";
modelEl.textContent = out.model;
if (borderColor) modelEl.style.color = borderColor;
box.appendChild(modelEl);
const tpsEl = document.createElement("span");
tpsEl.className = "demo-output-tps";
if (borderColor) tpsEl.style.color = borderColor;
box.appendChild(tpsEl);
const textEl = document.createElement("span");
textEl.className = "demo-output-text";
box.appendChild(textEl);
grid.appendChild(box);
// Look up TPS
let tps = null;
if (csvRows.length) {
const lookup = demoLookupTps(csvRows, out.model, selectedDevice, matchCriteria);
tps = lookup.found ? lookup.tps : (out.tps || null);
} else {
tps = out.tps || null;
}
if (tps != null) {
tpsEl.textContent = tps.toFixed(2) + " Tokens / sec";
tpsEl.classList.remove("oom");
textEl.style.display = "";
demoPreCalcHeight(box, textEl, out.text);
const handle = demoStreamText(textEl, out.text, tps);
activeStreams.push(handle);
} else {
tpsEl.textContent = "OOM";
tpsEl.classList.add("oom");
tpsEl.style.color = "";
textEl.style.display = "none";
box.style.minHeight = "";
}
});
}
function renderClip() {
const clip = clips[currentClipIdx];
// Title: clip label + setup details
h3.textContent = clip.label || data.title || "Output Examples";
clipSetup.textContent = clip.inference_setup || "";
clipSetup.style.display = clip.inference_setup ? "" : "none";
// Video β switch between pre-buffered video elements
const newVideo = clipVideos[currentClipIdx];
if (newVideo) {
videoWrap.style.display = "";
const oldVideo = activeVideoIdx >= 0 ? clipVideos[activeVideoIdx] : null;
const showNew = () => {
newVideo.style.opacity = "1";
newVideo.play();
if (oldVideo && oldVideo !== newVideo) {
oldVideo.style.opacity = "0";
oldVideo.pause();
oldVideo.currentTime = 0;
}
activeVideoIdx = currentClipIdx;
};
// If enough data is buffered, show immediately; otherwise wait
if (newVideo.readyState >= 3) {
showNew();
} else {
newVideo.addEventListener("canplay", showNew, { once: true });
}
} else {
videoWrap.style.display = "none";
if (activeVideoIdx >= 0 && clipVideos[activeVideoIdx]) {
clipVideos[activeVideoIdx].style.opacity = "0";
clipVideos[activeVideoIdx].pause();
}
activeVideoIdx = -1;
}
// Prompt
if (clip.prompt) {
promptEl.innerHTML = "<strong>Prompt:</strong> " + clip.prompt.replace(/\n/g, "<br>");
promptEl.style.display = "";
} else {
promptEl.style.display = "none";
}
// Citation
if (clip.citation) {
if (clip.citation_url) {
citeEl.innerHTML = clip.citation.replace(
/Autonomous Vehicle Domain Adaptation Gallery/,
`<a href="${clip.citation_url}" target="_blank" rel="noopener">Autonomous Vehicle Domain Adaptation Gallery</a>`
);
} else {
citeEl.textContent = clip.citation;
}
citeEl.style.display = "";
} else {
citeEl.style.display = "none";
}
// Outputs
renderOutputs();
updateArrows();
}
// ββ Toggle open/close βββββββββββββββββββββββββββββββββββββββββββββββ
toggle.addEventListener("click", () => {
const isOpen = content.classList.toggle("open");
toggle.classList.toggle("active", isOpen);
toggle.innerHTML = isOpen
? '<span class="demo-toggle-arrow">▶</span> Hide Demo'
: '<span class="demo-toggle-arrow">▶</span> Show Demo';
if (isOpen) {
renderClip();
} else {
cancelStreams();
}
});
inner.appendChild(card);
content.appendChild(inner);
section.appendChild(toggle);
section.appendChild(content);
container.appendChild(section);
}
|