|
| 1 | +// Copyright (C) 2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { LLMPipeline } from "openvino-genai-node"; |
| 5 | +import yargs from "yargs/yargs"; |
| 6 | +import { hideBin } from "yargs/helpers"; |
| 7 | +import { readFileSync } from "fs"; |
| 8 | + |
| 9 | +main(); |
| 10 | + |
| 11 | +async function main() { |
| 12 | + const argv = yargs(hideBin(process.argv)) |
| 13 | + .option("model", { |
| 14 | + alias: "m", |
| 15 | + type: "string", |
| 16 | + demandOption: true, |
| 17 | + describe: "Path to model and tokenizers base directory.", |
| 18 | + }) |
| 19 | + .option("prompt", { |
| 20 | + alias: "p", |
| 21 | + type: "string", |
| 22 | + describe: |
| 23 | + "The prompt to generate text. If without `-p` and `--pf`, the default prompt is `The Sky is blue because`.", |
| 24 | + }) |
| 25 | + .option("prompt_file", { |
| 26 | + alias: "pf", |
| 27 | + type: "string", |
| 28 | + describe: "Read prompt from file.", |
| 29 | + }) |
| 30 | + .option("num_warmup", { |
| 31 | + alias: "nw", |
| 32 | + type: "number", |
| 33 | + default: 1, |
| 34 | + describe: "Number of warmup iterations.", |
| 35 | + }) |
| 36 | + .option("num_iter", { |
| 37 | + alias: "n", |
| 38 | + type: "number", |
| 39 | + default: 2, |
| 40 | + describe: "Number of iterations.", |
| 41 | + }) |
| 42 | + .option("max_new_tokens", { |
| 43 | + alias: "mt", |
| 44 | + type: "number", |
| 45 | + default: 20, |
| 46 | + describe: "Maximal number of new tokens.", |
| 47 | + }) |
| 48 | + .option("device", { |
| 49 | + alias: "d", |
| 50 | + type: "string", |
| 51 | + default: "CPU", |
| 52 | + describe: "Device.", |
| 53 | + }) |
| 54 | + .parse(); |
| 55 | + |
| 56 | + let prompt; |
| 57 | + if (argv.prompt !== undefined && argv.prompt_file !== undefined) { |
| 58 | + console.error(`Cannot specify both --prompt and --prompt_file options simultaneously!`); |
| 59 | + process.exit(1); |
| 60 | + } else { |
| 61 | + if (argv.prompt_file !== undefined) { |
| 62 | + prompt = [readFileSync(argv.prompt_file, "utf-8")]; |
| 63 | + } else { |
| 64 | + prompt = argv.prompt === undefined ? ["The Sky is blue because"] : [argv.prompt]; |
| 65 | + } |
| 66 | + } |
| 67 | + if (prompt.length === 0 || prompt[0].trim() === "") { |
| 68 | + throw new Error("Prompt is empty!"); |
| 69 | + } |
| 70 | + |
| 71 | + const modelsPath = argv.model; |
| 72 | + const { device } = argv; |
| 73 | + const numWarmup = argv.num_warmup; |
| 74 | + const numIter = argv.num_iter; |
| 75 | + |
| 76 | + const config = { |
| 77 | + max_new_tokens: argv.max_new_tokens, |
| 78 | + apply_chat_template: false, |
| 79 | + return_decoded_results: true, |
| 80 | + }; |
| 81 | + |
| 82 | + let pipe; |
| 83 | + if (device === "NPU") { |
| 84 | + pipe = await LLMPipeline(modelsPath, device); |
| 85 | + } else { |
| 86 | + const schedulerConfig = { |
| 87 | + enable_prefix_caching: false, |
| 88 | + max_num_batched_tokens: Number.MAX_SAFE_INTEGER, |
| 89 | + }; |
| 90 | + pipe = await LLMPipeline(modelsPath, device, { schedulerConfig: schedulerConfig }); |
| 91 | + } |
| 92 | + |
| 93 | + for (let i = 0; i < numWarmup; i++) { |
| 94 | + await pipe.generate(prompt, config); |
| 95 | + } |
| 96 | + |
| 97 | + let res = await pipe.generate(prompt, config); |
| 98 | + let { perfMetrics } = res; |
| 99 | + for (let i = 0; i < numIter - 1; i++) { |
| 100 | + res = await pipe.generate(prompt, config); |
| 101 | + perfMetrics.add(res.perfMetrics); |
| 102 | + } |
| 103 | + |
| 104 | + console.log(`Output token size: ${perfMetrics.getNumGeneratedTokens()}`); |
| 105 | + console.log(`Load time: ${perfMetrics.getLoadTime()} ms`); |
| 106 | + console.log(`Generate time: ${perfMetrics.getGenerateDuration().mean} ± ${perfMetrics.getGenerateDuration().std} ms`); |
| 107 | + console.log(`Tokenization time: ${perfMetrics.getTokenizationDuration().mean} ± ${perfMetrics.getTokenizationDuration().std} ms`); |
| 108 | + console.log(`Detokenization time: ${perfMetrics.getDetokenizationDuration().mean} ± ${perfMetrics.getDetokenizationDuration().std} ms`); |
| 109 | + console.log(`TTFT: ${perfMetrics.getTTFT().mean} ± ${perfMetrics.getTTFT().std} ms`); |
| 110 | + console.log(`TPOT: ${perfMetrics.getTPOT().mean} ± ${perfMetrics.getTPOT().std} ms`); |
| 111 | + console.log(`Throughput : ${perfMetrics.getThroughput().mean} ± ${perfMetrics.getThroughput().std} tokens/s`); |
| 112 | +} |
0 commit comments