Spaces:
Runtime error
Runtime error
File size: 20,213 Bytes
0310320 2760456 94b17d0 f76cf5a 94b17d0 62d9954 0310320 b772265 0310320 62d9954 0310320 f76cf5a 0310320 2760456 0310320 2760456 0310320 2760456 0310320 2760456 0310320 2760456 c8e68c4 2760456 0310320 2760456 c8e68c4 2760456 c8e68c4 2760456 c8e68c4 2760456 0310320 2760456 0310320 2760456 0310320 94b17d0 b89cf6e 94b17d0 bf33293 94b17d0 bf33293 94b17d0 b89cf6e 94b17d0 6da0494 94b17d0 62d9954 94b17d0 b89cf6e bf33293 b89cf6e bf33293 b89cf6e bf33293 b89cf6e bf33293 b89cf6e bf33293 94b17d0 bf33293 b89cf6e bf33293 b89cf6e bf33293 94b17d0 bf33293 94b17d0 bf33293 94b17d0 6da0494 94b17d0 b89cf6e 94b17d0 6da0494 94b17d0 bf33293 94b17d0 6da0494 b89cf6e 6da0494 94b17d0 62d9954 94b17d0 bf33293 94b17d0 6da0494 62d9954 94b17d0 62d9954 94b17d0 62d9954 94b17d0 62d9954 94b17d0 b89cf6e 94b17d0 b89cf6e 94b17d0 6da0494 94b17d0 6da0494 94b17d0 6da0494 94b17d0 0310320 |
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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
#!/usr/bin/env node
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { table } from "table";
import prompts from "prompts";
import { searchModels, formatModel } from "./hf-api.js";
import { PIPELINE_DATA } from "@huggingface/tasks";
import type { ModelEntry } from "@huggingface/hub";
import { listFiles } from "@huggingface/hub";
const SERVER_URL = process.env.BENCH_SERVER_URL || "http://localhost:7860";
interface SubmitOptions {
platform?: "node" | "web";
modelId: string;
task: string;
mode?: "warm" | "cold";
repeats?: number;
dtype?: string;
batchSize?: number;
device?: string;
browser?: string;
headed?: boolean;
}
async function submitBenchmark(options: SubmitOptions) {
const response = await fetch(`${SERVER_URL}/api/benchmark`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(options),
});
if (!response.ok) {
throw new Error(`Failed to submit benchmark: ${response.statusText}`);
}
return await response.json();
}
async function getBenchmark(id: string) {
const response = await fetch(`${SERVER_URL}/api/benchmark/${id}`);
if (!response.ok) {
throw new Error(`Failed to get benchmark: ${response.statusText}`);
}
return await response.json();
}
async function listBenchmarks() {
const response = await fetch(`${SERVER_URL}/api/benchmarks`);
if (!response.ok) {
throw new Error(`Failed to list benchmarks: ${response.statusText}`);
}
return await response.json();
}
async function getQueueStatus() {
const response = await fetch(`${SERVER_URL}/api/queue`);
if (!response.ok) {
throw new Error(`Failed to get queue status: ${response.statusText}`);
}
return await response.json();
}
async function pollBenchmark(id: string, interval = 2000): Promise<any> {
return new Promise((resolve, reject) => {
const check = async () => {
try {
const result = await getBenchmark(id);
if (result.status === "completed") {
resolve(result);
} else if (result.status === "failed") {
reject(new Error(result.error));
} else {
console.log(`Status: ${result.status}...`);
setTimeout(check, interval);
}
} catch (error) {
reject(error);
}
};
check();
});
}
/**
* Fetch existing benchmark file paths from HuggingFace Dataset
* Returns a Set of file paths for quick lookup
*/
async function fetchExistingBenchmarks(datasetRepo: string, token?: string): Promise<Set<string>> {
console.log(`\nFetching existing benchmarks from ${datasetRepo}...`);
const existingFiles = new Set<string>();
try {
for await (const file of listFiles({
repo: {
type: "dataset",
name: datasetRepo,
},
credentials: token ? { accessToken: token } : undefined,
})) {
if (file.path.endsWith(".json")) {
existingFiles.add(file.path);
}
}
console.log(` Found ${existingFiles.size} existing benchmarks\n`);
} catch (error: any) {
console.warn(` Warning: Failed to fetch existing benchmarks: ${error.message}`);
console.warn(` Proceeding without exclusion filter\n`);
}
return existingFiles;
}
/**
* Generate the expected file path for a benchmark combination
* Must match the path generation logic in the server
*/
function generateBenchmarkPath(combo: {
task: string;
modelId: string;
platform: string;
mode: string;
device: string;
dtype: string;
batchSize: number;
browser?: string;
headed?: boolean;
}): string {
// Extract org and model name from modelId
const parts = combo.modelId.split("/");
const org = parts.length > 1 ? parts[0] : "default";
const modelName = parts.length > 1 ? parts[1] : parts[0];
// Build path components similar to server logic
const pathParts = [
combo.task,
org,
modelName,
];
// Build filename parts
const filenameParts = [
combo.platform,
combo.mode,
combo.device,
`b${combo.batchSize}`,
];
// Add dtype if specified and not fp32 (default)
if (combo.dtype && combo.dtype !== "fp32") {
filenameParts.push(combo.dtype);
}
// Add browser for web platform
if (combo.platform === "web" && combo.browser) {
filenameParts.push(combo.browser);
}
// Add headed indicator for web platform
if (combo.platform === "web" && combo.headed) {
filenameParts.push("headed");
}
const filename = filenameParts.join("_") + ".json";
return [...pathParts, filename].join("/");
}
yargs(hideBin(process.argv))
.command(
"submit <modelId> <task>",
"Submit a new benchmark request",
(yargs) => {
return yargs
.positional("modelId", {
describe: "Model ID to benchmark",
type: "string",
demandOption: true,
})
.positional("task", {
describe: "Task to perform (e.g., feature-extraction, fill-mask)",
choices: Object.keys(PIPELINE_DATA),
demandOption: true,
})
.option("platform", {
describe: "Platform to run on",
choices: ["node", "web"] as const,
default: "node" as const,
})
.option("mode", {
describe: "Cache mode",
choices: ["warm", "cold"] as const,
default: "warm" as const,
})
.option("repeats", {
describe: "Number of times to repeat the benchmark",
type: "number",
default: 3,
})
.option("batch-size", {
describe: "Batch size for inference",
type: "number",
default: 1,
})
.option("dtype", {
describe: "Data type",
choices: ["fp32", "fp16", "q8", "int8", "uint8", "q4", "bnb4", "q4f16"] as const,
default: "fp32" as const,
})
.option("device", {
describe: "Device for web platform",
type: "string",
default: "webgpu",
})
.option("browser", {
describe: "Browser for web platform",
choices: ["chromium", "firefox", "webkit"] as const,
default: "chromium" as const,
})
.option("headed", {
describe: "Run browser in headed mode",
type: "boolean",
default: false,
})
.option("wait", {
describe: "Wait for benchmark completion",
type: "boolean",
default: false,
});
},
async (argv) => {
const options: SubmitOptions = {
modelId: argv.modelId,
task: argv.task,
platform: argv.platform,
mode: argv.mode,
repeats: argv.repeats,
batchSize: argv.batchSize,
dtype: argv.dtype,
device: argv.device,
browser: argv.browser,
headed: argv.headed,
};
console.log("Submitting benchmark with options:");
console.log(JSON.stringify(options, null, 2));
const result = await submitBenchmark(options);
console.log(`β Benchmark queued: ${result.id}`);
console.log(` Position in queue: ${result.position}`);
if (argv.wait) {
console.log("\nWaiting for completion...");
const completed = await pollBenchmark(result.id);
console.log("\nβ
Benchmark completed!");
console.log(JSON.stringify(completed.result, null, 2));
} else {
console.log(`\nCheck status with: bench-client get ${result.id}`);
}
}
)
.command(
"get <id>",
"Get benchmark result by ID",
(yargs) => {
return yargs.positional("id", {
describe: "Benchmark ID",
type: "string",
demandOption: true,
});
},
async (argv) => {
const result = await getBenchmark(argv.id);
console.log(JSON.stringify(result, null, 2));
}
)
.command(
"list",
"List all benchmark results",
() => { },
async () => {
const result = await listBenchmarks();
console.log(`Total benchmarks: ${result.total}\n`);
const data = [
["ID", "Status", "Platform", "Model", "Task", "Mode", "Repeats", "Batch", "DType", "Device", "Browser", "Headed", "Duration"],
];
result.results.forEach((b: any) => {
const duration = b.completedAt && b.startedAt
? `${((b.completedAt - b.startedAt) / 1000).toFixed(1)}s`
: "-";
// Status with emoji
const statusMap: Record<string, string> = {
completed: "β
completed",
failed: "β failed",
running: "π running",
pending: "β³ pending",
};
const statusDisplay = statusMap[b.status] || b.status;
// Platform with emoji
const platformDisplay = b.platform === "node" ? "π’ node" : "π web";
// Headed with emoji
const headedDisplay = b.headed ? "ποΈ Yes" : "No";
data.push([
b.id.substring(0, 8),
statusDisplay,
platformDisplay,
b.modelId,
b.task,
b.mode,
b.repeats.toString(),
b.batchSize.toString(),
b.dtype || "-",
b.device || "-",
b.browser || "-",
headedDisplay,
duration,
]);
});
console.log(table(data));
}
)
.command(
"queue",
"Show queue status",
() => { },
async () => {
const result = await getQueueStatus();
console.log("Queue Status:");
console.log(` Pending: ${result.status.pending}`);
console.log(` Running: ${result.status.running}`);
console.log(` Completed: ${result.status.completed}`);
console.log(` Failed: ${result.status.failed}`);
if (result.queue.length > 0) {
console.log("\nCurrent Queue:");
result.queue.forEach((b: any) => {
console.log(` [${b.status}] ${b.id} - ${b.platform}/${b.modelId}`);
});
}
}
)
.command(
"batch [query...]",
"Search HuggingFace models and submit benchmarks for them",
(yargs) => {
return yargs
.positional("query", {
describe: "Optional search queries to filter model names (can specify multiple)",
type: "string",
array: true,
})
.option("task", {
describe: "Task type to filter models (e.g., feature-extraction, image-classification)",
choices: Object.keys(PIPELINE_DATA),
alias: "t",
})
.option("limit", {
describe: "Maximum number of models to benchmark",
type: "number",
})
.option("platform", {
describe: "Platform(s) to run on (can specify multiple)",
type: "array",
default: ["node"],
})
.option("mode", {
describe: "Cache mode(s) (can specify multiple)",
type: "array",
default: ["warm"],
})
.option("repeats", {
describe: "Number of times to repeat the benchmark",
type: "number",
default: 3,
})
.option("batch-size", {
describe: "Batch size(s) for inference (can specify multiple)",
type: "array",
default: [1],
})
.option("device", {
describe: "Device(s) for platform (can specify multiple)",
type: "array",
default: ["webgpu"],
})
.option("browser", {
describe: "Browser(s) for web platform (can specify multiple)",
type: "array",
default: ["chromium"],
})
.option("dtype", {
describe: "Data type(s) (can specify multiple)",
type: "array",
default: ["fp32"],
})
.option("yes", {
alias: "y",
describe: "Skip confirmation prompt",
type: "boolean",
default: false,
})
.option("exclude-existing", {
alias: "e",
describe: "Exclude benchmarks that already exist in the HF Dataset",
type: "boolean",
default: false,
})
.option("hf-dataset-repo", {
describe: "HuggingFace Dataset repository to check for existing benchmarks",
type: "string",
})
.option("hf-token", {
describe: "HuggingFace API token (for private datasets)",
type: "string",
});
},
async (argv) => {
// Validate that at least one of task or query is provided
const task = argv.task as keyof typeof PIPELINE_DATA | undefined;
const queries = argv.query && argv.query.length > 0 ? argv.query : undefined;
if (!task && !queries) {
console.error("Error: You must specify either a task type or search queries (or both)");
console.error("Examples:");
console.error(" batch feature-extraction bert");
console.error(" batch feature-extraction");
console.error(" batch bert distilbert");
process.exit(1);
}
const taskText = task ? `${task} ` : "";
const queryText = queries && queries.length > 0
? ` matching [${queries.join(", ")}]`
: "";
console.log(`Searching for ${taskText}models${queryText}...\n`);
let allModels: ModelEntry[] = [];
if (queries && queries.length > 0) {
// Search with each query and combine results
const modelSets: ModelEntry[][] = [];
for (const query of queries) {
const models = await searchModels({
task: task,
search: query,
limit: argv.limit,
});
modelSets.push(models);
console.log(` Found ${models.length} models for query "${query}"${task ? ` (task: ${task})` : ""}`);
}
// Deduplicate models by ID
const modelMap = new Map<string, ModelEntry>();
for (const models of modelSets) {
for (const model of models) {
modelMap.set(model.id, model);
}
}
allModels = Array.from(modelMap.values());
console.log(` Total unique models: ${allModels.length}\n`);
} else {
// No query specified, search by task only
allModels = await searchModels({
task: task,
limit: argv.limit,
});
}
if (allModels.length === 0) {
console.log("No models found.");
return;
}
console.log(`Found ${allModels.length} models:\n`);
allModels.forEach((model, index) => {
console.log(`${index + 1}. ${formatModel(model)}`);
});
// Generate all combinations
const platforms = argv.platform as string[];
const modes = argv.mode as string[];
const batchSizes = argv.batchSize as number[];
const devices = argv.device as string[];
const browsers = argv.browser as string[];
const dtypes = argv.dtype as string[];
const combinations: Array<{
modelId: string;
task?: string;
platform: string;
mode: string;
batchSize: number;
device: string;
browser: string;
dtype: string;
}> = [];
for (const model of allModels) {
for (const platform of platforms) {
for (const mode of modes) {
for (const batchSize of batchSizes) {
for (const device of devices) {
for (const browser of browsers) {
for (const dtype of dtypes) {
combinations.push({
modelId: (model as any).name || model.id,
task: model.task || undefined, // Store model's task for later use
platform,
mode,
batchSize,
device,
browser,
dtype,
});
}
}
}
}
}
}
}
// Filter out existing benchmarks if requested
let filteredCombinations = combinations;
if (argv.excludeExisting) {
const datasetRepo = argv.hfDatasetRepo || process.env.HF_DATASET_REPO;
const hfToken = argv.hfToken || process.env.HF_TOKEN;
if (!datasetRepo) {
console.error("\nβ Error: --exclude-existing requires --hf-dataset-repo or HF_DATASET_REPO env var");
process.exit(1);
}
const existingFiles = await fetchExistingBenchmarks(datasetRepo, hfToken);
// Filter combinations
filteredCombinations = combinations.filter((combo) => {
const modelTask = task || combo.task || "feature-extraction";
const benchmarkPath = generateBenchmarkPath({
task: modelTask,
modelId: combo.modelId,
platform: combo.platform,
mode: combo.mode,
device: combo.device,
dtype: combo.dtype,
batchSize: combo.batchSize,
browser: combo.browser,
headed: false, // Default, as it's not in the combination
});
return !existingFiles.has(benchmarkPath);
});
const excludedCount = combinations.length - filteredCombinations.length;
console.log(`\nπ Exclusion Filter:`);
console.log(` Total combinations: ${combinations.length}`);
console.log(` Existing benchmarks: ${excludedCount}`);
console.log(` New benchmarks to run: ${filteredCombinations.length}\n`);
}
console.log(`\nπ Benchmark Plan:`);
console.log(` Models: ${allModels.length}`);
console.log(` Platforms: ${platforms.join(", ")}`);
console.log(` Modes: ${modes.join(", ")}`);
console.log(` Batch Sizes: ${batchSizes.join(", ")}`);
console.log(` Devices: ${devices.join(", ")}`);
console.log(` Browsers: ${browsers.join(", ")}`);
console.log(` DTypes: ${dtypes.join(", ")}`);
console.log(` Total benchmarks: ${filteredCombinations.length}`);
// Ask for confirmation unless -y flag is used
if (!argv.yes) {
const response = await prompts({
type: "confirm",
name: "proceed",
message: `Proceed with submitting ${filteredCombinations.length} benchmark(s)?`,
initial: true,
});
if (!response.proceed) {
console.log("\nCancelled.");
return;
}
}
console.log(`\nSubmitting ${filteredCombinations.length} benchmarks...`);
const submitted: string[] = [];
const failed: Array<{ combo: string; error: string }> = [];
for (const combo of filteredCombinations) {
try {
// Use task from model if not specified in command
const modelTask = task || (combo as any).task || "feature-extraction";
const options: SubmitOptions = {
modelId: combo.modelId,
task: modelTask,
platform: combo.platform as "node" | "web",
mode: combo.mode as "warm" | "cold",
repeats: argv.repeats,
batchSize: combo.batchSize,
dtype: combo.dtype,
device: combo.device,
browser: combo.browser as "chromium" | "firefox" | "webkit",
};
const result = await submitBenchmark(options);
const desc = `${combo.modelId} [${combo.platform}/${combo.device}/${combo.mode}/b${combo.batchSize}/${combo.dtype}]`;
submitted.push(desc);
console.log(`β Queued: ${desc} (${result.id})`);
} catch (error: any) {
const desc = `${combo.modelId} [${combo.platform}/${combo.device}/${combo.dtype}]`;
failed.push({ combo: desc, error: error.message });
console.log(`β Failed: ${desc} - ${error.message}`);
}
}
console.log(`\nπ Summary:`);
console.log(` β Submitted: ${submitted.length}`);
console.log(` β Failed: ${failed.length}`);
if (submitted.length > 0) {
console.log(`\nCheck status with: bench-client queue`);
}
}
)
.demandCommand(1, "You need to specify a command")
.help()
.alias("h", "help")
.strict()
.parse();
export { submitBenchmark, getBenchmark, listBenchmarks, getQueueStatus, pollBenchmark };
|