Spaces:
Runtime error
Runtime error
update
Browse files- bench/src/web/main.ts +50 -3
bench/src/web/main.ts
CHANGED
|
@@ -9,10 +9,47 @@ const modeEl = document.getElementById("mode") as HTMLSelectElement;
|
|
| 9 |
const repeatsEl = document.getElementById("repeats") as HTMLInputElement;
|
| 10 |
const deviceEl = document.getElementById("device") as HTMLSelectElement;
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
async function runWarmWithReload(modelId: string, task: string, repeats: number, device: string, dtype?: string, batchSize: number = 1) {
|
| 13 |
const flag = sessionStorage.getItem("__warm_ready__");
|
| 14 |
if (!flag) {
|
| 15 |
statusEl.textContent = "prefetching (warmup) ...";
|
|
|
|
|
|
|
| 16 |
// Perform warmup and store flag
|
| 17 |
const { pipeline } = await import("@huggingface/transformers");
|
| 18 |
const options: any = { device };
|
|
@@ -20,7 +57,7 @@ async function runWarmWithReload(modelId: string, task: string, repeats: number,
|
|
| 20 |
const p = await pipeline(task, modelId, options);
|
| 21 |
const warmupInputs = Array(batchSize).fill("warmup");
|
| 22 |
await p(warmupInputs);
|
| 23 |
-
sessionStorage.setItem("__warm_ready__",
|
| 24 |
location.reload();
|
| 25 |
return null;
|
| 26 |
} else {
|
|
@@ -36,17 +73,22 @@ async function run() {
|
|
| 36 |
const mode = modeEl.value as "warm" | "cold";
|
| 37 |
const repeats = Math.max(1, parseInt(repeatsEl.value || "3", 10));
|
| 38 |
const device = deviceEl.value;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
out.textContent = "{}";
|
| 40 |
|
| 41 |
if (mode === "cold") {
|
| 42 |
statusEl.textContent = "clearing caches (cold)...";
|
| 43 |
-
|
|
|
|
| 44 |
if (r) {
|
| 45 |
out.textContent = JSON.stringify(r, null, 2);
|
| 46 |
statusEl.textContent = "done (cold)";
|
| 47 |
}
|
| 48 |
} else {
|
| 49 |
-
const r = await runWarmWithReload(modelId, task, repeats, device);
|
| 50 |
if (r) {
|
| 51 |
out.textContent = JSON.stringify(r, null, 2);
|
| 52 |
statusEl.textContent = "done (warm)";
|
|
@@ -54,11 +96,16 @@ async function run() {
|
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
// Auto-run if returning from warm reload
|
| 58 |
(async () => {
|
| 59 |
const flag = sessionStorage.getItem("__warm_ready__");
|
| 60 |
if (flag) {
|
| 61 |
try {
|
|
|
|
|
|
|
| 62 |
await run();
|
| 63 |
} catch (e) {
|
| 64 |
console.error(e);
|
|
|
|
| 9 |
const repeatsEl = document.getElementById("repeats") as HTMLInputElement;
|
| 10 |
const deviceEl = document.getElementById("device") as HTMLSelectElement;
|
| 11 |
|
| 12 |
+
// URL parameter utilities
|
| 13 |
+
function getUrlParams(): URLSearchParams {
|
| 14 |
+
return new URLSearchParams(window.location.search);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function setUrlParams(params: Record<string, string>) {
|
| 18 |
+
const url = new URL(window.location.href);
|
| 19 |
+
Object.entries(params).forEach(([key, value]) => {
|
| 20 |
+
url.searchParams.set(key, value);
|
| 21 |
+
});
|
| 22 |
+
window.history.replaceState({}, '', url);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function loadParamsFromUrl() {
|
| 26 |
+
const params = getUrlParams();
|
| 27 |
+
if (params.has('model')) modelEl.value = params.get('model')!;
|
| 28 |
+
if (params.has('task')) taskEl.value = params.get('task')!;
|
| 29 |
+
if (params.has('mode')) modeEl.value = params.get('mode')!;
|
| 30 |
+
if (params.has('repeats')) repeatsEl.value = params.get('repeats')!;
|
| 31 |
+
if (params.has('device')) deviceEl.value = params.get('device')!;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
function saveParamsToUrl(modelId: string, task: string, mode: string, repeats: number, device: string, dtype?: string, batchSize?: number) {
|
| 35 |
+
const params: Record<string, string> = {
|
| 36 |
+
model: modelId,
|
| 37 |
+
task,
|
| 38 |
+
mode,
|
| 39 |
+
repeats: String(repeats),
|
| 40 |
+
device,
|
| 41 |
+
};
|
| 42 |
+
if (dtype) params.dtype = dtype;
|
| 43 |
+
if (batchSize && batchSize !== 1) params['batch-size'] = String(batchSize);
|
| 44 |
+
setUrlParams(params);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
async function runWarmWithReload(modelId: string, task: string, repeats: number, device: string, dtype?: string, batchSize: number = 1) {
|
| 48 |
const flag = sessionStorage.getItem("__warm_ready__");
|
| 49 |
if (!flag) {
|
| 50 |
statusEl.textContent = "prefetching (warmup) ...";
|
| 51 |
+
// Save params to URL for reproducibility
|
| 52 |
+
saveParamsToUrl(modelId, task, 'warm', repeats, device, dtype, batchSize);
|
| 53 |
// Perform warmup and store flag
|
| 54 |
const { pipeline } = await import("@huggingface/transformers");
|
| 55 |
const options: any = { device };
|
|
|
|
| 57 |
const p = await pipeline(task, modelId, options);
|
| 58 |
const warmupInputs = Array(batchSize).fill("warmup");
|
| 59 |
await p(warmupInputs);
|
| 60 |
+
sessionStorage.setItem("__warm_ready__", "1");
|
| 61 |
location.reload();
|
| 62 |
return null;
|
| 63 |
} else {
|
|
|
|
| 73 |
const mode = modeEl.value as "warm" | "cold";
|
| 74 |
const repeats = Math.max(1, parseInt(repeatsEl.value || "3", 10));
|
| 75 |
const device = deviceEl.value;
|
| 76 |
+
const params = getUrlParams();
|
| 77 |
+
const dtype = params.get('dtype') || undefined;
|
| 78 |
+
const batchSize = params.has('batch-size') ? parseInt(params.get('batch-size')!, 10) : 1;
|
| 79 |
+
|
| 80 |
out.textContent = "{}";
|
| 81 |
|
| 82 |
if (mode === "cold") {
|
| 83 |
statusEl.textContent = "clearing caches (cold)...";
|
| 84 |
+
saveParamsToUrl(modelId, task, 'cold', repeats, device, dtype, batchSize);
|
| 85 |
+
const r = await runWebBenchmarkCold(modelId, task, repeats, device, dtype, batchSize);
|
| 86 |
if (r) {
|
| 87 |
out.textContent = JSON.stringify(r, null, 2);
|
| 88 |
statusEl.textContent = "done (cold)";
|
| 89 |
}
|
| 90 |
} else {
|
| 91 |
+
const r = await runWarmWithReload(modelId, task, repeats, device, dtype, batchSize);
|
| 92 |
if (r) {
|
| 93 |
out.textContent = JSON.stringify(r, null, 2);
|
| 94 |
statusEl.textContent = "done (warm)";
|
|
|
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
+
// Load parameters from URL on page load
|
| 100 |
+
loadParamsFromUrl();
|
| 101 |
+
|
| 102 |
// Auto-run if returning from warm reload
|
| 103 |
(async () => {
|
| 104 |
const flag = sessionStorage.getItem("__warm_ready__");
|
| 105 |
if (flag) {
|
| 106 |
try {
|
| 107 |
+
// Parameters are already in URL, just load them to form
|
| 108 |
+
loadParamsFromUrl();
|
| 109 |
await run();
|
| 110 |
} catch (e) {
|
| 111 |
console.error(e);
|